Prompt
How do I rotate service account tokens without breaking deployments?
Latest observation
Rotating Kubernetes service account tokens without breaking workloads is mostly about avoiding abrupt token invalidation and updating consumers gradually.
The safe approach
1. Prefer projected, short-lived tokens
If you’re using modern Kubernetes, use the BoundServiceAccountTokenVolume feature (now standard in recent versions). These tokens are:
- short-lived
- automatically rotated
- mounted into pods by kubelet
- less likely to break apps when rotated
Most in-cluster workloads should read the token from the mounted file and re-read it when needed, rather than caching it forever.
2. Don’t manually replace tokens used by running pods
If an app loads the token once at startup and never refreshes it, rotating the secret/token underneath it can cause auth failures.
Instead:
- deploy apps that watch/reload the token file
- or restart pods during the rollout
- or use a token projection with automatic rotation support
3. Rotate in stages
If you must rotate a long-lived token or secret-backed token:
- Create the new token
- Update the consuming deployment/config
- Roll out pods gradually
- Verify new pods authenticate
- Revoke/delete the old token only after all consumers are moved
This avoids a “cutover” where some pods still depend on the old credential.
If you’re using legacy secret-based service account tokens
Older clusters may still use manually managed secret tokens. Those are riskier because:
- they’re often long-lived
- apps frequently treat them as static
- deleting the secret can break existing pods immediately
In that case:
- create a new service account token
- update workloads to use the new token
- do a rolling restart
- keep the old token valid until traffic has fully shifted
Practical deployment patterns
Pattern A: Rolling restart after updating token
Good for apps that only read the token at startup.
- update the token reference in the Deployment/Secret/External Secret
- apply the change
kubectl rollout restart deployment/<name>- confirm readiness and auth
- then retire the old token
Pattern B: App re-reads token file on each request or refreshes it
Good for in-cluster service calls using projected tokens.
- mount token as a file
- have the app read it dynamically or on refresh
- let kubelet rotate it automatically
Pattern C: Dual-validity window
If your external system supports it:
- allow both old and new tokens for a period
- switch workloads to the new one
- remove the old token after the migration window
Important checks
- Verify whether your app uses the token file path or caches the token in memory.
- Check whether the token is used only for Kubernetes API access or also by an external service.
- Make sure readiness/liveness probes won’t fail during the rollout.
- Watch for client libraries that need explicit token refresh support.
Recommended best practice
For most clusters today:
- use projected service account tokens
- ensure apps can handle rotation
- use rolling updates instead of in-place secret replacement
- avoid long-lived static tokens unless absolutely necessary
If you want, I can give you:
- a Kubernetes YAML example for safe token rotation, or
- a step-by-step plan for legacy secret-based tokens.
Brands