Skip to content

feat(instance): liveness probe isolation checker - #7466

Merged
mnencia merged 13 commits into
cloudnative-pg:mainfrom
leonardoce:sondaganza
May 12, 2025
Merged

mnencia merged 13 commits into
cloudnative-pg:mainfrom
leonardoce:sondaganza

Conversation

@leonardoce

@leonardoce leonardoce commented May 1, 2025 •

Copy link
Copy Markdown
Contributor

Enhances the liveness probe logic for primary instances in HA clusters to detect network isolation scenarios.

The probe first checks if the Pod can reach the API server. If the API server is unreachable, it then attempts to contact peer PostgreSQL instances in the same cluster via a REST entrypoint.

If neither the API server nor any replicas are reachable, the liveness probe fails, prompting Kubernetes to restart the Pod.

Upon restart, the instance manager will refuse to start PostgreSQL, as it cannot download the Cluster definition—preventing unsafe behavior in isolated environments.

This behavior applies only to primary instances in clusters with HA replicas.

Closes: #7465

@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.22 labels May 1, 2025
@github-actions

github-actions Bot commented May 1, 2025

Copy link
Copy Markdown
Contributor

❗ By default, the pull request is configured to backport to all release branches.

  • To stop backporting this pr, remove the label: backport-requested ◀️ or add the label 'do not backport'
  • To stop backporting this pr to a certain release branch, remove the specific branch label: release-x.y

@gbartolini

gbartolini commented May 2, 2025 •

Copy link
Copy Markdown
Contributor

I built the operator with this patch on my laptop and followed Nikolay's instructions in #7407. I was able to see that the isolated primary shut down due to the liveness probe failing. Below I detail the whole process so that anyone can easily reproduce and test it.

The PoC can be considered successful and I believe we should proceed in the liveness probe direction. This is a very elegant, clean, linear and robust implementation. Most importantly, it resonates with Kubernetes HA principles to delegate to the actual workload the responsibility to kill themselves when isolated.

The next step is to refine the logs and the code, define the UI and the default values, and provide tests.

Crazy idea: if we see that this patch gets consensus before RC3, we could even consider introducing a feature-flag annotation in 1.26 to help us experiment with this for 1.27. Thoughts?

Reproducible case

I checked out this PR code and followed the instructions here: https://github.com/cloudnative-pg/cloudnative-pg/tree/main/contribute/development_environment#building-the-operator-and-deploying-it-locally

I created the following cnpg cluster:

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: cnpg
spec:
  instances: 3
  storage:
    size: 1Gi

I observed the primary (cnpg-1) ending in the pg-operator-e2e-v1-33-0-worker2 worker node. I ensured the operator was installed in a different worker node (3). Otherwise, we can just set it up in HA.

I connected via docker to the pg-operator-e2e-v1-33-0-worker2 worker node with:

docker exec -ti pg-operator-e2e-v1-33-0-worker2 bash

Once inside, I got the ID of the container that was running postgres with:

crictl ps

Made sure that this is a primary:

crictl exec $CONTAINER_ID pg_controldata

Then started streaming the logs of the container:

crictl logs -f $CONTAINER_ID

Then disconnected the network with:

docker network disconnect kind pg-operator-e2e-v1-33-0-worker2

The liveness probe started to fail:

