CodeBlanks Logo Icon CodeBlanks

kubectl create secret

Learn to create a Secret with kubectl create secret.

kubectl create secret

kubectl create secret has three subcommands: generic, tls and docker-registry. Values can also be read from files; that way passwords don't end up in the shell history:

kubectl create secret generic db-gizli --from-file=DB_PASSWORD=./parola.txt
kubectl create secret generic api-anahtar --from-env-file=gizli.env
kubectl create secret generic db-gizli --from-literal=DB_PASSWORD="$DB_PAROLA"

Rotating a Secret

When a password changes, the Secret is updated and the application is restarted to read the new value. As with ConfigMaps, the --dry-run=client -o yaml | kubectl apply -f - pattern is used:

kubectl create secret generic db-gizli --from-literal=DB_PASSWORD="$YENI_PAROLA" \
  --dry-run=client -o yaml | kubectl apply -f -
kubectl rollout restart deployment/api

kubectl describe doesn't show Secret values; it only prints their byte sizes.

Practice Rotate the password

Write two commands: generate the generic db-gizli Secret with the DB_PASSWORD key, using the value in the $YENI_PAROLA variable, as YAML without creating it, and pipe it into kubectl apply -f -; restart the api deployment.

Starter code

terminal
# buraya yaz

Sample solution

terminal
kubectl create secret generic db-gizli --from-literal=DB_PASSWORD="$YENI_PAROLA" --dry-run=client -o yaml | kubectl apply -f -
kubectl rollout restart deployment/api

Expected output

secret/db-gizli configured
deployment.apps/api restarted

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the drawback of writing --from-literal=DB_PASSWORD=gizli123?

  • The command doesn't work
  • The Secret type is wrong
  • The password is saved in the shell history Correct answer
  • The value isn't encrypted

Explanation: Reading from a file or an environment variable is safer.

2 Read the value from a file.

Question
kubectl create secret generic db-gizli ____=DB_PASSWORD=./parola.txt
Answer
kubectl create secret generic db-gizli --from-file=DB_PASSWORD=./parola.txt

Explanation: --from-file=key=path sets the key name.

3 Match.

Term Meaning
generic Generic Secret
tls Certificate
docker-registry Registry login
describe secret Shows only sizes

All lessons in Intermediate