What You Will Learn
Beginner
- What ConfigMaps are
- How to create and use them
- Injecting config into Pods
configmap.yamlyaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
DB_HOST: "postgres.default.svc.cluster.local"
DB_PORT: "5432"
LOG_LEVEL: "info"
config.yaml: |
database:
host: postgres
port: 5432
logging:
level: infoUsing ConfigMaps in Pods
Terminalyaml
# Method 1: As environment variables
spec:
containers:
- name: app
image: my-app
envFrom:
- configMapRef:
name: app-config
# Method 2: As a single env var
spec:
containers:
- name: app
env:
- name: DB_HOST
valueFrom:
configMapKeyRef:
name: app-config
key: DB_HOST
# Method 3: As a file (volume)
spec:
containers:
- name: app
volumeMounts:
- name: config
mountPath: /etc/config
volumes:
- name: config
configMap:
name: app-configWhy ConfigMaps?
Without ConfigMaps: config is baked into the image. Need to change a DB host? Rebuild the image. With ConfigMaps: change the ConfigMap, restart the Pod. Same image works in dev/staging/prod.Practical Exercise
Create a ConfigMap with DB_HOST and DB_PORT
Apply: kubectl apply -f configmap.yaml
Create a Pod that uses envFrom to load it
Exec in: kubectl exec -it pod -- env | grep DB
Verify the values are injected
Key Takeaways
- ConfigMap = key-value config data (non-sensitive).
- Three ways to use: envFrom (all), valueFrom (one), volume (file).
- Separates config from image - same image for dev/prod.
- ConfigMaps are NOT for secrets - use Secrets instead.
- Changing ConfigMap requires Pod restart to take effect.
Comments
Comments
Post a Comment