虚拟化容器,大数据,DBA,中间件,监控。

Kubernetes ConfigMap挂载导致容器目录覆盖的问题解决

21 12月
作者:admin|分类:容器虚拟化
apiVersion: v1
kind: ConfigMap
metadata:
  name: java-demo-config
data:
    application.yml: |
        server:
          port: 8080
        spring:
          datasource:
            url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
            username: root
            password: 123456789
            driver-class-name: com.mysql.jdbc.Driver
          freemarker:
            allow-request-override: false
            cache: true
            check-template-location: true
            charset: UTF-8
            content-type: text/html; charset=utf-8
            expose-request-attributes: false
            expose-session-attributes: false
            expose-spring-macro-helpers: false
            suffix: .ftl
            template-loader-path:
              - classpath:/templates/

挂载注意了:

如果挂载空目录没事,如果挂载了已有文件的目录到容器的目录,那么容器目录里面的文件都会被覆盖,解决办法是使用subpath指定其子路径。你将configmap挂载在指定的目录下,指定的目录是有文件的,你挂载之后只剩下挂载的文件,其余文件没有了,可以使用subpath指定文件名字解决这个问题

[root@k8s-master ~]# cat deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: java-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      project: www
      app: java-demo
  template:
    metadata:
      labels:
        project: www
        app: java-demo
    spec:
      imagePullSecrets:
      - name: registry-auth
      containers:
      - image: reg.ctnrs.com/demo/java-demo:v1
        name: java-demo
        volumeMounts:
        - name: config
          mountPath: "/usr/local/tomcat/webapps/ROOT/WEB-INF/classes/application.yml"
          subPath: application.yml
        resources:
          requests:
            cpu: 0.5
            memory: 500Mi
          limits: 
            cpu: 1
            memory: 1Gi
        livenessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 80
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 80
          periodSeconds: 10
      volumes:
      - name: config
        configMap:
          name: java-demo-config 
          items:
          - key: "application.yml"
            path: "application.yml"

现在要将配置挂载在容器

 - key: "application.yml"  这个指的是在configmap定义的下面

data:

application.yml: |

path: "application.yml" 具体是挂载在容器目录当中具体的文件名

一般上面两个保持一致

 

ConfigMap挂载导致容器目录覆盖的问题解决


问题表现:

将configmap的配置项挂载到指定的容器目录中,导致容器的被挂载目录下的所有文件不可见,只可见通过configmap挂载的文件
解决办法:

  1. volumes中设置path
  2. volumeMounts中这是subPath

ConfigMap 

[root@k8s-master ~]# cat configmap.yml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: configmaptest
  namespace: default
