Kubernetes Configmap explained using example

Content of reverseproxy.conf


---------------------------
server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_bind 127.0.0.1;
        proxy_pass http://127.0.0.1:3000;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

Commands to execute to create configmap


---------------------------
kubectl create configmap my-config --from-file=reverseproxy.conf
kubectl get cm
kubectl describe cm my-config

Example pod using configmap


---------------------------
apiVersion: v1
kind: Pod
metadata:
  name: helloworld-nginx
  labels:
    app: helloworld-nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.11
    ports:
    - containerPort: 80
    volumeMounts:
    - name: config-volume
      mountPath: /etc/nginx/conf.d
  - name: k8s-demo
    image: wardviaene/k8s-demo
    ports:
    - containerPort: 3000
  volumes:
    - name: config-volume
      configMap:
        name: my-config
        items:
        - key: reverseproxy.conf
          path: myconfo.conf

Validating configmap inside a pod

---------------------------
kubectl exec -it helloworld-nginx /bin/bash
cd /etc/nginx/conf.d
Rajesh Kumar
Follow me