如何在Kubernetes中将一个Map附加到一个部署?

3phpmpom  于 2023-11-17  发布在  Kubernetes
关注(0)|答案(2)|浏览(113)

根据这里的说明(https://kubernetes.io/docs/tasks/access-application-cluster/connecting-frontend-backend/)我正在尝试创建一个nginx部署,并使用curl-map配置它。我可以使用curl成功访问nginx(是的!)但该Map似乎并没有“粘”。唯一的事情是它应该做的权利,现在是向前的流量沿着。我已经看到了线程在这里(How do I load a configMap in to an environment variable?).虽然我使用相同的格式,他们的回答是不相关的。
有谁能告诉我如何正确配置YAMLMap吗?

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;
    }
}


我使用下面的代码行将其转换为一个XMLMap

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

z3yyvxxp

z3yyvxxp1#

您需要挂载EDMap,而不是将其用作环境变量,因为该设置不是键值格式。
您的部署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

字符串
您需要事先创建(应用)该Map。您可以从文件创建它:

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


或者你可以直接描述MapMap manifest:

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;
    }
    ...
}

tjrkku2a

tjrkku2a2#

你好,你可以直接描述配置Map

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;
    }
    ...
}

字符串

相关问题