Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Nginx 如何将文件装载为守护程序内的ConfigMap?_Nginx_Kubernetes_Configmap - Fatal编程技术网

Nginx 如何将文件装载为守护程序内的ConfigMap?

Nginx 如何将文件装载为守护程序内的ConfigMap?,nginx,kubernetes,configmap,Nginx,Kubernetes,Configmap,我有以下名为nginx-daemonset.conf的nginx配置文件,我想在我的守护程序中使用它: events { worker_connections 1024; } http { server { listen 80; location / { proxy_pass http://my-nginx; } } } 我使用以下命令创建了一个ConfigMap:kubect

我有以下名为nginx-daemonset.conf的nginx配置文件,我想在我的守护程序中使用它:

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

我有以下守护程序nginx-DaemonSet-deployment.yml,我正在尝试在其中装载此配置映射-因此使用了以前的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部署了此守护程序,但我新创建的Pod因以下错误而崩溃:

错误:无法启动容器nginx:来自守护程序的错误响应:OCI运行时创建失败:container_linux.go:370:启动容器进程导致:process_linux.go:459:container init导致:rootfs_linux.go:59:mounting/var/lib/kubelet/pods/cd9f6f7b-31db-4ab3-bbc0-189e1d392979/卷子路径/nginx2 conf/nginx/0到rootfs/var/lib/docker/overlay2/b21ccba2347a445fa40eca943a543c1103d9faeaaa0218f97f8e33baccdd4bb3/merged/etc/nginx/nginx.conf原因:非目录:未知:您是否尝试将目录装载到文件上,或者相反?检查指定的主机路径是否存在并且是否为预期类型

我以前用不同的nginx配置做过另一次部署,一切都很好,所以问题可能与守护程序有关


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

首先将配置文件创建为类似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;
            }
        }
    }

然后使用子路径创建守护程序集、卷、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中的子路径。

首先将配置文件创建为类似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;
            }
        }
    }

然后使用子路径创建守护程序集、卷、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中的子路径。

我遵循了您的方法,它可以工作!不知何故,我仍然无法清楚地理解您的方法和我的方法之间的主要区别,您能指出我做错了什么吗?您的配置用于目录装载,但如果您想要文件装载,您必须使用configMap中的路径和volumeMounts中的子路径。您在configMap中所指的路径是什么?我仍然很困惑:/configMap中volumes中的路径,这是我发送来回答的第二行代码的最后一行。我遵循了您的方法,它工作了!不知何故,我仍然无法清楚地理解您的方法和我的方法之间的主要区别,您能指出我做错了什么吗?您的配置用于目录装载,但如果您想要文件装载,您必须使用configMap中的路径和volumeMounts中的子路径。您在configMap中所指的路径是什么?我仍然感到困惑:/configMap中volumes的路径,这是我发送给应答的第二行代码的最后一行。