The core function of Kubernetes is choreography, and choreography operations are dependent on the controller object to complete, the advanced controller object control basic controller object, the basic controller object to control Pod, Pod inside the container again. The hierarchy of API objects in the Kubernetes project looks something like this. Today, ReplicaSet, the basic controller that manages a Pod, is introduced.

What is the ReplicaSet

Given ReplicaSet’s name, it controls the number of replicas, each of which is a Pod. ReplicaSet is a Kubernetes controller that ensures that we have a specified number of running REPLICas of pods. It ensures that the system is currently running as many pods as specified in the desired state.

In general,KubernetesIt is recommended to useDeploymentController instead of direct useReplicaSet.DeploymentIt’s a managementReplicaSetAnd providePodMore advanced controllers for declarative updates, application versioning, and many other functions. soDeploymentThe controller is not managed directlyPodObject, but byDeploymentmanagementReplicaSetAnd then byReplicaSetResponsible for managing thePodObject.

With this logic, you can see why ReplicaSet is a controller object that needs to be understood before you can learn about Deployment.

How does ReplicaSet manage PODS

ReplicaSet manages all containers with labels that match the Label Selector through a label-selector. When a Pod is created, it assumes that all pods are the same and stateless, so there is no precedence in the creation order. If another ReplicaSet is created using the same tag selector, the former ReplicaSet will assume that it created the pods, triggering logic in the control loop to delete the extra pods, and the new ReplicSet will create the pods again. The current state of both parties is never equal to the expected state, which can cause problems, so it is important to ensure that the ReplicaSet tag selector is unique.

ReplicaSet YAML file

The following configuration file template lists the main components of the YAML file that defines a ReplicaSet object:

apiVersion: apps/v1
kind: ReplicaSet   
metadata: 
  name: some-name
  labels:
    app: some-App
    tier: some-Tier
spec: 
  replicas: 3 Tell K8S how many copies it needs
  Selector: # tag selector for matching Pod
    matchLabels:
      tier: someTier
  template:
    metadata:
      labels:
        app: some-App
        tier: someTier
    spec: This is similar to the Pod object's spec definition
      Containers: 
Copy the code

All API objects in Kubernetes consist of four parts:

  • ApiVersion — the one currently in useKubernetesAPI version.
  • Kind – The kind of object you want to create.
  • Metadata — Metadata that uniquely represents the current object, such as name and namespace.
  • Spec – Specifies the configuration of the current object.

But the definition of ReplicaSet’s spec section looks slightly different from the spec definition of other objects. There are two spec fields in the above example. The first spec declares ReplicaSet properties that define how many Pod replicas there are (only one Pod will be deployed by default), selectors that match Pod tags, and so on. The second spec is used for configuration such as container properties in the Pod.

In fact, the contents of “.spec.template” are the properties to define when declaring a Pod object, so this section is also called a PodTemplate. Has a notable places: in. Spec. The selector that defined in the tag selector must be able to match to the spec. The template. The metadata. The labels in the definition of the Pod labels, otherwise Kubernetes will not be allowed to create ReplicaSet.

ReplicaSet use case

Now that you know what a replica set is and how to write a replica set declaration file, let’s start creating one. In this example, we will create an nginx application with three Pod copies from the replica.yaml file.

# replica.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicas
  labels:
    app: myapp
    tier: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
    matchExpressions:
      - {key: tier.operator: In.values: [frontend]}
  template:
    metadata:
      labels:
        app: myapp
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
Copy the code

First we run the kubectl create command to create the object using the declaration file defined above.

$ kubectl create -f replica.yaml
replicaset.apps "myapp-replicas" created
Copy the code

Next, use the command to ensure that the replica set is created successfully

$ kubectl get replicaset
NAME                            DESIRED   CURRENT   READY     AGE
myapp-replicas                  3         3         3         15s
Copy the code

The command return above shows that the Myapp-Replicas replica set has three replicas deployed, all of which are ready. Let’s take a look at the health of each Pod copy:

$ kubectl get pod
NAME                            READY     STATUS    RESTARTS   AGE
myapp-replicas-67rkp            1/1       Running   0          33s
myapp-replicas-6kfd8            1/1       Running   0          33s
myapp-replicas-s96sg            1/1       Running   0          33s
Copy the code

All three are running with zero restarts, which means our application has not crashed so far. The kubectl describe command can also be used to query the details of the ReplicaSet object. The Events section of the information details the choreography actions performed by the ReplicaSet controller.

$ kubectl describe replicaset myapp-replicas
Name:         myapp-replicas
Namespace:    default
Selector:     tier=frontend,tier in (frontend)
Labels:       app=myapp
              tier=frontend
Annotations:  
Replicas:     3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=myapp
           tier=frontend
  Containers:
   nginx:
    Image:        nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  
    Mounts:       
  Volumes:        
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  12m   replicaset-controller  Created pod: myapp-replicas-6kfd8
  Normal  SuccessfulCreate  12m   replicaset-controller  Created pod: myapp-replicas-67rkp
  Normal  SuccessfulCreate  12m   replicaset-controller  Created pod: myapp-replicas-s96sg
Copy the code

conclusion

As shown in the YAML file above, a ReplicaSet object is defined by the number of replicas and a Pod template. When you get to Deployment objects in the next article, you will see that the contents of the ReplicaSet object definition file are actually a subset of Deployment objects.

In practice, instead of creating a ReplicaSet object directly, we use a more advanced controller object, Deployment, which manages ReplicaSet.

Recommended Recent articles

How to avoid written in dynamic languages thinking Go code | 🏆 technology project the second phase of the essay

The public,