Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the operation of ReplicaSet

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

How to understand the operation of ReplicaSet, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

We use some examples to demonstrate the creation, view, update, delete and other operations of ReplicaSet.

Create

First, let's prepare a configuration file called replicaset.yaml, which is as follows:

ApiVersion: apps/v1kind: ReplicaSetmetadata: name: replicaset-runs-podspec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers:-name: nginx image: nginx:1.19.0

This ReplicaSet configuration ensures that there are three copies of Pod running at the same time.

Use the kubectl create command to create a ReplicaSet controller, as follows:

[root@ecs-d8b6 manifests] # kubectl create-f replicaset.yaml replicaset.apps/replicaset-runs-pod created

The command line output shows that a ReplicaSet controller named replicaset-runs-pod has been created.

View

Let's first take a look at the ReplicaSet controller we just created:

[root@ecs-d8b6 manifests] # kubectl get replicaset NAME DESIRED CURRENT READY AGEreplicaset-runs-pod 3 3 3 90s

You can see that the number of Pod expected by the controller replicaset-runs-pod (DESIRED) is 3, the number of Pod currently created (CURRENT) is also 3, and the number of Pod in the READY state (READY) is also 3, which is exactly the state we expect.

Then take a look at the actual running Pod:

[root@ecs-d8b6 manifests] # kubectl get pods NAME READY STATUS RESTARTS AGEreplicaset-runs-pod-dzhqp 1/1 Running 0 5mreplicaset-runs-pod-hpbs8 1/1 Running 0 5mreplicaset-runs-pod-xhx9j 1/1 Running 0 5m

You can see that there are three Pod running, and the Pod name is prefixed with its controller name, plus a random string.

Using the kubectl describe command to view the controller, you can see its record of creating the Pod:

[root@ecs-d8b6 manifests] # kubectl describe replicaset replicaset-runs-podName: replicaset-runs-podNamespace: defaultSelector: app=nginxLabels: Annotations: Replicas: 3 current / 3 desiredPods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 FailedPod Template: Labels: app=nginx Containers: nginx: Image: nginx:1.19.0 Port: Host Port: Environment: Mounts: Volumes: Events: Type Reason Age From Message-Normal SuccessfulCreate 8m5s replicaset-controller Created pod: replicaset-runs-pod-xhx9j Normal SuccessfulCreate 8m5s replicaset-controller Created pod: replicaset-runs-pod-dzhqp Normal SuccessfulCreate 8m5s replicaset-controller Created pod: replicaset-runs-pod-hpbs8

Through the final Events information, we can see that the above three Pod are created during the running of the controller replicaset-runs-pod.

Update to change the number of copies

We use the kubectl edit command to modify the controller replicaset-runs-pod to adjust the number of spec.replicas from 3 to 5:

[root@ecs-d8b6 manifests] # kubectl edit replicaset replicaset-runs-pod replicaset.apps/replicaset-runs-pod edited

Then look at Pod again:

[root@ecs-d8b6 manifests] # kubectl get pods NAME READY STATUS RESTARTS AGEreplicaset-runs-pod-5mtdk 1/1 Running 0 37sreplicaset-runs-pod-6rqqp 1/1 Running 0 37sreplicaset-runs-pod-dzhqp 1/1 Running 0 21mreplicaset-runs-pod-hpbs8 1/1 Running 0 21mreplicaset-runs-pod-xhx9j 1/1 Running 0 21m

You can see that two new Pod have been created, and the number of Pod copies is the same as the spec.replicas setting in the configuration.

Similarly, when we modify the controller replicaset-runs-pod configuration again and adjust the number of spec.replicas from 5 to 3, the extra number of Pod will be automatically deleted.

Simulate Pod exception

The ReplicaSet controller will always ensure that the number of Pod copies running in the cluster meets the expected value, and when the Pod is deleted, the controller will immediately create a new Pod to fill the gap:

[root@ecs-d8b6 manifests] # kubectl delete pods replicaset-runs-pod-dzhqppod "replicaset-runs-pod-dzhqp" deleted [root@ecs-d8b6 manifests] # kubectl get podsNAME READY STATUS RESTARTS AGEreplicaset-runs-pod-hpbs8 1 Running 0 31mreplicaset-runs-pod-tbccg 1 Running 0 9sreplicaset-runs-pod-xhx9j 1 Running 0 31m

In the above example, when a Pod managed by a ReplicaSet controller is manually deleted, a new Pod is created immediately, which can be seen by the Pod run time or controller event information.

Delete

When a ReplicaSet controller is deleted, its managed Pod is deleted by default, as shown below:

[root@ecs-d8b6 manifests] # kubectl delete-f replicaset.yaml replicaset.apps "replicaset-runs-pod" deleted [root@ecs-d8b6 manifests] # kubectl get podsNo resources found in default namespace.

The ability of the ReplicaSet controller focuses on the control of the number of replicas. It can ensure that a specified number of Pod replicas are running in the cluster at all times, and the number of Pod can be adjusted dynamically when the number of Pod replicas in the configuration changes.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report