Skip to content

Commit 227f9ed

Browse files
authored
Merge pull request #48083 from aryasoni98/issue-47922
Updated the podAffinity concept documentation
2 parents 7f7fdfa + f39d397 commit 227f9ed

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

content/en/docs/concepts/scheduling-eviction/assign-pod-node.md

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ information.
8383
## Affinity and anti-affinity
8484

8585
`nodeSelector` is the simplest way to constrain Pods to nodes with specific
86-
labels. Affinity and anti-affinity expands the types of constraints you can
86+
labels. Affinity and anti-affinity expand the types of constraints you can
8787
define. Some of the benefits of affinity and anti-affinity include:
8888

8989
- The affinity/anti-affinity language is more expressive. `nodeSelector` only
@@ -236,10 +236,12 @@ Pods, the default Kubernetes scheduler places those Pods and honors any
236236
### Inter-pod affinity and anti-affinity
237237

238238
Inter-pod affinity and anti-affinity allow you to constrain which nodes your
239-
Pods can be scheduled on based on the labels of **Pods** already running on that
239+
Pods can be scheduled on based on the labels of Pods already running on that
240240
node, instead of the node labels.
241241

242-
Inter-pod affinity and anti-affinity rules take the form "this
242+
#### Types of Inter-pod Affinity and Anti-affinity
243+
244+
Inter-pod affinity and anti-affinity take the form "this
243245
Pod should (or, in the case of anti-affinity, should not) run in an X if that X
244246
is already running one or more Pods that meet rule Y", where X is a topology
245247
domain like node, rack, cloud provider zone or region, or similar and Y is the
@@ -259,16 +261,14 @@ the node label that the system uses to denote the domain. For examples, see
259261
Inter-pod affinity and anti-affinity require substantial amounts of
260262
processing which can slow down scheduling in large clusters significantly. We do
261263
not recommend using them in clusters larger than several hundred nodes.
262-
{{< /note >}}
264+
{{</note>}}
263265

264266
{{< note >}}
265267
Pod anti-affinity requires nodes to be consistently labeled, in other words,
266268
every node in the cluster must have an appropriate label matching `topologyKey`.
267269
If some or all nodes are missing the specified `topologyKey` label, it can lead
268270
to unintended behavior.
269-
{{< /note >}}
270-
271-
#### Types of inter-pod affinity and anti-affinity
271+
{{</note>}}
272272

