Video in progress
How do you change the DB password of a service running in prod without downtime?
You need a window in which two passwords work at once: first I create a new user or password in the DB, then move the applications to the new one step by step, and delete the old one when everything has moved.
📖 Answer
The riskiest way is to change the password in the DB first and update the Secret afterwards. In between, every pod that tries to open a new connection fails, while the old connections in the HikariCP pool keep working until maxLifetime ends and hide the problem.
A safe sequence, using Postgres as the example:
-- 1. create a second login (or two users under one role)
CREATE ROLE shop_app_v2 LOGIN PASSWORD '...' IN ROLE shop_app;
- Update the Secret with the new login and password.
- Restart the pods:
kubectl rollout restart deploy/shop-api. A Secret passed through an env var does not change in a running pod, so the restart is required. - Check in monitoring that connections with the old login have dropped to zero (
pg_stat_activity). - Delete the old login.
A Secret mounted as a file (volume) is refreshed by the kubelet within a few minutes, but Spring's datasource does not re-read it on its own, so you still need a restart or a special mechanism.
The automated option is Vault's dynamic secrets: each pod gets its own temporary login, and it expires on its own after the period ends. This is powerful, but the app needs logic to renew the lease and recreate the pool. Spring Cloud Vault helps with that, but it adds complexity.
Sign up to mark studied questions and save your progress.