k8s ConfigMap挂载目录 热更新机制

K8s ConfigMap 挂载底层机制

nginx挂载配置

        volumeMounts:
        - name: nginx-conf
          mountPath: /etc/nginx/conf.d

      volumes:
      - name: nginx-conf
        configMap:
          name: nginx-cm

configMap文件

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-cm
data:
  nginx.conf: |
    server {
        listen 80;
        server_name _;

        root /usr/share/nginx/html;
        index index.html index.htm;

        location /healthz {
            access_log off;
            return 200 "healthy\n";
            add_header Content-Type text/plain;
        }

        location / {
            try_files $uri $uri/ =404;
        }

        # 日志配置
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
    }
  config.json: |
    ...
  test.txt: |
    ...

容器内挂载目录详情如下

root@nginx-test-7cf6494877-9dchv:/etc/nginx/conf.d# ls -al
total 12
drwxrwxrwx 3 root root 4096 Jul 16 01:39 .
drwxr-xr-x 3 root root 4096 May 22 18:25 ..
drwxr-xr-x 2 root root 4096 Jul 16 01:39 ..2026_07_16_01_39_48.1281209746
lrwxrwxrwx 1 root root   32 Jul 16 01:39 ..data -> ..2026_07_16_01_39_48.1281209746
lrwxrwxrwx 1 root root   18 Jul 16 01:36 config.json -> ..data/config.json
lrwxrwxrwx 1 root root   17 Jul 16 01:36 nginx.conf -> ..data/nginx.conf
lrwxrwxrwx 1 root root   15 Jul 16 01:36 test.txt -> ..data/test.txt
  1. ..2026_07_xxx:存放当前 ConfigMap 所有真实文件的临时目录,文件名带时间戳用于版本切换;
  2. ..data:软链接,指向上面带时间戳的真实目录;
  3. config.json/nginx.conf/test.txt:业务可见文件,是二级软链接,链路:文件 -> ..data/xxx -> 时间戳目录/xxx
  4. 这套机制是 K8s 实现ConfigMap 热更新的核心:
    • 修改 CM 后,kubelet 生成新时间戳目录放新文件;
    • 原子替换 ..data 软链接指向新目录;
    • 无需重启 Pod,程序重新读取文件就能拿到新内容。
posted @ 2026-07-16 09:52  ZovoeY  阅读(13)  评论(0)    收藏  举报