Skip to content

[zh-cn]sync assign-pod-node.md #51076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 71 additions & 29 deletions content/zh-cn/docs/concepts/scheduling-eviction/assign-pod-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ information.
## Affinity and anti-affinity

`nodeSelector` is the simplest way to constrain Pods to nodes with specific
labels. Affinity and anti-affinity expands the types of constraints you can
labels. Affinity and anti-affinity expand the types of constraints you can
define. Some of the benefits of affinity and anti-affinity include:
-->
## 亲和性与反亲和性 {#affinity-and-anti-affinity}
Expand Down Expand Up @@ -233,6 +233,7 @@ your Pod spec.
For example, consider the following Pod spec:
-->
你可以使用 Pod 规约中的 `.spec.affinity.nodeAffinity` 字段来设置节点亲和性。

例如,考虑下面的 Pod 规约:

{{% code_sample file="pods/pod-with-node-affinity.yaml" %}}
Expand Down Expand Up @@ -432,22 +433,26 @@ DaemonSet 控制器创建 Pod 时,默认的 Kubernetes 调度器负责放置 P
### Inter-pod affinity and anti-affinity

Inter-pod affinity and anti-affinity allow you to constrain which nodes your
Pods can be scheduled on based on the labels of **Pods** already running on that
Pods can be scheduled on based on the labels of Pods already running on that
node, instead of the node labels.
-->
### Pod 间亲和性与反亲和性 {#inter-pod-affinity-and-anti-affinity}

Pod 间亲和性与反亲和性使你可以基于已经在节点上运行的 **Pod** 的标签来约束
Pod 间亲和性与反亲和性使你可以基于已经在节点上运行的 Pod 的标签来约束
Pod 可以调度到的节点,而不是基于节点上的标签。

<!--
Inter-pod affinity and anti-affinity rules take the form "this
#### Types of Inter-pod Affinity and Anti-affinity

Inter-pod affinity and anti-affinity take the form "this
Pod should (or, in the case of anti-affinity, should not) run in an X if that X
is already running one or more Pods that meet rule Y", where X is a topology
domain like node, rack, cloud provider zone or region, or similar and Y is the
rule Kubernetes tries to satisfy.
-->
Pod 间亲和性与反亲和性的规则格式为“如果 X 上已经运行了一个或多个满足规则 Y 的 Pod,
#### Pod 间亲和性与反亲和性的类型

Pod 间亲和性与反亲和性的格式为“如果 X 上已经运行了一个或多个满足规则 Y 的 Pod,
则这个 Pod 应该(或者在反亲和性的情况下不应该)运行在 X 上”。
这里的 X 可以是节点、机架、云提供商可用区或地理区域或类似的拓扑域,
Y 则是 Kubernetes 尝试满足的规则。
Expand Down Expand Up @@ -496,13 +501,9 @@ Pod 反亲和性需要节点上存在一致性的标签。换言之,
{{< /note >}}

<!--
#### Types of inter-pod affinity and anti-affinity

