Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 37Kubernetes · Security

How to create a Kubernetes Secret safely

Database passwords, API keys and TLS keys go into Secrets. But a Kubernetes Secret is only base64 — not encrypted — and the most common leak is a Secret YAML committed to Git. This how-to creates Secrets without leaving the value in your shell history or in Git, uses them in a Pod, explains rotation, and shows the safe ways to keep secrets in a GitOps repo (SOPS, Sealed Secrets, External Secrets).

  • 5 Lessen
  • 8 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

37.1

The quick answer

kubectl create secret generic db-cred -n shop --from-env-file=./db.env
shred -u ./db.env
kubectl get secret db-cred -n shop
NAME      TYPE     DATA   AGE
db-cred   Opaque   2      5s

db.env holds DB_USER=app and DB_PASSWORD=… (one per line). Create it with 600 rights, use it once, delete it.

37.2

Step by step

  1. Do not type the value on the command line — --from-literal=DB_PASSWORD=… stays in your shell history. Use a file (or read -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)"
  1. Use it in the Pod — as environment variables or as files:
deployment.yaml (part)
      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
  1. Know what "secret" means here:
FactWhat 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).
  1. Secrets in Git (GitOps) — never plain. Three good options:
ToolIdea
SOPS (+ age or a cloud KMS)Encrypt the values in the YAML; Flux/Argo CD (with a plugin) decrypt in the cluster.
Sealed SecretsEncrypt with the cluster's public key; only the controller in the cluster can open it.
External Secrets OperatorKeep 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
  1. Read one value (for a check, on your own terminal only):
kubectl get secret db-cred -n shop -o jsonpath='{.data.DB_USER}' | base64 -d; echo
app

37.3

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.

kubectl exec deploy/api -n shop -- printenv DB_USER
gitleaks dir . -v

AGE_PUBLIC_KEY is your age public key (age1…); the private key stays in the cluster (Flux) or on the admin's machine.

37.4

Common problems

ProblemCauseFix
CreateContainerConfigError: secret "db-cred" not foundWrong namespace or name.Secrets are per namespace; kubectl get secret -n shop.
couldn't find key DB_PASS in SecretThe key name differs.kubectl describe secret db-cred lists the keys (not the values).
The app still uses the old passwordEnv variables are read only at start.kubectl rollout restart deploy/api -n shop.
Value has an extra newlineThe file had a newline at the end (--from-file).Use printf (no newline) or --from-env-file.
A Secret YAML was pushed to GitIt is leaked.Rotate the password now, then clean the history.
Argo CD shows the Secret values in the UI diffSecrets shown in diffs.Use External Secrets / SOPS; limit who can see the app in Argo CD.

37.5

Go further

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.

Alle commando's in dit hoofdstuk