Kubernetes ConfigMaps Practical - Hands-on Configuration Management Tutorial

Comprehensive hands-on tutorial for Kubernetes ConfigMaps covering creation methods, environment variable configuration, kubectl commands, YAML examples, and practical configuration management exercises with BusyBox containers

ConfigMap Example

Download the YAML configuration files for the ConfigMap example

Create the ConfigMap

kubectl apply -f cm.yaml

Applies the configuration from the YAML file to create the ConfigMap.

Get the ConfigMap info

kubectl get cm
kubectl describe configmap cm-example

Lists all ConfigMaps and shows detailed information about the cm-example ConfigMap.

Output ConfigMap in YAML format

kubectl get configmap cm-example -o YAML

Outputs the ConfigMap information in YAML format.

Deploy the pod

kubectl apply -f pod.yaml

Creates a pod that uses the ConfigMap data as environment variables.

Connect to the Busybox

kubectl exec mybox -it -- /bin/sh

Opens a shell inside the running Busybox container.

Display the CITY env variable

echo $CITY
exit

Displays the CITY environment variable value from the ConfigMap, then exits the container.

Cleanup

kubectl delete -f cm.yaml
kubectl delete -f pod.yaml --grace-period=0 --force

Deletes the ConfigMap and pod resources.

YAML Configuration Files

cm.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-example
data:
  state: Michigan
  city: Ann Arbor

ConfigMap Explanation:

ConfigMap Structure:
  • apiVersion: v1 → Specifies the Kubernetes API version
  • kind: ConfigMap → Defines this as a ConfigMap resource
  • metadata.name: cm-example → Names the ConfigMap "cm-example"
Data Section:
  • state: Michigan → Key-value pair for state information
  • city: Ann Arbor → Key-value pair for city information
How It Works:

ConfigMaps allow you to decouple configuration artifacts from container images. This ConfigMap stores two key-value pairs that can be consumed by pods as environment variables, command-line arguments, or configuration files.

pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: mybox
spec:
  restartPolicy: Always
  containers:
  - name: mybox
    image: busybox
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 250m
        memory: 256Mi    
    command:
      - sleep
      - "3600"
    env:
      - name: CITY
        valueFrom:
          configMapKeyRef:
            name: cm-example
            key: city

Pod Explanation:

Pod Structure:
  • apiVersion: v1 → Specifies the Kubernetes API version
  • kind: Pod → Defines this as a Pod resource
  • metadata.name: mybox → Names the pod "mybox"
  • restartPolicy: Always → Specifies restart policy
Container Configuration:
  • image: busybox → Uses the lightweight BusyBox image
  • resources → Defines CPU and memory requests/limits
  • command: ["sleep", "3600"] → Runs sleep command to keep container alive
Environment Variables:
  • env → Defines environment variables for the container
  • name: CITY → Names the environment variable "CITY"
  • valueFrom.configMapKeyRef → References a value from a ConfigMap
  • name: cm-example → References the ConfigMap named "cm-example"
  • key: city → Uses the value of the "city" key from the ConfigMap
How It Works:

This pod creates a BusyBox container that sleeps for 3600 seconds (1 hour). The container has an environment variable named CITY that gets its value from the "city" key in the "cm-example" ConfigMap. When you exec into the container and run "echo $CITY", it will display "Ann Arbor".