CC00183.CloudKubernetes——|KuberNetes&配置管理.V14|——|subpath.v02|subpath解决覆盖目录问题|

本文探讨了如何使用Kubernetes的SubPath特性来解决挂载文件时覆盖原有目录的问题,通过实例展示了在Docker、Nginx等环境中如何有效管理配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、使用SubPath解决覆盖目录问题
### --- 使用SubPath解决覆盖目录的问题,而是挂载这个文件;
~~~     为了解决这个问题,使用SubPath去挂载这个文件
~~~     key的值

[root@k8s-master01 ~]# kubectl edit deploy demo-nginx
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf      // 挂载文件写成nginx.conf
          name: config-volume
          subPath: /etc/nginx/nginx.conf        // 加入SubPath的参数;以文件的形式挂载,这个目录就不可以被覆盖。
**************省略部分内容******************** 
      volumes:
      - configMap:
          defaultMode: 420
          items:
          - key: nginx.conf                     // key值
            path: etc/nginx/nginx.conf          // path:注意前面的根是不可以写的,
          name: nginx-conf
        name: config-volume 
deployment.apps/demo-nginx edited               // 输出内容
### --- 查看创建的configmap配置文件

[root@k8s-master01 ~]# kubectl get cm nginx-conf -oyaml
data:                                           // key的值就是这个nginx.conf后面的内容
  nginx.conf: "\r\nuser  nginx;\r\nworker_processes  1;\r\n\r\nerror_log  /var/log/nginx/error.log warn;\r\npid        /var/run/nginx.pid;\r\n\r\n\r\nevents {\r\n    worker_connections\ 1024;\r\n}\r\n\r\n\r\nhttp {\r\n    include       /etc/nginx/mime.types;\r\n\   default_type  application/octet-stream;\r\n\r\n    log_format  main  '$remote_addr- $remote_user [$time_local] \"$request\" '\r\n                      '$status$body_bytes_sent \"$http_referer\" '\r\n                      '\"$http_user_agent\"\"$http_x_forwarded_for\"';\r\n\r\n    access_log  /var/log/nginx/access.log  main;\r\n\r\n\   sendfile        on;\r\n    #tcp_nopush     on;\r\n\r\n    keepalive_timeout\ 65;\r\n\r\n    #gzip  on;\r\n\r\n    include /etc/nginx/conf.d/*.conf;\r\n}\r\n"
kind: ConfigMap
### --- 查看更新后的容器

[root@k8s-master01 ~]# kubectl get po
NAME                          READY   STATUS      RESTARTS   AGE
busybox                       1/1     Running     79         5d22h
dapi-test-pod                 0/1     Completed   0          25h
dapi-test-pod-secret          0/1     Completed   0          94m
demo-nginx-6c987d4775-648m7   1/1     Running     0          119s
demo-nginx-6c987d4775-9kpgt   1/1     Running     0          114s
### --- 验证创建的容器是否挂载文件进来且没有覆盖原有的容器文件
~~~     所有的文件都还在,nginx.conf文件已经被挂载到该目录下

[root@k8s-master01 ~]# kubectl exec -ti demo-nginx-6c987d4775-9kpgt -- ls /etc/nginx
conf.d      koi-utf  mime.types  nginx.conf   uwsgi_paramsfastcgi_params    koi-win  modules     scgi_params  win-utf
~~~     # 这个文件已经被挂载进去,且这个目录也没有被覆盖。
~~~     在动态pv下,创建2个目录,在一个目录下,挂载到不同的目录下。

[root@k8s-master01 ~]# kubectl exec -ti demo-nginx-6c987d4775-648m7 -- cat /etc/nginx/nginx.conf
events {
    worker_connections  1024;
}
二、SubPath解决挂载文件覆盖目录问题
### --- SubPath解决覆盖的问题demo-nginx.yaml文件
~~~     将配置文件导出来;该配置文件没有健康检查参数,可以根据自己的需求把该需求加上。

[root@k8s-master01 ~]# kubectl get deploy demo-nginx -oyaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "4"
  creationTimestamp: "2020-03-16T14:36:14Z"
  generation: 5
  labels:
    app: demo-nginx
  name: demo-nginx
  namespace: default
  resourceVersion: "1338515"
  selfLink: /apis/apps/v1/namespaces/default/deployments/demo-nginx
  uid: ca0c135f-31a3-464c-b1f2-10f88e9f1c90
spec:
  progressDeadlineSeconds: 600
  replicas: 2
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: demo-nginx
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: demo-nginx
    spec:
      containers:
      - command:
        - sh
        - -c
        - sleep 3600
        image: nginx
        imagePullPolicy: IfNotPresent
        name: nginx
        ports:
        - containerPort: 80
          name: web
          protocol: TCP
        resources:
          limits:
            cpu: 100m
            memory: 270Mi
          requests:
            cpu: 100m
            memory: 70Mi
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf
          name: config-volume
          subPath: etc/nginx/nginx.conf
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - configMap:
          defaultMode: 420
          items:
          - key: nginx.conf
            path: etc/nginx/nginx.conf
          name: nginx-conf
        name: config-volume
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yanqi_vip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值