{"level":"info","ts":"2025-05-02T08:56:52.36495782Z","msg":"The API server is not reachable, triggering instance connectivity check","logging_pod":"cnpg-1"}
{"level":"error","ts":"2025-05-02T08:56:52.867765688Z","msg":"Instance connectivity error - liveness probe failing","logging_pod":"cnpg-1","error":"instance connectivity error for instance [cnpg-2] with ip [10.
244.1.11]: Get \"https://10.244.1.11:8000/failsafe\": context deadline exceeded (Client.Timeout exceeded while awaiting headers)","stacktrace":"github.com/cloudnative-pg/machinery/pkg/log.(*logger).Error\n\t/Us
ers/gabriele.bartolini/go/pkg/mod/github.com/cloudnative-pg/[email protected]/pkg/log/log.go:125\ngithub.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres/webserver/probes.(*livenessExecutor).IsHealthy\n
\tpkg/management/postgres/webserver/probes/liveness.go:137\ngithub.com/cloudnative-pg/cloudnative-pg/pkg/management/postgres/webserver.(*remoteWebserverEndpoints).isServerHealthy\n\tpkg/management/postgres/webs
erver/remote.go:236\nnet/http.HandlerFunc.ServeHTTP\n\t/usr/local/Cellar/go/1.24.2/libexec/src/net/http/server.go:2294\nnet/http.(*ServeMux).ServeHTTP\n\t/usr/local/Cellar/go/1.24.2/libexec/src/net/http/server.
go:2822\nnet/http.serverHandler.ServeHTTP\n\t/usr/local/Cellar/go/1.24.2/libexec/src/net/http/server.go:3301\nnet/http.initALPNRequest.ServeHTTP\n\t/usr/local/Cellar/go/1.24.2/libexec/src/net/http/server.go:397
4\nnet/http.(*http2serverConn).runHandler\n\t/usr/local/Cellar/go/1.24.2/libexec/src/net/http/h2_bundle.go:6529"}
{"level":"info","ts":"2025-05-02T08:56:52.945135215Z","msg":"readiness check failed, cannot check Cluster definition","logging_pod":"cnpg-1","err":"Get \"https://10.96.0.1:443/apis/postgresql.cnpg.io/v1/namespa
ces/default/clusters/cnpg\": context canceled"}

The pod is correctly shut down when the liveness probe is declared failed.

The pod is restarted but Postgres never starts as the API server cannot be reached.

Running pg_controldata again on the container, before the kubelet kills it again, returns exactly what we are aiming for:

pg_control version number:            1700
Catalog version number:               202406281
Database system identifier:           7499763067810955291
Database cluster state:               shut down
pg_control last modified:             Fri 02 May 2025 08:38:02 AM UTC
Latest checkpoint location:           0/8000028
Latest checkpoint's REDO location:    0/8000028
Latest checkpoint's REDO WAL file:    000000020000000000000008

@armru

armru commented May 5, 2025

Copy link
Copy Markdown
Member

/test limit=local

@github-actions

github-actions Bot commented May 5, 2025

Copy link
Copy Markdown
Contributor

@armru
armru force-pushed the sondaganza branch 3 times, most recently from 641565b to 5f1050b Compare May 6, 2025 09:54
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label May 6, 2025
@leonardoce
leonardoce marked this pull request as ready for review May 7, 2025 08:12
@leonardoce
leonardoce requested a review from a team May 7, 2025 08:12
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement 🪄 New feature or request labels May 7, 2025
Comment thread api/v1/cluster_types.go Resolved
@armru
armru self-requested a review May 12, 2025 10:27
@gbartolini gbartolini added do not backport This PR must not be backported - it will be in the next minor release and removed backport-requested ◀️ This pull request should be backported to all supported releases release-1.22 labels May 12, 2025
leonardoce and others added 12 commits May 12, 2025 17:57
Enhances the liveness probe logic for primary instances in HA clusters
to detect network isolation scenarios.

The probe first checks if the Pod can reach the API server. If the API
server is unreachable, it then attempts to contact peer PostgreSQL
instances in the same cluster via a REST entrypoint.

If neither the API server nor any replicas are reachable, the liveness
probe fails, prompting Kubernetes to restart the Pod.

Upon restart, the instance manager will refuse to start PostgreSQL, as
it cannot download the Cluster definition—preventing unsafe behavior in
isolated environments.

This behavior applies only to primary instances in clusters with HA
replicas.

Closes: cloudnative-pg#7465

Signed-off-by: Leonardo Cecchi <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Armando Ruocco <[email protected]>
Signed-off-by: Niccolò Fei <[email protected]>
In kindest/node 1.27 & 1.28, the version of crictl installed doesn't support
the `--namespace flag`. Use label selectors instead.

Signed-off-by: Niccolò Fei <[email protected]>
Signed-off-by: Gabriele Bartolini <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
@leonardoce

Copy link
Copy Markdown
Contributor Author

/test

@github-actions

Copy link
Copy Markdown
Contributor

@cloudnative-pg cloudnative-pg deleted a comment from github-actions Bot May 12, 2025
Signed-off-by: Marco Nenciarini <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not backport This PR must not be backported - it will be in the next minor release enhancement 🪄 New feature or request lgtm This PR has been approved by a maintainer ok to merge 👌 This PR can be merged size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Experimental - Isolation Checker for the liveness probe of a primary

7 participants