Similar to [node affinity](#node-affinity) are two types of Pod affinity and
anti-affinity as follows:
-->
#### Pod 间亲和性与反亲和性的类型

与[节点亲和性](#node-affinity)类似,Pod 的亲和性与反亲和性也有两种类型:

- `requiredDuringSchedulingIgnoredDuringExecution`
Expand Down Expand Up @@ -530,13 +531,54 @@ spec.
对于 Pod 间反亲和性,可以使用 Pod 规约中的 `.affinity.podAntiAffinity` 字段。

<!--
#### Scheduling a group of pods with inter-pod affinity to themselves
#### Scheduling Behavior

When scheduling a new Pod, the Kubernetes scheduler evaluates the Pod's affinity/anti-affinity rules in the context of the current cluster state:

1. Hard Constraints (Node Filtering):
- `podAffinity.requiredDuringSchedulingIgnoredDuringExecution` and `podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution`:
- The scheduler ensures the new Pod is assigned to nodes that satisfy these required affinity and anti-affinity rules based on existing Pods.
-->
#### 调度行为

在调度新 Pod 时,Kubernetes 调度器会根据当前集群状态评估 Pod 的亲和性/反亲和性规则:

1. 硬约束(节点过滤):
- `podAffinity.requiredDuringSchedulingIgnoredDuringExecution` 和
`podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution`:
- 调度器基于现有 Pod,确保新 Pod 被分配到满足这些必需的亲和性和反亲和性规则的节点上。

<!--
2. Soft Constraints (Scoring):
- `podAffinity.preferredDuringSchedulingIgnoredDuringExecution` and `podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution`:
- The scheduler scores nodes based on how well they meet these preferred affinity and anti-affinity rules to optimize Pod placement.
-->
2. 软约束(评分):
- `podAffinity.preferredDuringSchedulingIgnoredDuringExecution` 和
`podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution`:
- 调度器根据节点满足这些优选的亲和性和反亲和性规则的程度来评分,以优化 Pod 的放置。

<!--
3. Ignored Fields:
- Existing Pods' `podAffinity.preferredDuringSchedulingIgnoredDuringExecution`:
- These preferred affinity rules are not considered during the scheduling decision for new Pods.
- Existing Pods' `podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution`:
- Similarly, preferred anti-affinity rules of existing Pods are ignored during scheduling.
-->
3. 忽略的字段:
- 现有 Pod 的 `podAffinity.preferredDuringSchedulingIgnoredDuringExecution`:
- 在为新 Pod 做调度决策时,不会考虑这些优选的亲和性规则。
- 现有 Pod 的 `podAntiAffinity.preferredDuringSchedulingIgnoredDuringExecution`:
- 同样,在调度时会忽略现有 Pod 的优选反亲和性规则。

<!--
#### Scheduling a Group of Pods with Inter-pod Affinity to Themselves

If the current Pod being scheduled is the first in a series that have affinity to themselves,
it is allowed to be scheduled if it passes all other affinity checks. This is determined by
verifying that no other pod in the cluster matches the namespace and selector of this pod,
that the pod matches its own terms, and the chosen node matches all requested topologies.
This ensures that there will not be a deadlock even if all the pods have inter-pod affinity
verifying that no other Pod in the cluster matches the namespace and selector of this Pod,
that the Pod matches its own terms, and the chosen node matches all requested topologies.
This ensures that there will not be a deadlock even if all the Pods have inter-pod affinity
specified.
-->
#### 调度一组具有 Pod 间亲和性的 Pod {#scheduling-a-group-of-pods-with-inter-pod-affinity-to-themselves}
Expand All @@ -548,7 +590,7 @@ specified.
这确保即使所有的 Pod 都配置了 Pod 间亲和性,也不会出现调度死锁的情况。

<!--
#### Pod affinity example {#an-example-of-a-pod-that-uses-pod-affinity}
#### Pod Affinity example {#an-example-of-a-pod-that-uses-pod-affinity}

Consider the following Pod spec:
-->
Expand Down Expand Up @@ -613,8 +655,7 @@ refer to the [design proposal](https://git.k8s.io/design-proposals-archive/sched
You can use the `In`, `NotIn`, `Exists` and `DoesNotExist` values in the
`operator` field for Pod affinity and anti-affinity.
-->
你可以针对 Pod 间亲和性与反亲和性为其 `operator` 字段使用 `In`、`NotIn`、`Exists`、
`DoesNotExist` 等值。
你可以针对 Pod 间亲和性与反亲和性为其 `operator` 字段使用 `In`、`NotIn`、`Exists`、`DoesNotExist` 等值。

<!--
Read [Operators](#operators)
Expand Down Expand Up @@ -658,7 +699,7 @@ affinity/anti-affinity definition appears.
如果 `namespaces` 被忽略或者为空,则默认为 Pod 亲和性/反亲和性的定义所在的名字空间。

<!--
#### Namespace selector
#### Namespace Selector
-->
#### 名字空间选择算符 {#namespace-selector}

Expand Down Expand Up @@ -695,17 +736,18 @@ When you want to disable it, you have to disable it explicitly via the

<!--
Kubernetes includes an optional `matchLabelKeys` field for Pod affinity
or anti-affinity. The field specifies keys for the labels that should match with the incoming Pod's labels,
or anti-affinity. The field specifies keys for the labels that should match with the incoming Pod's labels,
when satisfying the Pod (anti)affinity.

The keys are used to look up values from the pod labels; those key-value labels are combined
The keys are used to look up values from the Pod labels; those key-value labels are combined
(using `AND`) with the match restrictions defined using the `labelSelector` field. The combined
filtering selects the set of existing pods that will be taken into Pod (anti)affinity calculation.
filtering selects the set of existing Pods that will be taken into Pod (anti)affinity calculation.
-->
Kubernetes 在 Pod 亲和性或反亲和性中包含一个可选的 `matchLabelKeys` 字段。
此字段指定了应与传入 Pod 的标签匹配的标签键,以满足 Pod 的(反)亲和性。

这些键用于从 Pod 的标签中查找值;这些键值标签与使用 `labelSelector` 字段定义的匹配限制组合(使用 `AND` 操作)。
这些键用于从 Pod 的标签中查找值;这些键值标签与使用 `labelSelector`
字段定义的匹配限制组合(使用 `AND` 操作)。
这种组合的过滤机制选择将用于 Pod(反)亲和性计算的现有 Pod 集合。

{{< caution >}}
Expand Down Expand Up @@ -803,11 +845,11 @@ When you want to disable it, you have to disable it explicitly via the

<!--
Kubernetes includes an optional `mismatchLabelKeys` field for Pod affinity
or anti-affinity. The field specifies keys for the labels that should **not** match with the incoming Pod's labels,
or anti-affinity. The field specifies keys for the labels that should not match with the incoming Pod's labels,
when satisfying the Pod (anti)affinity.
-->
Kubernetes 为 Pod 亲和性或反亲和性提供了一个可选的 `mismatchLabelKeys` 字段。
此字段指定了在满足 Pod(反)亲和性时,**不**应与传入 Pod 的标签匹配的键。
此字段指定了在满足 Pod(反)亲和性时,不应与传入 Pod 的标签匹配的键。

{{< caution >}}
<!--
Expand Down Expand Up @@ -841,20 +883,20 @@ spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
# ensure that pods associated with this tenant land on the correct node pool
# ensure that Pods associated with this tenant land on the correct node pool
- matchLabelKeys:
- tenant
topologyKey: node-pool
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
# ensure that pods associated with this tenant can't schedule to nodes used for another tenant
# ensure that Pods associated with this tenant can't schedule to nodes used for another tenant
- mismatchLabelKeys:
- tenant # whatever the value of the "tenant" label for this Pod, prevent
# scheduling to nodes in any pool where any Pod from a different
# tenant is running.
labelSelector:
# We have to have the labelSelector which selects only Pods with the tenant label,
# otherwise this Pod would have Pods from daemonsets as well, for example,
# otherwise this Pod would have anti-affinity against Pods from daemonsets as well, for example,
# which aren't supposed to have the tenant label.
matchExpressions:
- key: tenant
Expand Down Expand Up @@ -887,8 +929,8 @@ spec:
# 都会阻碍此 Pod 被调度到这些节点池中的节点上
labelSelector:
# 我们必须有一个 labelSelector,只选择具有 “tenant” 标签的 Pod,
# 否则此 Pod 也会与来自 DaemonSet 的 Pod 发生冲突
# 而这些 Pod 不应该具有 “tenant” 标签
# 否则此 Pod 也会与来自 DaemonSet 的 Pod 产生反亲和性
# 例如,这些 Pod 不应该具有 “tenant” 标签
matchExpressions:
- key: tenant
operator: Exists
Expand Down Expand Up @@ -1173,7 +1215,7 @@ The following operators can only be used with `nodeAffinity`.
{{<note>}}
<!--
`Gt` and `Lt` operators will not work with non-integer values. If the given value
doesn't parse as an integer, the pod will fail to get scheduled. Also, `Gt` and `Lt`
doesn't parse as an integer, the Pod will fail to get scheduled. Also, `Gt` and `Lt`
are not available for `podAffinity`.
-->
`Gt` 和 `Lt` 操作符不能与非整数值一起使用。
Expand Down