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

オープン
#11,988 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
68/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
go, kubernetes, postgresql

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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
主要言語
Go
スター
9.7k
フォーク
826
平均マージ
2日 1時間
マージ済み PR(30日)
122

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cloudnative-pg/cloudnative-pg のほかの issue

cloudnative-pg/cloudnative-pg の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。