在k8s当中,我们使用挂载,结果是直接将该目录覆盖被挂载的那个目录,如果只是想挂一个文件进去,那么可以使用subpath。或者多个应用共用一个卷,也可以使用subpath来分别映射到卷的不同的文件夹
https://cloud.tencent.com/developer/article/1639540
https://blog.csdn.net/a772304419/article/details/125910287
同pod不同容器共享卷示例

apiVersion: v1
kind: Pod
metadata:
  name: my-lamp-site
spec:
    containers:
    - name: mysql
      image: mysql
      volumeMounts:
      - mountPath: /var/lib/mysql
        name: site-data
        subPath: mysql
    - name: php
      image: php
      volumeMounts:
      - mountPath: /var/www/html
        name: site-data
        subPath: html
    volumes:
    - name: site-data
      persistentVolumeClaim:
        claimName: my-lamp-site-data

configmap单独文件挂载示例(注意,这种方式挂载的时候,容器将无法接收到configmap的更新!
准备一个nginx.conf
制作cm,直接写yaml也行
kubectl create configmap confnginx --from-file=nginx.conf
准备一个容器挂载这个

apiVersion: apps/v1
kind: Deployment
metadata:
  name: centos-test
spec: 
  replicas: 1
  selector:
    matchLabels:
      app: centos-mount
  template: 
    metadata: 
      labels: 
        app: centos-mount
    spec: 
      containers: 
      - name: "centos-mount"
        image: centos
        imagePullPolicy: IfNotPresent
        command: #因为这个不是个后端类容器,不跑个东西会直接退出
          - "sleep"
          - "100"
        lifecycle:
          preStop:  
            exec: 
              command: 
                - sleep
                - '5'
        volumeMounts: 
          - mountPath: /var/tmp/nginx.conf
            name: nginx-config
            subPath: nginx.conf
      volumes: 
        - name: nginx-config
          configMap: 
            name: confnginx

查看效果

[root@master ~]# kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
centos-test-99f68b898-2q7tg   1/1     Running   0          5s
[root@master ~]# kubectl exec -it centos-test-99f68b898-2q7tg -- bash
[root@centos-test-99f68b898-2q7tg /]# cat /var/tmp/nginx.conf 
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

我们现在来把目录改成原本有文件的目录/tmp下,再apply看看

#mountPath: /tmp/nginx.conf
[root@master ~]# kubectl exec -it centos-test-758957f456-pgrd8 -- bash
[root@centos-test-758957f456-pgrd8 /]# ls /tmp/
ks-script-4luisyla  ks-script-o23i7rc2  ks-script-x6ei4wuu  nginx.conf
#可以看到原目录并没有被覆盖

现在我们来把subpath给删掉,来看看有什么不同

#mountPath: /tmp/nginx.conf
[root@master ~]# kubectl exec -it centos-test-54c9d4fb48-5gtsw -- bash
[root@centos-test-54c9d4fb48-5gtsw /]# cat /tmp/nginx.conf/
cat: /tmp/nginx.conf/: Is a directory
#路径被理解成了文件夹,而不是文件
[root@centos-test-54c9d4fb48-5gtsw /]# cat /tmp/nginx.conf/
..2023_02_03_01_12_49.136363453/ ..data/                          nginx.conf                       
[root@centos-test-54c9d4fb48-5gtsw /]# cat /tmp/nginx.conf/
..2023_02_03_01_12_49.136363453/ ..data/                          nginx.conf                       
[root@centos-test-54c9d4fb48-5gtsw /]# cat /tmp/nginx.conf/nginx.conf 
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

#mountPath: /tmp/
[root@master ~]# kubectl exec -it centos-test-765958c6fb-n62mx -- bash
[root@centos-test-765958c6fb-n62mx /]# ls /tmp/
nginx.conf
#直接被覆盖了

标签: none

评论已关闭