|NO.Z.00182|——————————|CloudNative|——|KuberNetes&配置管理.V13|——|subpath.v01|使用subpath挂载目录|

一、SubPath概述
### --- configMap和Secret:

~~~     configMap和Secret它挂载的时候是会覆盖这个目录的,所以覆盖这个目录是可以被解决掉的。
~~~     若是挂载一个文件到这个目录中,可能会覆盖掉这个目录原有的文件。
~~~     引入SubPath来解决这个覆盖的问题
二、SubPath实验不使用SubPath挂载整个目录,导致该目录下所有文件被覆盖
### --- 容器不使用SubPath挂载目录

~~~     # 以demo-nginx的容器
~~~     它是有一个nginx-config文件的,若是把这个文件放在配置文件里面,
~~~     若是想要更改这个配置文件里的内容,需要更改这个容器,会非常的麻烦。
~~~     所以我们会把nginx-config这个文件放在configmap去管理。
~~~     但是放在configmap去管理,若是直接把它挂载到/etc/nginx下,
~~~     它会把这个/etc/nginx下其它的文件覆盖掉。
~~~     # 若是直接挂载到/etc/nginx下,它会覆盖掉这个目录下的所有文件;这个容器会出问题,
~~~     # 所以我们通过SubPath的形式以文件的形式挂载,不覆盖整个目录
[root@k8s-master01 ~]# kubectl get po
NAME                        READY   STATUS      RESTARTS   AGE
busybox                     1/1     Running     78         5d20h
dapi-test-pod               0/1     Completed   0          24h
dapi-test-pod-secret        1/1     Running     0          10m
demo-nginx-59569f79-2bmnd   1/1     Running     0          2d4h
demo-nginx-59569f79-vxr6c   1/1     Running     0          2d1h
[root@k8s-master01 ~]# kubectl exec -ti demo-nginx-59569f79-2bmnd  -- cat /etc/nginx/nginx.conf
user  nginx;
worker_processes  1;
events {
    worker_connections  1024;
}
[root@k8s-master01 ~]# kubectl exec -ti demo-nginx-59569f79-2bmnd  -- ls /etc/nginx
conf.d  fastcgi_params  koi-utf  koi-win  mime.types  modules  nginx.conf  scgi_params  uwsgi_params  win-utf
### --- 将nginx.conf文件导出来,导入到容器nginx.conf文件中

[root@k8s-master01 ~]# kubectl exec -ti demo-nginx-59569f79-2bmnd  -- cat /etc/nginx/nginx.conf > nginx.conf
~~~     # 以nginx.conf文件形式创建一个configmap;

[root@k8s-master01 ~]# kubectl create cm nginx-conf --from-file=nginx.conf
configmap/nginx-conf created
三、将创建的configmap挂载到demo-nginx容器中
### --- 查看创建的configmap

[root@k8s-master01 ~]# kubectl get cm
NAME                          DATA   AGE
nginx-conf                    1      5s
### --- 把它挂载到deployment中,直接挂载查看效果

[root@k8s-master01 ~]# kubectl edit deploy demo-nginx
    spec:                                   // 挂载是以容器为基础的,那个容器需要挂载,就在那个容器下写入volumeMounts
      containers:
      - image: nginx:1.15.2
        imagePullPolicy: IfNotPresent
        command: ["sh", "-c", "sleep 3600"] // 更改一下启动命令,只是为了实验验证,不存在真实生产环境。
        name: demo-nginx                    // 那个容器需要使用,就写在哪个容器下面
        volumeMounts:
        - name: config-volume
          mountPath: /etc/nginx 
*********省略部分内容**************
      volumes:                              // 写入挂载文件内容
      - name: config-volume                 // volumes是和DNSPolicy同一级别的;是和containers对齐的,
        configMap:
          name: nginx-conf
status:                                     // 在status上面写入配置文件内容
deployment.apps/demo-nginx edited
### --- 查看创建的容器

[root@k8s-master01 ~]# kubectl get po
demo-nginx-5869b6bc6b-tzdzs   1/1     Running     0          89s     172.18.195.21    k8s-master03   <none>           <none>
### --- 进去容器查看创建的nginx.conf配置文件

[root@k8s-master01 ~]# kubectl exec -ti demo-nginx-5869b6bc6b-tzdzs -- ls /etc/nginx
nginx.conf                              // 可以看到这个nginx下只有nginx.conf文件;其它的目录都被覆盖掉了。

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(56)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示