ConfigMaps and Secrets
You'll learn: How to separate configuration and sensitive data from your application code using Kubernetes ConfigMaps and Secrets.
The Problem: Hardcoded Configuration
Imagine you built an application with database URLs, API endpoints, and passwords baked directly into your code. Every environment change—development, staging, production—requires rebuilding your container image. Even worse, your credentials are now visible in your source code repository. This violates the principle of separating configuration from code.
ConfigMaps: Externalized Configuration
ConfigMaps store non-sensitive configuration data as key-value pairs or entire configuration files. Think of them as a settings file that lives outside your container but gets injected at runtime.
You might store:
- Feature flags (
ENABLE_BETA_FEATURES=true) - API endpoint URLs
- Application-specific settings
- Configuration files (like
nginx.conf)
ConfigMaps can be consumed as environment variables, command-line arguments, or mounted as files in your Pod's filesystem.
Secrets: Protected Sensitive Data
Secrets work similarly to ConfigMaps but are designed for sensitive information. Kubernetes stores Secrets base64-encoded (not encrypted by default) and provides additional access controls.
Store in Secrets:
- Database passwords
- API keys and tokens
- TLS certificates
- SSH keys
Analogy: ConfigMaps are like a bulletin board in your office—anyone can read settings openly. Secrets are like a locked filing cabinet—still accessible to authorized pods, but with restricted access and handled more carefully.
Why This Matters
Separating configuration allows you to deploy the same container image across all environments, changing only the ConfigMap or Secret. Your code stays clean, credentials stay out of version control, and updates don't require rebuilding images.
Key Takeaway: ConfigMaps handle non-sensitive settings while Secrets manage sensitive data, both keeping configuration external to your container images for flexibility, security, and compliance with the twelve-factor app methodology.