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

时间:2020-11-01 12:37:39

标签: nginx kubernetes configmap

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

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有关。

请,我如何克服此错误并正确安装配置文件?

1 个答案:

答案 0 :(得分:4)

首先将您的配置文件创建为像nginx-conf这样的ConfigMap:

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和最后的mount 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中的subPath”。

相关问题