|NO.Z.00183|——————————|CloudNative|——|KuberNetes&配置管理.V14|——|subpath.v02|subpath解决覆盖目录问题|
一、使用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
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了