What Is a Kubernetes Secret? Store Sensitive Data, Expose It to Pods, and Manage It Safely
A Kubernetes Secret is an API resource for sensitive values that a Pod or cluster component needs, such as passwords, tokens, private keys, or TLS certificates. Separating these values from application images and ordinary configuration lets you provide them only to the containers that need them. However, the name “Secret” and base64 encoding do not mean encryption. By default, Secret data may be stored unencrypted in etcd, so safe use also depends on storage encryption, least-privilege access, and preventing accidental exposure.
What does a Secret protect, and what does it not protect?
Secrets are intended for confidential data, unlike ConfigMaps, which are for non-confidential settings. You can use the default Opaque type for arbitrary key-value data or select a built-in type for common formats. For example, kubernetes.io/tls expects the keys tls.crt and tls.key, while kubernetes.io/dockerconfigjson holds registry configuration. A type communicates the intended format and can enforce the presence of certain keys, but it does not prove that a certificate or credential is valid or safe.
Values in the Secret data field are base64-encoded strings. Base64 is a representation, not encryption. If you commit a manifest containing base64-encoded credentials, anyone who can read the repository can recover them. stringData lets a manifest specify ordinary strings that Kubernetes merges into data; it does not make the source file safe to share. Kubernetes also cautions that stringData does not work well with server-side apply.
By default, Secret objects are stored unencrypted in etcd. Cluster administrators need to configure encryption for API data at rest and verify that existing Secrets are actually encrypted. This is not enabled simply by choosing a particular Kubernetes version. For a managed cluster, check the provider’s official documentation for its encryption feature and behavior. For a self-managed cluster, review API server configuration, key backups, and the process for re-encrypting existing objects. Access control with least-privilege RBAC is a separate requirement.
How do the two Pod delivery methods differ?
A Pod can consume a Secret through environment variables or files in a volume. Choose based on how the application reads and refreshes the value.
| Delivery method | Where the app sees it | What happens after the Secret changes | When it fits and what to consider |
|---|---|---|---|
env[].valueFrom.secretKeyRef | A named environment variable | Environment variables in an already-running process do not update. Recreate the Pod before the process can use a new value. | Convenient when an app or library expects credentials in environment variables. Prevent diagnostic tools, process dumps, or application logs from exposing them. |
| Secret volume | One file per key at a mounted path | A regular Secret volume eventually projects new content after kubelet change detection and synchronization. The app still needs to reread the file or reload its configuration. Files mounted with subPath do not receive automated updates. | Suitable for file-reading applications and makes it easy to expose only selected keys. Do not assume the app reloads automatically. |
The Pod and the referenced Secret must be in the same namespace. Create the Secret before a Pod that depends on it. If a required Secret or key is missing, container startup can be delayed or fail. In a Pod with several containers, configure the Secret only for the container that needs it. On Linux nodes, Kubernetes documents that kubelet stores a mounted Secret copy in tmpfs rather than durable storage. This does not prevent the container process or privileged node actors from reading values they can access.
Create a Secret from files and verify it without printing its values
The following example assumes an app in namespace demo reads a username and password from files. Obtain credentials from a secret manager or protected location, and keep the files out of source control and shared folders. The command reads files instead of placing credential text in shell arguments. Paths and resource names are illustrative; these commands were not run on a cluster.
kubectl create secret generic app-credentials \
--namespace=demo \
--from-file=username=./secure-inputs/username.txt \
--from-file=password=./secure-inputs/password.txt
kubectl describe secret app-credentials --namespace=demoNamespace demo must already exist. kubectl describe secret shows key names and byte counts without displaying values by default. In contrast, kubectl get secret ... -o yaml or a JSONPath query of .data can reveal the base64 strings in screen output or logs. Do not use them casually to inspect a Secret.
The Deployment below shows one Secret delivered both as environment variables and as read-only files. In a real workload, replace the example BusyBox image with the application image and keep only the method your app needs. The example container sleeps without using the credentials. Configure the real app not to log secret values and inject them only into the container that needs them.
apiVersion: apps/v1
kind: Deployment
metadata:
name: secret-demo
namespace: demo
spec:
replicas: 1
selector:
matchLabels:
app: secret-demo
template:
metadata:
labels:
app: secret-demo
spec:
containers:
- name: app
image: busybox:1.36.1
command: ["sh", "-c", "sleep 3600"]
env:
- name: DB_USER
valueFrom:
secretKeyRef:
name: app-credentials
key: username
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: app-credentials
key: password
volumeMounts:
- name: credentials
mountPath: /var/run/app-secrets
readOnly: true
volumes:
- name: credentials
secret:
secretName: app-credentials
items:
- key: username
path: username
- key: password
path: passwordAfter applying the manifest, check rollout status and confirm the references without printing the value. The commands below are examples and were not executed.
kubectl apply -f secret-demo.yaml
kubectl rollout status deployment/secret-demo --namespace=demo
kubectl exec deployment/secret-demo --namespace=demo -- sh -c 'test -n "$DB_PASSWORD"'
kubectl exec deployment/secret-demo --namespace=demo -- test -s /var/run/app-secrets/passwordThe first test checks only that the environment variable is nonempty; the second checks that the mounted file is nonempty. Neither proves that the application authenticated successfully to an external database. Verify that through an application health check or safe logs that do not expose credentials.
What happens to running Pods when a Secret changes?
Updating a Secret does not change an environment variable in an already-running container. Restart the Deployment so it creates Pods that receive the new value, then verify rollout success. A Secret volume can eventually project new content, but the timing depends on kubelet synchronization and cache behavior; the application must reread or reload the file. A subPath mount does not receive automatic updates.
When replacing a real credential, coordinate the Secret with the system that accepts it. If the external system supports a period when both old and new credentials work, you can deploy the new Secret during that overlap, verify authentication, and then revoke the old credential. If overlapping credentials are unsupported or the provider uses a different renewal process, follow that service’s procedure. Rolling back a Deployment restores its Pod template, not the previous Secret data. Restore Secret contents separately from a securely retained source if you need the old value.
Setting immutable: true prevents accidental data changes and can reduce kubelet watch load in clusters with many Secrets. Once a Secret is immutable, you cannot make it mutable again or edit data. Replace it by deleting and recreating the Secret; dependent Pods should also be recreated to use the replacement. Check how your deployment tooling handles this lifecycle before using immutable Secrets for frequently renewed credentials.
How can you reduce Secret exposure through access and storage?
A Secret is a way to provide a value to authorized consumers, not a vault that protects it everywhere it can be read. list or watch permissions can expose many Secrets in a namespace. A user who can create a Pod that uses a Secret may also be able to read its value through that Pod. Grant only the RBAC permissions each workload needs and separate workloads with different sensitivity levels into namespaces. Treat privileged containers as a separate risk boundary: Kubernetes warns that a privileged container can access Secrets used on its node.
Base64 is not encryption. Committing a Secret manifest or writing values to CI output, terminal logs, or application logs can expose them to anyone who can read those records. File-based creation still requires protected source files and a safe cleanup lifecycle. For Kubernetes Secret objects, configure etcd encryption at rest and least-privilege RBAC. If you prefer to keep values outside the cluster, evaluate an external secret store and CSI integration, and verify its provider, synchronization, and rotation behavior in that system’s documentation.
Which Secret type should you use, and what about service account tokens?
Use Opaque for most user-defined credentials. For TLS, kubernetes.io/tls makes the purpose clear; for registry authentication, use kubernetes.io/dockerconfigjson. An imagePullSecrets reference for a private image must also point to a Secret in the Pod’s namespace.
A kubernetes.io/service-account-token Secret is a legacy way to store a long-lived ServiceAccount token. For Pod tokens in Kubernetes v1.22 and later, prefer short-lived, automatically rotating tokens obtained through the TokenRequest API or a projected volume. Evaluate the reason and risk before persisting a manually created, non-expiring token in a readable API object.
References
Kubernetes Secret structure, types, use, and security boundaries
Good practices for Kubernetes Secrets
kubectl create secret generic command reference
Distribute Secret credentials to Pods securely
Encrypt confidential data stored in etcd
Scope: Kubernetes Secret v1, including Opaque and common built-in types, data/stringData, the 1 MiB limit, Pod environment-variable and volume delivery, update propagation, immutable Secrets, and access control. Official Kubernetes documentation was checked on 2026-11-23. Commands and Deployment are illustrative and were not run on a cluster or tested with credentials. Storage encryption, tokens, and external secret-store behavior vary by provider and configuration; consult the official documentation for your environment.