默认在pod中通过volume挂载数据的时候,如果挂载目录下原来有文件,挂载后将被覆盖掉。有的时候,只是希望将文件挂载到某个目录,但希望只是挂载该文件,不要影响挂载目录下的其他文件。这时候就可以用 subPath,subPath的目的是为了在单一pod中多次使用同一个volume而设计的。

示例

创建ConfigMap

  • ConfigMap
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
  namespace: default
data:
  nginx.conf: |-
   user  nginx;
   worker_processes  1;

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

   events {
        worker_connections  1024;
    }

    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

        include /etc/nginx/conf.d/*.conf;
    }   
  • 创建ConfigMap
1
$ kubectl apply -f nginx-config.yaml

创建pod

  • 创建pod,通过上面的ConfigMap挂载nginx.conf配置文件
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ngtest
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.7.9
          ports:
          - containerPort: 80
          volumeMounts:
            - name: nginx-config
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
      volumes:
        - name: nginx-config
          configMap:
            name: nginx-conf
  • 创建上面的deplyment
1
$ kubectl apply -f nginx-dpl.yaml

验证

  • 查看pod状态
1
2
3
4
5
$ kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
......
ngtest-5dd5c685d6-g9f69           1/1     Running   0          2m9s
......
  • 进入容器查看nginx.conf文件
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$ kubectl exec -it ngtest-5dd5c685d6-g9f69 /bin/bash
root@ngtest-5dd5c685d6-g9f69:/# ls /etc/nginx/
conf.d	fastcgi_params	koi-utf  koi-win  mime.types  nginx.conf  scgi_params  uwsgi_params  win-utf
root@ngtest-5dd5c685d6-g9f69:/# cat /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;

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

events {
     worker_connections  1024;
 }

 http {
     include       /etc/nginx/mime.types;
     default_type  application/octet-stream;

     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                       '$status $body_bytes_sent "$http_referer" '
                       '"$http_user_agent" "$http_x_forwarded_for"';

     access_log  /var/log/nginx/access.log  main;

     sendfile        on;
     #tcp_nopush     on;

     keepalive_timeout  65;

     #gzip  on;

     include /etc/nginx/conf.d/*.conf;
 }

Nginx.conf文件正是我们上面创建的ConfigMap对象里的内容,验证成功

使用ConfigMap的subPath挂载为容器的Volume,Kubernetes不会自动热更新