Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
Step by step
- Do not type the value on the command line —
--from-literal=DB_PASSWORD=…stays in your shell history. Use a file (orread -s):
read -rs DB_PASSWORD && printf 'DB_USER=app\nDB_PASSWORD=%s\n' "$DB_PASSWORD" > db.env && chmod 600 db.env
kubectl create secret generic db-cred -n shop --from-env-file=db.env
kubectl create secret tls shop-tls -n shop --cert=fullchain.pem --key=privkey.pem
kubectl create secret docker-registry regcred -n shop --docker-server=registry.example.com --docker-username='robot$shop+pull' --docker-password="$(cat pull-token)"- Use it in the Pod — as environment variables or as files:
containers:
- name: api
envFrom:
- secretRef:
name: db-cred
volumeMounts:
- name: tls
mountPath: /etc/tls
readOnly: true
volumes:
- name: tls
secret:
secretName: shop-tls
defaultMode: 0400- Know what "secret" means here:
| Fact | What to do |
|---|---|
| Values are base64, not encrypted. | Never paste kubectl get secret -o yaml into tickets or chat. |
| In etcd they are plain unless you turn on encryption at rest. | EncryptionConfiguration (kubeadm) — managed clusters (EKS/AKS/GKE) offer KMS encryption. |
Anyone with get secrets (or who can create Pods in the namespace) can read them. | Tight RBAC; separate namespaces per app/team. |
| Env variables do not change in a running Pod. | After rotation, restart: kubectl rollout restart deploy/api. Mounted files update by themselves (after a short delay). |
- Secrets in Git (GitOps) — never plain. Three good options:
| Tool | Idea |
|---|---|
| SOPS (+ age or a cloud KMS) | Encrypt the values in the YAML; Flux/Argo CD (with a plugin) decrypt in the cluster. |
| Sealed Secrets | Encrypt with the cluster's public key; only the controller in the cluster can open it. |
| External Secrets Operator | Keep the secret in Vault / AWS / Azure Key Vault; the operator creates the Kubernetes Secret. |
kubectl create secret generic db-cred -n shop --from-env-file=db.env --dry-run=client -o yaml > db-cred.yaml
sops --encrypt --age "$AGE_PUBLIC_KEY" --encrypted-regex '^(data|stringData)$' --in-place db-cred.yaml- Read one value (for a check, on your own terminal only):
Check that it worked
The Pod starts (no CreateContainerConfigError), the app connects, and the value is nowhere else: not in history, not in Git (gitleaks git), not in a ticket.
AGE_PUBLIC_KEY is your age public key (age1…); the private key stays in the cluster (Flux) or on the admin's machine.
Common problems
| Problem | Cause | Fix |
|---|---|---|
CreateContainerConfigError: secret "db-cred" not found | Wrong namespace or name. | Secrets are per namespace; kubectl get secret -n shop. |
couldn't find key DB_PASS in Secret | The key name differs. | kubectl describe secret db-cred lists the keys (not the values). |
| The app still uses the old password | Env variables are read only at start. | kubectl rollout restart deploy/api -n shop. |
| Value has an extra newline | The file had a newline at the end (--from-file). | Use printf (no newline) or --from-env-file. |
| A Secret YAML was pushed to Git | It is leaked. | Rotate the password now, then clean the history. |
| Argo CD shows the Secret values in the UI diff | Secrets shown in diffs. | Use External Secrets / SOPS; limit who can see the app in Argo CD. |
Go further
- Command pages: kubectl, sops.
- Next how-tos: Remove a secret from Git history, Find why a Pod does not start, Push an image to a private registry, Give a user read-only access with RBAC, Encrypt Kubernetes secrets in Git with SOPS.
- Learn it: CKS — The problem with Secrets, CKS — Encryption at rest, GitOps — Secrets: Sealed, SOPS, ESO.
- Full guides: SOPS, Vault.
1Is a Kubernetes Secret encrypted?
Not by itself: the value is base64. Encryption at rest in etcd must be turned on, and RBAC must limit who reads it.
2Why should you not use --from-literal=PASSWORD=... on a shared server?
The password stays in the shell history.