|
| 1 | +--- |
| 2 | +layout: blog |
| 3 | +title: "Kubernetes 1.26: Eviction policy for unhealthy pods guarded by PodDisruptionBudgets" |
| 4 | +date: 2023-01-06 |
| 5 | +slug: "unhealthy-pod-eviction-policy-for-pdbs" |
| 6 | +--- |
| 7 | + |
| 8 | +**Authors:** Filip Křepinský (Red Hat), Morten Torkildsen (Google), Ravi Gudimetla (Apple) |
| 9 | + |
| 10 | + |
| 11 | +Ensuring the disruptions to your applications do not affect its availability isn't a simple |
| 12 | +task. Last month's release of Kubernetes v1.26 lets you specify an _unhealthy pod eviction policy_ |
| 13 | +for [PodDisruptionBudgets](/docs/concepts/workloads/pods/disruptions/#pod-disruption-budgets) (PDBs) |
| 14 | +to help you maintain that availability during node management operations. |
| 15 | +In this article, we will dive deeper into what modifications were introduced for PDBs to |
| 16 | +give application owners greater flexibility in managing disruptions. |
| 17 | + |
| 18 | +## What problems does this solve? |
| 19 | + |
| 20 | +API-initiated eviction of pods respects PodDisruptionBudgets (PDBs). This means that a requested [voluntary disruption](https://kubernetes.io/docs/concepts/scheduling-eviction/#pod-disruption) |
| 21 | +via an eviction to a Pod, should not disrupt a guarded application and `.status.currentHealthy` of a PDB should not fall |
| 22 | +below `.status.desiredHealthy`. Running pods that are [Unhealthy](/docs/tasks/run-application/configure-pdb/#healthiness-of-a-pod) |
| 23 | +do not count towards the PDB status, but eviction of these is only possible in case the application |
| 24 | +is not disrupted. This helps disrupted or not yet started application to achieve availability |
| 25 | +as soon as possible without additional downtime that would be caused by evictions. |
| 26 | + |
| 27 | +Unfortunately, this poses a problem for cluster administrators that would like to drain nodes |
| 28 | +without any manual interventions. Misbehaving applications with pods in `CrashLoopBackOff` |
| 29 | +state (due to a bug or misconfiguration) or pods that are simply failing to become ready |
| 30 | +make this task much harder. Any eviction request will fail due to violation of a PDB, |
| 31 | +when all pods of an application are unhealthy. Draining of a node cannot make any progress |
| 32 | +in that case. |
| 33 | + |
| 34 | +On the other hand there are users that depend on the existing behavior, in order to: |
| 35 | +- prevent data-loss that would be caused by deleting pods that are guarding an underlying resource or storage |
| 36 | +- achieve the best availability possible for their application |
| 37 | + |
| 38 | +Kubernetes 1.26 introduced a new experimental field to the PodDisruptionBudget API: `.spec.unhealthyPodEvictionPolicy`. |
| 39 | +When enabled, this field lets you support both of those requirements. |
| 40 | + |
| 41 | +## How does it work? |
| 42 | + |
| 43 | +API-initiated eviction is the process that triggers graceful pod termination. |
| 44 | +The process can be initiated either by calling the API directly, |
| 45 | +by using a kubectl drain command, or other actors in the cluster. |
| 46 | +During this process every pod removal is consulted with appropriate PDBs, |
| 47 | +to ensure that a sufficient number of pods is always running in the cluster. |
| 48 | + |
| 49 | +The following policies allow PDB authors to have a greater control how the process deals with unhealthy pods. |
| 50 | + |
| 51 | +There are two policies `IfHealthyBudget` and `AlwaysAllow` to choose from. |
| 52 | + |
| 53 | +The former, `IfHealthyBudget`, follows the existing behavior to achieve the best availability |
| 54 | +that you get by default. |
| 55 | + |
| 56 | +By setting the `spec.unhealthyPodEvictionPolicy` field of your PDB to `AlwaysAllow`, |
| 57 | +you are choosing the best effort availability for your application. |
| 58 | +With this policy it is always possible to evict unhealthy pods. |
| 59 | +This will make it easier to maintain and upgrade your clusters. |
| 60 | + |
| 61 | +We think that `AlwaysAllow` will often be a better choice, but for some critical workloads you may |
| 62 | +still prefer to protect even unhealthy Pods from node drains or other forms of API-initiated |
| 63 | +eviction. |
| 64 | + |
| 65 | +## How do I use it? |
| 66 | + |
| 67 | +This is an alpha feature, which means you have to enable the `PDBUnhealthyPodEvictionPolicy` |
| 68 | +[feature gate](/docs/reference/command-line-tools-reference/feature-gates/), |
| 69 | +with the command line argument `--feature-gates=PDBUnhealthyPodEvictionPolicy=true` |
| 70 | +to the kube-apiserver. |
| 71 | + |
| 72 | +Here's an example. Assume that you've enabled the feature gate in your cluster, and that you |
| 73 | +already defined a Deployment that runs a plain webserver. You labelled the Pods for that |
| 74 | +Deployment with `app: nginx`. |
| 75 | +You want to limit avoidable disruption, and you know that best effort availability is |
| 76 | +sufficient for this app. |
| 77 | +You decide to allow evictions even if those webserver pods are unhealthy. |
| 78 | +You create a PDB to guard this application, with the `AlwaysAllow` policy for evicting |
| 79 | +unhealthy pods: |
| 80 | + |
| 81 | +```yaml |
| 82 | +apiVersion: policy/v1 |
| 83 | +kind: PodDisruptionBudget |
| 84 | +metadata: |
| 85 | + name: nginx-pdb |
| 86 | +spec: |
| 87 | + selector: |
| 88 | + matchLabels: |
| 89 | + app: nginx |
| 90 | + maxUnavailable: 1 |
| 91 | + unhealthyPodEvictionPolicy: AlwaysAllow |
| 92 | +``` |
| 93 | +
|
| 94 | +
|
| 95 | +## How can I learn more? |
| 96 | +
|
| 97 | +
|
| 98 | +- Read the KEP: [Unhealthy Pod Eviction Policy for PDBs](https://github.com/kubernetes/enhancements/tree/master/keps/sig-apps/3017-pod-healthy-policy-for-pdb) |
| 99 | +- Read the documentation: [Unhealthy Pod Eviction Policy](/docs/tasks/run-application/configure-pdb/#unhealthy-pod-eviction-policy) for PodDisruptionBudgets |
| 100 | +- Review the Kubernetes documentation for [PodDisruptionBudgets](docs/concepts/workloads/pods/disruptions/#pod-disruption-budgets), [draining of Nodes](docs/tasks/administer-cluster/safely-drain-node/) and [evictions](docs/concepts/scheduling-eviction/api-eviction/) |
| 101 | +
|
| 102 | +
|
| 103 | +## How do I get involved? |
| 104 | +
|
| 105 | +If you have any feedback, please reach out to us in the [#sig-apps](https://kubernetes.slack.com/archives/C18NZM5K9) channel on Slack (visit https://slack.k8s.io/ for an invitation if you need one), or on the SIG Apps mailing list: [email protected] |
| 106 | +
|
0 commit comments