Kubernetes Secret
A Kubernetes Secret stores sensitive data such as passwords, tokens, and TLS certificates separately from Pod specs, with base64 encoding and optional encryption at rest.
A Secret is a Kubernetes object designed to hold sensitive data: passwords, API tokens, OAuth credentials, SSH keys, and TLS certificates. Like a ConfigMap, it decouples sensitive configuration from container images. Unlike a ConfigMap, Secrets are base64-encoded in etcd, can be encrypted at rest using EncryptionConfiguration, and can be given stricter RBAC policies so that only workloads that need them can access them. Understanding Secrets correctly — including their security limitations — is a critical interview topic.
Secret Types
| Type | Use Case |
|---|---|
Opaque (default) |
Arbitrary user-defined key-value data (passwords, API tokens) |
kubernetes.io/tls |
TLS certificate and private key (tls.crt and tls.key) |
kubernetes.io/dockerconfigjson |
Docker registry credentials for image pulls |
kubernetes.io/service-account-token |
Auto-mounted token for ServiceAccount authentication (legacy) |
kubernetes.io/basic-auth |
Username and password for basic authentication |
kubernetes.io/ssh-auth |
SSH private key |
Creating a Secret
From the command line (values are auto base64-encoded):
kubectl create secret generic db-credentials \
--from-literal=DB_USERNAME=appuser \
--from-literal=DB_PASSWORD=change-me
# Verify (values are shown base64-encoded)
kubectl get secret db-credentials -o yaml
From a manifest (you must base64-encode the values yourself):
echo -n 'appuser' | base64 # prints the encoded value to paste below
echo -n 'change-me' | base64 # prints the encoded value to paste below
apiVersion: v1
kind: Secret
metadata:
name: db-credentials
namespace: default
type: Opaque
data:
DB_USERNAME: YXBwdXNlcg==
DB_PASSWORD: <base64-encoded-password>
You can also use stringData to supply plain-text values; Kubernetes encodes them automatically on write:
apiVersion: v1
kind: Secret
metadata:
name: db-credentials
namespace: default
type: Opaque
stringData:
DB_USERNAME: appuser
DB_PASSWORD: change-me
Consuming a Secret in a Pod
As Environment Variables
apiVersion: v1
kind: Pod
metadata:
name: spring-app
spec:
containers:
- name: spring-app
image: eclipse-temurin:21-jre
command: ["java", "-jar", "/app/app.jar"]
env:
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: db-credentials
key: DB_USERNAME
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-credentials
key: DB_PASSWORD
As a Volume (Recommended for Sensitive Files)
Mounting as a volume avoids the risk of environment variable leakage through process listings or error logs:
spec:
volumes:
- name: db-secret-vol
secret:
secretName: db-credentials
containers:
- name: spring-app
image: eclipse-temurin:21-jre
volumeMounts:
- name: db-secret-vol
mountPath: /etc/secrets
readOnly: true
# Files available at /etc/secrets/DB_USERNAME and /etc/secrets/DB_PASSWORD
TLS Secret
# Create a TLS Secret from cert and key files
kubectl create secret tls nginx-tls \
--cert=tls.crt \
--key=tls.key
apiVersion: v1
kind: Secret
metadata:
name: nginx-tls
type: kubernetes.io/tls
data:
tls.crt: <base64-encoded certificate>
tls.key: <base64-encoded private key>
Common Mistakes
- Secrets are base64-encoded, not encrypted by default. Anyone with
kubectl get secretaccess can decode them trivially withbase64 -d. EnableEncryptionConfigurationon the API server to encrypt Secrets at rest inetcd, or use an external secrets manager (HashiCorp Vault, AWS Secrets Manager) with a CSI driver. - Committing Secret manifests with real values to Git. Even base64-encoded values are trivially decoded. Never commit Secrets with real credentials. Use sealed secrets, SOPS, or external secret operators to store encrypted forms.
- Using environment variables for highly sensitive credentials. Env vars are visible in process listings (
/proc/<pid>/environ) and may be leaked in crash dumps. Mount Secrets as files for the highest security. - Not scoping RBAC for Secrets. Without explicit RBAC restrictions, any Pod that can read Secrets in a namespace can access all of them. Grant
geton Secrets only to the ServiceAccounts that need them. - Referencing a non-existent Secret key. Just like ConfigMap, a missing
secretKeyRef.keycauses aCreateContainerConfigErrorand the Pod will not start.
Best Practices
- Enable encryption at rest for Secrets using
EncryptionConfigurationor a KMS plugin. - Use an external secrets manager (Vault, AWS Secrets Manager, GCP Secret Manager) with the Secrets Store CSI Driver for secrets rotation and audit trails.
- Apply RBAC policies that restrict
get/liston Secrets to only the ServiceAccounts that need them. - Mount Secrets as read-only volumes rather than environment variables for the most sensitive credentials.
- Rotate credentials regularly and update Secrets using
kubectl create secret --dry-run=client -o yaml | kubectl apply -f -.
Interview Questions
1. What is a Kubernetes Secret and how does it differ from a ConfigMap?
A Secret is designed for sensitive data (passwords, tokens, certificates), while a ConfigMap is for non-sensitive configuration. Secrets are base64-encoded in etcd and can be encrypted at rest; ConfigMaps are plain text. Secrets can also be given stricter RBAC policies to limit which Pods or users can read them.
2. Are Kubernetes Secrets actually secure?
By default, Secrets are only base64-encoded in etcd — not encrypted. They can be decoded trivially. To make Secrets genuinely secure: enable EncryptionConfiguration on the API server to encrypt them at rest in etcd, restrict RBAC to limit who can read them, and consider using external secrets management systems like HashiCorp Vault with the CSI driver for rotation and audit logging.
3. What are the two ways to consume a Secret in a Pod?
As environment variables using env.valueFrom.secretKeyRef or envFrom.secretRef, which sets the value at container startup; and as volume mounts, where each key becomes a file in a directory, mounted with restricted file permissions. Volume mounts are preferred for sensitive credentials because env vars can leak through process listings and crash dumps.
4. What is stringData in a Secret manifest?
stringData lets you provide plain-text values in a Secret manifest instead of manually base64-encoding them. Kubernetes encodes the values before persisting them to etcd. stringData is write-only — when you read the Secret back, values appear only in the data field as base64.
5. What happens if a Pod references a Secret that does not exist?
The Pod cannot start and enters a Pending state. The events will show Unable to mount volumes or CreateContainerConfigError. The Pod will remain pending until the Secret is created.
6. How would you rotate a Secret without downtime?
Update the Secret with the new credential using kubectl create secret generic <name> --from-literal=key=newvalue --dry-run=client -o yaml | kubectl apply -f -. If the Secret is mounted as a volume, the files update within ~60 seconds. If consumed as env vars, trigger a rolling restart: kubectl rollout restart deployment/<name>.
7. What is an imagePullSecret?
An imagePullSecret is a Secret of type kubernetes.io/dockerconfigjson that contains registry credentials. You reference it in a Pod's spec.imagePullSecrets field (or attach it to a ServiceAccount), allowing the kubelet to authenticate with a private container registry when pulling images.
Summary
- A
Secretstores sensitive data (passwords, tokens, TLS certs) separately from Pod specs, with base64 encoding. - Base64 encoding is not encryption — enable
EncryptionConfigurationor use an external secrets manager for real security. - Consume Secrets as volume mounts (preferred) or environment variables; volume mounts are more secure.
- Never commit Secret manifests with real credentials to Git; use sealed secrets or external secret operators.
- Restrict Secret access via RBAC so only the ServiceAccounts that need a Secret can read it.
Continue Learning
Was this guide helpful?
CodeWithVenu is free to read, with no paywall. If this guide saved you time or helped you prepare, a small voluntary contribution helps me write more practical tutorials, architecture guides, and interview-prep content.