Skip to content

[Feature Request] Allow configurable PDBs #2570

Closed

Description

@fullykubed

Use Case: We want to allow eviction of the primary DB instance in order to perform upgrades on the underlying infrastructure automatically, for example updating a kubernetes node. While that will cause degraded performance during the failover, that is aligned with our SLOs for our uses of a majority of the clusters.

Problem: The current operator sets up a PDB that never allows the primary to be evicted. No configuration parameters are exposed to override this default behavior.

Edit:

I have expanded on additional use cases and reasons why this is critically important in a follow-up comment.

Activity

  1. leonardoce commented on Aug 12, 2023

    @leonardoce
    Contributor

    Hello.

    The operator is watching over nodes.
    When the node where the primary Pod is running is being cordoned, it will automatically switchover to the most advanced replica running on a node which is not cordoned.

    This will allow you to evict the Pods that are running on the node you drained, even if the PDB is still there.
    Is this enough for you?

  2. fullykubed commented on Aug 12, 2023

    @fullykubed
    ContributorAuthor

    Thanks for the response. That is helpful info and confirmed that cordoning works for failovers to a running replica. However, there are a handful of scenarios that we encounter that don't work.

    • EKS node upgrades (docs): The cordoning happens after the eviction and causes a PodEvictionFailure due to not being able to evict the pg instances. We would like to avoid enabling the force parameter globally here as we have other cases where we would want PDBs for other resources to stop a node upgrade.

    • Clusters that do not have any replicas still get this PDB. The cordoning strategy does not work in this case. We run some ephemeral DB clusters without replicas or backups as they are meant to just be temporary. In this case, the clusters do not need to be highly available and we don't want PDBs at all.

  3. fullykubed commented on Aug 12, 2023

    @fullykubed
    ContributorAuthor

    It would be nice to just have a simple boolean parameter on Cluster that disables the PDB management behavior in the operator for that particular cluster.

  4. leonardoce commented on Aug 14, 2023

    @leonardoce
    Contributor

    I think I got it now.
    You can set the cluster in maintenance mode, and that will remove the PDB while the maintenance mode is set.
    More info:

    https://cloudnative-pg.io/documentation/1.20/kubernetes_upgrade/#kubernetes-upgrade

    Would this help?

  5. fullykubed commented on Aug 14, 2023

    @fullykubed
    ContributorAuthor

    Unfortunately not. We don't want to disable the self-healing features. We want to be able to evict the primary and have the controller self-heal as it is able to.

    For our ephemeral clusters, we want to avoid adding another control loop that has to toggle flags on the Cluster resource just to use the eviction API in our automations.

    For our use case, we specifically want to allow disruptions of the primary. The PDB prevents that. That's a great default, but we specifically want to opt-out on a per-cluster basis.

  6. vladimirfx commented on Aug 27, 2023

    @vladimirfx

    Similar situation here. We have many DBs without replicas (only backups) that is ok to shutdown on cluster upgrades. Ability to just disable PDB on cluster basis help us too.

  7. tomaszkrzyzanowski commented on Sep 4, 2023

    @tomaszkrzyzanowski

    Same here, we don't have control over the clusters where the CNPG is running, and its PDB is breaking the K8s cluster upgrades. It's not possible to switch the clusters to maintenance mode – the support is not informing us about K8s upgrades, and we don't have control over infra.

    PDB toggle would be much appreciated :)

  8. alfredtm commented on Sep 6, 2023

    @alfredtm

    Same here, we are using AKS with Autoupgrade and the PDB stops the upgrade. Need to evict the pod manually for the node to restart.

  9. fullykubed commented on Oct 27, 2023

    @fullykubed
    ContributorAuthor

    @leonardoce Does this use case here make sense to add this capability to disable the PDB as a non-default option? We would like to use this project, but this is a blocker.

  10. fullykubed commented on Oct 27, 2023

    @fullykubed
    ContributorAuthor

    For anyone curious for a workaround, we settled on running this in a periodic cronjob in the interim. Unfortunately, this is necessary as the operator will restore the PDB on every cluster update or when the operator pods restart. Additionally, the operator does not respect field managers so field edits such as manually adjusting minAvailable get overwritten.

    #!/bin/bash
    
    kubectl get pdb --all-namespaces -o json | jq -r \
      '.items[] | 
       select(.metadata.annotations["cnpg.io/operatorVersion"]? != null) |
       "\(.metadata.namespace) \(.metadata.name)"' | \
    while read -r namespace name; do
      kubectl delete pdb "$name" -n "$namespace"
    done
  11. fullykubed commented on Oct 29, 2023

    @fullykubed
    ContributorAuthor

    The saga continues. Apparently, the script above does not work in later versions as the operator is in a realtime watch mode for changes or deletions of the pdb and will restore the pdb immediately after you try to remove them. The only way to buy yourself a bit of breathing room is to keep the pdb, remove the ownerReferences, and the change the minAvailable to 0.

    #!/usr/bin/env bash
    
    # This script will patch all pdbs managed by the cnpg operator to allow primary disruption
    
    set -eo pipefail
    
    kubectl get pdb --all-namespaces -o json | jq -r \
      '.items[] |
       select(.metadata.annotations["cnpg.io/operatorVersion"]? != null) |
       "\(.metadata.namespace) \(.metadata.name) \(.metadata.ownerReferences? != null)"' | \
    while read -r namespace name has_owner_references; do
      # Delete owner references if they exist
      if [ "$has_owner_references" = "true" ]; then
        kubectl patch pdb "$name" -n "$namespace" --type='json' -p='[{"op": "remove", "path": "/metadata/ownerReferences"}]'
      fi
    
      # Set minAvailable to 0
      kubectl patch pdb "$name" -n "$namespace" --type='json' -p='[{"op": "replace", "path": "/spec/minAvailable", "value":0}]'
    done
    

    Granted it will still restore itself occasionally like when the operator restarts, so you have to run this in a cronjob.

  12. Christian-Probst-DATEV commented on Nov 21, 2023

    @Christian-Probst-DATEV

    We need that feature too! Please.

  13. jbattiato commented on Dec 12, 2023

    @jbattiato
    Contributor

    Hi @jclangst

    Could you please elaborate more on how the self healing in a single node cluster would help you while you are evicting the only primary pod?

    What are you trying to achieve?

    This is how CNPG allows you to upgrade K8S node in case of a single node cluster:

    nodeMaintenanceWindow:
      inProgress: true
      reusePVC: true
    

    as explained by Leonardo a few posts above:
    https://cloudnative-pg.io/documentation/1.21/kubernetes_upgrade/

    These settings will automatically remove the PodDisruptionBudget blocker to allow pod eviction when node drain is requested.

    Basically, when you run a single node cluster you accept the downtime while upgrading the Kubernetes node.
    The primary pod will get back up and running once the node is ready to schedule workloads again. The primary then reattaches itself to the PVC and starts Postgres.

    Would that be enough for you?

  14. Christian-Probst-DATEV commented on Dec 13, 2023

    @Christian-Probst-DATEV

    We have a team doing the maintenance for k8s and our Team is running the database operator. 3 Nodes, 1 Primary, 2 Standbys. Our Team doesn't know when maintenance is taking place and is not involved with it. We need a way to remove, better avoid, all PDB's by declaration.

  15. jbattiato commented on Dec 13, 2023

    @jbattiato
    Contributor

    We have a team doing the maintenance for k8s and our Team is running the database operator. 3 Nodes, 1 Primary, 2 Standbys. Our Team doesn't know when maintenance is taking place and is not involved with it. We need a way to remove, better avoid, all PDB's by declaration.

    Hi @Christian-Probst-DATEV

    You don't need to remove PDB when a cluster has at least one replica. In this case the PDB does not prevent the drain.
    Indeed, the Operator will ensure the primary is always running on a schedulable node. That means, when the node drain kicks in against the node where the primary is running, the Operator will promote the most aligned replica before allowing the eviction of the primary pod.

    I just did this test in a kind cluster with three (virtual) K8S nodes. I created a CNPG cluster with 2 nodes, one primary and one standby. I have not set the nodeMaintenanceWindow, so the default was set to false. I did execute kubectl cordon against the node where the primary was running. The result was the Operator performed the switchover to promote the standby as the new primary, on the K8S node that was still schedulable.
    CNPG version: compiled from main, using the hack/setup-cluster.sh script.

    So, my questions now are:

    • did you face any other blockers? Or
    • the Operator did not behave as expected in your specific case?

    Please, elaborate more on the issue so we can help you resolve any blockers.

  16. 17 remaining items

  17. added a commit that references this issue on Mar 11, 2024
    82ab399
  18. added a commit that references this issue on Mar 12, 2024
    3d654b3
  19. jsilvela commented on Mar 12, 2024

    @jsilvela
    Contributor

    Closing with the merge of #3837

  20. jsilvela commented on Mar 12, 2024

    @jsilvela
    Contributor
  21. pgodowski commented on Aug 23, 2024

    @pgodowski
    Contributor

    Can we please ask to backport this into 1.22 which is the current LTS version?
    I think it was supposed to be included in 1.22 as per @gbartolini 's comment PR comment, but I don't see it in 1.22.

  22. gbartolini commented on Aug 23, 2024

    @gbartolini
    Contributor

    Can we please ask to backport this into 1.22 which is the current LTS version? I think it was supposed to be included in 1.22 as per @gbartolini 's comment PR comment, but I don't see it in 1.22.

    Doing.

  23. added a commit that references this issue on Aug 23, 2024
    91d0dea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement 🪄

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions