K8S进阶实践 之 ConfigMap配置文件挂载的使用场景
一、单文件挂载到空目录
实例演示要求:
基于nginx.conf配置文件创建一个ConfigMap配置
把该ConfigMap配置,挂载到/etc/application目录
1、nginx.conf配置文件如下:
[root@k8s-master cm]# cat nginx.conf user nginx; worker_processes 4; #修改了这个位置error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 2048; #修改了这个位置} 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; }
2、基于该nginx.conf文件创建的ConfigMap
[root@k8s-master cm]# kubectl create cm nginx-conf --from-file=nginx.conf configmap/nginx-conf created
3、创建相应的deployment,并挂载该ConfigMap
apiVersion: apps/v1kind: Deploymentmetadata: name: demo namespace: defaultspec: selector: matchLabels: app: demo template: metadata: labels: app: demo spec: volumes: - configMap: name: nginx-conf #指定使用ConfigMap的名称 name: config #volumes的名称 containers: - name: nginx image: nginx:alpine imagePullPolicy: IfNotPresent volumeMounts: - mountPath: "/etc/application" #容器挂载的目录(空的) name: config #指定上面的volumes名称
4、我们尝试将ConfigMap为nginx-conf配置挂载到/etc/profile.d/非空目录的情况
修改挂载路径
重新创建并验证结果
二、多文件挂载到空目录
1、创建多一个test.conf配置文件
2、创建ConfigMap配置
3、查看ConfigMap配置的内容
4、查看pod的挂载情况
三、子路径挂载(只替换或上传文件,不删除原有目录内容)
实现目标:把nginx.conf上传到/etc/nginx/test.conf(基于单文件挂载的例子)
1、创建ConfigMap
2、修改pod容器的挂载方法
apiVersion: apps/v1kind: Deploymentmetadata: name: demo namespace: defaultspec: selector: matchLabels: app: demo template: metadata: labels: app: demo spec: volumes: - name: config configMap: name: nginx-conf items: - key: nginx.conf path: nginx-conf-directory containers: - name: nginx image: nginx:alpine imagePullPolicy: IfNotPresent volumeMounts: - mountPath: "/etc/nginx/test.conf" name: config subPath: nginx.conf
3、创建并应用