Secret Rotation Automation
Advanced15 min
Manage and rotate Kubernetes secrets safely, including creating, updating, and verifying secret propagation to pods.
Prerequisites
- -kubectl configured
- -Understanding of Kubernetes secrets
Steps
1
List all secrets in a namespace
View existing secrets and their types.
$ kubectl get secrets -n <namespace> -o custom-columns='NAME:.metadata.name,TYPE:.type,AGE:.metadata.creationTimestamp'
2
Create a new secret from literal values
Create a secret with key-value pairs from the command line.
$ kubectl create secret generic db-credentials --from-literal=username=admin --from-literal=password=$(openssl rand -base64 24) --dry-run=client -o yaml | kubectl apply -f -
Using --dry-run=client -o yaml | kubectl apply allows you to update existing secrets without deleting them first.
3
View a secret's contents (decoded)
Decode and display the values stored in a secret.
$ kubectl get secret db-credentials -o jsonpath='{.data}' | jq 'to_entries[] | {key: .key, value: (.value | @base64d)}'
Be careful displaying decoded secrets in shared terminals or CI logs.
4
Restart pods to pick up rotated secrets
After updating a secret, restart the deployment so pods get the new values.
$ kubectl rollout restart deployment/<deployment-name>
Alternatively, mount secrets as volumes instead of environment variables. Volume-mounted secrets are updated automatically without pod restarts.
5
Verify pods have the updated secret
Check that running pods see the new secret values.
$ kubectl exec <pod-name> -- env | grep -i 'DB_\|SECRET_\|PASSWORD'
Full Script
FAQ
Discussion
Loading comments...