How to Create Kubernetes Secrets
Published:
A Kubernetes Secret separates sensitive configuration from an application image. It does not make a credential safe to publish. This walkthrough uses only the invented value demo-only and creates a manifest for review before any cluster change.
Generate an Opaque Secret in the browser
- Open the Kubernetes Secret generator.
- Set Secret name to
demo-appand Namespace todefault. - Enter
API_TOKENin a Key field anddemo-onlyin its Value field. Use + Add key-value pair for additional entries. - Select Generate Secret, then inspect the YAML before copying or downloading it.
apiVersion: v1
kind: Secret
metadata:
name: demo-app
namespace: default
type: Opaque
data:
API_TOKEN: ZGVtby1vbmx5
The browser encodes each value as Base64. The result above decodes back to demo-only; it is intentionally not a usable credential. The tool does not connect to Kubernetes, create a namespace or verify your cluster’s admission rules.
Produce the same kind of manifest with kubectl
With kubectl installed, client dry-run prints an object without creating the Secret:
kubectl create secret generic demo-app --namespace=default --from-literal=API_TOKEN=demo-only --dry-run=client -o yaml
Generated metadata may differ from the browser output. Compare the resource name, namespace, type and decoded data rather than expecting byte-for-byte YAML equality. The kubectl command reference also documents file-based inputs. For real credentials, avoid putting literal values in shell history or process arguments; use your approved secret delivery mechanism.
Choose between data and stringData
data contains Base64 strings. stringData accepts unencoded strings that the API converts into data. Base64 provides no encryption, and Kubernetes warns that Secrets are stored unencrypted in etcd by default unless encryption at rest is configured. Restrict access with RBAC and configure appropriate storage protection. The Kubernetes Secrets documentation covers these distinctions and notes limitations of stringData with server-side apply.
Check the target before applying
Save the reviewed synthetic example as secret.yaml. These checks identify your current context and ask the API to validate the request without persisting it:
kubectl config current-context
kubectl apply --dry-run=server -f secret.yaml
Server dry-run contacts the cluster and needs permissions. Once the target and contents are correct, applying the file is a separate, intentional change:
kubectl apply -f secret.yaml
Keep real values out of Git, terminal recordings and support tickets, including when Base64-encoded. Test how your application consumes updates: a mounted Secret and a value read into an environment variable do not have the same update behavior.