@@ -153,7 +153,7 @@ information.
153
153
## Affinity and anti-affinity
154
154
155
155
`nodeSelector` is the simplest way to constrain Pods to nodes with specific
156
- labels. Affinity and anti-affinity expands the types of constraints you can
156
+ labels. Affinity and anti-affinity expand the types of constraints you can
157
157
define. Some of the benefits of affinity and anti-affinity include:
158
158
-->
159
159
## 亲和性与反亲和性 {#affinity-and-anti-affinity}
@@ -233,6 +233,7 @@ your Pod spec.
233
233
For example, consider the following Pod spec:
234
234
-->
235
235
你可以使用 Pod 规约中的 ` .spec.affinity.nodeAffinity ` 字段来设置节点亲和性。
236
+
236
237
例如,考虑下面的 Pod 规约:
237
238
238
239
{{% code_sample file="pods/pod-with-node-affinity.yaml" %}}
@@ -432,22 +433,26 @@ DaemonSet 控制器创建 Pod 时,默认的 Kubernetes 调度器负责放置 P
432
433
# ## Inter-pod affinity and anti-affinity
433
434
434
435
Inter-pod affinity and anti-affinity allow you to constrain which nodes your
435
- Pods can be scheduled on based on the labels of ** Pods** already running on that
436
+ Pods can be scheduled on based on the labels of Pods already running on that
436
437
node, instead of the node labels.
437
438
-->
438
439
# ## Pod 间亲和性与反亲和性 {#inter-pod-affinity-and-anti-affinity}
439
440
440
- Pod 间亲和性与反亲和性使你可以基于已经在节点上运行的 ** Pod** 的标签来约束
441
+ Pod 间亲和性与反亲和性使你可以基于已经在节点上运行的 Pod 的标签来约束
441
442
Pod 可以调度到的节点,而不是基于节点上的标签。
442
443
443
444
<!--
444
- Inter-pod affinity and anti-affinity rules take the form "this
445
+ # ### Types of Inter-pod Affinity and Anti-affinity
446
+
447
+ Inter-pod affinity and anti-affinity take the form "this
445
448
Pod should (or, in the case of anti-affinity, should not) run in an X if that X
446
449
is already running one or more Pods that meet rule Y", where X is a topology
447
450
domain like node, rack, cloud provider zone or region, or similar and Y is the
448
451
rule Kubernetes tries to satisfy.
449
452
-->
450
- Pod 间亲和性与反亲和性的规则格式为“如果 X 上已经运行了一个或多个满足规则 Y 的 Pod,
453
+ # ### Pod 间亲和性与反亲和性的类型
454
+
455
+ Pod 间亲和性与反亲和性的格式为“如果 X 上已经运行了一个或多个满足规则 Y 的 Pod,
451
456
则这个 Pod 应该(或者在反亲和性的情况下不应该)运行在 X 上”。
452
457
这里的 X 可以是节点、机架、云提供商可用区或地理区域或类似的拓扑域,
453
458
Y 则是 Kubernetes 尝试满足的规则。
@@ -496,13 +501,9 @@ Pod 反亲和性需要节点上存在一致性的标签。换言之,
496
501
{{< /note >}}
497
502
498
503
<!--
499
- # ### Types of inter-pod affinity and anti-affinity
500
-
501
504
Similar to [node affinity](#node-affinity) are two types of Pod affinity and
502
505
anti-affinity as follows :
503
506
-->
504
- # ### Pod 间亲和性与反亲和性的类型
505
-
506
507
与[节点亲和性](#node-affinity)类似,Pod 的亲和性与反亲和性也有两种类型:
507
508
508
509
- ` requiredDuringSchedulingIgnoredDuringExecution`
@@ -530,13 +531,54 @@ spec.
530
531
对于 Pod 间反亲和性,可以使用 Pod 规约中的 `.affinity.podAntiAffinity` 字段。
531
532
532
533
<!--
533
- # ### Scheduling a group of pods with inter-pod affinity to themselves
534
+ # ### Scheduling Behavior
535
+
536
+ When scheduling a new Pod, the Kubernetes scheduler evaluates the Pod's affinity/anti-affinity rules in the context of the current cluster state :
537
+
538
+ 1. Hard Constraints (Node Filtering) :
539
+ - `podAffinity.requiredDuringSchedulingIgnoredDuringExecution` and `podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution` :
540
+ - The scheduler ensures the new Pod is assigned to nodes that satisfy these required affinity and anti-affinity rules based on existing Pods.
541
+ -->
542
+ # ### 调度行为
543
+
544
+ 在调度新 Pod 时,Kubernetes 调度器会根据当前集群状态评估 Pod 的亲和性/反亲和性规则:
545
+
546
+ 1. 硬约束(节点过滤):
547
+ - ` podAffinity.requiredDuringSchedulingIgnoredDuringExecution` 和
548
+ ` podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution` :
549
+ - 调度器基于现有 Pod,确保新 Pod 被分配到满足这些必需的亲和性和反亲和性规则的节点上。
550
+
551
+ <!--
552
+ 2. Soft Constraints (Scoring) :
553
+ - `podAffinity.preferredDuringSchedulingIgnoredDuringExecution` and `podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution` :
554
+ - The scheduler scores nodes based on how well they meet these preferred affinity and anti-affinity rules to optimize Pod placement.
555
+ -->
556
+ 2. 软约束(评分):
557
+ - ` podAffinity.preferredDuringSchedulingIgnoredDuringExecution` 和
558
+ ` podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution` :
559
+ - 调度器根据节点满足这些优选的亲和性和反亲和性规则的程度来评分,以优化 Pod 的放置。
560
+
561
+ <!--
562
+ 3. Ignored Fields :
563
+ - Existing Pods' `podAffinity.preferredDuringSchedulingIgnoredDuringExecution` :
564
+ - These preferred affinity rules are not considered during the scheduling decision for new Pods.
565
+ - Existing Pods' `podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution` :
566
+ - Similarly, preferred anti-affinity rules of existing Pods are ignored during scheduling.
567
+ -->
568
+ 3. 忽略的字段:
569
+ - 现有 Pod 的 `podAffinity.preferredDuringSchedulingIgnoredDuringExecution`:
570
+ - 在为新 Pod 做调度决策时,不会考虑这些优选的亲和性规则。
571
+ - 现有 Pod 的 `podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution`:
572
+ - 同样,在调度时会忽略现有 Pod 的优选反亲和性规则。
573
+
574
+ <!--
575
+ # ### Scheduling a Group of Pods with Inter-pod Affinity to Themselves
534
576
535
577
If the current Pod being scheduled is the first in a series that have affinity to themselves,
536
578
it is allowed to be scheduled if it passes all other affinity checks. This is determined by
537
- verifying that no other pod in the cluster matches the namespace and selector of this pod ,
538
- that the pod matches its own terms, and the chosen node matches all requested topologies.
539
- This ensures that there will not be a deadlock even if all the pods have inter-pod affinity
579
+ verifying that no other Pod in the cluster matches the namespace and selector of this Pod ,
580
+ that the Pod matches its own terms, and the chosen node matches all requested topologies.
581
+ This ensures that there will not be a deadlock even if all the Pods have inter-pod affinity
540
582
specified.
541
583
-->
542
584
# ### 调度一组具有 Pod 间亲和性的 Pod {#scheduling-a-group-of-pods-with-inter-pod-affinity-to-themselves}
@@ -548,7 +590,7 @@ specified.
548
590
这确保即使所有的 Pod 都配置了 Pod 间亲和性,也不会出现调度死锁的情况。
549
591
550
592
<!--
551
- # ### Pod affinity example {#an-example-of-a-pod-that-uses-pod-affinity}
593
+ # ### Pod Affinity example {#an-example-of-a-pod-that-uses-pod-affinity}
552
594
553
595
Consider the following Pod spec :
554
596
-->
@@ -613,8 +655,7 @@ refer to the [design proposal](https://git.k8s.io/design-proposals-archive/sched
613
655
You can use the `In`, `NotIn`, `Exists` and `DoesNotExist` values in the
614
656
` operator` field for Pod affinity and anti-affinity.
615
657
-->
616
- 你可以针对 Pod 间亲和性与反亲和性为其 `operator` 字段使用 `In`、`NotIn`、`Exists`、
617
- ` DoesNotExist` 等值。
658
+ 你可以针对 Pod 间亲和性与反亲和性为其 `operator` 字段使用 `In`、`NotIn`、`Exists`、`DoesNotExist` 等值。
618
659
619
660
<!--
620
661
Read [Operators](#operators)
@@ -658,7 +699,7 @@ affinity/anti-affinity definition appears.
658
699
如果 `namespaces` 被忽略或者为空,则默认为 Pod 亲和性/反亲和性的定义所在的名字空间。
659
700
660
701
<!--
661
- # ### Namespace selector
702
+ # ### Namespace Selector
662
703
-->
663
704
# ### 名字空间选择算符 {#namespace-selector}
664
705
@@ -695,17 +736,18 @@ When you want to disable it, you have to disable it explicitly via the
695
736
696
737
<!--
697
738
Kubernetes includes an optional `matchLabelKeys` field for Pod affinity
698
- or anti-affinity. The field specifies keys for the labels that should match with the incoming Pod's labels,
739
+ or anti-affinity. The field specifies keys for the labels that should match with the incoming Pod's labels,
699
740
when satisfying the Pod (anti)affinity.
700
741
701
- The keys are used to look up values from the pod labels; those key-value labels are combined
742
+ The keys are used to look up values from the Pod labels; those key-value labels are combined
702
743
(using `AND`) with the match restrictions defined using the `labelSelector` field. The combined
703
- filtering selects the set of existing pods that will be taken into Pod (anti)affinity calculation.
744
+ filtering selects the set of existing Pods that will be taken into Pod (anti)affinity calculation.
704
745
-->
705
746
Kubernetes 在 Pod 亲和性或反亲和性中包含一个可选的 `matchLabelKeys` 字段。
706
747
此字段指定了应与传入 Pod 的标签匹配的标签键,以满足 Pod 的(反)亲和性。
707
748
708
- 这些键用于从 Pod 的标签中查找值;这些键值标签与使用 `labelSelector` 字段定义的匹配限制组合(使用 `AND` 操作)。
749
+ 这些键用于从 Pod 的标签中查找值;这些键值标签与使用 `labelSelector`
750
+ 字段定义的匹配限制组合(使用 `AND` 操作)。
709
751
这种组合的过滤机制选择将用于 Pod(反)亲和性计算的现有 Pod 集合。
710
752
711
753
{{< caution >}}
@@ -803,11 +845,11 @@ When you want to disable it, you have to disable it explicitly via the
803
845
804
846
<!--
805
847
Kubernetes includes an optional `mismatchLabelKeys` field for Pod affinity
806
- or anti-affinity. The field specifies keys for the labels that should ** not** match with the incoming Pod's labels,
848
+ or anti-affinity. The field specifies keys for the labels that should not match with the incoming Pod's labels,
807
849
when satisfying the Pod (anti)affinity.
808
850
-->
809
851
Kubernetes 为 Pod 亲和性或反亲和性提供了一个可选的 `mismatchLabelKeys` 字段。
810
- 此字段指定了在满足 Pod(反)亲和性时,**不**应与传入 Pod 的标签匹配的键。
852
+ 此字段指定了在满足 Pod(反)亲和性时,不应与传入 Pod 的标签匹配的键。
811
853
812
854
{{< caution >}}
813
855
<!--
@@ -841,20 +883,20 @@ spec:
841
883
affinity:
842
884
podAffinity:
843
885
requiredDuringSchedulingIgnoredDuringExecution:
844
- # ensure that pods associated with this tenant land on the correct node pool
886
+ # ensure that Pods associated with this tenant land on the correct node pool
845
887
- matchLabelKeys:
846
888
- tenant
847
889
topologyKey: node-pool
848
890
podAntiAffinity:
849
891
requiredDuringSchedulingIgnoredDuringExecution:
850
- # ensure that pods associated with this tenant can't schedule to nodes used for another tenant
892
+ # ensure that Pods associated with this tenant can't schedule to nodes used for another tenant
851
893
- mismatchLabelKeys:
852
894
- tenant # whatever the value of the "tenant" label for this Pod, prevent
853
895
# scheduling to nodes in any pool where any Pod from a different
854
896
# tenant is running.
855
897
labelSelector:
856
898
# We have to have the labelSelector which selects only Pods with the tenant label,
857
- # otherwise this Pod would have Pods from daemonsets as well, for example,
899
+ # otherwise this Pod would have anti-affinity against Pods from daemonsets as well, for example,
858
900
# which aren't supposed to have the tenant label.
859
901
matchExpressions:
860
902
- key: tenant
@@ -887,8 +929,8 @@ spec:
887
929
# 都会阻碍此 Pod 被调度到这些节点池中的节点上
888
930
labelSelector:
889
931
# 我们必须有一个 labelSelector,只选择具有 “tenant” 标签的 Pod,
890
- # 否则此 Pod 也会与来自 DaemonSet 的 Pod 发生冲突 ,
891
- # 而这些 Pod 不应该具有 “tenant” 标签
932
+ # 否则此 Pod 也会与来自 DaemonSet 的 Pod 产生反亲和性 ,
933
+ # 例如,这些 Pod 不应该具有 “tenant” 标签
892
934
matchExpressions:
893
935
- key: tenant
894
936
operator: Exists
@@ -1173,7 +1215,7 @@ The following operators can only be used with `nodeAffinity`.
1173
1215
{{<note>}}
1174
1216
<!--
1175
1217
` Gt` and `Lt` operators will not work with non-integer values. If the given value
1176
- doesn't parse as an integer, the pod will fail to get scheduled. Also, `Gt` and `Lt`
1218
+ doesn't parse as an integer, the Pod will fail to get scheduled. Also, `Gt` and `Lt`
1177
1219
are not available for `podAffinity`.
1178
1220
-->
1179
1221
` Gt` 和 `Lt` 操作符不能与非整数值一起使用。
0 commit comments