Kubernetes Detailed Explanation (20) - ReplicaSet Controller

Today, I will continue to introduce the relevant knowledge of Linux operation and maintenance. The main content of this article is the ReplicaSet controller.

1. Overview of ReplicaSet Controller

The ReplicaSet controller is an implementation of the Pod class controller, which is used to ensure that the number of Pod object replicas it manages can meet the user's desired number at any time. After the ReplicaSet controller is started, it will automatically find and manage eligible Pod objects in the cluster. The ReplicaSet controller replaces the earlier ReplicationController controller in the Kubernetes cluster. The ReplicaSet controller can implement the following functions:
1. Ensure that the number of Pod resource objects meets the expected value. Reduce or expand the Pod according to the difference between the expected value and the current value.
2. Ensure that the Pod is running healthy. When a failure is detected on the node running the Pod resource object, the scheduler is requested to create Pods on other nodes.
3. Elastic expansion. The ReplicaSet controller can flexibly expand or shrink the Pod resource objects. When necessary, the HPA controller can also realize automatic scaling of the Pod resource scale.

2. Core fields of ReplicaSet controller

In the resource manifest, the core fields under the spec of the ReplicaSet controller are as follows:
replicas: Specifies the desired number of Pod replicas.
selector: Specifies the label selector of the ReplicaSet controller, which supports two matching mechanisms: matchLables and matchExpressions.
template: Some parameters used to define the Pod resource object controlled by the ReplicaSet.
minReadySeconds: used to define how long the Pod will be available after it starts, the default is 0 seconds, and the unit is seconds when set by this parameter.
We can use the command to view some parameters of the ReplicaSet controller, execute the command:

kubectl explain rs

You can view the core fields of ReplicaSet. The execution result of this command is as follows:
insert image description here
Execute the command:

kubectl explain rs.spec

You can view the fields in the spec section under the ReplicaSet controller. The command execution results are as follows:
insert image description here

Three, ReplicaSet controller resource list creation

Next, let's actually create a ReplicaSet controller by means of a resource list. Create a ReplicaSet.yaml file and write the following:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      demo: replicaset
      img: myapp
  template:
    metadata:
      name: rs-pod
      namespace: default
      labels:
        demo: replicaset
        img: myapp
    spec:
      containers:
      - name: rs-pod-myapp
        image: ikubernetes/myapp:v1
        ports:
        - name: http
          containerPort: 80

The resource manifest file after the configuration is completed is as follows:
insert image description here
It can be seen that in the above configuration, the template field under the spec field defines the relevant parameters of the Pod controlled by the ReplicaSet controller. Therefore, it must be noted during configuration that the label under the selector.matchLabels field of the ReplicaSet controller must be the same as the label of the Pod under the template.
After completing the above configuration, we execute the command:

kubectl apply -f ReplicaSet.yaml

Turn on the ReplicaSet controller. The command execution result is as follows:
insert image description here
After completion, let's look at our ReplicaSet controller and execute the command:

kubectl get rs

The command execution result is as follows:
insert image description here
As can be seen from the above figure, our ReplicaSet controller has been successfully created. After that, let's take a look at the Pods controlled by the ReplicaSet controller and execute the command:

kubectl get pods

The execution results are as follows:
insert image description here
As can be seen from the results of the above commands, we successfully created the ReplicaSet controller through the resource configuration list!
Originality is not easy, please indicate the source for reprinting: https://blog.csdn.net/weixin_40228200

Guess you like

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