[Bug]: Former primary is evicted mid-handover during node drain because cnpg.io/instanceRole=unhealthy matches neither PDB

Aperta
#11,988 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go, kubernetes, postgresql

Direzione di ricerca

The two PDBs are built in pkg/specs/poddisruptionbudget.go; start by enumerating the selectors it emits for <cluster>-primary and <cluster>. Then read markOldPrimaryAsUnhealthy in internal/controller/replicas.go:224 and the switchover path at internal/controller/cluster_controller.go:409-421 to see when a pod carries cnpg.io/instanceRole=unhealthy and when it flips back to replica. The change is a third PDB with maxUnavailable: 0 selecting the unhealthy role when enablePDB is true; extend the PDB spec unit tests to assert it exists.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Is there an existing issue already for this bug?
  • I have searched for an existing issue, and could not find anything. I believe this is a new bug.
I have read the troubleshooting guide
  • I have read the troubleshooting guide and I think this is a new bug.
I am running a supported version of CloudNativePG
  • I am running a supported version of CloudNativePG.
Version

1.30.1 / 1.30.2 (Helm chart cloudnative-pg-0.29.1); verified on main at e9eec0d as well

What version of Kubernetes are you using?

1.35.7

What is your Kubernetes environment?

Cloud: Azure AKS

How did you install the operator?

Helm

What happened?

When a node hosting the primary instance is cordoned and drained (e.g., during an AKS node pool roll/upgrade), the operator triggers a switchover and immediately relabels the former primary Pod to cnpg.io/instanceRole=unhealthy (markOldPrimaryAsUnhealthy in internal/controller/replicas.go:224 and internal/controller/cluster_controller.go:409-421).

Neither of the operator's PodDisruptionBudgets (pkg/specs/poddisruptionbudget.go) matches that label:

  • <cluster>-primary selects cnpg.io/instanceRole: primary
  • <cluster> selects cnpg.io/instanceRole: replica

Because the former primary is now selected by no PDB, the ongoing kubectl drain immediately evicts the Pod mid-handover, while its instance manager is shutting down PostgreSQL to step down cleanly. Terminated mid-handover, the former primary never releases the primary lease; the promotion target then blocks waiting for the primary lease to expire, turning a ~4 s switchover into a ~40 s write outage.

Observed in production (2026-10-20, 3-instance cluster, enablePDB: true, node pool roll):

  • Control switchover on the same roll 3 minutes earlier (pg-2 → pg-1): pg-2 finished demotion at 04:52:37, was evicted at 04:52:38, and pg-1 acquired the primary lease at 04:52:38.8 (~4 s total, 0 failed requests).
  • Failing switchover (pg-1 → pg-3):
    • 04:55:52: node carrying pg-1 cordoned & drained
    • 04:55:55: SwitchingOver pg-1 → pg-3
    • 04:55:56.5: pg-1 "Shutting it down to allow it to be demoted"
    • 04:55:58: drain evicts pg-1 (Killing: Stopping container postgres) — 3 s into handover
    • 04:56:17: pg-3 "Primary lease not yet acquired"
    • 04:56:36: pg-3 "Acquired primary lease" (~40 s write outage; all client probes returned 503/connection errors between 04:56:04 and 04:56:40)

Workaround that verified the root cause:
Adding an independent PDB selecting the unhealthy role with maxUnavailable: 0:

apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
  name: pg-handover
spec:
  selector:
    matchLabels:
      cnpg.io/cluster: pg
      cnpg.io/instanceRole: unhealthy
  maxUnavailable: 0

With default unhealthyPodEvictionPolicy: IfHealthyBudget, even a non-Ready former primary is held while labelled unhealthy. Once it finishes stepping down and releases the lease, the operator promotes the target and relabels the former primary to replica, at which point the drain proceeds under the replicas PDB.

Expected:
When enablePDB: true, the former primary should remain protected from voluntary eviction until handover completes (for example via an operator-managed handover PDB for cnpg.io/instanceRole=unhealthy, or equivalent protection until the primary lease is released and the pod transitions to replica).

(Note: this is distinct from #11897 and #10103, which concern the replicas PDB allowing a standby to be evicted; here the former primary itself is evicted mid-handover while carrying cnpg.io/instanceRole=unhealthy.)

Cluster resource
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: pg
spec:
  instances: 3
  enablePDB: true
  primaryUpdateStrategy: unsupervised
  primaryUpdateMethod: switchover
  storage:
    size: 50Gi
Relevant log output
# 2026-10-20 — node drain during AKS node pool roll
04:55:55   Cluster/pg  SwitchingOver  Switching over from pg-1 to pg-3
04:55:56.5 Pod/pg-1    instance-mgr   Shutting it down to allow it to be demoted
04:55:58   Pod/pg-1    Killing        Stopping container postgres
04:56:17   Pod/pg-3    instance-mgr   Primary lease not yet acquired
04:56:36   Pod/pg-3    instance-mgr   Acquired primary lease
Code of Conduct
  • I agree to follow this project's Code of Conduct
Lingua principale
Go
Stelle
9.7k
Fork
826
Merge medio
2g 1h
PR unite (30g)
122

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cloudnative-pg/cloudnative-pg

Tutte le issue di cloudnative-pg/cloudnative-pg

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.