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

πŸ” 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!

Related Posts

Kubernetes RBAC cookbook: common roles (dev, SRE, read-only) safely

Kubernetes RBAC is one of those things that feels annoying until the day it saves your cluster from β€œoops, I deleted prod.” The tricky part isn’t what…

Read More

General Build Pipeline (CI) in Azure DevOps

Let’s walk through a general-purpose Azure DevOps build pipeline template that can be applied to most common application types (web apps, APIs, microservices, etc.) using CI/CD best…

Read More

Kubernetes Certifications in 2025: Which One Should You Choose?

Kubernetes Certifications in 2025: Which One Should You Choose? Kubernetes has become the cornerstone of modern cloud-native application deployment and orchestration. Whether you’re a beginner just diving…

Read More

Top 5 Kubernetes Tools That Will Replace Your Bash Scripts in 2025

πŸš€ Top 5 Kubernetes Tools That Will Replace Your Bash Scripts in 2025 For years, DevOps engineers and platform teams have relied on Bash scripts to automate…

Read More

Kubernetes Horror Stories (and What We Learned From Them)

☠️ Kubernetes Horror Stories (and What We Learned From Them) Because what doesn’t kill your cluster makes it stronger. Kubernetes is powerful β€” but with great power…

Read More

Why Your Kubernetes Costs Are Out of Control – and How to Fix It (2025 Guide)

πŸ’Έ Why Your Kubernetes Costs Are Out of Control – and How to Fix It (2025 Guide) Kubernetes is powerful β€” but it isn’t cheap. While it…

Read More
guest
0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x