273273
Similar to [node affinity](#node-affinity) are two types of Pod affinity and
274274
anti-affinity as follows:
@@ -287,16 +287,34 @@ To use inter-pod affinity, use the `affinity.podAffinity` field in the Pod spec.
287287
For inter-pod anti-affinity, use the `affinity.podAntiAffinity` field in the Pod
288288
spec.
289289

290-
#### Scheduling a group of pods with inter-pod affinity to themselves
290+
#### Scheduling Behavior
291+
292+
When scheduling a new Pod, the Kubernetes scheduler evaluates the Pod's affinity/anti-affinity rules in the context of the current cluster state:
293+
294+
1. Hard Constraints (Node Filtering):
295+
- `podAffinity.requiredDuringSchedulingIgnoredDuringExecution` and `podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution`:
296+
- The scheduler ensures the new Pod is assigned to nodes that satisfy these required affinity and anti-affinity rules based on existing Pods.
297+
298+
2. Soft Constraints (Scoring):
299+
- `podAffinity.preferredDuringSchedulingIgnoredDuringExecution` and `podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution`:
300+
- The scheduler scores nodes based on how well they meet these preferred affinity and anti-affinity rules to optimize Pod placement.
301+
302+
3. Ignored Fields:
303+
- Existing Pods' `podAffinity.preferredDuringSchedulingIgnoredDuringExecution`:
304+
- These preferred affinity rules are not considered during the scheduling decision for new Pods.
305+
- Existing Pods' `podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution`:
306+
- Similarly, preferred anti-affinity rules of existing Pods are ignored during scheduling.
307+
308+
#### Scheduling a Group of Pods with Inter-pod Affinity to Themselves
291309

292310
If the current Pod being scheduled is the first in a series that have affinity to themselves,
293311
it is allowed to be scheduled if it passes all other affinity checks. This is determined by
294-
verifying that no other pod in the cluster matches the namespace and selector of this pod,
295-
that the pod matches its own terms, and the chosen node matches all requested topologies.
296-
This ensures that there will not be a deadlock even if all the pods have inter-pod affinity
312+
verifying that no other Pod in the cluster matches the namespace and selector of this Pod,
313+
that the Pod matches its own terms, and the chosen node matches all requested topologies.
314+
This ensures that there will not be a deadlock even if all the Pods have inter-pod affinity
297315
specified.
298316

299-
#### Pod affinity example {#an-example-of-a-pod-that-uses-pod-affinity}
317+
#### Pod Affinity Example {#an-example-of-a-pod-that-uses-pod-affinity}
300318

301319
Consider the following Pod spec:
302320

@@ -351,7 +369,7 @@ of namespaces which the `labelSelector` should match against using the
351369
If omitted or empty, `namespaces` defaults to the namespace of the Pod where the
352370
affinity/anti-affinity definition appears.
353371

354-
#### Namespace selector
372+
#### Namespace Selector
355373

356374
{{< feature-state for_k8s_version="v1.24" state="stable" >}}
357375

@@ -373,12 +391,12 @@ When you want to disable it, you have to disable it explicitly via the
373391
{{< /note >}}
374392

375393
Kubernetes includes an optional `matchLabelKeys` field for Pod affinity
376-
or anti-affinity. The field specifies keys for the labels that should match with the incoming Pod's labels,
394+
or anti-affinity. The field specifies keys for the labels that should match with the incoming Pod's labels,
377395
when satisfying the Pod (anti)affinity.
378396

379-
The keys are used to look up values from the pod labels; those key-value labels are combined
397+
The keys are used to look up values from the Pod labels; those key-value labels are combined
380398
(using `AND`) with the match restrictions defined using the `labelSelector` field. The combined
381-
filtering selects the set of existing pods that will be taken into Pod (anti)affinity calculation.
399+
filtering selects the set of existing Pods that will be taken into Pod (anti)affinity calculation.
382400

383401
{{< caution >}}
384402
It's not recommended to use `matchLabelKeys` with labels that might be updated directly on pods.
@@ -430,7 +448,7 @@ When you want to disable it, you have to disable it explicitly via the
430448
{{< /note >}}
431449

432450
Kubernetes includes an optional `mismatchLabelKeys` field for Pod affinity
433-
or anti-affinity. The field specifies keys for the labels that should **not** match with the incoming Pod's labels,
451+
or anti-affinity. The field specifies keys for the labels that should not match with the incoming Pod's labels,
434452
when satisfying the Pod (anti)affinity.
435453

436454
{{< caution >}}
@@ -454,20 +472,20 @@ spec:
454472
affinity:
455473
podAffinity:
456474
requiredDuringSchedulingIgnoredDuringExecution:
457-
# ensure that pods associated with this tenant land on the correct node pool
475+
# ensure that Pods associated with this tenant land on the correct node pool
458476
- matchLabelKeys:
459477
- tenant
460478
topologyKey: node-pool
461479
podAntiAffinity:
462480
requiredDuringSchedulingIgnoredDuringExecution:
463-
# ensure that pods associated with this tenant can't schedule to nodes used for another tenant
481+
# ensure that Pods associated with this tenant can't schedule to nodes used for another tenant
464482
- mismatchLabelKeys:
465483
- tenant # whatever the value of the "tenant" label for this Pod, prevent
466484
# scheduling to nodes in any pool where any Pod from a different
467485
# tenant is running.
468486
labelSelector:
469487
# We have to have the labelSelector which selects only Pods with the tenant label,
470-
# otherwise this Pod would have Pods from daemonsets as well, for example,
488+
# otherwise this Pod would have anti-affinity against Pods from daemonsets as well, for example,
471489
# which aren't supposed to have the tenant label.
472490
matchExpressions:
473491
- key: tenant
@@ -651,10 +669,10 @@ The following operators can only be used with `nodeAffinity`.
651669
| `Gt` | The field value will be parsed as an integer, and that integer is less than the integer that results from parsing the value of a label named by this selector |
652670
| `Lt` | The field value will be parsed as an integer, and that integer is greater than the integer that results from parsing the value of a label named by this selector |
653671

654-
655672
{{<note>}}
673+
656674
`Gt` and `Lt` operators will not work with non-integer values. If the given value
657-
doesn't parse as an integer, the pod will fail to get scheduled. Also, `Gt` and `Lt`
675+
doesn't parse as an integer, the Pod will fail to get scheduled. Also, `Gt` and `Lt`
658676
are not available for `podAffinity`.
659677
{{</note>}}
660678

@@ -666,4 +684,4 @@ are not available for `podAffinity`.
666684
- Learn about how the [topology manager](/docs/tasks/administer-cluster/topology-manager/) takes part in node-level
667685
resource allocation decisions.
668686
- Learn how to use [nodeSelector](/docs/tasks/configure-pod-container/assign-pods-nodes/).
669-
- Learn how to use [affinity and anti-affinity](/docs/tasks/configure-pod-container/assign-pods-nodes-using-node-affinity/).
687+
- Learn how to use [affinity and anti-affinity](/docs/tasks/configure-pod-container/assign-pods-nodes-using-node-affinity/).

0 commit comments

Comments
 (0)