Wednesday, September 2, 2026

Creating a ConfigMap in Kubernetes

 


What is a ConfigMap?


A ConfigMap is a Kubernetes object which is used to store non-sensitive configuration data in the form of key-value pairs.


Examples:


Application environment

Server URL

Application port

Log level




Why we Use a ConfigMap?


Instead of hardcoding values in the Deployment, Store them in a ConfigMap and use them wherever it needed.




Create a ConfigMap


kubectl create configmap app-config --from-literal=APP_ENV=Production --from-literal=APP_PORT=8080

kubectl get configmap

kubectl describe configmap app-config




Use ConfigMap in a Deployment


apiVersion: apps/v1

kind: Deployment

metadata:

  name: nginx-config-demo


spec:

  replicas: 1


  selector:

    matchLabels:

      app: nginx-config-demo


  template:

    metadata:

      labels:

        app: nginx-config-demo


    spec:

      containers:

      - name: nginx

        image: nginx


        env:

        - name: APP_ENV

          valueFrom:

            configMapKeyRef:

              name: app-config

              key: APP_ENV


        - name: APP_PORT

          valueFrom:

            configMapKeyRef:

              name: app-config

              key: APP_PORT




Apply - kubectl apply -f deployment.yaml





Verify Inside the Pod


Get the Pod name - kubectl get pods

Login to the Pod - kubectl exec -it <pod-name> -- bash

Check values - printenv APP_ENV

Check values - printenv APP_PORT





ConfigMap YAML Example


apiVersion: v1

kind: ConfigMap

metadata:

  name: app-config


data:

  APP_ENV: Production

  APP_PORT: "8080"

  LOG_LEVEL: INFO



kubectl apply -f configmap.yaml




No comments:

Post a Comment

testing