,

Kubernetes Secrets vs. ConfigMaps – Which One and When? (2025 Guide)

Posted by

πŸ” Kubernetes Secrets vs. ConfigMaps – Which One and When? (2025 Guide)

In the world of Kubernetes, separating code from configuration is a best practice. But when it comes to managing settings like environment variables, API tokens, and database credentials β€” you’ll come across two powerful tools:

  • βœ… ConfigMaps
  • πŸ”’ Secrets

They both store configuration, but they are NOT the same β€” and using them interchangeably could lead to security issues or operational headaches.

Let’s break them down β€” from basics to best practices β€” and figure out which one to use, when.


🧠 What’s the Difference Between a ConfigMap and a Secret?

FeatureConfigMapSecret
PurposeStore non-sensitive config dataStore sensitive data like passwords, tokens
EncodingStored in plain textStored in base64-encoded format
Access ControlReadable by defaultCan be restricted using RBAC
EncryptionNot encrypted at restCan be encrypted at rest (KMS or etcd config)
MountingAs files or env varsSame (files/env), but secured
Example UseApp name, log level, config togglesAPI keys, DB passwords, SSH keys

πŸ”§ 1. What is a ConfigMap?

ConfigMaps are used to store non-sensitive configuration in a key-value format.

βœ… Common Use Cases:

  • Application name, port, timezone
  • Feature flags
  • JSON configs
  • External URLs

✍️ Example:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  APP_NAME: "myapp"
  LOG_LEVEL: "debug"

πŸ“¦ Usage in Pod:

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

πŸ” 2. What is a Secret?

Secrets are used to store sensitive data like passwords, tokens, SSH keys, and TLS certs.

Though they’re base64-encoded by default (⚠️ not encrypted), Kubernetes supports encryption at rest and RBAC to control access.

βœ… Common Use Cases:

  • DB credentials
  • OAuth tokens
  • TLS certificates
  • Service account keys

✍️ Example:

apiVersion: v1
kind: Secret
metadata:
  name: db-secret
type: Opaque
data:
  DB_PASSWORD: cGFzc3dvcmQxMjM=  # base64 encoded

πŸ” echo -n 'password123' | base64 = cGFzc3dvcmQxMjM=

πŸ“¦ Usage in Pod:

env:
- name: DB_PASSWORD
  valueFrom:
    secretKeyRef:
      name: db-secret
      key: DB_PASSWORD

πŸ” 3. Are Secrets Really Secure?

Yes, if you configure them properly:

FeatureEnabled By
πŸ”’ Encryption at RestSet up in EncryptionConfiguration for etcd
βœ… RBAC Access ControlUse Role + RoleBinding to restrict access
πŸ‘οΈ Audit LoggingLog access to Secrets
πŸ”„ Automatic RotationUse external Secret managers (Vault, AWS Secrets Manager)

⚠️ By default, Secrets are just base64 β€” not truly secure.
Always combine them with proper encryption, least-privilege access, and rotation practices.


πŸ”€ 4. How to Choose – ConfigMap or Secret?

ScenarioUse
βœ… Application nameConfigMap
βœ… Logging level (debug/info)ConfigMap
πŸ” API token for 3rd-party serviceSecret
πŸ” Database passwordSecret
βœ… Feature toggle flagsConfigMap
πŸ” SSL cert/key pairSecret (TLS type)
βœ… External API URLConfigMap
πŸ” OAuth client secretSecret

🧠 Rule of Thumb:
If you wouldn’t share the value on a public forum, put it in a Secret.


πŸ› οΈ 5. Best Practices

βœ… ConfigMaps:

  • Keep unrelated config keys in separate ConfigMaps.
  • Use volumes if you want to mount full configs (e.g., YAML or JSON files).
  • Version them using labels or annotations.

βœ… Secrets:

  • Enable encryption at rest for etcd.
  • Store only what is absolutely necessary.
  • Integrate with external secret managers like:
    • HashiCorp Vault
    • AWS Secrets Manager
    • Azure Key Vault
  • Rotate credentials regularly and use initContainers or sidecars for dynamic secrets.

🧩 Bonus: Use Both Together

Yes, you can β€” and often should β€” use ConfigMaps and Secrets together in the same pod.

Example:

env:
- name: ENV
  valueFrom:
    configMapKeyRef:
      name: app-config
      key: ENV
- name: DB_PASSWORD
  valueFrom:
    secretKeyRef:
      name: db-secret
      key: DB_PASSWORD

This keeps your sensitive data isolated, while making general configuration easily manageable.


πŸ’Ό Real-World Example: A Web App in K8s

Config ItemSourceReason
DB_HOSTConfigMapSafe to expose
DB_PORTConfigMapNon-sensitive
DB_PASSWORDSecretNeeds encryption
API_KEYSecretConfidential
APP_ENVConfigMapJust toggles
TLS Key/CertSecret (TLS type)Secure by default

🧠 Final Thoughts

  • Use ConfigMaps for what’s safe to expose
  • Use Secrets for anything you’d protect with encryption
  • Secure your Secrets with RBAC, encryption, and external managers
  • Keep everything version-controlled and auditable

Don’t treat your Kubernetes cluster like a vault.
Treat it like an API-driven operating system, and secure it the same way.


Just let me know!

Leave a Reply

Your email address will not be published. Required fields are marked *

0
Would love your thoughts, please comment.x
()
x