How do Kubernetes Secrets work, and are they secure?
IntermediateConceptDatabase connections in apps
Answer
Short answer
A Secret is a Kubernetes object that holds sensitive values (passwords, tokens, certificates) and makes them available to pods as environment variables or mounted files. By default the values are only base64-encoded, not encrypted, so security depends on enabling encryption at rest in etcd, strict RBAC, and ideally sourcing them from an external secret manager.
Example
kubectl create secret generic orders-db \
--from-literal=username=orders_app \
--from-literal=password='S3cret!'
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef: { name: orders-db, key: password }
# or as files:
volumeMounts: [{ name: db, mountPath: /etc/secrets, readOnly: true }]
volumes: [{ name: db, secret: { secretName: orders-db } }]
Base64 is not encryption
kubectl get secret orders-db -o jsonpath='{.data.password}' | base64 -d # S3cret!
Making them safer
- Encryption at rest for etcd (managed clusters like EKS/AKS support KMS encryption).
- RBAC — only specific service accounts can
getsecrets in a namespace. - Don't commit Secret YAML to Git — use External Secrets Operator, Sealed Secrets or SOPS.
- Prefer mounted files for very sensitive values (env vars can leak via crash dumps or
/proc).
What to say in the interview
"Secrets are base64-encoded objects exposed as env vars or files. They're only as secure as etcd encryption and RBAC, so we enable KMS encryption, restrict access, and populate them from Vault or a cloud secret manager via External Secrets rather than storing YAML in Git."
More on Database connections in apps
How does an application connect to a database, and how should connection details be passed?BeginnerAfter deployment, the app can't connect to the database. How do you troubleshoot it?IntermediateWhat is a connection string? Explain its parts.BeginnerWhat are the default ports for common databases?BeginnerWhy should database credentials never be stored in code or Git?BeginnerWhat is the External Secrets Operator?Intermediate