Skip to content

Commit 7ac42d2

Browse files
authored
Merge pull request #33364 from howieyuen/statefulset
[zh]sync concept of statefulset
2 parents 261e0ab + 56de9d6 commit 7ac42d2

File tree

1 file changed

+205
-25
lines changed

1 file changed

+205
-25
lines changed

content/zh/docs/concepts/workloads/controllers/statefulset.md

Lines changed: 205 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,14 @@ metadata:
116116
spec:
117117
selector:
118118
matchLabels:
119-
app: nginx # has to match .spec.template.metadata.labels
119+
app: nginx # 必须匹配 .spec.template.metadata.labels
120120
serviceName: "nginx"
121-
replicas: 3 # by default is 1
121+
replicas: 3 # 默认值是 1
122+
minReadySeconds: 10 # 默认值是 0
122123
template:
123124
metadata:
124125
labels:
125-
app: nginx # has to match .spec.selector.matchLabels
126+
app: nginx # 必须匹配 .spec.selector.matchLabels
126127
spec:
127128
terminationGracePeriodSeconds: 10
128129
containers:
@@ -166,17 +167,47 @@ The name of a StatefulSet object must be a valid
166167
StatefulSet 的命名需要遵循[DNS 子域名](/zh/docs/concepts/overview/working-with-objects/names#dns-subdomain-names)规范。
167168

168169
<!--
169-
## Pod Selector
170+
### Pod Selector
170171
-->
171-
## Pod 选择算符 {#pod-selector}
172+
### Pod 选择算符 {#pod-selector}
172173

173174
<!--
174-
You must set the `.spec.selector` field of a StatefulSet to match the labels of its `.spec.template.metadata.labels`. Prior to Kubernetes 1.8, the `.spec.selector` field was defaulted when omitted. In 1.8 and later versions, failing to specify a matching Pod Selector will result in a validation error during StatefulSet creation.
175+
You must set the `.spec.selector` field of a StatefulSet to match the labels of its `.spec.template.metadata.labels`. Failing to specify a matching Pod Selector will result in a validation error during StatefulSet creation.
175176
-->
176177
你必须设置 StatefulSet 的 `.spec.selector` 字段,使之匹配其在
177-
`.spec.template.metadata.labels` 中设置的标签。在 Kubernetes 1.8 版本之前,
178-
被忽略 `.spec.selector` 字段会获得默认设置值。
179-
在 1.8 和以后的版本中,未指定匹配的 Pod 选择器将在创建 StatefulSet 期间导致验证错误。
178+
`.spec.template.metadata.labels` 中设置的标签。
179+
未指定匹配的 Pod 选择器将在创建 StatefulSet 期间导致验证错误。
180+
181+
<!--
182+
### Volume Claim Templates
183+
184+
You can set the `.spec.volumeClaimTemplates` which can provide stable storage using [PersistentVolumes](/docs/concepts/storage/persistent-volumes/) provisioned by a PersistentVolume Provisioner.
185+
-->
186+
### 卷申领模版 {#volume-claim-templates}
187+
188+
你可以设置 `.spec.volumeClaimTemplates`,
189+
它可以使用 PersistentVolume 制备程序所准备的
190+
[PersistentVolumes](/zh/docs/concepts/storage/persistent-volumes/) 来提供稳定的存储。
191+
192+
<!-- ### Minimum ready seconds -->
193+
### 最短就绪秒数 {#minimum-ready-seconds}
194+
195+
{{< feature-state for_k8s_version="v1.23" state="beta" >}}
196+
197+
<!--
198+
`.spec.minReadySeconds` is an optional field that specifies the minimum number of seconds for which a newly
199+
created Pod should be ready without any of its containers crashing, for it to be considered available.
200+
Please note that this feature is beta and enabled by default. Please opt out by unsetting the StatefulSetMinReadySeconds flag, if you don't
201+
want this feature to be enabled. This field defaults to 0 (the Pod will be considered
202+
available as soon as it is ready). To learn more about when a Pod is considered ready, see [Container Probes](/docs/concepts/workloads/pods/pod-lifecycle/#container-probes).
203+
-->
204+
`.spec.minReadySeconds` 是一个可选字段,
205+
它指定新创建的 Pod 应该准备好且其任何容器不崩溃的最小秒数,以使其被视为可用。
206+
请注意,此功能是测试版,默认启用。如果你不希望启用此功能,
207+
请通过取消设置 StatefulSetMinReadySeconds 标志来选择退出。
208+
该字段默认为 0(Pod 准备就绪后将被视为可用)。
209+
要了解有关何时认为 Pod 准备就绪的更多信息,
210+
请参阅[容器探针](/zh/docs/concepts/workloads/pods/pod-lifecycle/#container-probes)。
180211

181212
<!--
182213
## Pod Identity
@@ -372,12 +403,12 @@ until web-0 is Running and Ready.
372403
<!--
373404
### Pod Management Policies
374405

375-
In Kubernetes 1.7 and later, StatefulSet allows you to relax its ordering guarantees while
406+
StatefulSet allows you to relax its ordering guarantees while
376407
preserving its uniqueness and identity guarantees via its `.spec.podManagementPolicy` field.
377408
-->
378409
### Pod 管理策略 {#pod-management-policies}
379410

380-
在 Kubernetes 1.7 及以后的版本中,StatefulSet 允许你放宽其排序保证,
411+
StatefulSet 允许你放宽其排序保证,
381412
同时通过它的 `.spec.podManagementPolicy` 域保持其唯一性和身份保证。
382413

383414
<!--
@@ -519,24 +550,173 @@ StatefulSet will then begin to recreate the Pods using the reverted template.
519550
恢复模板后,还必须删除 StatefulSet 尝试使用错误的配置来运行的 Pod。这样,
520551
StatefulSet 才会开始使用被还原的模板来重新创建 Pod。
521552

522-
<!--
523-
### Minimum ready seconds
553+
<!-- ## PersistentVolumeClaim retention -->
554+
## PersistentVolumeClaim 保留 {#persistentvolumeclaim-retention}
524555

525-
`.spec.minReadySeconds` is an optional field that specifies the minimum number of seconds for which a newly
526-
created Pod should be ready without any of its containers crashing, for it to be considered available.
527-
This defaults to 0 (the Pod will be considered available as soon as it is ready). To learn more about when
528-
a Pod is considered ready, see [Container Probes](/docs/concepts/workloads/pods/pod-lifecycle/#container-probes).
556+
{{< feature-state for_k8s_version="v1.23" state="alpha" >}}
557+
558+
<!--
559+
The optional `.spec.persistentVolumeClaimRetentionPolicy` field controls if
560+
and how PVCs are deleted during the lifecycle of a StatefulSet. You must enable the
561+
`StatefulSetAutoDeletePVC` [feature gate](/docs/reference/command-line-tools-reference/feature-gates/)
562+
to use this field. Once enabled, there are two policies you can configure for each
563+
StatefulSet:
564+
-->
565+
在 StatefulSet 的生命周期中,可选字段
566+
`.spec.persistentVolumeClaimRetentionPolicy` 控制是否删除以及如何删除 PVC。
567+
使用该字段,你必须启用 `StatefulSetAutoDeletePVC`
568+
[特性门控](/zh/docs/reference/command-line-tools-reference/feature-gates/)。
569+
启用后,你可以为每个 StatefulSet 配置两个策略:
570+
571+
<!--
572+
`whenDeleted`
573+
: configures the volume retention behavior that applies when the StatefulSet is deleted
529574

530-
Please note that this field only works if you enable the `StatefulSetMinReadySeconds` [feature gate](/docs/reference/command-line-tools-reference/feature-gates/).
575+
`whenScaled`
576+
: configures the volume retention behavior that applies when the replica count of
577+
the StatefulSet is reduced; for example, when scaling down the set.
578+
579+
For each policy that you can configure, you can set the value to either `Delete` or `Retain`.
531580
-->
532-
### 最短就绪秒数 {#minimum-ready-seconds}
581+
`whenDeleted`
582+
: 配置删除 StatefulSet 时应用的卷保留行为
533583

534-
{{< feature-state for_k8s_version="v1.22" state="alpha" >}}
584+
`whenScaled`
585+
: 配置当 StatefulSet 的副本数减少时应用的卷保留行为;例如,缩小集合时。
586+
587+
对于你可以配置的每个策略,你可以将值设置为 `Delete` 或 `Retain`。
535588

536-
`.spec.minReadySeconds` 是一个可选字段,用于指定新创建的 Pod 就绪(没有任何容器崩溃)后被认为可用的最小秒数。
537-
默认值是 0(Pod 就绪时就被认为可用)。要了解 Pod 何时被认为已就绪,请参阅[容器探针](/zh/docs/concepts/workloads/pods/pod-lifecycle/#container-probes)。
589+
<!--
590+
`Delete`
591+
: The PVCs created from the StatefulSet `volumeClaimTemplate` are deleted for each Pod
592+
affected by the policy. With the `whenDeleted` policy all PVCs from the
593+
`volumeClaimTemplate` are deleted after their Pods have been deleted. With the
594+
`whenScaled` policy, only PVCs corresponding to Pod replicas being scaled down are
595+
deleted, after their Pods have been deleted.
596+
-->
597+
`Delete`
598+
: 对于受策略影响的每个 Pod,基于 StatefulSet 的 `volumeClaimTemplate` 字段创建的 PVC 都会被删除。
599+
使用 `whenDeleted` 策略,所有来自 `volumeClaimTemplate` 的 PVC 在其 Pod 被删除后都会被删除。
600+
使用 `whenScaled` 策略,只有与被缩减的 Pod 副本对应的 PVC 在其 Pod 被删除后才会被删除。
601+
602+
<!--
603+
`Retain` (default)
604+
: PVCs from the `volumeClaimTemplate` are not affected when their Pod is
605+
deleted. This is the behavior before this new feature.
606+
-->
607+
`Retain`(默认)
608+
: 来自 `volumeClaimTemplate` 的 PVC 在 Pod 被删除时不受影响。这是此新功能之前的行为。
609+
610+
<!--
611+
Bear in mind that these policies **only** apply when Pods are being removed due to the
612+
StatefulSet being deleted or scaled down. For example, if a Pod associated with a StatefulSet
613+
fails due to node failure, and the control plane creates a replacement Pod, the StatefulSet
614+
retains the existing PVC. The existing volume is unaffected, and the cluster will attach it to
615+
the node where the new Pod is about to launch.
616+
617+
The default for policies is `Retain`, matching the StatefulSet behavior before this new feature.
618+
619+
Here is an example policy.
620+
-->
621+
请记住,这些策略**仅**适用于由于 StatefulSet 被删除或被缩小而被删除的 Pod。
622+
例如,如果与 StatefulSet 关联的 Pod 由于节点故障而失败,
623+
并且控制平面创建了替换 Pod,则 StatefulSet 保留现有的 PVC。
624+
现有卷不受影响,集群会将其附加到新 Pod 即将启动的节点上。
625+
626+
策略的默认值为 `Retain`,与此新功能之前的 StatefulSet 行为相匹配。
538627

539-
请注意只有当你启用 `StatefulSetMinReadySeconds` [特性门控](/zh/docs/reference/command-line-tools-reference/feature-gates/)时,该字段才会生效。
628+
这是一个示例策略。
629+
630+
```yaml
631+
apiVersion: apps/v1
632+
kind: StatefulSet
633+
...
634+
spec:
635+
persistentVolumeClaimRetentionPolicy:
636+
whenDeleted: Retain
637+
whenScaled: Delete
638+
...
639+
```
640+
641+
<!--
642+
The StatefulSet {{<glossary_tooltip text="controller" term_id="controller">}} adds [owner
643+
references](/docs/concepts/overview/working-with-objects/owners-dependents/#owner-references-in-object-specifications)
644+
to its PVCs, which are then deleted by the {{<glossary_tooltip text="garbage collector"
645+
term_id="garbage-collection">}} after the Pod is terminated. This enables the Pod to
646+
cleanly unmount all volumes before the PVCs are deleted (and before the backing PV and
647+
volume are deleted, depending on the retain policy). When you set the `whenDeleted`
648+
policy to `Delete`, an owner reference to the StatefulSet instance is placed on all PVCs
649+
associated with that StatefulSet.
650+
-->
651+
StatefulSet {{<glossary_tooltip text="控制器" term_id="controller">}}为其 PVC 添加了
652+
[属主引用](/zh/docs/concepts/overview/working-with-objects/owners-dependents/#owner-references-in-object-specifications),
653+
这些 PVC 在 Pod 终止后被{{<glossary_tooltip text="垃圾回收器" term_id="garbage-collection">}}删除。
654+
这使 Pod 能够在删除 PVC 之前(以及在删除后备 PV 和卷之前,取决于保留策略)干净地卸载所有卷。
655+
当你设置 `whenDeleted` 删除策略,对 StatefulSet 实例的属主引用放置在与该 StatefulSet 关联的所有 PVC 上。
656+
657+
<!--
658+
The `whenScaled` policy must delete PVCs only when a Pod is scaled down, and not when a
659+
Pod is deleted for another reason. When reconciling, the StatefulSet controller compares
660+
its desired replica count to the actual Pods present on the cluster. Any StatefulSet Pod
661+
whose id greater than the replica count is condemned and marked for deletion. If the
662+
`whenScaled` policy is `Delete`, the condemned Pods are first set as owners to the
663+
associated StatefulSet template PVCs, before the Pod is deleted. This causes the PVCs
664+
to be garbage collected after only the condemned Pods have terminated.
665+
-->
666+
`whenScaled` 策略必须仅在 Pod 缩减时删除 PVC,而不是在 Pod 因其他原因被删除时删除。
667+
执行协调操作时,StatefulSet 控制器将其所需的副本数与集群上实际存在的 Pod 进行比较。
668+
对于 StatefulSet 中的所有 Pod 而言,如果其 ID 大于副本数,则将被废弃并标记为需要删除。
669+
如果 `whenScaled` 策略是 `Delete`,则在删除 Pod 之前,
670+
首先将已销毁的 Pod 设置为与 StatefulSet 模板 对应的 PVC 的属主。
671+
这会导致 PVC 仅在已废弃的 Pod 终止后被垃圾收集。
672+
673+
<!--
674+
This means that if the controller crashes and restarts, no Pod will be deleted before its
675+
owner reference has been updated appropriate to the policy. If a condemned Pod is
676+
force-deleted while the controller is down, the owner reference may or may not have been
677+
set up, depending on when the controller crashed. It may take several reconcile loops to
678+
update the owner references, so some condemned Pods may have set up owner references and
679+
other may not. For this reason we recommend waiting for the controller to come back up,
680+
which will verify owner references before terminating Pods. If that is not possible, the
681+
operator should verify the owner references on PVCs to ensure the expected objects are
682+
deleted when Pods are force-deleted.
683+
-->
684+
这意味着如果控制器崩溃并重新启动,在其属主引用更新到适合策略的 Pod 之前,不会删除任何 Pod。
685+
如果在控制器关闭时强制删除了已废弃的 Pod,则属主引用可能已被设置,也可能未被设置,具体取决于控制器何时崩溃。
686+
更新属主引用可能需要几个协调循环,因此一些已废弃的 Pod 可能已经被设置了属主引用,而其他可能没有。
687+
出于这个原因,我们建议等待控制器恢复,控制器将在终止 Pod 之前验证属主引用。
688+
如果这不可行,则操作员应验证 PVC 上的属主引用,以确保在强制删除 Pod 时删除预期的对象。
689+
690+
<!--
691+
### Replicas
692+
693+
`.spec.replicas` is an optional field that specifies the number of desired Pods. It defaults to 1.
694+
695+
Should you manually scale a deployment, example via `kubectl scale
696+
statefulset statefulset --replicas=X`, and then you update that StatefulSet
697+
based on a manifest (for example: by running `kubectl apply -f
698+
statefulset.yaml`), then applying that manifest overwrites the manual scaling
699+
that you previously did.
700+
-->
701+
### 副本数 {#replicas}
702+
703+
`.spec.replicas` 是一个可选字段,用于指定所需 Pod 的数量。它的默认值为 1。
704+
705+
如果你手动扩缩已部署的负载,例如通过 `kubectl scale statefulset statefulset --replicas=X`,
706+
然后根据清单更新 StatefulSet(例如:通过运行 `kubectl apply -f statefulset.yaml`),
707+
那么应用该清单的操作会覆盖你之前所做的手动缩放。
708+
709+
<!--
710+
If a [HorizontalPodAutoscaler](/docs/tasks/run-application/horizontal-pod-autoscale/)
711+
(or any similar API for horizontal scaling) is managing scaling for a
712+
Statefulset, don't set `.spec.replicas`. Instead, allow the Kubernetes
713+
{{<glossary_tooltip text="control plane" term_id="control-plane" >}} to manage
714+
the `.spec.replicas` field automatically.
715+
-->
716+
如果 [HorizontalPodAutoscaler](/zh/docs/tasks/run-application/horizontal-pod-autoscale/)
717+
(或任何类似的水平缩放 API)正在管理 Statefulset 的缩放,
718+
请不要设置 `.spec.replicas`。
719+
相反,允许 Kubernetes 控制平面自动管理 `.spec.replicas` 字段。
540720

541721
## {{% heading "whatsnext" %}}
542722

@@ -556,7 +736,7 @@ Please note that this field only works if you enable the `StatefulSetMinReadySec
556736
* Read about [PodDisruptionBudget](/docs/concepts/workloads/pods/disruptions/) and how
557737
you can use it to manage application availability during disruptions.
558738
-->
559-
* 了解 [Pods](/zh/docs/concepts/workloads/pods)。
739+
* 了解 [Pod](/zh/docs/concepts/workloads/pods)。
560740
* 了解如何使用 StatefulSet
561741
* 跟随示例[部署有状态应用](/zh/docs/tutorials/stateful-application/basic-stateful-set/)。
562742
* 跟随示例[使用 StatefulSet 部署 Cassandra](/zh/docs/tutorials/stateful-application/cassandra/)。
@@ -567,4 +747,4 @@ Please note that this field only works if you enable the `StatefulSetMinReadySec
567747
* 了解如何[配置 Pod 以使用 PersistentVolume 作为存储](/zh/docs/tasks/configure-pod-container/configure-persistent-volume-storage/)。
568748
* `StatefulSet` 是 Kubernetes REST API 中的顶级资源。阅读 {{< api-reference page="workload-resources/stateful-set-v1" >}}
569749
对象定义理解关于该资源的 API。
570-
* 阅读[Pod 干扰预算(Disruption Budget)](/zh/docs/concepts/workloads/pods/disruptions/),了解如何在干扰下运行高度可用的应用。
750+
* 阅读 [Pod 干扰预算(Disruption Budget)](/zh/docs/concepts/workloads/pods/disruptions/),了解如何在干扰下运行高度可用的应用。

0 commit comments

Comments
 (0)