Skip to content

🎓 LEVEL 36 DEBRIEF: ConfigMap Key Management

Congratulations! You've mastered ConfigMap key references - essential for application configuration in Kubernetes!


📊 What You Fixed

The Problem:

data:
  app_name: "MyApp"
  app_version: "1.0.0"
  # ❌ Missing: database_host

env:
- name: DATABASE_HOST
  valueFrom:
    configMapKeyRef:
      key: database_host  # ❌ Key doesn't exist!

The Solution:

data:
  app_name: "MyApp"
  app_version: "1.0.0"
  database_host: "postgres.k8squest.svc.cluster.local"  # ✅ Added!


🔍 ConfigMap Fundamentals

ConfigMaps store non-sensitive configuration data as key-value pairs.

Three Ways to Use ConfigMaps

1. Environment Variables:

env:
- name: APP_NAME
  valueFrom:
    configMapKeyRef:
      name: app-config
      key: app_name

2. All Keys as Environment Variables:

envFrom:
- configMapRef:
    name: app-config
# Creates env vars for ALL keys

3. As Files (Volume Mount):

volumes:
- name: config
  configMap:
    name: app-config
volumeMounts:
- name: config
  mountPath: /etc/config
# Each key becomes a file


💥 Common Mistakes

Mistake 1: Key Typo

data:
  database_host: "..."  # Defined as database_host

env:
- valueFrom:
    configMapKeyRef:
      key: databaseHost  # ❌ Wrong case!

Mistake 2: Missing Required Key

# ConfigMap has: app_name, app_version
# Pod needs: app_name, app_version, database_host
# Result: Pod fails to start

Mistake 3: Using configMapKeyRef Without Optional Flag

env:
- name: OPTIONAL_CONFIG
  valueFrom:
    configMapKeyRef:
      name: config
      key: optional_key  # ❌ If missing, pod fails
      # Should add: optional: true

🛡️ Best Practices

  1. Validate all required keys exist:

    kubectl get configmap app-config -o jsonpath='{.data}'
    

  2. Use optional for non-critical configs:

    configMapKeyRef:
      key: feature_flag
      optional: true
    

  3. Document expected keys:

    metadata:
      annotations:
        required-keys: "app_name,database_host,api_key"
    

  4. Use envFrom for all keys:

    envFrom:
    - configMapRef:
        name: app-config
    


🎯 Key Takeaways

  1. All referenced keys must exist - Or pod fails to start
  2. Keys are case-sensitive - database_host ≠ databaseHost
  3. Three usage patterns - Env vars, envFrom, volume mounts
  4. Use optional: true for non-critical - Allows pod to start
  5. Validate before deployment - Check ConfigMap has all required keys

Well done! You understand ConfigMap key management! 🎉