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

2024-05-10

根据此处找到的说明(https://kubernetes.io/docs/tasks/access-application-cluster/connecting-frontend-backend/ https://kubernetes.io/docs/tasks/access-application-cluster/connecting-frontend-backend/)我正在尝试创建一个 nginx 部署并使用配置映射对其进行配置。我可以使用curl 成功访问nginx(是的!),但是configmap 似乎没有“粘住”。它现在唯一应该做的就是转发流量。我在这里看到了这个话题(如何将 configMap 加载到环境变量中? https://stackoverflow.com/questions/52773494/how-do-i-load-a-configmap-in-to-an-environment-variable)。尽管我使用相同的格式,但他们的答案不相关。

谁能告诉我如何正确配置配置映射? 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;
    }
} 

我使用以下行将其转换为配置映射

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

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

您的部署 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;
    }
    ...
} 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 configmap 附加到 Kubernetes 中的部署? 的相关文章

随机推荐