data:
  configmap: test

  cni-conf.json: |
    {
      "name": "cbr0",
      "cniVersion": "0.3.1",
      "plugins": [
        {
          "type": "flannel",
          "delegate": {
            "hairpinMode": true,
            "isDefaultGateway": true
          }
        },
        {
          "type": "portmap",
          "capabilities": {
            "portMappings": true
          }
        }
      ]
    }
  net-conf.json: |
    {
      "Network": "10.244.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }


[root@k8s-master ~]# kubectl get cm 
NAME                        DATA   AGE
smartcity-frontend-config   2      48m

[root@k8s-master ~]# kubectl describe cm  smartcity-frontend-config 
Name:         smartcity-frontend-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
config-js:
----
{
  "name": "cbr0",
  "cniVersion": "0.3.1",
  "plugins": [
    {
      "type": "flannel",
      "delegate": {
        "hairpinMode": true,
        "isDefaultGateway": true
      }
    },
    {
      "type": "portmap",
      "capabilities": {
        "portMappings": true
      }
    }
  ]
}

log4j-js-json:
----
{
  "Network": "10.244.0.0/16",
  "Backend": {
    "Type": "vxlan"
  }
}

Events:  <none>

 Deployment(正确挂载方式)

[root@k8s-master ~]# cat deployment.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: pod-readiness
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: readiness
  template:
    metadata:
      labels:
        app: readiness
    spec:
      containers:
      - name: busybox
        image: busybox
        command: ['sh','-c','sleep 3600']
        volumeMounts:
        - name: smartcity-frontend-config
          mountPath: /var/config.js
          subPath: config-js
        - name: smartcity-frontend-config
          mountPath: /var/log4js.json
          subPath: log4j-js-json

      volumes:
      - name: smartcity-frontend-config
        configMap:
          name: smartcity-frontend-config
          items:
          - key: log4j-js-json
            path: log4j-js-json
          - key: config-js
            path: config-js




[root@k8s-master ~]# kubectl get pod
NAME                               READY   STATUS      RESTARTS   AGE
pod-readiness-bdc6f8c88-7w9rw      1/1     Running     0          13m
pod-readiness-bdc6f8c88-9cpk4      1/1     Running     0          13m
pod-readiness-bdc6f8c88-rkl7x      1/1     Running     0          13m


[root@k8s-master ~]# kubectl exec -it pod-readiness-bdc6f8c88-7w9rw -- sh
/ # ls /var
config.js    log4js.json  run          spool        www
/ # cat /var/config.js 
{
  "name": "cbr0",
  "cniVersion": "0.3.1",
  "plugins": [
    {
      "type": "flannel",
      "delegate": {
        "hairpinMode": true,
        "isDefaultGateway": true
      }
    },
    {
      "type": "portmap",
      "capabilities": {
        "portMappings": true
      }
    }
  ]
}
/ # 

 

 

总结一下模板


 ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: smartcity-frontend-config
  namespace: portal-frontend
data:
  config-js: | xxxx自定义文件内容
  log4j-js-json: | xxxx自定义文件内容

Deployment(正确挂载方式)

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: smartcity-frontend
  namespace: portal-frontend
spec:
  replicas: 2
  selector:
    matchLabels:
      app: smartcity-frontend
  template:
    metadata:
      labels:
        app: smartcity-frontend
    spec:
      containers:
      - name: node
        image: hub.cloud.pub/frontend/node:2.12.4
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: smartcity-frontend-config
          mountPath: /opt/frontend/config.js
          subPath: path/to/config.js
        - name: smartcity-frontend-config
          mountPath: /opt/frontend/log4js.json
          subPath: path/to/log4js.json
      volumes:
      - name: smartcity-frontend-config
        configMap:
          name: smartcity-frontend-config
          items:
          - key: config-js
            path: path/to/config.js
          - key: log4j-js-json
            path: path/to/log4js.json

例如nginx

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginxconfig
  namespace: mt-math
data:
  nginx.conf: |
    #########wyl530
    user nginx;
    worker_processes auto;
    error_log /etc/nginx/error.log;
    pid /run/nginx.pid;
    include /usr/share/nginx/modules/*.conf;
    events {
        worker_connections 1024;
    }
    http {
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        server_tokens     off;
        access_log        /usr/share/nginx/html/access.log  main;
        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   65;
        types_hash_max_size 2048;
        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;
        include /etc/nginx/conf.d/*.conf;
        server {
            listen       80 default_server;
            listen       [::]:80 default_server;
            server_name  _;
            root         /usr/share/nginx/html;
            include /etc/nginx/default.d/*.conf;
            location / {
            }
            error_page 404 /404.html;
                location = /40x.html {
            }
            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
        }

    }
          volumeMounts:
          - name: nginx-nfs
            mountPath: /usr/share/nginx/html
          - name: nginx-pvc
            mountPath: /var/log/nginx
            subPath: nginx.conf
          - name: nginx-etc       #挂载数据节点名称
            mountPath: /etc/nginx/nginx.conf #挂载此目录
            subPath: nginx.conf
      volumes:                    #设置挂载
      - name: nginx-nfs           #挂载的数据节点名称
        nfs:                      #挂载的服务类型
          server: 192.168.0.14    #服务Ip
          path: /NFS/wwwroot      #挂载数据目录
      - name: nginx-pvc           #挂载数据节点名称
        persistentVolumeClaim:    #服务类型
          claimName: nfs-data-pvc #数据源名称
      - name: nginx-etc           #挂载数据节点名称
        configMap:
         name: nginxconfig        #指定创建configMap的名称
         items:
          - key: nginx.conf       #key为文件名称
            path: nginx.conf      #文件路径内容

 

浏览609 评论0
返回
目录
返回
首页
Kubernetes 安全之访问控制 API 请求访问控制和认证 Kubernetes 应用配置与管理