From 56de9d601590f4e2f8fc56066dd2960e08d5e341 Mon Sep 17 00:00:00 2001 From: howieyuen Date: Sun, 1 May 2022 16:32:03 +0800 Subject: [PATCH] [zh]sync concept of statefulset --- .../workloads/controllers/statefulset.md | 230 ++++++++++++++++-- 1 file changed, 205 insertions(+), 25 deletions(-) diff --git a/content/zh/docs/concepts/workloads/controllers/statefulset.md b/content/zh/docs/concepts/workloads/controllers/statefulset.md index 26f00ae25c..a7919d8207 100644 --- a/content/zh/docs/concepts/workloads/controllers/statefulset.md +++ b/content/zh/docs/concepts/workloads/controllers/statefulset.md @@ -116,13 +116,14 @@ metadata: spec: selector: matchLabels: - app: nginx # has to match .spec.template.metadata.labels + app: nginx # 必须匹配 .spec.template.metadata.labels serviceName: "nginx" - replicas: 3 # by default is 1 + replicas: 3 # 默认值是 1 + minReadySeconds: 10 # 默认值是 0 template: metadata: labels: - app: nginx # has to match .spec.selector.matchLabels + app: nginx # 必须匹配 .spec.selector.matchLabels spec: terminationGracePeriodSeconds: 10 containers: @@ -166,17 +167,47 @@ The name of a StatefulSet object must be a valid StatefulSet 的命名需要遵循[DNS 子域名](/zh/docs/concepts/overview/working-with-objects/names#dns-subdomain-names)规范。 -## Pod 选择算符 {#pod-selector} +### Pod 选择算符 {#pod-selector} 你必须设置 StatefulSet 的 `.spec.selector` 字段,使之匹配其在 -`.spec.template.metadata.labels` 中设置的标签。在 Kubernetes 1.8 版本之前, -被忽略 `.spec.selector` 字段会获得默认设置值。 -在 1.8 和以后的版本中,未指定匹配的 Pod 选择器将在创建 StatefulSet 期间导致验证错误。 +`.spec.template.metadata.labels` 中设置的标签。 +未指定匹配的 Pod 选择器将在创建 StatefulSet 期间导致验证错误。 + + +### 卷申领模版 {#volume-claim-templates} + +你可以设置 `.spec.volumeClaimTemplates`, +它可以使用 PersistentVolume 制备程序所准备的 +[PersistentVolumes](/zh/docs/concepts/storage/persistent-volumes/) 来提供稳定的存储。 + + +### 最短就绪秒数 {#minimum-ready-seconds} + +{{< feature-state for_k8s_version="v1.23" state="beta" >}} + + +`.spec.minReadySeconds` 是一个可选字段, +它指定新创建的 Pod 应该准备好且其任何容器不崩溃的最小秒数,以使其被视为可用。 +请注意,此功能是测试版,默认启用。如果你不希望启用此功能, +请通过取消设置 StatefulSetMinReadySeconds 标志来选择退出。 +该字段默认为 0(Pod 准备就绪后将被视为可用)。 +要了解有关何时认为 Pod 准备就绪的更多信息, +请参阅[容器探针](/zh/docs/concepts/workloads/pods/pod-lifecycle/#container-probes)。 ### Pod 管理策略 {#pod-management-policies} -在 Kubernetes 1.7 及以后的版本中,StatefulSet 允许你放宽其排序保证, +StatefulSet 允许你放宽其排序保证, 同时通过它的 `.spec.podManagementPolicy` 域保持其唯一性和身份保证。 +## PersistentVolumeClaim 保留 {#persistentvolumeclaim-retention} -`.spec.minReadySeconds` is an optional field that specifies the minimum number of seconds for which a newly -created Pod should be ready without any of its containers crashing, for it to be considered available. -This defaults to 0 (the Pod will be considered 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). +{{< feature-state for_k8s_version="v1.23" state="alpha" >}} -Please note that this field only works if you enable the `StatefulSetMinReadySeconds` [feature gate](/docs/reference/command-line-tools-reference/feature-gates/). + -### 最短就绪秒数 {#minimum-ready-seconds} +在 StatefulSet 的生命周期中,可选字段 +`.spec.persistentVolumeClaimRetentionPolicy` 控制是否删除以及如何删除 PVC。 +使用该字段,你必须启用 `StatefulSetAutoDeletePVC` +[特性门控](/zh/docs/reference/command-line-tools-reference/feature-gates/)。 +启用后,你可以为每个 StatefulSet 配置两个策略: -{{< feature-state for_k8s_version="v1.22" state="alpha" >}} + +`whenDeleted` +: 配置删除 StatefulSet 时应用的卷保留行为 -请注意只有当你启用 `StatefulSetMinReadySeconds` [特性门控](/zh/docs/reference/command-line-tools-reference/feature-gates/)时,该字段才会生效。 +`whenScaled` +: 配置当 StatefulSet 的副本数减少时应用的卷保留行为;例如,缩小集合时。 + +对于你可以配置的每个策略,你可以将值设置为 `Delete` 或 `Retain`。 + + +`Delete` +: 对于受策略影响的每个 Pod,基于 StatefulSet 的 `volumeClaimTemplate` 字段创建的 PVC 都会被删除。 + 使用 `whenDeleted` 策略,所有来自 `volumeClaimTemplate` 的 PVC 在其 Pod 被删除后都会被删除。 + 使用 `whenScaled` 策略,只有与被缩减的 Pod 副本对应的 PVC 在其 Pod 被删除后才会被删除。 + + +`Retain`(默认) +: 来自 `volumeClaimTemplate` 的 PVC 在 Pod 被删除时不受影响。这是此新功能之前的行为。 + + +请记住,这些策略**仅**适用于由于 StatefulSet 被删除或被缩小而被删除的 Pod。 +例如,如果与 StatefulSet 关联的 Pod 由于节点故障而失败, +并且控制平面创建了替换 Pod,则 StatefulSet 保留现有的 PVC。 +现有卷不受影响,集群会将其附加到新 Pod 即将启动的节点上。 + +策略的默认值为 `Retain`,与此新功能之前的 StatefulSet 行为相匹配。 + +这是一个示例策略。 + +```yaml +apiVersion: apps/v1 +kind: StatefulSet +... +spec: + persistentVolumeClaimRetentionPolicy: + whenDeleted: Retain + whenScaled: Delete +... +``` + + +StatefulSet {{}}为其 PVC 添加了 +[属主引用](/zh/docs/concepts/overview/working-with-objects/owners-dependents/#owner-references-in-object-specifications), +这些 PVC 在 Pod 终止后被{{}}删除。 +这使 Pod 能够在删除 PVC 之前(以及在删除后备 PV 和卷之前,取决于保留策略)干净地卸载所有卷。 +当你设置 `whenDeleted` 删除策略,对 StatefulSet 实例的属主引用放置在与该 StatefulSet 关联的所有 PVC 上。 + + +`whenScaled` 策略必须仅在 Pod 缩减时删除 PVC,而不是在 Pod 因其他原因被删除时删除。 +执行协调操作时,StatefulSet 控制器将其所需的副本数与集群上实际存在的 Pod 进行比较。 +对于 StatefulSet 中的所有 Pod 而言,如果其 ID 大于副本数,则将被废弃并标记为需要删除。 +如果 `whenScaled` 策略是 `Delete`,则在删除 Pod 之前, +首先将已销毁的 Pod 设置为与 StatefulSet 模板 对应的 PVC 的属主。 +这会导致 PVC 仅在已废弃的 Pod 终止后被垃圾收集。 + + +这意味着如果控制器崩溃并重新启动,在其属主引用更新到适合策略的 Pod 之前,不会删除任何 Pod。 +如果在控制器关闭时强制删除了已废弃的 Pod,则属主引用可能已被设置,也可能未被设置,具体取决于控制器何时崩溃。 +更新属主引用可能需要几个协调循环,因此一些已废弃的 Pod 可能已经被设置了属主引用,而其他可能没有。 +出于这个原因,我们建议等待控制器恢复,控制器将在终止 Pod 之前验证属主引用。 +如果这不可行,则操作员应验证 PVC 上的属主引用,以确保在强制删除 Pod 时删除预期的对象。 + + +### 副本数 {#replicas} + +`.spec.replicas` 是一个可选字段,用于指定所需 Pod 的数量。它的默认值为 1。 + +如果你手动扩缩已部署的负载,例如通过 `kubectl scale statefulset statefulset --replicas=X`, +然后根据清单更新 StatefulSet(例如:通过运行 `kubectl apply -f statefulset.yaml`), +那么应用该清单的操作会覆盖你之前所做的手动缩放。 + + +如果 [HorizontalPodAutoscaler](/zh/docs/tasks/run-application/horizontal-pod-autoscale/) +(或任何类似的水平缩放 API)正在管理 Statefulset 的缩放, +请不要设置 `.spec.replicas`。 +相反,允许 Kubernetes 控制平面自动管理 `.spec.replicas` 字段。 ## {{% heading "whatsnext" %}} @@ -556,7 +736,7 @@ Please note that this field only works if you enable the `StatefulSetMinReadySec * Read about [PodDisruptionBudget](/docs/concepts/workloads/pods/disruptions/) and how you can use it to manage application availability during disruptions. --> -* 了解 [Pods](/zh/docs/concepts/workloads/pods)。 +* 了解 [Pod](/zh/docs/concepts/workloads/pods)。 * 了解如何使用 StatefulSet * 跟随示例[部署有状态应用](/zh/docs/tutorials/stateful-application/basic-stateful-set/)。 * 跟随示例[使用 StatefulSet 部署 Cassandra](/zh/docs/tutorials/stateful-application/cassandra/)。 @@ -567,4 +747,4 @@ Please note that this field only works if you enable the `StatefulSetMinReadySec * 了解如何[配置 Pod 以使用 PersistentVolume 作为存储](/zh/docs/tasks/configure-pod-container/configure-persistent-volume-storage/)。 * `StatefulSet` 是 Kubernetes REST API 中的顶级资源。阅读 {{< api-reference page="workload-resources/stateful-set-v1" >}} 对象定义理解关于该资源的 API。 -* 阅读[Pod 干扰预算(Disruption Budget)](/zh/docs/concepts/workloads/pods/disruptions/),了解如何在干扰下运行高度可用的应用。 +* 阅读 [Pod 干扰预算(Disruption Budget)](/zh/docs/concepts/workloads/pods/disruptions/),了解如何在干扰下运行高度可用的应用。