首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在ConfigMap中将文件挂载为DaemonSet?

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

Stack Overflow用户
提问于 2020-11-01 12:37:39
回答 1查看 883关注 0票数 1

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

代码语言:javascript
运行
复制
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 installment.yml),在其中我试图挂载这个ConfigMap -因此使用了前面的nginx配置文件:

代码语言:javascript
运行
复制
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部署了这个with,但是新创建的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有关。

请注意,如何克服此错误并正确挂载配置文件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-01 13:05:30

首先,将您的配置文件创建为类似于nginx的ConfigMap:

代码语言:javascript
运行
复制
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、卷、configMap和finial y挂载volumeMounts:

代码语言:javascript
运行
复制
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

注意,对于文件挂载,而不是目录挂载,您必须使用"path in configMap“和"subPath in volumeMounts”。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64631969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档