“This is the 20th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

【 K8S series 】 K8S Learning 26-5, Statefulset Combat 3

In the previous section, we talked about how to use Statefulset to deploy stateful applications. Statefulset allows each pod deployed to have a separate persistent volume declaration and persistent volume

Before we compared Statefulset and ReplicaSet, there are similarities and differences between them. The differences have been shared in the previous article. Let’s look at the similarities

Statefulset capacity expansion and reduction

Take a look at the one Statefulset we have in our environment and the two pods it manages

And 2 persistent volume declarations PVC and 2 persistent volume PVCS

Start manually deleting a POD

When we manually delete STA-kubia-0, Statefulset will immediately create a pod that looks exactly like the one that was missing because of the tag because it manages two copies

By checking the name and index value of pod, we expect curl to access the interface provided by POD, and obtain the corresponding data

Example Change the number of copies to 3

Kubectl get statefulSet changes the SPEC REPLICas field

After modifying the number of copies, run the following command to view the resource information

kubectl get statefulset
kubectl edit statefulset sta-kubia
kubectl get po | grep sta
kubectl get pv
kubectl get pvc
Copy the code

After the number of copies was changed to 3, Statefulset expanded the capacity. We found that a POD was indeed created with an index of 2, and 1 was also created for PVC and PV. The effect was consistent with our theory after actual operation

Change the number of copies to 1

So we now start to shrink, the effect will be the same as we theoretically?

The operation is similar to the above. Change the number of copies to 1

Curl curl curl curl curl curl curl curl curl curl curl curl curl curl curl curl curl curl curl

Change the number of copies back to 3

Now that we expand, will Statefulset restore all the two pods that were deleted? To be exactly the same, huh?

We see the effect and our theory is exactly the same, no deviation oh

Sta-kubia-1 and STA-Kubia-2 are back. Is there any joy in getting them back?

Statefulset said, “I’m going to do what I told you to do, and NOW I’m going to do what I told you to do. That’s not bad.

Illustrate the above case

The figure is divided into 3 steps. This figure is used to indicate that we manually deleted a POD or because of an exception. At this time, the number of copies of Statefulset is 2

  1. Removed 1 pod STA-kubiA-1
  2. The volume corresponding to STA-kubiA-1 is still in the environment and has not been deleted
  3. Statefulset recognizes that one pod is missing from the tag, so it creates a new POD that is identical to the original pod, with the same identity, status, and so on. The new POD sta-KubiA-1 still uses the original volume 1

The following figure shows how to modify Statefulset replicas for scaling

Again, there are 3 steps

  1. In the environment, the number of repilicas copies is 3. Edit Statefulset to change the number of repilicas copies to 1. At this time, K8S will be deleted from the highest index from the largest to the smallest, and finally there will be 1 pod left, namely sta-kubiA-0
  2. After deleting two PODS, Statefulset will not delete the original POD volume
  3. If we manually changed the number of repilicas copies from 1 to 3, what would Statefulset do to restore the original two pods, STA-KubiA-1 and STA-Kubia-2, that could still use volume 1 and volume 2

Statefulset and Deployment have one thing in common, that is, when Statefulset deletes pods, volumes are not deleted, while Deployment does not delete RS/RS in rolling upgrade

So can we think of Statefulset upgrade as supporting rolling upgrade like Deployment?

K8s is supported in version 1.7 and operates in a similar way to Deployment. If you have any doubts about the details of Deployment display upgrade application, please refer to article [K8S series] learning 25 -2. Deployment Upgrade Application

Today is here, learning, if there is a deviation, please correct

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~