Author: Md Toriqul Islam
Focus: Advanced Kubernetes Secrets Management
Note: This reference guide documents commands for educational purposes. Always validate commands in a safe environment before production use.
Before diving into specific commands, here's how to get started quickly:
- Create your first secret:
kubectl create secret generic my-first-secret \
--from-literal=username=admin \
--from-literal=password=secretpassword
- Verify it works:
kubectl describe secret my-first-secret
- Use it in a pod:
kubectl create deployment test-deploy --image=nginx
kubectl set env deployment/test-deploy --from=secret/my-first-secret
Now, let's explore all available commands in detail!
Understanding different ways to create secrets is crucial. Each method has its use case:
# 1. Create from literal values
kubectl create secret generic db-credentials \
--from-literal=username=admin \
--from-literal=password=secretpassword \
--from-literal=host=db.example.com \
--from-literal=port=3306
# 2. Create from files
kubectl create secret generic tls-certs \
--from-file=./tls.key \
--from-file=./tls.crt \
--from-file=./ca.crt
# 3. Create from configuration file
kubectl apply -f secret-config.yaml
# 4. Create Docker registry secret
kubectl create secret docker-registry regcred \
--docker-server=<your-registry-server> \
--docker-username=<your-username> \
--docker-password=<your-password> \
--docker-email=<your-email>
# 5. Create TLS secret
kubectl create secret tls tls-secret \
--cert=path/to/cert/file \
--key=path/to/key/file
These commands help you manage existing secrets effectively:
# Export secret to a file
kubectl get secret db-credentials -o yaml > exported-secret.yaml
# Edit secret directly
kubectl edit secret db-credentials
# Patch a secret
kubectl patch secret db-credentials -p '{"data":{"password":"bmV3cGFzc3dvcmQ="}}'
# Scale deployments using the secret
kubectl scale deployment secure-app --replicas=0
kubectl scale deployment secure-app --replicas=3
# Label secrets
kubectl label secret db-credentials environment=production
Different ways to inspect and verify your secrets:
# List all secrets
kubectl get secrets --all-namespaces
# Get detailed secret information
kubectl describe secret db-credentials
# Get specific secret values
kubectl get secret db-credentials -o jsonpath='{.data.username}' | base64 --decode
kubectl get secret db-credentials -o jsonpath='{.data.password}' | base64 --decode
# View secret in YAML format
kubectl get secret db-credentials -o yaml
# List secrets with labels
kubectl get secrets -l environment=production
# 1. Deploy pods with secrets
kubectl apply -f pod-with-env.yaml
kubectl apply -f pod-with-volume.yaml
kubectl apply -f deployment.yaml
# 2. Verify secret mounting
kubectl exec secret-vol-pod -- ls /etc/secrets
kubectl exec secret-vol-pod -- cat /etc/secrets/username
# 3. Check environment variables
kubectl exec secret-env-pod -- env | grep DB_
kubectl exec -it secret-env-pod -- sh -c 'echo $DB_PASSWORD'
# 4. Update pod with new secret
kubectl delete pod secret-env-pod
kubectl apply -f pod-with-env.yaml
# Encode values
echo -n 'admin' | base64
echo -n 'secretpassword' | base64
echo -n 'db.example.com' | base64
echo -n '3306' | base64
# Decode values
echo 'YWRtaW4=' | base64 --decode
echo 'c2VjcmV0cGFzc3dvcmQ=' | base64 --decode
# Encode file content
base64 -w 0 < tls.key > tls.key.base64
base64 -w 0 < tls.crt > tls.crt.base64
# Update existing secret
kubectl create secret generic db-credentials \
--from-literal=username=newadmin \
--from-literal=password=newpass \
-o yaml --dry-run=client | kubectl replace -f -
# Patch specific fields
kubectl patch secret db-credentials --type='json' -p='[{"op": "replace", "path": "/data/password", "value":"bmV3cGFzc3dvcmQ="}]'
# Check pod status
kubectl get pods
kubectl describe pod secret-env-pod
# Check events
kubectl get events --sort-by=.metadata.creationTimestamp
# Check logs
kubectl logs secret-env-pod
kubectl logs -f deployment/secure-app
# Check mount points
kubectl exec secret-vol-pod -- mount | grep secrets
# Debug secret mounting
kubectl debug secret-vol-pod -it --image=busybox
# Check secret permissions
kubectl auth can-i get secrets
kubectl auth can-i create secrets
kubectl auth can-i update secrets
# Verify service account permissions
kubectl auth can-i get secrets --as=system:serviceaccount:secure-ns:restricted-sa
# Create namespace and configure RBAC
kubectl create namespace secure-ns
kubectl config set-context --current --namespace=secure-ns
kubectl apply -f rbac.yaml
# Verify RBAC permissions
kubectl auth can-i get secrets --as=system:serviceaccount:secure-ns:restricted-sa
kubectl auth can-i update secrets --as=system:serviceaccount:secure-ns:restricted-sa
# List roles and bindings
kubectl get roles
kubectl get rolebindings
# Create service account
kubectl create serviceaccount secret-admin
# Create role
kubectl create role secret-manager \
--verb=get,list,watch,create,update \
--resource=secrets
# Bind role to service account
kubectl create rolebinding secret-admin-binding \
--role=secret-manager \
--serviceaccount=default:secret-admin
# Rotate secrets
kubectl create secret generic db-credentials \
--from-literal=username=admin \
--from-literal=password=newpassword \
-o yaml --dry-run=client | kubectl replace -f -
# Update applications
kubectl rollout restart deployment secure-app
# Backup all secrets
kubectl get secrets -A -o yaml > all-secrets-backup.yaml
# Backup specific secret
kubectl get secret db-credentials -o yaml > db-credentials-backup.yaml
# Delete individual resources
kubectl delete -f deployment.yaml
kubectl delete -f pod-with-env.yaml
kubectl delete -f pod-with-volume.yaml
kubectl delete -f secret-config.yaml
kubectl delete -f rbac.yaml
# Delete all resources in namespace
kubectl delete namespace secure-ns
# Remove specific secrets
kubectl delete secret db-credentials
kubectl delete secret tls-certs
-
Secret Naming Conventions
- Use descriptive names
- Include environment/purpose
- Follow consistent patterns
-
Security Measures
- Enable encryption at rest
- Use RBAC strictly
- Rotate secrets regularly
- Limit secret access
-
Monitoring
- Track secret usage
- Monitor access patterns
- Audit secret changes
- Log suspicious activities
-
Maintenance
- Regular secret rotation
- Backup procedures
- Access review
- Documentation updates
💡 Best Practice: Store sensitive data in Kubernetes Secrets, never in ConfigMaps or environment variables directly.
⚠️ Warning: Base64 encoding is not encryption. Always use additional security measures in production.
📝 Note: Secret values are limited to 1MB in size.
🔒 Security: Always follow the principle of least privilege when granting access to secrets.
-
Secret Not Showing Up?
- Check namespace
- Verify RBAC permissions
- Confirm base64 encoding
-
Pod Can't Access Secret?
- Check serviceAccount
- Verify mount paths
- Check secret name spelling
-
Base64 Issues?
- Use -n with echo
- Check for newlines
- Verify encoding/decoding
If you're stuck, try these steps:
- Check pod logs
- Describe the pod
- Verify secret exists
- Check RBAC permissions