如何在 DaemonSet 中将文件挂载为 ConfigMap?

How do I mount file as ConfigMap inside DaemonSet?

我有以下 nginx 配置文件(名为 nginx-daemonset.conf),我想在我的 Daemonset 中使用它:

events {
    worker_connections 1024;
}

http {

    server {
        listen 80;
        
        location / {
            proxy_pass http://my-nginx;
        }
    }
}

我使用以下命令创建了一个 ConfigMap:kubectl create configmap nginx2.conf --from-file=nginx-daemonset.conf

我有以下 DaemonSet (nginx-daemonset-deployment.yml),我试图在其中安装此 ConfigMap - 因此使用了以前的 nginx 配置文件:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nginx-daemonset
  namespace: kube-system
  labels:
    k8s-app: nginx-daemonset
spec:
  selector:
    matchLabels:
      name: nginx-daemonset
  template:
    metadata:
      labels:
        name: nginx-daemonset
    spec:
      tolerations:
      # this toleration is to have the daemonset runnable on master nodes
      # remove it if your masters can't run pods
      - key: node-role.kubernetes.io/master
        effect: NoSchedule
      containers:
      - name: nginx
        image: nginx
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        - name: nginx2-conf
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
      volumes:
      - name: nginx2-conf
        configMap:
            name: nginx2.conf

我使用 kubectl apply -f nginx-daemonset-deployment.yml 部署了这个 Daemonset,但我新创建的 Pod 崩溃并出现以下错误:

Error: failed to start container "nginx": Error response from daemon: OCI runtime create failed: container_linux.go:370: starting container process caused: process_linux.go:459: container init caused: rootfs_linux.go:59: mounting "/var/lib/kubelet/pods/cd9f6f7b-31db-4ab3-bbc0-189e1d392979/volume-subpaths/nginx2-conf/nginx/0" to rootfs at "/var/lib/docker/overlay2/b21ccba23347a445fa40eca943a543c1103d9faeaaa0218f97f8e33bacdd4bb3/merged/etc/nginx/nginx.conf" caused: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

我之前用不同的 nginx 配置做了另一个部署,一切正常,所以这个问题可能与 DaemonSet 有某种关系。

请问如何克服这个错误并正确挂载配置文件?

首先将您的配置文件创建为 ConfigMap,例如 nginx-conf:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
data:
  envfile: |
    events {
        worker_connections 1024;
    }

    http {

        server {
            listen 80;
            
            location / {
                proxy_pass http://my-nginx;
            }
        }
    }

然后使用子路径创建您的 DaemonSet、卷、configMap 和最终安装 volumeMounts:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - name: http
          containerPort: 80
        volumeMounts:
        - mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
          readOnly: true
          name: nginx-vol
      volumes:
      - name: nginx-vol
        configMap:
          name: nginx-conf
          items:
            - key: envfile
              path: nginx.conf

请注意,对于文件挂载而不是目录挂载,您必须使用“configMap 中的路径”和“volumeMounts 中的子路径”。