Skip to content

Commit 48a4657

Browse files
authored
Merge pull request #44473 from sftim/20231222_revise_statefulset_tutorial
Revise StatefulSet tutorial updates advice
2 parents 78f86c6 + ca37e9a commit 48a4657

File tree

1 file changed

+68
-41
lines changed

1 file changed

+68
-41
lines changed

content/en/docs/tutorials/stateful-application/basic-stateful-set.md

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -545,28 +545,25 @@ When exploring a Pod's [stable storage](#writing-to-stable-storage), we saw that
545545

546546
## Updating StatefulSets
547547

548-
In Kubernetes 1.7 and later, the StatefulSet controller supports automated updates. The
548+
The StatefulSet controller supports automated updates. The
549549
strategy used is determined by the `spec.updateStrategy` field of the
550-
StatefulSet API Object. This feature can be used to upgrade the container
550+
StatefulSet API object. This feature can be used to upgrade the container
551551
images, resource requests and/or limits, labels, and annotations of the Pods in a
552-
StatefulSet. There are two valid update strategies, `RollingUpdate` and
553-
`OnDelete`.
552+
StatefulSet.
554553

555-
`RollingUpdate` update strategy is the default for StatefulSets.
554+
There are two valid update strategies, `RollingUpdate` (the default) and
555+
`OnDelete`.
556556

557557
### RollingUpdate {#rolling-update}
558558

559559
The `RollingUpdate` update strategy will update all Pods in a StatefulSet, in
560560
reverse ordinal order, while respecting the StatefulSet guarantees.
561561

562-
Patch the `web` StatefulSet to apply the `RollingUpdate` update strategy:
562+
You can split updates to a StatefulSet that uses the `RollingUpdate` strategy
563+
into _partitions_, by specifying `.spec.updateStrategy.rollingUpdate.partition`.
564+
You'll practice that later in this tutorial.
563565

564-
```shell
565-
kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"type":"RollingUpdate"}}}'
566-
```
567-
```
568-
statefulset.apps/web patched
569-
```
566+
First, try a simple rolling update.
570567

571568
In one terminal window, patch the `web` StatefulSet to change the container
572569
image again:
@@ -627,7 +624,7 @@ StatefulSet controller terminates each Pod, and waits for it to transition to Ru
627624
Ready prior to updating the next Pod. Note that, even though the StatefulSet
628625
controller will not proceed to update the next Pod until its ordinal successor
629626
is Running and Ready, it will restore any Pod that fails during the update to
630-
its current version.
627+
that Pod's existing version.
631628

632629
Pods that have already received the update will be restored to the updated version,
633630
and Pods that have not yet received the update will be restored to the previous
@@ -655,21 +652,33 @@ the status of a rolling update to a StatefulSet
655652

656653
#### Staging an update
657654

