Bird
Raised Fist0
Microservicessystem_design~7 mins

ConfigMaps and Secrets in Microservices - System Design Guide

Choose your learning style10 modes available
Problem Statement
Hardcoding configuration data and sensitive information like passwords inside application code or container images causes security risks and operational inflexibility. When configuration changes, redeploying the entire application is slow and error-prone, and leaking secrets can compromise the whole system.
Solution
ConfigMaps and Secrets separate configuration data and sensitive information from application code by storing them externally and injecting them into containers at runtime. ConfigMaps hold non-sensitive configuration as key-value pairs, while Secrets store sensitive data encrypted or base64-encoded. This allows dynamic updates without rebuilding images and improves security by controlling access separately.
Architecture
ConfigMap
(non-sensitive
→Kubernetes
↓
Secret
(sensitive
→Kubernetes

This diagram shows how ConfigMaps and Secrets are stored in Kubernetes and injected into application containers via the API server at runtime.

Trade-offs
✓ Pros
→
Separates configuration and secrets from application code, enabling safer and faster updates.
→
Supports dynamic configuration changes without rebuilding or redeploying containers.
→
Improves security by controlling access to secrets separately from application code.
→
Standardized Kubernetes resources simplify management and integration with orchestration.
✗ Cons
→
Secrets stored in Kubernetes are base64-encoded by default, which is not strong encryption without additional measures.
→
Managing secret rotation and access policies adds operational complexity.
→
Applications must be designed to reload configuration dynamically or restart to pick up changes.
Use when deploying microservices on Kubernetes or similar orchestrators that support externalized configuration and secrets, especially at scale with frequent config changes or sensitive data.
Avoid if running simple, single-instance applications without orchestration or if the platform does not support external config injection, or when secrets require hardware-backed encryption not provided by the orchestrator.
Real World Examples
Netflix
Uses Kubernetes Secrets to securely manage API keys and credentials for their microservices, enabling dynamic updates without redeploying services.
Spotify
Employs ConfigMaps to manage feature flags and environment-specific settings across their containerized services, allowing rapid configuration changes.
Airbnb
Leverages Kubernetes Secrets integrated with external vaults to protect sensitive user data and service credentials in their microservices architecture.
Code Example
This example shows how hardcoded configuration and secrets inside the code are replaced by environment variables injected at runtime. This enables external management of configuration and secrets, improving security and flexibility.
Microservices
### Before: Hardcoded configuration and secrets inside code
class Service:
    def __init__(self):
        self.db_host = "db.example.com"
        self.db_user = "admin"
        self.db_password = "password123"  # Hardcoded secret

    def connect(self):
        # Connect to database using above credentials
        pass


### After: Configuration and secrets injected via environment variables
import os

class Service:
    def __init__(self):
        self.db_host = os.getenv("DB_HOST")
        self.db_user = os.getenv("DB_USER")
        self.db_password = os.getenv("DB_PASSWORD")  # Injected secret

    def connect(self):
        # Connect to database using injected credentials
        pass
OutputSuccess
Alternatives
Environment Variables
Injects configuration and secrets directly as environment variables inside containers instead of using Kubernetes resources.
Use when: Use for simple applications or when orchestration support for ConfigMaps/Secrets is unavailable.
External Secret Management Systems
Stores secrets outside the orchestrator in dedicated vaults like HashiCorp Vault or AWS Secrets Manager, accessed at runtime.
Use when: Choose when stronger encryption, audit logging, and secret rotation policies are required beyond Kubernetes native capabilities.
Embedded Configuration Files
Bundles configuration files inside container images or mounts them via volumes instead of using ConfigMaps.
Use when: Use when configuration rarely changes and external management overhead is not justified.
Summary
ConfigMaps and Secrets separate configuration and sensitive data from application code in microservices.
They enable dynamic updates and improve security by managing data externally and injecting it at runtime.
Proper use requires understanding their differences and designing applications to handle configuration changes.

Practice

(1/5)
1. What is the primary purpose of a ConfigMap in a microservices environment?
easy
A. To store encrypted passwords and tokens securely
B. To store non-sensitive configuration data separately from application code
C. To manage network traffic between microservices
D. To monitor the health of microservices

Solution

  1. Step 1: Understand ConfigMap role

    A ConfigMap is designed to hold configuration data that is not sensitive, such as environment variables or config files, separate from the application code.
  2. Step 2: Differentiate from Secrets

    Secrets are used for sensitive data like passwords, not ConfigMaps.
  3. Final Answer:

    To store non-sensitive configuration data separately from application code -> Option B
  4. Quick Check:

    ConfigMap = Non-sensitive config [OK]
Hint: ConfigMaps hold safe config, Secrets hold sensitive info [OK]
Common Mistakes:
  • Confusing ConfigMaps with Secrets
  • Thinking ConfigMaps encrypt data
  • Assuming ConfigMaps manage network or health
2. Which of the following is the correct way to create a Secret in Kubernetes using a literal value?
easy
A. kubectl create secret generic mysecret --from-literal=password=12345
B. kubectl create configmap mysecret --from-literal=password=12345
C. kubectl secret create mysecret --from-file=password.txt
D. kubectl create secret mysecret --password=12345

Solution

  1. Step 1: Recall Kubernetes Secret creation syntax

    The correct command uses kubectl create secret generic with --from-literal to create a secret from a literal key-value pair.
  2. Step 2: Eliminate incorrect options

    kubectl create configmap mysecret --from-literal=password=12345 uses configmap instead of secret, kubectl secret create mysecret --from-file=password.txt uses wrong command syntax, kubectl create secret mysecret --password=12345 uses invalid flag.
  3. Final Answer:

    kubectl create secret generic mysecret --from-literal=password=12345 -> Option A
  4. Quick Check:

    Secret creation uses 'kubectl create secret generic' [OK]
Hint: Secret creation uses 'kubectl create secret generic' [OK]
Common Mistakes:
  • Using configmap command instead of secret
  • Wrong flags like --password
  • Confusing file and literal input
3. Given this Kubernetes pod spec snippet, what will be the value of environment variable API_KEY inside the container?
env:
  - name: API_KEY
    valueFrom:
      secretKeyRef:
        name: mysecret
        key: api_key
medium
A. An empty string because 'api_key' is not defined
B. The literal string 'api_key'
C. The value stored under key 'api_key' in the Secret named 'mysecret'
D. The value of a ConfigMap named 'mysecret'

Solution

  1. Step 1: Understand env var from secretKeyRef

    The environment variable API_KEY is set from a Secret named mysecret, specifically the key api_key.
  2. Step 2: Clarify what value is assigned

    The value assigned is the secret data stored under that key, not the literal string or empty.
  3. Final Answer:

    The value stored under key 'api_key' in the Secret named 'mysecret' -> Option C
  4. Quick Check:

    secretKeyRef pulls secret value by key [OK]
Hint: secretKeyRef uses secret's key value, not literal text [OK]
Common Mistakes:
  • Assuming it uses the key name as string
  • Confusing ConfigMap with Secret
  • Thinking it defaults to empty if key missing
4. A developer tries to mount a Secret as a volume in a pod but gets an error. Which of these is the most likely cause?
medium
A. The Secret does not exist or has a different name than specified
B. The ConfigMap is missing required keys
C. The pod spec uses configMapKeyRef instead of secretKeyRef
D. The volume mount path is outside the container filesystem

Solution

  1. Step 1: Identify common Secret mount errors

    Mounting a Secret as a volume requires the Secret to exist with the exact name referenced.
  2. Step 2: Evaluate other options

    ConfigMap keys missing affect ConfigMaps, not Secrets. Using configMapKeyRef instead of secretKeyRef is for env vars, not volumes. Volume mount path errors cause different errors.
  3. Final Answer:

    The Secret does not exist or has a different name than specified -> Option A
  4. Quick Check:

    Secret volume mount fails if Secret missing [OK]
Hint: Check Secret name exists before mounting volume [OK]
Common Mistakes:
  • Mixing ConfigMap and Secret references
  • Ignoring Secret existence before mount
  • Confusing env var refs with volume mounts
5. You want to securely provide database credentials to multiple microservices without exposing them in code or logs. Which approach best uses ConfigMaps and Secrets together?
hard
A. Embed credentials directly in microservice code and use ConfigMaps for feature flags
B. Store credentials in ConfigMaps and encrypt them manually; use Secrets for logging configs
C. Put all configs and secrets in ConfigMaps for simplicity; avoid Secrets
D. Store all credentials in a Secret and non-sensitive configs in ConfigMaps; mount both as environment variables

Solution

  1. Step 1: Separate sensitive and non-sensitive data

    Best practice is to keep sensitive data like credentials in Secrets and non-sensitive configs in ConfigMaps.
  2. Step 2: Use environment variables for flexibility

    Mounting both as environment variables allows microservices to access configs securely without code changes.
  3. Final Answer:

    Store all credentials in a Secret and non-sensitive configs in ConfigMaps; mount both as environment variables -> Option D
  4. Quick Check:

    Secrets for sensitive, ConfigMaps for safe config [OK]
Hint: Secrets for sensitive data, ConfigMaps for safe config [OK]
Common Mistakes:
  • Putting secrets in ConfigMaps unencrypted
  • Embedding credentials in code
  • Using Secrets for non-sensitive data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard