Home > OS >  How do I attach a configmap to a deployment in Kubernetes?
How do I attach a configmap to a deployment in Kubernetes?

Time:02-11

Based on the instructions found here (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 (How do I load a configMap in to an environment variable?). although I am using the same format, their answer was not relevant.

Can anyone tell me how to properly configure the configmaps? the yaml is

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 

the nginx-conf is

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

I turn it into a configmap using the following line

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

CodePudding user response:

You need to mount the configMap rather than use it as an environment variable, as the setting is not a key-value format.

Your Deployment yaml should be like below:

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

You need to create (apply) the configMap beforehand. You can create it from file:

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

or you can directly describe configMap 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;
    }
    ...
} 
  • Related