Pass sensitive data without baking it into container images
Security note: Environment variables are visible in kubectl describe pod, docker inspect, and /proc/<pid>/environ. For highly sensitive secrets, consider using a secrets manager sidecar or volume-mounted secret files instead of env vars. Source: CKA Day 11
Exam Trap:configMapKeyRef.name refers to the ConfigMap object name, not the file name. The key must exist in the ConfigMap or the Pod will fail to start. Source: CKA Day 11
Warning: Secrets are base64-encoded, not encrypted, by default. Anyone with read access to the Secret object can decode the values. Enable encryption at rest for production clusters. Source: CKA Day 11
Downward API: Pod-Generated Values
Kubernetes can inject metadata about the Pod itself as environment variables:
This creates one environment variable per key in the ConfigMap. Caution: Key names must be valid environment variable names (no hyphens, must start with a letter or underscore).
Troubleshooting Environment Variables
# List env vars inside a running containerkubectl exec my-pod -- env | sort# Check if a specific variable is setkubectl exec my-pod -- sh -c 'echo $DB_PASSWORD'# Verify ConfigMap contentskubectl get configmap app-config -o yaml# Verify Secret contents (base64 decoded)kubectl get secret db-secret -o jsonpath='{.data.password}' | base64 -d
Issue
Cause
Fix
Variable not set
Typo in name or missing valueFrom
Check YAML spelling and indentation
Invalid value
ConfigMap/Secret key doesn’t exist
Verify the referenced object and key
CreateContainerConfigError
Secret or ConfigMap missing
Create the referenced object first
Base64 garbage
Forgot to encode value in Secret
Use `echo -n “value”
CKA Exam Patterns
# Quickly create a Pod with an env varkubectl run debug --image=busybox --env="KEY=VALUE" --restart=Never -- sleep 3600# Create a ConfigMap from literal valueskubectl create configmap app-config --from-literal=db_host=postgres --from-literal=db_port=5432# Create a Secret from literal values (auto base64 encodes)kubectl create secret generic db-secret --from-literal=password=secret123
Exam Tip:kubectl create secret automatically base64-encodes the value for you. You don’t need to encode manually when using the CLI. Source: CKA Day 11
Practical Practice
Exam-style hands-on tasks for this topic. Complete each task before reviewing the solution. Time yourself — CKA tasks average 5–7 minutes.
Task 1: Inject a Literal Environment Variable
You are asked to create a Pod logger with environment variable LOG_LEVEL=debug injected directly.
Requirements: Use the env field in the container spec.
Verification:kubectl exec logger -- env | grep LOG_LEVELSolution:
kubectl run logger --image=busybox --restart=Never --env="LOG_LEVEL=debug" -- sleep 3600
Task 2: Inject a ConfigMap Value as an Env Var
You are asked to inject the ConfigMap key database.url into a Pod api as environment variable DB_URL.
Requirements: Use valueFrom with configMapKeyRef.
Verification:kubectl exec api -- printenv DB_URLSolution:
# Assume ConfigMap my-config already has key database.urlkubectl run api --image=busybox --restart=Never --dry-run=client -o yaml > api.yaml# Edit api.yaml to add under containers[].env:# - name: DB_URL# valueFrom:# configMapKeyRef:# name: my-config# key: database.urlkubectl apply -f api.yaml
Task 3: Inject Pod Metadata with the Downward API
You are asked to inject the Pod’s own namespace name into an env var POD_NAMESPACE.
Requirements: Use valueFrom with fieldRef.
Verification:kubectl exec <pod> -- printenv POD_NAMESPACESolution:
kubectl run meta --image=busybox --restart=Never --dry-run=client -o yaml > meta.yaml# Edit meta.yaml to add under containers[].env:# - name: POD_NAMESPACE# valueFrom:# fieldRef:# fieldPath: metadata.namespacekubectl apply -f meta.yaml