Repository navigation
Description
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 searched issues and PRs for drainTaints, drain taints, unschedulable, rolling update and switchover. #11185 and #11294 change when the switchover target is picked, but not which instance is eligible, so I don't think either fixes this.
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.
Version
1.30 (latest patch): v1.30.1. The code involved is unchanged on main.
What version of Kubernetes are you using?
1.36 (v1.36.5+k3s1)
What is your Kubernetes environment?
Self-managed: k3s
How did you install the operator?
YAML manifest
What happened?
A rolling update with primaryUpdateMethod: switchover can loop the primary between two instances indefinitely. It loops when:
- one instance runs on a node carrying a taint listed in
DRAIN_TAINTS, and - the replicas are tied on LSN.
Each iteration restarts postgres on both instances. The loop does not end on its own. On our cluster it ran 12 switchovers in about 11 minutes, until we ran kubectl cnpg promote by hand.
Setup. A 3-instance cluster, one instance per site, synchronous replication (method: any, number: 1).
- We keep primaries off one site by putting a
PreferNoScheduletaint on its nodes and adding that key to the operator'sDRAIN_TAINTS:
DRAIN_TAINTS=node.kubernetes.io/unschedulable,ToBeDeletedByClusterAutoscaler,karpenter.sh/disrupted,karpenter.sh/disruption,khetem.com/no-primary - Instances during the roll:
integration-postgres-1on the tainted nodenode-hel1integration-postgres-3onnode-nbg1(primary when the roll started)integration-postgres-6onnode-fsn1
- The roll was an image digest change.
Two election paths, each with a different filter. Both break an LSN tie by pod name (PostgresqlStatusList.Less, pkg/postgres/status.go).
- Rolling update. The rolling update upgrades the replicas, then switches over to
podList.Items[1](cluster_upgrade.go#L233). That target is not checked againstDRAIN_TAINTS. On a tie,-1sorts before-6, so the tainted instance becomes primary. - Move-off. On the next reconcile,
setPrimaryOnSchedulableNodesees the primary on a drain-tainted node and switches to the first ready candidate on another node (replicas.go#L321-L347). It runs beforereconcileResourcesin the same reconcile (cluster_controller.go#L586-L595). This loop does not check whether a candidate still needs the rollout. On a tie,-3sorts before-6, so it picks-3: the just-demoted old primary, still on the old image. - Back to step 1. Now the primary (
-3) needs restarting for the new image, so the rolling update switches toItems[1]=-1again.
The loop only breaks if -6 pulls ahead on LSN, or someone promotes -6 by hand. -3 was recreated on the new image only after our manual promote.
When it loops. Call the tainted instance H, the primary at roll start P, and the other untainted instance G. On an exact LSN tie, the roll loops when H < G and P < G by pod name. If H < G but G < P, you get a single leg through the tainted node, and then it settles. We have seen this across seven rolls on our clusters. Two of them were overridden by a small LSN lead (one was 56 bytes).
Expected. One of these:
- the rolling update's switchover target is filtered with the same
isNodeUnschedulableOrBeingDrainedcheck the move-off uses; or - the move-off prefers (or only takes) candidates that already match the target spec.
Either change alone ends the loop. The first also avoids the transient primary on a drain-tainted node.
Cluster resource
apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: integration-postgres
spec:
instances: 3
imageName: ghcr.io/cloudnative-pg/postgresql:18.6-system-trixie@sha256:<digest> # digest changed to trigger the roll
primaryUpdateStrategy: unsupervised
primaryUpdateMethod: switchover
enablePDB: false
failoverDelay: 60
switchoverDelay: 120
smartShutdownTimeout: 30
postgresql:
synchronous:
method: any
number: 1
failoverQuorum: true
affinity:
enablePodAntiAffinity: true
podAntiAffinityType: required
topologyKey: kubernetes.io/hostname
topologySpreadConstraints:
- maxSkew: 1
minDomains: 3
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
cnpg.io/cluster: integration-postgres
cnpg.io/podRole: instanceRelevant log output
# operator log (fields trimmed, node names shortened)
18:39:45Z Recreating instance pod pod=integration-postgres-6 reason="the instance is using a different image"
18:39:59Z Recreating instance pod pod=integration-postgres-1 reason="the instance is using a different image"
18:40:25Z The primary needs to be restarted, we'll trigger a switchover to do that currentPrimary=integration-postgres-3 targetPrimary=integration-postgres-1
18:40:44Z Current primary is running on unschedulable node, triggering a switchover currentPrimary=integration-postgres-1 currentPrimaryNode=node-hel1 targetPrimary=integration-postgres-3 targetPrimaryNode=node-nbg1
18:41:08Z The primary needs to be restarted, we'll trigger a switchover to do that currentPrimary=integration-postgres-3 targetPrimary=integration-postgres-1
18:41:27Z Current primary is running on unschedulable node, triggering a switchover currentPrimary=integration-postgres-1 targetPrimary=integration-postgres-3
18:41:51Z The primary needs to be restarted, ... targetPrimary=integration-postgres-1
18:42:21Z Current primary is running on unschedulable node, ... targetPrimary=integration-postgres-3
18:43:00Z The primary needs to be restarted, ... targetPrimary=integration-postgres-1
18:43:53Z Current primary is running on unschedulable node, ... targetPrimary=integration-postgres-3
18:44:49Z The primary needs to be restarted, ... targetPrimary=integration-postgres-1
18:46:22Z Current primary is running on unschedulable node, ... targetPrimary=integration-postgres-3
18:48:18Z The primary needs to be restarted, ... targetPrimary=integration-postgres-1
18:51:15Z Current primary is running on unschedulable node, ... targetPrimary=integration-postgres-3
18:53:34Z (manual) kubectl cnpg promote integration-postgres integration-postgres-6
18:59:02Z Recreating instance pod pod=integration-postgres-3 reason="the instance is using a different image"
# At every decision the instance status showed the two replicas at equal receivedLsn/replayLsn,
# e.g. 18:40:44 -3 and -6 at 9A/AD1662D8, 18:51:15 both at 9A/B711C210.
# Postgres container restarts over the loop: -1 = 6, -3 = 6, -6 = 0.Code of Conduct
- I agree to follow this project's Code of Conduct
Activity
ardentperf commentedon Oct 30, 2026
cf. #7799 (comment)
this GH issue seems to describe a situation, but still seems a little vague to me
i'm thinking that it should be pretty straight forward to say "DRAIN_TAINTS should be honored in all paths the same as during maintenance - tainted nodes should not be chosen as primary"
that may resolve this issue, even though it's coming from a slightly different angle