From 34369cba01d84221883aa78d527626e02c3db935 Mon Sep 17 00:00:00 2001 From: Sean Wei Date: Sat, 18 Jun 2022 14:44:00 +0800 Subject: [PATCH] [zh] Sync basic-stateful-set.md --- .../basic-stateful-set.md | 972 +++++++++--------- 1 file changed, 508 insertions(+), 464 deletions(-) diff --git a/content/zh-cn/docs/tutorials/stateful-application/basic-stateful-set.md b/content/zh-cn/docs/tutorials/stateful-application/basic-stateful-set.md index e7c37ff3b0..e51177de71 100644 --- a/content/zh-cn/docs/tutorials/stateful-application/basic-stateful-set.md +++ b/content/zh-cn/docs/tutorials/stateful-application/basic-stateful-set.md @@ -20,109 +20,115 @@ weight: 10 - -本教程介绍了如何使用 [StatefulSets](/zh/docs/concepts/workloads/controllers/statefulset/) 来管理应用。 -演示了如何创建、删除、扩容/缩容和更新 StatefulSets 的 Pods。 - - +本教程介绍了如何使用 +{{< glossary_tooltip text="StatefulSet" term_id="statefulset" >}} +来管理应用。 +演示了如何创建、删除、扩容/缩容和更新 StatefulSet 的 Pod。 ## {{% heading "prerequisites" %}} - - 在开始本教程之前,你应该熟悉以下 Kubernetes 的概念: -* [Pods](/zh/docs/concepts/workloads/pods/) -* [Cluster DNS](/zh/docs/concepts/services-networking/dns-pod-service/) -* [Headless Services](/zh/docs/concepts/services-networking/service/#headless-services) -* [PersistentVolumes](/zh/docs/concepts/storage/persistent-volumes/) + +* [Pods](/zh-cn/docs/concepts/workloads/pods/) +* [Cluster DNS](/zh-cn/docs/concepts/services-networking/dns-pod-service/) +* [Headless Services](/zh-cn/docs/concepts/services-networking/service/#headless-services) +* [PersistentVolumes](/zh-cn/docs/concepts/storage/persistent-volumes/) +* [PersistentVolume Provisioning](https://github.com/kubernetes/examples/tree/master/staging/persistent-volume-provisioning/) +* [StatefulSets](/zh-cn/docs/concepts/workloads/controllers/statefulset/) +* [kubectl](/zh-cn/docs/reference/kubectl/kubectl/) 命令行工具 +{{< note >}} - -本教程假设你的集群被配置为动态的提供 PersistentVolumes。如果没有这样配置,在开始本教程之前,你需要手动准备 2 个 1 GiB 的存储卷。 - - +本教程假设你的集群被配置为动态制备 PersistentVolume 卷。 +如果没有这样配置,在开始本教程之前,你需要手动准备 2 个 1 GiB 的存储卷。 +{{< /note >}} ## {{% heading "objectives" %}} - +StatefulSet 旨在与有状态的应用及分布式系统一起使用。然而在 Kubernetes +上管理有状态应用和分布式系统是一个宽泛而复杂的话题。 +为了演示 StatefulSet 的基本特性,并且不使前后的主题混淆,你将会使用 StatefulSet 部署一个简单的 web 应用。 + +在阅读本教程后,你将熟悉以下内容: + - -StatefulSets 旨在与有状态的应用及分布式系统一起使用。然而在 Kubernetes 上管理有状态应用和分布式系统是一个宽泛而复杂的话题。 -为了演示 StatefulSet 的基本特性,并且不使前后的主题混淆,你将会使用 StatefulSet 部署一个简单的 web 应用。 - -在阅读本教程后,你将熟悉以下内容: - * 如何创建 StatefulSet -* StatefulSet 怎样管理它的 Pods +* StatefulSet 怎样管理它的 Pod * 如何删除 StatefulSet * 如何对 StatefulSet 进行扩容/缩容 -* 如何更新一个 StatefulSet 的 Pods - - - +* 如何更新一个 StatefulSet 的 Pod +## 创建 StatefulSet {#creating-a-statefulset} + - -## 创建 StatefulSet - - -作为开始,使用如下示例创建一个 StatefulSet。它和 [StatefulSets](/zh/docs/concepts/workloads/controllers/statefulset/) 概念中的示例相似。 -它创建了一个 [Headless Service](/zh/docs/concepts/services-networking/service/#headless-services) `nginx` 用来发布 StatefulSet `web` 中的 Pod 的 IP 地址。 +作为开始,使用如下示例创建一个 StatefulSet。它和 +[StatefulSet](/zh-cn/docs/concepts/workloads/controllers/statefulset/) 概念中的示例相似。 +它创建了一个 [Headless Service](/zh-cn/docs/concepts/services-networking/service/#headless-services) +`nginx` 用来发布 StatefulSet `web` 中的 Pod 的 IP 地址。 {{< codenew file="application/web/web.yaml" >}} +下载上面的例子并保存为文件 `web.yaml`。 + - -下载上面的例子并保存为文件 `web.yaml`。 - - -你需要使用两个终端窗口。 在第一个终端中,使用 [`kubectl get`](/zh/docs/user-guide/kubectl/{{< param "version" >}}/#get) 来查看 StatefulSet 的 Pods 的创建情况。 +你需要使用两个终端窗口。在第一个终端中,使用 +[`kubectl get`](/docs/reference/generated/kubectl/kubectl-commands/#get) +来监视 StatefulSet 的 Pod 的创建情况。 ```shell kubectl get pods -w -l app=nginx @@ -131,10 +137,10 @@ kubectl get pods -w -l app=nginx - -在另一个终端中,使用 [`kubectl apply`](/zh/docs/reference/generated/kubectl/kubectl-commands/#apply) 来创建定义在 `web.yaml` 中的 Headless Service 和 StatefulSet。 +在另一个终端中,使用 [`kubectl apply`](/docs/reference/generated/kubectl/kubectl-commands/#apply) +来创建定义在 `web.yaml` 中的 Headless Service 和 StatefulSet。 ```shell kubectl apply -f web.yaml @@ -146,12 +152,10 @@ statefulset.apps/web created - -上面的命令创建了两个 Pod,每个都运行了一个 [NGINX](https://www.nginx.com) web 服务器。 -获取 `nginx` Service 和 `web` StatefulSet 来验证是否成功的创建了它们。 +上面的命令创建了两个 Pod,每个都运行了一个 [NginX](https://www.nginx.com) Web 服务器。 +获取 `nginx` Service: ```shell kubectl get service nginx @@ -160,10 +164,12 @@ kubectl get service nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx ClusterIP None 80/TCP 12s ``` + -...然后获取 `web` StatefulSet,以验证两者均已成功创建: +然后获取 `web` StatefulSet,以验证两者均已成功创建: + ```shell kubectl get statefulset web ``` @@ -173,19 +179,17 @@ web 2 1 20s ``` +### 顺序创建 Pod {#ordered-pod-creation} -For a StatefulSet with N replicas, when Pods are being deployed, they are -created sequentially, in order from {0..N-1}. Examine the output of the + - -### 顺序创建 Pod - - -对于一个拥有 N 个副本的 StatefulSet,Pod 被部署时是按照 {0 …… N-1} 的序号顺序创建的。 +对于一个拥有 _n_ 个副本的 StatefulSet,Pod 被部署时是按照 _{0..n-1}_ 的序号顺序创建的。 在第一个终端中使用 `kubectl get` 检查输出。这个输出最终将看起来像下面的样子。 ```shell @@ -205,31 +209,33 @@ web-1 1/1 Running 0 18s -请注意在 `web-0` Pod 处于 [Running和Ready](/zh/docs/user-guide/pod-states) 状态后 `web-1` Pod 才会被启动。 +请注意,直到 `web-0` Pod 处于 _Running_(请参阅 +[Pod 阶段]((/zh-cn/docs/concepts/workloads/pods/pod-lifecycle/#pod-phase))) +并 _Ready_(请参阅 [Pod 状况](/zh-cn/docs/concepts/workloads/pods/pod-lifecycle/#pod-conditions)中的 +`type`)状态后,`web-1` Pod 才会被启动。 +## StatefulSet 中的 Pod {#pods-in-a-statefulset} -## StatefulSet 中的 Pod + +StatefulSet 中的每个 Pod 拥有一个唯一的顺序索引和稳定的网络身份标识。 + +### 检查 Pod 的顺序索引 {#examining-the-pod-s-ordinal-index} -StatefulSet 中的 Pod 拥有一个唯一的顺序索引和稳定的网络身份标识。 - - -### 检查 Pod 的顺序索引 - - -获取 StatefulSet 的 Pod。 + +获取 StatefulSet 的 Pod: ```shell kubectl get pods -l app=nginx @@ -244,26 +250,31 @@ web-1 1/1 Running 0 1m As mentioned in the [StatefulSets](/docs/concepts/workloads/controllers/statefulset/) concept, the Pods in a StatefulSet have a sticky, unique identity. This identity is based on a unique ordinal index that is assigned to each Pod by the -StatefulSet controller. The Pods' names take the form -`-`. Since the `web` StatefulSet has two -replicas, it creates two Pods, `web-0` and `web-1`. +StatefulSet {{< glossary_tooltip term_id="controller" text="controller">}}. +The Pods' names take the form `-`. +Since the `web` StatefulSet has two replicas, it creates two Pods, `web-0` and `web-1`. +--> +如同 [StatefulSet](/zh-cn/docs/concepts/workloads/controllers/statefulset/) 概念中所提到的, +StatefulSet 中的每个 Pod 拥有一个具有黏性的、独一无二的身份标志。 +这个标志基于 StatefulSet +{{< glossary_tooltip term_id="controller" text="控制器">}}分配给每个 +Pod 的唯一顺序索引。 +Pod 的名称的形式为 `-<序号索引>`。 +`web` StatefulSet 拥有两个副本,所以它创建了两个 Pod:`web-0` 和 `web-1`。 + +### 使用稳定的网络身份标识 {#using-stable-network-identities} + - -如同 [StatefulSets](/zh/docs/concepts/workloads/controllers/statefulset/) 概念中所提到的, -StatefulSet 中的 Pod 拥有一个具有黏性的、独一无二的身份标志。 -这个标志基于 StatefulSet 控制器分配给每个 Pod 的唯一顺序索引。 -Pod 的名称的形式为`-`。 -`web`StatefulSet 拥有两个副本,所以它创建了两个 Pod:`web-0`和`web-1`。 - -### 使用稳定的网络身份标识 - -每个 Pod 都拥有一个基于其顺序索引的稳定的主机名。使用[`kubectl exec`](/zh/docs/reference/generated/kubectl/kubectl-commands/#exec)在每个 Pod 中执行`hostname`。 +每个 Pod 都拥有一个基于其顺序索引的稳定的主机名。使用 +[`kubectl exec`](/docs/reference/generated/kubectl/kubectl-commands/#exec) +在每个 Pod 中执行 `hostname`: ```shell for i in 0 1; do kubectl exec "web-$i" -- sh -c 'hostname'; done @@ -277,28 +288,31 @@ web-1 Use [`kubectl run`](/docs/reference/generated/kubectl/kubectl-commands/#run) to execute a container that provides the `nslookup` command from the `dnsutils` package. Using `nslookup` on the Pods' hostnames, you can examine their in-cluster DNS -addresses. +addresses: --> - -使用 [`kubectl run`](/zh/docs/reference/generated/kubectl/kubectl-commands/#run) +使用 [`kubectl run`](/docs/reference/generated/kubectl/kubectl-commands/#run) 运行一个提供 `nslookup` 命令的容器,该命令来自于 `dnsutils` 包。 -通过对 Pod 的主机名执行 `nslookup`,你可以检查他们在集群内部的 DNS 地址。 +通过对 Pod 的主机名执行 `nslookup`,你可以检查他们在集群内部的 DNS 地址: ```shell kubectl run -i --tty --image busybox:1.28 dns-test --restart=Never --rm ``` + -这将启动一个新的 shell。在新 shell 中,运行: +这将启动一个新的 Shell。在新 Shell 中运行: + ```shell # Run this in the dns-test container shell nslookup web-0.nginx ``` + 输出类似于: + ``` Server: 10.0.0.10 Address 1: 10.0.0.10 kube-dns.kube-system.svc.cluster.local @@ -314,29 +328,36 @@ Name: web-1.nginx Address 1: 10.244.2.6 ``` + +(现在可以退出容器 Shell:`exit`) + - headless service 的 CNAME 指向 SRV 记录(记录每个 Running 和 Ready 状态的 Pod)。 SRV 记录指向一个包含 Pod IP 地址的记录表项。 -在一个终端中查看 StatefulSet 的 Pod。 + +在一个终端中监视 StatefulSet 的 Pod: ```shell kubectl get pod -w -l app=nginx ``` + - -在另一个终端中使用 [`kubectl delete`](/zh/docs/reference/generated/kubectl/kubectl-commands/#delete) 删除 StatefulSet 中所有的 Pod。 +在另一个终端中使用 +[`kubectl delete`](/docs/reference/generated/kubectl/kubectl-commands/#delete) +删除 StatefulSet 中所有的 Pod: ```shell kubectl delete pod -l app=nginx @@ -348,10 +369,9 @@ pod "web-1" deleted - -等待 StatefulSet 重启它们,并且两个 Pod 都变成 Running 和 Ready 状态。 +等待 StatefulSet 重启它们,并且两个 Pod 都变成 Running 和 Ready 状态: ```shell kubectl get pod -w -l app=nginx @@ -368,11 +388,11 @@ web-1 1/1 Running 0 34s ``` - 使用 `kubectl exec` 和 `kubectl run` 查看 Pod 的主机名和集群内部的 DNS 表项。 +首先,查看 Pod 的主机名: ```shell for i in 0 1; do kubectl exec web-$i -- sh -c 'hostname'; done @@ -381,26 +401,32 @@ for i in 0 1; do kubectl exec web-$i -- sh -c 'hostname'; done web-0 web-1 ``` + 然后,运行: + ``` kubectl run -i --tty --image busybox:1.28 dns-test --restart=Never --rm /bin/sh ``` + -这将启动一个新的 shell。在新 shell 中,运行: +这将启动一个新的 Shell。在新 Shell 中,运行: + ```shell # Run this in the dns-test container shell nslookup web-0.nginx ``` + 输出类似于: + ``` Server: 10.0.0.10 Address 1: 10.0.0.10 kube-dns.kube-system.svc.cluster.local @@ -415,19 +441,33 @@ Address 1: 10.0.0.10 kube-dns.kube-system.svc.cluster.local Name: web-1.nginx Address 1: 10.244.2.8 ``` + + +(现在可以退出容器 Shell:`exit`) + +Pod 的序号、主机名、SRV 条目和记录名称没有改变,但和 Pod 相关联的 IP 地址可能发生了改变。 +在本教程中使用的集群中它们就改变了。这就是为什么不要在其他应用中使用 +StatefulSet 中 Pod 的 IP 地址进行连接,这点很重要。 - + +如果你需要查找并连接一个 StatefulSet 的活动成员,你应该查询 Headless Service 的 CNAME。 +和 CNAME 相关联的 SRV 记录只会包含 StatefulSet 中处于 Running 和 Ready 状态的 Pod。 + - -Pod 的序号、主机名、SRV 条目和记录名称没有改变,但和 Pod 相关联的 IP 地址可能发生了改变。 -在本教程中使用的集群中它们就改变了。这就是为什么不要在其他应用中使用 StatefulSet 中的 Pod 的 IP 地址进行连接,这点很重要。 - - -如果你需要查找并连接一个 StatefulSet 的活动成员,你应该查询 Headless Service 的 CNAME。 -和 CNAME 相关联的 SRV 记录只会包含 StatefulSet 中处于 Running 和 Ready 状态的 Pod。 - - -如果你的应用已经实现了用于测试 liveness 和 readiness 的连接逻辑,你可以使用 Pod 的 SRV 记录(`web-0.nginx.default.svc.cluster.local`, -`web-1.nginx.default.svc.cluster.local`)。因为他们是稳定的,并且当你的 Pod 的状态变为 Running 和 Ready 时,你的应用就能够发现它们的地址。 - +如果你的应用已经实现了用于测试是否已存活(liveness)并就绪(readiness)的连接逻辑, +你可以使用 Pod 的 SRV 记录(`web-0.nginx.default.svc.cluster.local`、 +`web-1.nginx.default.svc.cluster.local`)。因为他们是稳定的,并且当你的 +Pod 的状态变为 Running 和 Ready 时,你的应用就能够发现它们的地址。 +### 写入稳定的存储 {#writing-to-stable-storage} -### 写入稳定的存储 - -获取 `web-0` 和 `web-1` 的 PersistentVolumeClaims。 + +获取 `web-0` 和 `web-1` 的 PersistentVolumeClaims: ```shell kubectl get pvc -l app=nginx ``` + 输出类似于: + ``` NAME STATUS VOLUME CAPACITY ACCESSMODES AGE www-web-0 Bound pvc-15c268c7-b507-11e6-932f-42010a800002 1Gi RWO 48s @@ -472,27 +506,38 @@ www-web-1 Bound pvc-15c79307-b507-11e6-932f-42010a800002 1Gi RWO ``` +StatefulSet 控制器创建了两个 +{{< glossary_tooltip text="PersistentVolumeClaims" term_id="persistent-volume-claim" >}}, +绑定到两个 +{{< glossary_tooltip text="PersistentVolumes" term_id="persistent-volume" >}}。 -StatefulSet 控制器创建了两个 PersistentVolumeClaims,绑定到两个 [PersistentVolumes](/zh/docs/concepts/storage/volumes/)。由于本教程使用的集群配置为动态提供 PersistentVolume,所有的 PersistentVolume 都是自动创建和绑定的。 + +由于本教程使用的集群配置为动态制备 +PersistentVolume 卷,所有的 PersistentVolume 卷都是自动创建和绑定的。 + +NginX Web 服务器默认会加载位于 `/usr/share/nginx/html/index.html` 的 index 文件。 +StatefulSet `spec` 中的 `volumeMounts` 字段保证了 `/usr/share/nginx/html` +文件夹由一个 PersistentVolume 卷支持。 -NGINX web 服务器默认会加载位于 `/usr/share/nginx/html/index.html` 的 index 文件。 -StatefulSets `spec` 中的 `volumeMounts` 字段保证了 `/usr/share/nginx/html` 文件夹由一个 PersistentVolume 支持。 - - -将 Pod 的主机名写入它们的`index.html`文件并验证 NGINX web 服务器使用该主机名提供服务。 + +将 Pod 的主机名写入它们的 `index.html` 文件并验证 NginX Web 服务器使用该主机名提供服务: ```shell for i in 0 1; do kubectl exec "web-$i" -- sh -c 'echo "$(hostname)" > /usr/share/nginx/html/index.html'; done @@ -511,7 +556,6 @@ you will need to fix the permissions of the directory mounted by the `volumeMoun (due to a [bug when using hostPath volumes](https://github.com/kubernetes/kubernetes/issues/2630)), by running: --> - 请注意,如果你看见上面的 curl 命令返回了 **403 Forbidden** 的响应,你需要像这样修复使用 `volumeMounts` (原因归咎于[使用 hostPath 卷时存在的缺陷](https://github.com/kubernetes/kubernetes/issues/2630)) 挂载的目录的权限 @@ -522,25 +566,22 @@ by running: - 在你重新尝试上面的 `curl` 命令之前。 {{< /note >}} - -在一个终端查看 StatefulSet 的 Pod。 +在一个终端监视 StatefulSet 的 Pod: ```shell kubectl get pod -w -l app=nginx ``` - -在另一个终端删除 StatefulSet 所有的 Pod。 +在另一个终端删除 StatefulSet 所有的 Pod: ```shell kubectl delete pod -l app=nginx @@ -549,11 +590,11 @@ kubectl delete pod -l app=nginx pod "web-0" deleted pod "web-1" deleted ``` + - 在第一个终端里检查 `kubectl get` 命令的输出,等待所有 Pod 变成 Running 和 Ready 状态。 ```shell @@ -571,10 +612,9 @@ web-1 1/1 Running 0 34s ``` - -验证所有 web 服务器在继续使用它们的主机名提供服务。 +验证所有 Web 服务器在继续使用它们的主机名提供服务: ``` for i in 0 1; do kubectl exec -i -t "web-$i" -- curl http://localhost/; done @@ -588,35 +628,37 @@ web-1 Even though `web-0` and `web-1` were rescheduled, they continue to serve their hostnames because the PersistentVolumes associated with their PersistentVolumeClaims are remounted to their `volumeMounts`. No matter what -node `web-0`and `web-1` are scheduled on, their PersistentVolumes will be +node `web-0` and `web-1` are scheduled on, their PersistentVolumes will be mounted to the appropriate mount points. +--> +虽然 `web-0` 和 `web-1` 被重新调度了,但它们仍然继续监听各自的主机名,因为和它们的 +PersistentVolumeClaim 相关联的 PersistentVolume 卷被重新挂载到了各自的 `volumeMount` 上。 +不管 `web-0` 和 `web-1` 被调度到了哪个节点上,它们的 PersistentVolume 卷将会被挂载到合适的挂载点上。 + +## 扩容/缩容 StatefulSet {#scaling-a-statefulset} + + - -虽然 `web-0` 和 `web-1` 被重新调度了,但它们仍然继续监听各自的主机名,因为和它们的 PersistentVolumeClaim 相关联的 PersistentVolume 被重新挂载到了各自的 `volumeMount` 上。 -不管 `web-0` 和 `web-1` 被调度到了哪个节点上,它们的 PersistentVolumes 将会被挂载到合适的挂载点上。 - - -## 扩容/缩容 StatefulSet - 扩容/缩容 StatefulSet 指增加或减少它的副本数。这通过更新 `replicas` 字段完成。 -你可以使用[`kubectl scale`](/zh/docs/user-guide/kubectl/{{< param "version" >}}/#scale) -或者[`kubectl patch`](/zh/docs/user-guide/kubectl/{{< param "version" >}}/#patch)来扩容/缩容一个 StatefulSet。 +你可以使用 [`kubectl scale`](/docs/reference/generated/kubectl/kubectl-commands/#scale) +或者 [`kubectl patch`](/docs/reference/generated/kubectl/kubectl-commands/#patch) 来扩容/缩容一个 StatefulSet。 + +### 扩容 {#scaling-up} -### 扩容 - - -在一个终端窗口观察 StatefulSet 的 Pod。 + +在一个终端窗口监视 StatefulSet 的 Pod: ```shell kubectl get pods -w -l app=nginx @@ -624,9 +666,9 @@ kubectl get pods -w -l app=nginx - -在另一个终端窗口使用 `kubectl scale` 扩展副本数为 5。 +to 5: +--> +在另一个终端窗口使用 `kubectl scale` 扩展副本数为 5: ```shell kubectl scale sts web --replicas=5 @@ -634,11 +676,11 @@ kubectl scale sts web --replicas=5 ``` statefulset.apps/web scaled ``` + - 在第一个 终端中检查 `kubectl get` 命令的输出,等待增加的 3 个 Pod 的状态变为 Running 和 Ready。 ```shell @@ -669,19 +711,20 @@ The StatefulSet controller scaled the number of replicas. As with created each Pod sequentially with respect to its ordinal index, and it waited for each Pod's predecessor to be Running and Ready before launching the subsequent Pod. - -### Scaling Down - -In one terminal, watch the StatefulSet's Pods. --> - StatefulSet 控制器扩展了副本的数量。 -如同[创建 StatefulSet](#顺序创建pod) 所述,StatefulSet 按序号索引顺序的创建每个 Pod,并且会等待前一个 Pod 变为 Running 和 Ready 才会启动下一个 Pod。 +如同[创建 StatefulSet](#ordered-pod-creation) 所述,StatefulSet 按序号索引顺序创建各个 +Pod,并且会等待前一个 Pod 变为 Running 和 Ready 才会启动下一个 Pod。 -### 缩容 + +### 缩容 {#scaling-down} - -在一个终端观察 StatefulSet 的 Pod。 + +在一个终端监视 StatefulSet 的 Pod: ```shell kubectl get pods -w -l app=nginx @@ -689,10 +732,9 @@ kubectl get pods -w -l app=nginx - -在另一个终端使用 `kubectl patch` 将 StatefulSet 缩容回三个副本。 +在另一个终端使用 `kubectl patch` 将 StatefulSet 缩容回三个副本: ```shell kubectl patch sts web -p '{"spec":{"replicas":3}}' @@ -704,7 +746,6 @@ statefulset.apps/web patched - 等待 `web-4` 和 `web-3` 状态变为 Terminating。 ```shell @@ -727,21 +768,20 @@ web-3 1/1 Terminating 0 42s +### 顺序终止 Pod {#ordered-pod-termination} + - -### 顺序终止 Pod - - 控制器会按照与 Pod 序号索引相反的顺序每次删除一个 Pod。在删除下一个 Pod 前会等待上一个被完全关闭。 - -获取 StatefulSet 的 PersistentVolumeClaims。 + +获取 StatefulSet 的 PersistentVolumeClaims: ```shell kubectl get pvc -l app=nginx @@ -759,46 +799,50 @@ www-web-4 Bound pvc-e11bb5f8-b508-11e6-932f-42010a800002 1Gi RWO +五个 PersistentVolumeClaims 和五个 PersistentVolume 卷仍然存在。 +查看 Pod 的[稳定存储](#stable-storage),我们发现当删除 StatefulSet 的 +Pod 时,挂载到 StatefulSet 的 Pod 的 PersistentVolume 卷不会被删除。 +当这种删除行为是由 StatefulSet 缩容引起时也是一样的。 + +## 更新 StatefulSet {#updating-statefulsets} + +从 Kubernetes 1.7 版本开始,StatefulSet 控制器支持自动更新。 +更新策略由 StatefulSet API 对象的 `spec.updateStrategy` 字段决定。这个特性能够用来更新一个 +StatefulSet 中 Pod 的的容器镜像、资源请求和限制、标签和注解。 + +`RollingUpdate` 更新策略是 StatefulSet 默认策略。 -五个 PersistentVolumeClaims 和五个 PersistentVolumes 仍然存在。 -查看 Pod 的 [稳定存储](#stable-storage),我们发现当删除 StatefulSet 的 Pod 时,挂载到 StatefulSet 的 Pod 的 PersistentVolumes 不会被删除。 -当这种删除行为是由 StatefulSet 缩容引起时也是一样的。 - - -## 更新 StatefulSet - - -Kubernetes 1.7 版本的 StatefulSet 控制器支持自动更新。 -更新策略由 StatefulSet API Object 的`spec.updateStrategy` 字段决定。这个特性能够用来更新一个 StatefulSet 中的 Pod 的 container images,resource requests,以及 limits,labels 和 annotations。 -`RollingUpdate`滚动更新是 StatefulSets 默认策略。 - + +### 滚动更新 {#rolling-update} +`RollingUpdate` 更新策略会更新一个 StatefulSet 中的所有 +Pod,采用与序号索引相反的顺序并遵循 StatefulSet 的保证。 -### Rolling Update 策略 - - -`RollingUpdate` 更新策略会更新一个 StatefulSet 中所有的 Pod,采用与序号索引相反的顺序并遵循 StatefulSet 的保证。 - - -Patch `web` StatefulSet 来执行 `RollingUpdate` 更新策略。 + +对 `web` StatefulSet 应用 Patch 操作来应用 `RollingUpdate` 更新策略: ```shell kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"type":"RollingUpdate"}}}' @@ -806,12 +850,12 @@ kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"type":"RollingUpda ``` statefulset.apps/web patched ``` + - -在一个终端窗口中 patch `web` StatefulSet 来再次的改变容器镜像。 +在一个终端窗口中对 `web` StatefulSet 执行 patch 操作来再次改变容器镜像: ```shell kubectl patch statefulset web --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"gcr.io/google_containers/nginx-slim:0.8"}]' @@ -821,18 +865,19 @@ statefulset.apps/web patched ``` - -在另一个终端监控 StatefulSet 中的 Pod。 +在另一个终端监控 StatefulSet 中的 Pod: ```shell -kubectl get po -l app=nginx -w +kubectl get pod -l app=nginx -w ``` + 输出类似于: + ``` NAME READY STATUS RESTARTS AGE web-0 1/1 Running 0 7m @@ -874,21 +919,26 @@ StatefulSet controller terminates each Pod, and waits for it to transition to Ru Ready prior to updating the next Pod. Note that, even though the StatefulSet controller will not proceed to update the next Pod until its ordinal successor is Running and Ready, it will restore any Pod that fails during the update to -its current version. Pods that have already received the update will be -restored to the updated version, and Pods that have not yet received the -update will be restored to the previous version. In this way, the controller -attempts to continue to keep the application healthy and the update consistent -in the presence of intermittent failures. - -Get the Pods to view their container images. +its current version. --> +StatefulSet 里的 Pod 采用和序号相反的顺序更新。在更新下一个 Pod 前,StatefulSet +控制器终止每个 Pod 并等待它们变成 Running 和 Ready。 +请注意,虽然在顺序后继者变成 Running 和 Ready 之前 StatefulSet 控制器不会更新下一个 +Pod,但它仍然会重建任何在更新过程中发生故障的 Pod,使用的是它们当前的版本。 -StatefulSet 里的 Pod 采用和序号相反的顺序更新。在更新下一个 Pod 前,StatefulSet 控制器终止每个 Pod 并等待它们变成 Running 和 Ready。 -请注意,虽然在顺序后继者变成 Running 和 Ready 之前 StatefulSet 控制器不会更新下一个 Pod,但它仍然会重建任何在更新过程中发生故障的 Pod,使用的是它们当前的版本。 + 已经接收到更新请求的 Pod 将会被恢复为更新的版本,没有收到请求的 Pod 则会被恢复为之前的版本。 像这样,控制器尝试继续使应用保持健康并在出现间歇性故障时保持更新的一致性。 -获取 Pod 来查看他们的容器镜像。 + +获取 Pod 来查看它们的容器镜像: ```shell for p in 0 1 2; do kubectl get pod "web-$p" --template '{{range $i, $c := .spec.containers}}{{$c.image}}{{end}}'; echo; done @@ -902,32 +952,37 @@ k8s.gcr.io/nginx-slim:0.8 +StatefulSet 中的所有 Pod 现在都在运行之前的容器镜像。 -**Tip** You can also use `kubectl rollout status sts/` to view -the status of a rolling update. +{{< note >}} + +你还可以使用 `kubectl rollout status sts/<名称>` 来查看 +StatefulSet 的滚动更新状态。 +{{< /note >}} + +#### 分段更新 {#staging-an-update} + + - -StatefulSet 中的所有 Pod 现在都在运行之前的容器镜像。 - - -**小窍门**:你还可以使用 `kubectl rollout status sts/` 来查看 rolling update 的状态。 - - -#### 分段更新 - 你可以使用 `RollingUpdate` 更新策略的 `partition` 参数来分段更新一个 StatefulSet。 -分段的更新将会使 StatefulSet 中的其余所有 Pod 保持当前版本的同时仅允许改变 StatefulSet 的 `.spec.template`。 +分段的更新将会使 StatefulSet 中的其余所有 Pod 保持当前版本的同时允许改变 +StatefulSet 的 `.spec.template`。 - -Patch `web` StatefulSet 来对 `updateStrategy` 字段添加一个分区。 + +对 `web` StatefulSet 执行 Patch 操作以为 `updateStrategy` 字段添加一个分区: ```shell kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"type":"RollingUpdate","rollingUpdate":{"partition":3}}}}' @@ -937,10 +992,9 @@ statefulset.apps/web patched ``` - -再次 Patch StatefulSet 来改变容器镜像。 +再次 Patch StatefulSet 来改变容器镜像: ```shell kubectl patch statefulset web --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"k8s.gcr.io/nginx-slim:0.7"}]' @@ -950,10 +1004,9 @@ statefulset.apps/web patched ``` - -删除 StatefulSet 中的 Pod。 +删除 StatefulSet 中的 Pod: ```shell kubectl delete pod web-2 @@ -965,7 +1018,6 @@ pod "web-2" deleted - 等待 Pod 变成 Running 和 Ready。 ```shell @@ -980,10 +1032,9 @@ web-2 1/1 Running 0 18s ``` - -获取 Pod 的容器。 +获取 Pod 的容器镜像: ```shell kubectl get pod web-2 --template '{{range $i, $c := .spec.containers}}{{$c.image}}{{end}}' @@ -994,27 +1045,29 @@ k8s.gcr.io/nginx-slim:0.8 - -请注意,虽然更新策略是 `RollingUpdate`,StatefulSet 控制器还是会使用原始的容器恢复 Pod。 +请注意,虽然更新策略是 `RollingUpdate`,StatefulSet 还是会使用原始的容器恢复 Pod。 这是因为 Pod 的序号比 `updateStrategy` 指定的 `partition` 更小。 + +#### 金丝雀发布 {#rolling-out-a-canary} -#### 灰度发布 + +你可以通过减少[上文](#staging-an-update)指定的 +`partition` 来进行金丝雀发布,以此来测试你的程序的改动。 -你可以通过减少 [上文](#分段更新)指定的 `partition` 来进行灰度发布,以此来测试你的程序的改动。 - - -通过 patch 命令修改 StatefulSet 来减少分区。 + +通过 patch 命令修改 StatefulSet 来减少分区: ```shell kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"type":"RollingUpdate","rollingUpdate":{"partition":2}}}}' @@ -1026,7 +1079,6 @@ statefulset.apps/web patched - 等待 `web-2` 变成 Running 和 Ready。 ```shell @@ -1041,10 +1093,9 @@ web-2 1/1 Running 0 18s ``` - -获取 Pod 的容器。 +获取 Pod 的容器: ```shell kubectl get pod web-2 --template '{{range $i, $c := .spec.containers}}{{$c.image}}{{end}}' @@ -1058,14 +1109,14 @@ k8s.gcr.io/nginx-slim:0.7 When you changed the `partition`, the StatefulSet controller automatically updated the `web-2` Pod because the Pod's ordinal was greater than or equal to the `partition`. - -Delete the `web-1` Pod. --> +当你改变 `partition` 时,StatefulSet 会自动更新 `web-2` +Pod,这是因为 Pod 的序号大于或等于 `partition`。 -当你改变 `partition` 时,StatefulSet 会自动的更新 `web-2` Pod,这是因为 Pod 的序号大于或等于 `partition`。 - - -删除 `web-1` Pod。 + +删除 `web-1` Pod: ```shell kubectl delete pod web-1 @@ -1077,16 +1128,17 @@ pod "web-1" deleted - 等待 `web-1` 变成 Running 和 Ready。 ```shell kubectl get pod -l app=nginx -w ``` + 输出类似于: + ``` NAME READY STATUS RESTARTS AGE web-0 1/1 Running 0 6m @@ -1102,10 +1154,9 @@ web-1 1/1 Running 0 18s ``` - -获取 `web-1` Pod 的容器。 +获取 `web-1` Pod 的容器镜像: ```shell kubectl get pod web-1 --template '{{range $i, $c := .spec.containers}}{{$c.image}}{{end}}' @@ -1121,28 +1172,31 @@ ordinal that is greater than or equal to the partition will be updated when the StatefulSet's `.spec.template` is updated. If a Pod that has an ordinal less than the partition is deleted or otherwise terminated, it will be restored to its original configuration. +--> +`web-1` 被按照原来的配置恢复,因为 Pod 的序号小于分区。当指定了分区时,如果更新了 +StatefulSet 的 `.spec.template`,则所有序号大于或等于分区的 Pod 都将被更新。 +如果一个序号小于分区的 Pod 被删除或者终止,它将被按照原来的配置恢复。 + +#### 分阶段的发布 {#phased-roll-outs} + + - -`web-1` 被按照原来的配置恢复,因为 Pod 的序号小于分区。当指定了分区时,如果更新了 StatefulSet 的 `.spec.template`,则所有序号大于或等于分区的 Pod 都将被更新。 -如果一个序号小于分区的 Pod 被删除或者终止,它将被按照原来的配置恢复。 - - -#### 分阶段的发布 - -你可以使用类似[灰度发布](#灰度发布)的方法执行一次分阶段的发布(例如一次线性的、等比的或者指数形式的发布)。 +你可以使用类似[金丝雀发布](#rolling-out-a-canary)的方法执行一次分阶段的发布 +(例如一次线性的、等比的或者指数形式的发布)。 要执行一次分阶段的发布,你需要设置 `partition` 为希望控制器暂停更新的序号。 - -分区当前为`2`。请将分区设置为`0`。 + +分区当前为 `2`。请将分区设置为 `0`: ```shell kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"type":"RollingUpdate","rollingUpdate":{"partition":0}}}}' @@ -1154,16 +1208,17 @@ statefulset.apps/web patched - 等待 StatefulSet 中的所有 Pod 变成 Running 和 Ready。 ```shell kubectl get pod -l app=nginx -w ``` + 输出类似于: + ``` NAME READY STATUS RESTARTS AGE web-0 1/1 Running 0 3m @@ -1181,11 +1236,11 @@ web-0 0/1 Pending 0 0s web-0 0/1 ContainerCreating 0 0s web-0 1/1 Running 0 3s ``` - -获取 Pod 的容器。 + +获取 StatefulSet 中 Pod 的容器镜像详细信息: ```shell for p in 0 1 2; do kubectl get pod "web-$p" --template '{{range $i, $c := .spec.containers}}{{$c.image}}{{end}}'; echo; done @@ -1197,46 +1252,48 @@ k8s.gcr.io/nginx-slim:0.7 ``` +将 `partition` 改变为 `0` 以允许 StatefulSet 继续更新过程。 + +### OnDelete 策略 {#on-delete} + +`OnDelete` 更新策略实现了传统(1.7 之前)行为,它也是默认的更新策略。 +当你选择这个更新策略并修改 StatefulSet 的 `.spec.template` 字段时,StatefulSet 控制器将不会自动更新 Pod。 - + +## 删除 StatefulSet {#deleting-statefulsets} + +StatefulSet 同时支持级联和非级联删除。使用非级联方式删除 StatefulSet 时,StatefulSet +的 Pod 不会被删除。使用级联删除时,StatefulSet 和它的 Pod 都会被删除。 + +### 非级联删除 {#non-cascading-delete} + - -将 `partition` 改变为 `0` 以允许 StatefulSet 控制器继续更新过程。 - -### On Delete 策略 - -`OnDelete` 更新策略实现了传统(1.7 之前)行为,它也是默认的更新策略。 -当你选择这个更新策略并修改 StatefulSet 的 `.spec.template` 字段时,StatefulSet 控制器将不会自动的更新 Pod。 - -## 删除 StatefulSet - - -StatefulSet 同时支持级联和非级联删除。使用非级联方式删除 StatefulSet 时,StatefulSet 的 Pod 不会被删除。使用级联删除时,StatefulSet 和它的 Pod 都会被删除。 - - -### 非级联删除 - - -在一个终端窗口查看 StatefulSet 中的 Pod。 +在一个终端窗口监视 StatefulSet 中的 Pod。 ``` kubectl get pods -w -l app=nginx @@ -1248,9 +1305,9 @@ StatefulSet. Make sure to supply the `--cascade=orphan` parameter to the command. This parameter tells Kubernetes to only delete the StatefulSet, and to not delete any of its Pods. --> - -使用 [`kubectl delete`](/zh/docs/reference/generated/kubectl/kubectl-commands/#delete) 删除 StatefulSet。 -请确保提供了 `--cascade=orphan` 参数给命令。这个参数告诉 Kubernetes 只删除 StatefulSet 而不要删除它的任何 Pod。 +使用 [`kubectl delete`](/docs/reference/generated/kubectl/kubectl-commands/#delete) +删除 StatefulSet。请确保提供了 `--cascade=orphan` 参数给命令。这个参数告诉 +Kubernetes 只删除 StatefulSet 而不要删除它的任何 Pod。 ```shell kubectl delete statefulset web --cascade=orphan @@ -1260,10 +1317,9 @@ statefulset.apps "web" deleted ``` - -获取 Pod 来检查他们的状态。 +获取 Pod 来检查它们的状态: ```shell kubectl get pods -l app=nginx @@ -1277,11 +1333,10 @@ web-2 1/1 Running 0 5m - 虽然 `web` 已经被删除了,但所有 Pod 仍然处于 Running 和 Ready 状态。 -删除 `web-0`。 +删除 `web-0`: ```shell kubectl delete pod web-0 @@ -1291,10 +1346,9 @@ pod "web-0" deleted ``` - -获取 StatefulSet 的 Pod。 +获取 StatefulSet 的 Pod: ```shell kubectl get pods -l app=nginx @@ -1307,13 +1361,12 @@ web-2 1/1 Running 0 7m +由于 `web` StatefulSet 已经被删除,`web-0` 没有被重新启动。 + - -由于 `web` StatefulSet 已经被删除,`web-0`没有被重新启动。 - - 在一个终端监控 StatefulSet 的 Pod。 ```shell @@ -1322,10 +1375,11 @@ kubectl get pods -w -l app=nginx -在另一个终端里重新创建 StatefulSet。请注意,除非你删除了 `nginx` Service (你不应该这样做),你将会看到一个错误,提示 Service 已经存在。 +在另一个终端里重新创建 StatefulSet。请注意,除非你删除了 `nginx` +Service(你不应该这样做),你将会看到一个错误,提示 Service 已经存在。 ```shell kubectl apply -f web.yaml @@ -1334,16 +1388,17 @@ kubectl apply -f web.yaml statefulset.apps/web created service/nginx unchanged ``` - +请忽略这个错误。它仅表示 kubernetes 进行了一次创建 _nginx_ headless Service +的尝试,尽管那个 Service 已经存在。 + + - -请忽略这个错误。它仅表示 kubernetes 进行了一次创建 nginx Headless Service 的尝试,尽管那个 Service 已经存在。 - - 在第一个终端中运行并检查 `kubectl get` 命令的输出。 ```shell @@ -1371,18 +1426,17 @@ Running and Ready, it adopted this Pod. Since you recreated the StatefulSet with `replicas` equal to 2, once `web-0` had been recreated, and once `web-1` had been determined to already be Running and Ready, `web-2` was terminated. - -Let's take another look at the contents of the `index.html` file served by the -Pods' webservers: --> - 当重新创建 `web` StatefulSet 时,`web-0` 被第一个重新启动。 由于 `web-1` 已经处于 Running 和 Ready 状态,当 `web-0` 变成 Running 和 Ready 时, StatefulSet 会接收这个 Pod。由于你重新创建的 StatefulSet 的 `replicas` 等于 2, 一旦 `web-0` 被重新创建并且 `web-1` 被认为已经处于 Running 和 Ready 状态时,`web-2` 将会被终止。 - -让我们再看看被 Pod 的 web 服务器加载的 `index.html` 的内容: + +让我们再看看被 Pod 的 Web 服务器加载的 `index.html` 的内容: ```shell for i in 0 1; do kubectl exec -i -t "web-$i" -- curl http://localhost/; done @@ -1399,21 +1453,20 @@ serves the hostname originally entered into its `index.html` file. This is because the StatefulSet never deletes the PersistentVolumes associated with a Pod. When you recreated the StatefulSet and it relaunched `web-0`, its original PersistentVolume was remounted. +--> +尽管你同时删除了 StatefulSet 和 `web-0` Pod,但它仍然使用最初写入 `index.html` 文件的主机名进行服务。 +这是因为 StatefulSet 永远不会删除和一个 Pod 相关联的 PersistentVolume 卷。 +当你重建这个 StatefulSet 并且重新启动了 `web-0` 时,它原本的 PersistentVolume 卷会被重新挂载。 + +### 级联删除 {#cascading-delete} + - -尽管你同时删除了 StatefulSet 和 `web-0` Pod,但它仍然使用最初写入 `index.html` 文件的主机名进行服务。 -这是因为 StatefulSet 永远不会删除和一个 Pod 相关联的 PersistentVolumes。 -当你重建这个 StatefulSet 并且重新启动了 `web-0` 时,它原本的 PersistentVolume 会被重新挂载。 - - -### 级联删除 - - -在一个终端窗口观察 StatefulSet 里的 Pod。 +在一个终端窗口监视 StatefulSet 里的 Pod。 ```shell kubectl get pods -w -l app=nginx @@ -1423,7 +1476,6 @@ kubectl get pods -w -l app=nginx In another terminal, delete the StatefulSet again. This time, omit the `--cascade=orphan` parameter. --> - 在另一个窗口中再次删除这个 StatefulSet。这次省略 `--cascade=orphan` 参数。 ```shell @@ -1438,7 +1490,6 @@ statefulset.apps "web" deleted Examine the output of the `kubectl get` command running in the first terminal, and wait for all of the Pods to transition to Terminating. --> - 在第一个终端检查 `kubectl get` 命令的输出,并等待所有的 Pod 变成 Terminating 状态。 ```shell @@ -1466,17 +1517,19 @@ As you saw in the [Scaling Down](#scaling-down) section, the Pods are terminated one at a time, with respect to the reverse order of their ordinal indices. Before terminating a Pod, the StatefulSet controller waits for the Pod's successor to be completely terminated. - -Note that, while a cascading delete will delete the StatefulSet and its Pods, -it will not delete the Headless Service associated with the StatefulSet. You -must delete the `nginx` Service manually. --> - 如同你在[缩容](#ordered-pod-termination)一节看到的,Pod 按照和他们序号索引相反的顺序每次终止一个。 在终止一个 Pod 前,StatefulSet 控制器会等待 Pod 后继者被完全终止。 - -请注意,虽然级联删除会删除 StatefulSet 和它的 Pod,但它并不会删除和 StatefulSet 关联的 Headless Service。你必须手动删除`nginx` Service。 +{{< note >}} + +尽管级联删除会删除 StatefulSet 及其 Pod,但级联不会删除与 StatefulSet +关联的 Headless Service。你必须手动删除 `nginx` Service。 +{{< /note >}} ```shell kubectl delete service nginx @@ -1487,10 +1540,9 @@ service "nginx" deleted ``` - -再一次重新创建 StatefulSet 和 Headless Service。 +再一次重新创建 StatefulSet 和 headless Service: ```shell kubectl apply -f web.yaml @@ -1503,10 +1555,9 @@ statefulset.apps/web created - -当 StatefulSet 所有的 Pod 变成 Running 和 Ready 时,获取它们的 `index.html` 文件的内容。 +当 StatefulSet 所有的 Pod 变成 Running 和 Ready 时,获取它们的 `index.html` 文件的内容: ```shell for i in 0 1; do kubectl exec -i -t "web-$i" -- curl http://localhost/; done @@ -1520,15 +1571,15 @@ web-1 +即使你已经删除了 StatefulSet 和它的全部 Pod,这些 Pod 将会被重新创建并挂载它们的 +PersistentVolume 卷,并且 `web-0` 和 `web-1` 将继续使用它的主机名提供服务。 -即使你已经删除了 StatefulSet 和它的全部 Pod,这些 Pod 将会被重新创建并挂载它们的 PersistentVolumes,并且 `web-0` 和 `web-1` 将仍然使用它们的主机名提供服务。 - - -最后删除 `nginx` service... + +最后删除 `nginx` service ```shell kubectl delete service nginx @@ -1538,7 +1589,10 @@ kubectl delete service nginx service "nginx" deleted ``` -... 并且删除 `web` StatefulSet: + +并且删除 `web` StatefulSet: ```shell kubectl delete statefulset web @@ -1550,74 +1604,73 @@ statefulset "web" deleted +## Pod 管理策略 {#pod-management-policy} + - -## Pod 管理策略 - - - 对于某些分布式系统来说,StatefulSet 的顺序性保证是不必要和/或者不应该的。 这些系统仅仅要求唯一性和身份标志。为了解决这个问题,在 Kubernetes 1.7 中 我们针对 StatefulSet API 对象引入了 `.spec.podManagementPolicy`。 此选项仅影响扩缩操作的行为。更新不受影响。 -### OrderedReady Pod 管理策略 + +### OrderedReady Pod 管理策略 {#orderedready-pod-management} + +`OrderedReady` Pod 管理策略是 StatefulSet 的默认选项。它告诉 +StatefulSet 控制器遵循上文展示的顺序性保证。 -`OrderedReady` pod 管理策略是 StatefulSets 的默认选项。它告诉 StatefulSet 控制器遵循上文展示的顺序性保证。 + +### Parallel Pod 管理策略 {#parallel-pod-management} - -### Parallel Pod 管理策略 - - -`Parallel` pod 管理策略告诉 StatefulSet 控制器并行的终止所有 Pod, + +`Parallel` Pod 管理策略告诉 StatefulSet 控制器并行的终止所有 Pod, 在启动或终止另一个 Pod 前,不必等待这些 Pod 变成 Running 和 Ready 或者完全终止状态。 {{< codenew file="application/web/web-parallel.yaml" >}} - 下载上面的例子并保存为 `web-parallel.yaml`。 + +这份清单和你在上文下载的完全一样,只是 `web` StatefulSet 的 +`.spec.podManagementPolicy` 设置成了 `Parallel`。 -这份清单和你在上文下载的完全一样,只是 `web` StatefulSet 的 `.spec.podManagementPolicy` 设置成了 `Parallel`。 - - -在一个终端窗口查看 StatefulSet 中的 Pod。 + +在一个终端窗口监视 StatefulSet 中的 Pod。 ```shell -kubectl get po -lapp=nginx -w +kubectl get pod -l app=nginx -w ``` - 在另一个终端窗口创建清单中的 StatefulSet 和 Service: ```shell @@ -1631,7 +1684,6 @@ statefulset.apps/web created - 查看你在第一个终端中运行的 `kubectl get` 命令的输出。 ```shell @@ -1651,14 +1703,14 @@ web-1 1/1 Running 0 10s - StatefulSet 控制器同时启动了 `web-0` 和 `web-1`。 -保持第二个终端打开,并在另一个终端窗口中扩容 StatefulSet。 + +保持第二个终端打开,并在另一个终端窗口中扩容 StatefulSet: ```shell kubectl scale statefulset/web --replicas=4 @@ -1670,7 +1722,6 @@ statefulset.apps/web scaled - 在 `kubectl get` 命令运行的终端里检查它的输出。 ``` @@ -1685,17 +1736,15 @@ web-3 1/1 Running 0 26s - StatefulSet 启动了两个新的 Pod,而且在启动第二个之前并没有等待第一个变成 Running 和 Ready 状态。 ## {{% heading "cleanup" %}} + 你应该打开两个终端,准备在清理过程中运行 `kubectl` 命令。 ```shell @@ -1706,8 +1755,7 @@ kubectl delete sts web - -你可以监测 `kubectl get` 来查看那些 Pod 被删除 +你可以监视 `kubectl get` 来查看那些 Pod 被删除 ```shell kubectl get pod -l app=nginx -w @@ -1739,37 +1787,33 @@ web-3 0/1 Terminating 0 9m ``` +在删除过程中,StatefulSet 将并发的删除所有 Pod,在删除一个 +Pod 前不会等待它的顺序后继者终止。 -StatefulSet 控制器将并发的删除所有 Pod,在删除一个 Pod 前不会等待它的顺序后继者终止。 - - -关闭 `kubectl get` 命令运行的终端并删除`nginx` Service。 + +关闭 `kubectl get` 命令运行的终端并删除 `nginx` Service: ```shell kubectl delete svc nginx ``` - -## {{% heading "cleanup" %}} - - +{{< note >}} +你需要删除本教程中用到的 PersistentVolume 卷的持久化存储介质。 - + - -你需要删除本教程中用到的 PersistentVolumes 的持久化存储介质。基于你的环境、存储配置和提供方式,按照必须的步骤保证回收所有的存储。 - - - +基于你的环境、存储配置和制备方式,按照必须的步骤保证回收所有的存储。 +{{< /note >}} \ No newline at end of file