A Secret is a Kubernetes object designed for sensitive data: a password, an API key, a certificate. It takes these values out of the manifest of a Deployment, which is versioned and shared, and keeps them in a separate object that the Pod receives as an environment variable or as a file. In this lesson, you start from a Deployment whose password is written in clear text, you move it into a Secret created on the command line, you prove it from inside the container, then you read the error of a wrong key.
Prerequisites: the demo cluster from First cluster, the lesson Deployments and the lesson ConfigMaps, whose injection mechanisms are the same. All commands are typed in the default namespace.
What you will learn
- Recognize the flaw of a password written in a manifest
- Create a Secret on the command line, without writing its value in a versioned manifest
- Say why base64 is not encryption, and what the Secret type brings anyway
- Inject a Secret as an environment variable and as a mounted file, and prove it in the container
- Read in
kubectl describethe error of a wrong Secret key - Recognize the signs that call for an external secrets manager
What is the problem with a password in a manifest?
A manifest is committed to Git, copied into a ticket, displayed in a terminal. A password that appears in it is found everywhere the manifest goes. Create a Deployment that carries its password in the env field to see the flaw:
kubectl apply -f - <<'EOF'apiVersion: apps/v1kind: Deploymentmetadata: name: comptespec: replicas: 1 selector: matchLabels: app: compte template: metadata: labels: app: compte spec: containers: - name: compte image: busybox:1.38.0@sha256:a46257e06c82ad85242d0d38077d0a52d9896389ca60a1a59d3463db586e5fa4 command: ["sleep", "3600"] env: - name: DB_PASSWORD value: "S3cr3t!P@ss"EOFkubectl rollout status deployment/comptekubectl get deployment compte -o yaml | grep -A1 DB_PASSWORDdeployment.apps/compte createdWaiting for deployment "compte" rollout to finish: 0 of 1 updated replicas are available...deployment "compte" successfully rolled out {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"compte","namespace":"default"},"spec":{"replicas":1,"selector":{"matchLabels":{"app":"compte"}},"template":{"metadata":{"labels":{"app":"compte"}},"spec":{"containers":[{"command":["sleep","3600"],"env":[{"name":"DB_PASSWORD","value":"S3cr3t!P@ss"}],"image":"busybox:1.38.0@sha256:a46257e06c82ad85242d0d38077d0a52d9896389ca60a1a59d3463db586e5fa4","name":"compte"}]}}}} creationTimestamp: "2026-11-11T07:08:16Z"-- - name: DB_PASSWORD value: S3cr3t!P@ssThe last command reads the password directly in the Deployment object: anyone who reads the Deployment also reads the password. The grep even shows it twice. The first line is the kubectl.kubernetes.io/last-applied-configuration annotation, where kubectl apply keeps a copy of the applied manifest, password included. After the -- line, the env field shows value: S3cr3t!P@ss in clear text. The value must be moved into a Secret.
How do you create a Secret without writing the password in a manifest?
The kubectl create secret generic command creates a Secret from a file (--from-file) or from a literal value (--from-literal). The file prevents the value from appearing in the shell history, which happens with --from-literal. In practice, the file comes from a password manager or a generator; here, it is written by hand so that the demonstration can be replayed:
printf 'S3cr3t!P@ss' > password.txtkubectl create secret generic db-secret --from-file=password=password.txtrm password.txtkubectl get secret db-secretkubectl describe secret db-secretsecret/db-secret createdNAME TYPE DATA AGEdb-secret Opaque 1 0sName: db-secretNamespace: defaultLabels: <none>Annotations: <none>
Type: Opaque
Data====password: 11 bytesThe Secret has the key password and the type Opaque, and describe only shows its size (11 bytes), not its value. This masking only applies to this command: the value can be read in other ways, as the next section shows.
Why is base64 not encryption?
Kubernetes stores the values of a Secret encoded in base64. It is a transport encoding, which lets you fit binary data into YAML or JSON, and it is undone with a single command. Read the Secret you just created:
kubectl get secret db-secret -o jsonpath='{.data.password}'echokubectl get secret db-secret -o jsonpath='{.data.password}' | base64 -dechoUzNjcjN0IVBAc3M=S3cr3t!P@ssThe first command shows the encoded value, UzNjcjN0IVBAc3M=, and the second the decoded value, S3cr3t!P@ss, that of the starting password. Anyone who can read the Secrets of the namespace therefore reads all the passwords.
The Secret is not protected by its encoding, but by what controls access to it: RBAC, that is, the rules that say who is allowed to read what in the cluster. The lesson Protect the Secrets of a Kubernetes cluster (in French) covers RBAC, encryption of data at rest and external managers.
The Secret type still brings three things. It is a distinct object, which RBAC can protect separately from Deployments. describe masks its value. And mounted as a file, it stays in memory rather than on the disk of the node.
How do you inject the Secret into the Deployment?
A Secret is injected like a ConfigMap: as an environment variable with secretKeyRef, or as a file with a volume of type secret. The manifest below replaces the clear-text password of the compte Deployment with a reference to db-secret, and also mounts the Secret in /etc/secret:
kubectl apply -f - <<'EOF'apiVersion: apps/v1kind: Deploymentmetadata: name: comptespec: replicas: 1 selector: matchLabels: app: compte template: metadata: labels: app: compte spec: containers: - name: compte image: busybox:1.38.0@sha256:a46257e06c82ad85242d0d38077d0a52d9896389ca60a1a59d3463db586e5fa4 command: ["sleep", "3600"] env: - name: DB_PASSWORD valueFrom: secretKeyRef: name: db-secret key: password volumeMounts: - name: secret mountPath: /etc/secret readOnly: true volumes: - name: secret secret: secretName: db-secretEOFkubectl rollout status deployment/comptedeployment.apps/compte configuredWaiting for deployment "compte" rollout to finish: 1 old replicas are pending termination...Waiting for deployment "compte" rollout to finish: 1 old replicas are pending termination...deployment "compte" successfully rolled outProve both sides: the value arrives in the container, and it has left the manifest.
kubectl exec deploy/compte -- printenv DB_PASSWORDkubectl exec deploy/compte -- ls /etc/secretkubectl exec deploy/compte -- cat /etc/secret/passwordechokubectl exec deploy/compte -- grep /etc/secret /proc/mountskubectl get deployment compte -o yaml | grep -c 'S3cr3t' || trueS3cr3t!P@sspasswordS3cr3t!P@sstmpfs /etc/secret tmpfs ro,relatime,size=4009776k,inode64,noswap 0 00The application receives its password as before, as a variable and as a file, but the manifest of the Deployment no longer holds any trace of it: the last count is 0, last-applied-configuration annotation included, since the second application of the manifest replaced the first.
The file carries the name of the key of the Secret. The /proc/mounts line confirms that it lives on an in-memory file system (tmpfs), mounted read-only (ro), and not on the disk of the node.
Prefer the file to the variable when the application can read a file: an environment variable ends up more easily in a log or an error report.
As with a ConfigMap, a variable is never refreshed after a change of the Secret: only replacing the Pod updates it. The lesson ConfigMaps details it.
Which Secret types exist?
The Opaque type, used above, fits any arbitrary key-value pair. The two other common types impose a structure of keys that Kubernetes checks at creation. The table gives, for each one, the command that creates it (values between angle brackets are to be replaced with yours):
| Type | Creation command | Use |
|---|---|---|
Opaque | kubectl create secret generic <name> --from-literal=<key>=<value> | Passwords, API keys, anything with no imposed shape |
kubernetes.io/tls | kubectl create secret tls <name> --cert=<file.crt> --key=<file.key> | A certificate and its private key, for example for an Ingress |
kubernetes.io/dockerconfigjson | kubectl create secret docker-registry <name> --docker-server=<registry> --docker-username=<user> --docker-password=<password> | The credentials of a private image registry |
What happens when the Secret key is wrong?
As with a ConfigMap, a Pod that references a Secret or a key that cannot be found does not start: it stays in CreateContainerConfigError. Trigger the error with a Pod that asks for the key motdepasse instead of password, then read the diagnosis in kubectl describe. The error is intentional:
kubectl apply -f - <<'EOF'apiVersion: v1kind: Podmetadata: name: casse-secretspec: containers: - name: casse-secret image: busybox:1.38.0@sha256:a46257e06c82ad85242d0d38077d0a52d9896389ca60a1a59d3463db586e5fa4 command: ["sleep", "3600"] env: - name: DB_PASSWORD valueFrom: secretKeyRef: name: db-secret key: motdepasseEOFkubectl wait --for=jsonpath='{.status.containerStatuses[0].state.waiting.reason}'=CreateContainerConfigError pod/casse-secret --timeout=60skubectl get pod casse-secretkubectl describe pod casse-secret | grep -iE 'CreateContainerConfigError|motdepasse'pod/casse-secret createdpod/casse-secret condition metNAME READY STATUS RESTARTS AGEcasse-secret 0/1 CreateContainerConfigError 0 4s Reason: CreateContainerConfigError DB_PASSWORD: <set to the key 'motdepasse' in secret 'db-secret'> Optional: false Warning Failed 0s (x2 over 1s) kubelet spec.containers{casse-secret}: Error: couldn't find key motdepasse in Secret default/db-secretThe filter keeps three lines. Reason: CreateContainerConfigError gives the state of the container, the DB_PASSWORD line shows the faulty reference (the key motdepasse of the Secret db-secret), and the Warning event gives the cause: couldn't find key motdepasse in Secret default/db-secret. This message names the key that cannot be found and the Secret that does not contain it. Compare with the real keys in kubectl describe secret db-secret: the cause is often a typo in the name of the key.
When should you move to an external secrets manager?
A Kubernetes Secret is enough for one application and a small team. Three signs call for an external manager: secrets must be renewed automatically, an audit must trace who reads what, or several clusters must share the same values. Choosing a tool, with the encryption of data at rest and the RBAC that go with it, is covered in Protect the Secrets of a Kubernetes cluster (in French).
Put it into practice
Two labs complete the lesson. The first starts from a Deployment that carries its password in clear text, moves it into a Secret and injects it as a variable and as a file; the validation reads both sides, the value having left the manifest while arriving in the running container. The second makes a ConfigMap and a Secret live together in the same Pod, and the validation checks from inside the container that each value did arrive.
Clean up what you created in the demo cluster:
kubectl delete deployment comptekubectl delete pod casse-secretkubectl delete secret db-secretdeployment.apps "compte" deleted from default namespacepod "casse-secret" deleted from default namespacesecret "db-secret" deleted from default namespaceKey takeaways
- A password written in a manifest is found everywhere the manifest goes: it goes into a Secret.
- A Secret is created on the command line, from a file or an input, never from a value written in a versioned manifest.
- base64 is not encryption:
kubectl get secret -o jsonpaththenbase64 -dreturns the value. - What the Secret type brings: a distinct object that RBAC can protect, a display masked by
describe, an in-memory mount. - A Secret is injected as a variable (
secretKeyRef) or as a file (secretvolume), the file carrying the name of the key. - An environment variable is never refreshed; only replacing the Pod updates it.
- A Secret or a key that cannot be found blocks the Pod in
CreateContainerConfigError, readable inkubectl describe. - Automatic renewal, audit and multi-cluster sharing call for an external secrets manager.
Test your knowledge
Seven questions on the points that cost dearly: what base64 does not do, what gets refreshed, and how to read a wrong key.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Kubernetes namespaces: organize and isolate your resources logically
A new full quiz with random questions
Work again only on the questions you missed