如何将 configmap 附加到 Kubernetes 中的部署?

How do I attach a configmap to a deployment in Kubernetes?

基于此处找到的说明 (https://kubernetes.io/docs/tasks/access-application-cluster/connecting-frontend-backend/) I am trying to create an nginx deployment and configure it using a config-map. I can successfully access nginx using curl (yea!) but the configmap does not appear to be "sticking." The only thing it is supposed to do right now is forward the traffic along. I have seen the thread here ()。尽管我使用相同的格式,但他们的回答并不相关。

谁能告诉我如何正确配置配置映射? yaml 是

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: sandbox
spec:
  selector:
    matchLabels:
      run: nginx
      app: dsp
      tier: frontend
  replicas: 2
  template:
    metadata:
      labels:
        run: nginx
        app: dsp
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx
        env:
        # Define the environment variable
        - name: nginx-conf
          valueFrom:
            configMapKeyRef:
              # The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
              name: nginx-conf
              # Specify the key associated with the value
              key: nginx.conf
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
containerPort: 80 

nginx-conf 是

 # The identifier Backend is internal to nginx, and used to name this specific upstream
upstream Backend {
    # hello is the internal DNS name used by the backend Service inside Kubernetes
    server dsp;
}

server {
    listen 80;

    location / {
        # The following statement will proxy traffic to the upstream named Backend
        proxy_pass http://Backend;
    }
} 

我使用下面的行将它变成一个 configmap

kubectl create configmap -n sandbox nginx-conf --from-file=apps/nginx.conf

您需要挂载 configMap 而不是将其用作环境变量,因为设置不是 key-value 格式。

您的部署 yaml 应如下所示:

containers:
- name: nginx
  image: nginx
  volumeMounts:
  - mountPath: /etc/nginx
    name: nginx-conf
volumes:
- name: nginx-conf
  configMap: 
    name: nginx-conf
    items:
      - key: nginx.conf
        path: nginx.conf

您需要事先创建(应用)configMap。您可以从文件创建它:

kubectl create configmap nginx-conf --from-file=nginx.conf

或者您可以直接描述 configMap 清单:

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
data:
  nginx.conf: |
    # The identifier Backend is internal to nginx, and used to name this specific upstream
    upstream Backend {
        # hello is the internal DNS name used by the backend Service inside Kubernetes
        server dsp;
    }
    ...
}