
Overview
Kubernetes Secrets provide a way to store and manage sensitive information such as passwords, tokens, and keys. Unlike ConfigMaps (which store plain-text configuration), Secrets store values as base64-encoded data. Remember: base64 is only encoding, not encryption — apply RBAC, encryption at rest, and external KMS integrations for production workloads.Kubernetes Secrets store values as base64-encoded data. This encoding hides values from casual viewing but is not strong encryption. Use encryption at rest and appropriate RBAC policies for production.
Example application: what not to do
Below is a simple Python Flask app that connects to MySQL. The database hostname, username, and password are hardcoded — which is insecure and not recommended.Move non-sensitive configuration to ConfigMaps
ConfigMaps are suitable for non-sensitive values (hostnames, feature flags). Do not store credentials here in production. Example ConfigMap (declarative):DB_Password above should not be stored in a ConfigMap — shown here only for contrast.
Secrets vs ConfigMaps (quick comparison)
Creating Secrets
Two common workflows:- Imperative:
kubectl create secretfrom literals or files. - Declarative: apply a YAML Secret manifest where
datais base64-encoded.
Declarative creation requires base64-encoded values
Convert values to base64 on a Linux/macOS host:kubectl create -f or kubectl apply -f):
Inspecting Secrets
List secrets:--decode or -d; macOS may use -D.)
Injecting Secrets into Pods
Secrets can be exposed to containers in two common ways:- Environment variables (single keys or all keys at once).
- Mounted as files via a secret volume (each key becomes a file with the decoded content).
envFrom:
app-secret becomes an environment variable inside the container (e.g., DB_Host, DB_User, DB_Password).
Example: mount the Secret as files inside a volume:
Best practices and security considerations
- Do not store plaintext credentials in ConfigMaps.
- Treat base64 as encoding, not encryption—enable encryption at rest for Secrets on the cluster.
- Restrict Secrets access using RBAC policies.
- Consider integrating with an external secrets manager (HashiCorp Vault, AWS KMS/GCP KMS/Azure Key Vault) for higher assurance.
Remember: base64 encoding is not encryption. For production workloads, enable encryption at rest for Secrets, restrict access via RBAC, and consider integrating with an external secrets manager or KMS for better security.
- Kubernetes Secrets documentation: https://kubernetes.io/docs/concepts/configuration/secret/
- Kubernetes ConfigMap documentation: https://kubernetes.io/docs/concepts/configuration/configmap/
- Best practices for secret management: https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/