CKAD — ConfigMaps

1 minute read

Configuration Management in Kubernetes

Applications need configuration — database URLs, feature flags, API keys, passwords. Hard-coding these in container images is a bad practice. Instead, Kubernetes provides ConfigMaps for non-sensitive data and Secrets for sensitive data.

This chapter covers the Application Environment, Configuration & Security domain (25% of the CKAD exam — the highest-weighted domain).

ConfigMaps

A ConfigMap stores key-value pairs that you inject into Pods as environment variables or mount as files.

Creating ConfigMaps

From Literals

kubectl create configmap app-config \
  --from-literal=DATABASE_HOST=postgres.default.svc \
  --from-literal=DATABASE_PORT=5432 \
  --from-literal=LOG_LEVEL=info

From a File

# Create a config file
cat <<EOF > app.properties
database.host=postgres.default.svc
database.port=5432
log.level=info
max.connections=100
EOF

kubectl create configmap app-config --from-file=app.properties

From an Env File

# Create an env file
cat <<EOF > app.env
DATABASE_HOST=postgres.default.svc
DATABASE_PORT=5432
LOG_LEVEL=info
EOF

kubectl create configmap app-config --from-env-file=app.env

Declarative (YAML)

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  DATABASE_HOST: "postgres.default.svc"
  DATABASE_PORT: "5432"
  LOG_LEVEL: "info"
  nginx.conf: |
    server {
      listen 80;
      server_name localhost;
      location / {
        proxy_pass http://backend:8080;
      }
    }

Viewing ConfigMaps

kubectl get configmap app-config
kubectl describe configmap app-config
kubectl get configmap app-config -o yaml

ConfigMap Immutability

For production, you can make a ConfigMap immutable to prevent accidental changes:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  LOG_LEVEL: "info"
immutable: true

Once set, you cannot modify the data. You have to delete and recreate the ConfigMap.


Next: Using ConfigMaps →

Updated: