The database is supposed to shut down, but the pod sits in Terminating for three minutes. Not an error but intended: CloudNativePG first shuts PostgreSQL down “smart” and waits until all clients close their connections.
Symptom
kubectl -n kurz annotate cluster db --overwrite cnpg.io/hibernation=on 22:07:03 302 1/1/0 1/1/0 db:Terminating 22:07:09 302 1/1/0 1/1/0 db:Terminating 22:10:02 302 1/1/0 1/1/0 db:Terminating 22:10:08 503 1/1/0 1/1/0 db: 22:10:14 503 1/1/0 0/1/0 db: 22:10:20 000 0/1/0 0/1/0 db:
Columns: time, HTTP status of the application, READY/RESTARTS of both pods, state of the database pod. For three minutes the application still serves without errors.
Diagnosis
Watch the database pod's state every few seconds – it stays Terminating until the deadline, then it is gone.
"db:$(kubectl -n kurz get pods -l cnpg.io/cluster=db --no-headers 2>/dev/null | awk '{print $3}')"
Causes
smart shutdownPostgreSQL waits for its clients
During a smart shutdown, PostgreSQL accepts no new connections but waits until the existing ones are closed.
connection poolPools never close their connections
The application pods each hold a pool of open connections and never return them on their own. Only after smartShutdownTimeout (default 180 seconds) does CloudNativePG switch to the fast shutdown.
Fix
kubectl -n kurz annotate cluster db --overwrite cnpg.io/hibernation=off kubectl -n kurz wait --for=condition=Ready cluster/db --timeout=300s cluster.postgresql.cnpg.io/db annotated cluster.postgresql.cnpg.io/db condition met
Don't: Delete the pod with --force --grace-period=0. The three minutes are the time in which clients end cleanly – a forced deletion risks aborted transactions.
System design on Kubernetes, Day 1: kurz as a deployment – CloudNativePG, liveness versus readiness and a database in hibernation.
Go to day 1 →
FAQ
What does the annotation cnpg.io/hibernation=on do?
It sends the cluster into hibernation – the pods go, the volume with the data stays. With =off it comes back, in the course in twelve seconds until Ready.
Why does the application first answer 503 and then not at all?
Without a database it answers 503. After periodSeconds × failureThreshold both pods are no longer ready, the service has no endpoints, and curl gets no connection (000).