π§ Concept 7: ConfigMap & Secret (Configuration Management π―)




π 1. Core Idea (1-line)
π ConfigMap & Secret are used to externalize configuration from your container
π§ 2. Why This Concept Exists (VERY IMPORTANT β οΈ)
Bad practice β:
image: app:v1
env:
DB_PASSWORD: mypassword123Problems:
-
Hardcoded values π¬
-
Not secure π
-
Not reusable π
π Solution:
-
ConfigMap β normal config
-
Secret β sensitive data
π¦ 3. ConfigMap (Non-sensitive data)
π Used for:
-
URLs
-
Config files
-
Feature flags
Example:
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
APP_ENV: production
LOG_LEVEL: debugπ 4. Secret (Sensitive data)
π Used for:
-
Passwords
-
API keys
-
Tokens
Example:
apiVersion: v1
kind: Secret
metadata:
name: app-secret
type: Opaque
data:
DB_PASSWORD: bXlwYXNzd29yZA==π Value is base64 encoded (NOT encrypted β οΈ)
β οΈ 5. VERY IMPORTANT Difference
| Feature | ConfigMap | Secret |
|---|---|---|
| Data type | Plain text | Base64 encoded |
| Use case | Non-sensitive | Sensitive |
| Security | Low | Slightly better |
π But:
β οΈ Secret is NOT fully secure unless:
-
Encryption at rest enabled
-
RBAC properly configured
βοΈ 6. How to Use in Pod
As Environment Variables:
envFrom:
- configMapRef:
name: app-config
- secretRef:
name: app-secretAs Volume (VERY IMPORTANT π₯)
volumes:
- name: config-vol
configMap:
name: app-configπ Mounted as files inside container
π₯ 7. Real-world Example (DevOps / ML)
You deploy ML API:
-
DB URL β ConfigMap
-
DB Password β Secret
π Clean + secure + flexible
π₯ 8. Key Benefits
-
No hardcoding
-
Easy updates
-
Reusable configs
-
Better security separation
β οΈ 9. Common Mistakes
β Putting secrets in ConfigMap
β Thinking base64 = encryption
β Hardcoding creds in image
πΌ 10. Interview Answer
π βConfigMap stores non-sensitive configuration data, while Secret is used to store sensitive information like passwords and tokens, both of which can be injected into pods.β
β‘ 11. CKA Commands
kubectl create configmap app-config --from-literal=key=value
kubectl create secret generic app-secret \
--from-literal=DB_PASSWORD=mypassword
kubectl get cm
kubectl get secretπ§ 12. Memory Trick
π ConfigMap = settings βοΈ
π Secret = passwords π
π₯ 13. Pro Insight (Real-world)
In production:
-
Use Vault / AWS Secrets Manager
-
Donβt rely only on K8s Secret
π Next Step
Bol:
π βnextβ
Then we go to:
π₯ Concept 8: Volume & Persistent Volume (Storage π― β VERY IMPORTANT)