Kubernetes Detailed Explanation (21) - ReplicaSet Controller Practical Application

Today, I will continue to introduce the relevant knowledge of Linux operation and maintenance. The main content of this article is the actual application of the ReplicaSet controller.
In the above Kubernetes Detailed Explanation (20) - ReplicaSet Controller , we explained the basic content and creation of the ReplicaSet controller. Today, we will conduct the actual combat of the ReplicaSet controller.

1. ReplicaSet controller application upgrade

First, let's use the ReplicaSet controller for application upgrades. In the above, after the ReplicaSet controller is created, let's check the image version of the Pod controlled by the controller and execute the command:

kubectl get pods -o custom-columns=Name:metadata.name,Image:spec.contianers[0].image

In the above command, we used the -o custom-columns parameter to specify the displayed columns. Among them, Name and Image are the column names, followed by a colon, and then specify the displayed content. The displayed content is expressed using Kubernetes key field names, with commas separating different columns.
The execution result of this command is as follows:
insert image description here
As can be seen from the above figure, the version of the Pod in the current Kubernetes cluster is v1. Next, let's upgrade the version of the Pod controlled by the ReplicaSet controller to v2. Open the resource list configuration file of the ReplicaSet controller, and change the image field from v1 to v2. The modified content is shown in the red circle in the following figure:
insert image description here
After that, we recreate the ReplicaSet controller and execute the command:

kubectl apply -f ReplicaSet.yaml

After the execution is complete, execute the command again:

kubectl get pods -o custom-columns=Name:metadata.name,Image:spec.contianers[0].image

Looking at the mirror version of the Pod controlled by the ReplicaSet controller, we found that the ReplicaSet controller did not complete the version update operation. This is because the ReplicaSet does not automatically complete the update, this command will only update the newly created Pod Controller to the new version. If we complete the version update of the Pod, we also need to manually delete the original Pod controller to completely complete the Pod update.
So we execute the command:

kubectl delete pods -l demo=replicaset

Delete the Pod resource object controlled by the ReplicaSet controller, and then execute the command again:

kubectl get pods -o custom-columns=Name:metadata.name,Image:spec.contianers[0].image

Check the version of the Pod and you will find that the Pod Controller has been updated.
The whole process is as follows:
insert image description here
As can be seen from the above figure, the image version of the current Pod controlled by the ReplicaSet controller is v2, and the version of our ReplicaSet controller has been successfully upgraded!

2. ReplicaSet controller expansion and contraction

Next, we will expand and shrink the ReplicaSet controller. First, we execute the command:

kubectl get rs

View the number of Pods under the ReplicaSet controller. After that, we execute the expansion command:

kubectl scale replicasets rs --replicas=3

In this command, scale is the keyword of the expansion command, replicasets specifies that the object to be expanded is the ReplicaSet controller, the following rs is the name of the ReplicaSet controller, and the following –replicas=3 indicates the number of Pods controlled by the controller Set to 3.
After that, we execute the rs view command again to view the number of Pods after the ReplicaSet controller is expanded.
The execution of the above process is as follows:
insert image description here
As can be seen from the above figure, before the expansion, the number of Pods controlled by the ReplicaSet is 2, and after the expansion, the number of Pods controlled by the ReplicaSet is 3, and the expansion of the ReplicaSet is successful!
As for the scaling of the ReplicaSet controller, the basic commands are the same as the scaling commands, except that the value after the –replicas parameter is smaller than the actual number of Pods. On the basis of the above, execute the command:

kubectl scale replicasets rs --replicas=1

The number of Pods in the ReplicaSet controller can be reduced to 1. The execution result of this command is as follows:
insert image description here

3. ReplicaSet controller resource deletion

Finally, let's introduce the deletion of the ReplicaSet controller resource.
If we want to delete the ReplicaSet resource controller, we can execute the command:

kubectl delete 【ReplicaSet控制器名称】

As shown below:
insert image description here
However, as can be seen from the above figure, when the ReplicaSet is deleted in this way, the Pods managed by the controller will also be deleted.
Sometimes, we want to delete the ReplicaSet controller, but we want the Pod managed by the controller to still exist, then we can add --cascade=false after the delete command to execute the command:

kubectl delete replicasets rs --cascade=false

In this way, you can delete the ReplicaSet controller but not the Pod it controls. The execution result of this command is as follows:
insert image description here
Original is not easy, please explain the source of reprint: https://blog.csdn.net/weixin_40228200

Guess you like

Origin blog.csdn.net/weixin_40228200/article/details/124349230