658-
You can stage an update to a StatefulSet by using the `partition` parameter of
659-
the `RollingUpdate` update strategy. A staged update will keep all of the Pods
660-
in the StatefulSet at the current version while allowing mutations to the
661-
StatefulSet's `.spec.template`.
655+
You can split updates to a StatefulSet that uses the `RollingUpdate` strategy
656+
into _partitions_, by specifying `.spec.updateStrategy.rollingUpdate.partition`.
657+
658+
For more context, you can read [Partitioned rolling updates](/docs/concepts/workloads/controllers/statefulset/#partitions)
659+
in the StatefulSet concept page.
660+
661+
You can stage an update to a StatefulSet by using the `partition` field within
662+
`.spec.updateStrategy.rollingUpdate`.
663+
For this update, you will keep the existing Pods in the StatefulSet
664+
unchanged whilst you change the pod template for the StatefulSet.
665+
Then you - or, outside of a tutorial, some external automation - can
666+
trigger that prepared update.
662667

663-
Patch the `web` StatefulSet to add a partition to the `updateStrategy` field:
668+
First, patch the `web` StatefulSet to add a partition to the `updateStrategy` field:
664669

665670
```shell
671+
# The value of "partition" determines which ordinals a change applies to
672+
# Make sure to use a number bigger than the last ordinal for the
673+
# StatefulSet
666674
kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"type":"RollingUpdate","rollingUpdate":{"partition":3}}}}'
667675
```
668676
```
669677
statefulset.apps/web patched
670678
```
671679

672-
Patch the StatefulSet again to change the container's image:
680+
Patch the StatefulSet again to change the container image that this
681+
StatefulSet uses:
673682

674683
```shell
675684
kubectl patch statefulset web --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"registry.k8s.io/nginx-slim:0.7"}]'
@@ -687,7 +696,7 @@ kubectl delete pod web-2
687696
pod "web-2" deleted
688697
```
689698

690-
Wait for the Pod to be Running and Ready.
699+
Wait for the replacement `web-2` Pod to be Running and Ready:
691700

692701
```shell
693702
# End the watch when you see that web-2 is healthy
@@ -711,13 +720,16 @@ registry.k8s.io/nginx-slim:0.8
711720
```
712721

713722
Notice that, even though the update strategy is `RollingUpdate` the StatefulSet
714-
restored the Pod with its original container. This is because the
723+
restored the Pod with the original container image. This is because the
715724
ordinal of the Pod is less than the `partition` specified by the
716725
`updateStrategy`.
717726

718727
#### Rolling out a canary
719728

720-
You can roll out a canary to test a modification by decrementing the `partition`
729+
You're now going to try a [canary rollout](https://glossary.cncf.io/canary-deployment/)
730+
of that staged change.
731+
732+
You can roll out a canary (to test the modified template) by decrementing the `partition`
721733
you specified [above](#staging-an-update).
722734

723735
Patch the StatefulSet to decrement the partition:
@@ -731,7 +743,10 @@ kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"type":"RollingUpda
731743
statefulset.apps/web patched
732744
```
733745

734-
Wait for `web-2` to be Running and Ready.
746+
The control plane triggers replacement for `web-2` (implemented by
747+
a graceful **delete** followed by creating a new Pod once the deletion
748+
is complete).
749+
Wait for the new `web-2` Pod to be Running and Ready.
735750

736751
```shell
737752
# This should already be running
@@ -863,18 +878,32 @@ continue the update process.
863878

864879
### OnDelete {#on-delete}
865880

866-
The `OnDelete` update strategy implements the legacy (1.6 and prior) behavior,
867-
When you select this update strategy, the StatefulSet controller will not
868-
automatically update Pods when a modification is made to the StatefulSet's
869-
`.spec.template` field. This strategy can be selected by setting the
881+
You select this update strategy for a StatefulSet by setting the
870882
`.spec.template.updateStrategy.type` to `OnDelete`.
871883

884+
Patch the `web` StatefulSet to use the `OnDelete` update strategy:
885+
886+
```shell
887+
kubectl patch statefulset web -p '{"spec":{"updateStrategy":{"type":"OnDelete"}}}'
888+
```
889+
```
890+
statefulset.apps/web patched
891+
```
892+
893+
When you select this update strategy, the StatefulSet controller does not
894+
automatically update Pods when a modification is made to the StatefulSet's
895+
`.spec.template` field. You need to manage the rollout yourself - either
896+
manually, or using separate automation.
872897

873898
## Deleting StatefulSets
874899

875-
StatefulSet supports both Non-Cascading and Cascading deletion. In a
876-
Non-Cascading Delete, the StatefulSet's Pods are not deleted when the StatefulSet is deleted. In a Cascading Delete, both the StatefulSet and its Pods are
877-
deleted.
900+
StatefulSet supports both _non-cascading_ and _cascading_ deletion. In a
901+
non-cascading **delete**, the StatefulSet's Pods are not deleted when the
902+
StatefulSet is deleted. In a cascading **delete**, both the StatefulSet and
903+
its Pods are deleted.
904+
905+
Read [Use Cascading Deletion in a Cluster](/docs/tasks/administer-cluster/use-cascading-deletion/)
906+
to learn about cascading deletion generally.
878907

879908
### Non-cascading delete
880909

@@ -888,7 +917,7 @@ kubectl get pods --watch -l app=nginx
888917
Use [`kubectl delete`](/docs/reference/generated/kubectl/kubectl-commands/#delete) to delete the
889918
StatefulSet. Make sure to supply the `--cascade=orphan` parameter to the
890919
command. This parameter tells Kubernetes to only delete the StatefulSet, and to
891-
not delete any of its Pods.
920+
**not** delete any of its Pods.
892921

893922
```shell
894923
kubectl delete statefulset web --cascade=orphan
@@ -982,7 +1011,7 @@ with `replicas` equal to 2, once `web-0` had been recreated, and once
9821011
`web-1` had been determined to already be Running and Ready, `web-2` was
9831012
terminated.
9841013

985-
Let's take another look at the contents of the `index.html` file served by the
1014+
Now take another look at the contents of the `index.html` file served by the
9861015
Pods' webservers:
9871016

9881017
```shell
@@ -1051,7 +1080,7 @@ the Pod's successor to be completely terminated.
10511080

10521081
{{< note >}}
10531082
Although a cascading delete removes a StatefulSet together with its Pods,
1054-
the cascade does not delete the headless Service associated with the StatefulSet.
1083+
the cascade does **not** delete the headless Service associated with the StatefulSet.
10551084
You must delete the `nginx` Service manually.
10561085
{{< /note >}}
10571086

@@ -1114,14 +1143,11 @@ statefulset "web" deleted
11141143

11151144
For some distributed systems, the StatefulSet ordering guarantees are
11161145
unnecessary and/or undesirable. These systems require only uniqueness and
1117-
identity. To address this, in Kubernetes 1.7, we introduced
1118-
`.spec.podManagementPolicy` to the StatefulSet API Object.
1119-
1120-
### OrderedReady Pod management
1146+
identity.
11211147

1122-
`OrderedReady` pod management is the default for StatefulSets. It tells the
1123-
StatefulSet controller to respect the ordering guarantees demonstrated
1124-
above.
1148+
You can specify a Pod management policy to avoid this strict ordering;
1149+
either [`OrderedReady`](/docs/concepts/workloads/controllers/statefulset/#orderedready-pod-management) (the default)
1150+
or [`Parallel`](/docs/concepts/workloads/controllers/statefulset/#parallel-pod-management).
11251151

11261152
### Parallel Pod management
11271153

@@ -1170,7 +1196,8 @@ web-0 1/1 Running 0 10s
11701196
web-1 1/1 Running 0 10s
11711197
```
11721198

1173-
The StatefulSet controller launched both `web-0` and `web-1` at the same time.
1199+
The StatefulSet controller launched both `web-0` and `web-1` at almost the
1200+
same time.
11741201

11751202
Keep the second terminal open, and, in another terminal window scale the
11761203
StatefulSet:

0 commit comments

Comments
 (0)