Analysis on the Controller and Label of kubernetes

  • 2021-10-25 08:17:23
  • OfStack

Common controllers in catalog 01 k8s RC controller Deployment controller Statefulset controller 02 Label (tag) concept

Common controllers in 01 k8s

We learned earlier that Pod is the smallest scheduling unit in the k8s cluster, and Pod is composed of Pause container + application container.

In k8s, it often happens that one kind of resource object manages another kind of resource object, which we call "controller".

Let's simply look at the types of controllers and their functions. In fact, each controller has its own characteristics. We will analyze it one by one in the future. Now we only need a concept.

RC Controller

It defines a desired scenario that declares that the number of copies of an Pod meets a specific expected value at any given time. For example, in MySQL's 1 master and 2 slave structure, we expect that there will be 2 slave libraries at any time. If it is not enough, we need to expand 1 slave library.

In its definition, the following three key parameters are needed:

1. Expected number of Pod

2. The label of Pod, that is, a type identification

3. Create the Pod template for the new Pod

Its general structure is as follows:


apiVersion : vl
kind: ReplicationController
metadata:
    name: rc-mysql-slave
spec :
    replicas: 2  #  Expected value 
    selector:    #  Label 
      app: mysql
    template:    #  Template 
      xxx:  xxx

By comparing the above text, you can see the basic yaml file template of RC.

Once RC1 is defined and submitted to master of k8s, controller manager will patrol the current surviving target Pod and ensure that the current surviving Pod is equal to the expected Pod. If there are more, it will be deleted, and if there are fewer, it will be created.

Deployment controller

It is a new concept introduced in k8s 1.2. This controller is 90% similar to RC controller. The difference between RC and Pod is that it can get the "deployment progress" of current Pod at any time. Its yaml file definition is roughly as follows:


apiVersion: extensions / vlbetal
kind : Deployment
metadata:
    name: dep-mysql-slave
spec:
    replicas: 2
    selector:
       xxxx : xxxx
    template : 
       xxxx : xxxx

With Deployment, almost all scenarios using RC can be replaced by Deployment.

Statefulset Controller

This controller also generates some of the expected values of Pod, but it differs from RC and Deployment in that the Pod it generates is stateful.

In the Pod generated by RC and Deployment, we only need to generate the expected Pod, which is similar to the two slave libraries of MySQL in the 1 master 2 slave architecture. They don't have the concept of sequence and weight. However, the generation sequence or weight of Pod controlled by Statefulset controller has a definite logical relationship. For example, in the master-slave architecture of MySQL, it is necessary to become a master node and then generate a slave node. At this time, it is more appropriate to use statefulset.

There are one more commonly used controller types, which we will share in detail next time. Here we know the concept of "controller".

02 Label (Tag) Concept

In the last article, we talked about several key fields in the yaml file of Pod in k8s. Today, let's look at one attribute of another Pod, that is, Label.

Label means tag, and its format is also key-value format. It can be attached to an object in k8s cluster, including but not limited to Pod, Node, RC, etc. The binding relationship between resource objects and Label can be 1-to-1 or many-to-1, and different label can manage resources in groups.

When we label a resource, we can use the label selector Label Selector to select the resource for scheduling, as follows:


apiVersion : vl
kind: Pod
metadata:
    name : myweb
    labels:
       app : mysql

The above example defines an Pod for us with the label app=mysql.

Then we define a "controller", use the tag selector selector in the controller, let it select this Pod of this app=mysql, and keep this Pod in two copies in the cluster as follows:


apiVersion : vl
kind : ReplicationController
metadata:
    name: myweb
spec :
    replicas : 2
    selector:
        app : mysql
    template:
        xxxx

The new version of Selector can also specify more detailed filters, mainly with the following parameters:


apiVersion : vl
kind : ReplicationController
metadata:
    name: myweb
spec :
    replicas : 1
    selector:
       matchLabels :
         app: myweb
       matchExpressions:
        - {key: aaa , perator: In , values: [mysql-slave]}
        - {key: bbb , operator : Notin , values: [mysql-master)}
    template:
        xxxx

One parameter is matchLabels, which can be followed by multiple label conditions in key-value format;

The other parameter is matchExpression, which can specify an array with conditional operation. In the above example, the aaa parameter is in mysql-slave, but the bbb parameter is not in mysql-master.

If these two parameters appear at the same time, AND will be taken automatically, and their intersection will be used as the final filtering condition to filter Pod

It can be seen that the use of tags makes it more flexible and convenient for the "controller" to select the controlled objects.

The above is the detailed analysis of kubernetes controller and label, more information about kubernetes controller and label please pay attention to other related articles on this site!


Related articles: