ConfigMap Example
Download the YAML configuration files for the ConfigMap example
Create the ConfigMap
Applies the configuration from the YAML file to create the ConfigMap.
Get the ConfigMap info
Lists all ConfigMaps and shows detailed information about the cm-example ConfigMap.
Output ConfigMap in YAML format
Outputs the ConfigMap information in YAML format.
Deploy the pod
Creates a pod that uses the ConfigMap data as environment variables.
Connect to the Busybox
Opens a shell inside the running Busybox container.
Display the CITY env variable
Displays the CITY environment variable value from the ConfigMap, then exits the container.
Cleanup
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".