“This is the 26th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

The basics of POD have been briefly described above, and the underlying principles can be carefully considered when we use some advanced knowledge of POD later

We will continue to learn about Lable, RC and HPA

What is Label?

Label is a label. For example, in our hand-written YAML file, each key-value pair is a label, such as key =value

Labels are usually defined when resources are defined, but they can also be added, edited, and deleted dynamically after objects are created

We write labels that can be attached to Node, Service, Pod, and RC. Any number of labels can be defined for each type of resource, and the same label can be added to any number of resource objects

That’s a very flexible point

Why should labels be attached to various resource objects?

Because labels and label selectors cannot be defined separately, they must be attached to a class of resource objects in order to be able to manage those resources and group them using label selectors

For example, write an openResty, write an RC that uses OpenResty, and Service that calls openResty’s label selector

apiVersion: v1
kind: ReplicationController	
metadata: 
  name: openresty
spec:
  replicas: 2
  selector:
    app: openresty
  tmplate:
    metadata:
      labels:
        app: openresty
      spec:
        containers:
        - name: openresty
        ports:
        - containerPort: 80
----------------------------
apiVersion: v1
kind: Service	
metadata: 
  name: openresty
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30125
  selector:
    app: openresty
Copy the code

What is RC?

What RC is, ReplicationController replica controller

When we define an RC and submit it to K8S, this is an expectation. The Controller Manager component on the Master node is notified and checks the PODS periodically to make sure that the number of pods is the same as we expect

If the current detected quantity is different from our expected quantity, the controller will add and remove pods, which enables pod dynamic expansion and contraction

As mentioned earlier, we can use the following command to set the number of copies we want

Kubectl scale RC service name --replicas= specific numberCopy the code

What is the difference between RC and Replica Sets?

There is no difference between them in essence, but Replica Sets support more

RC Replica Sets
Support for equity-based label selector Support for collection-based label selectors
After K8S 1.2, Replica Sets are mainly used by resource objects such as Deployment

In order to improve the disaster recovery capacity of our application, even if the program only has a COPY of POD, it should be managed by RC, because RC can manage pod creation, deletion, update and other arrangement mechanism, without POD itself blind worry

What is HPA?

HPA is the Horizontal Pod Autoscal, a Horizontal expansion of Pod, which is also a resource object for K8S

Simple principles of HPA

HAP adjusts the number of target POD copies by tracking and analyzing the load changes of RC’s POD

K8S provides two ways to expand and shrink a POD

  • The first is that we scale up and down RC, Deployment by command
Kubectl scale RC /deployment service name --replicasCopy the code
  • The second way is to use HPA to automatically expand and shrink the capacity

The automatic mode requires the user to specify a range of replicas according to a specified performance indicator, and the system will automatically adjust the range according to the performance indicator

HPA small case

  • Write your own Nginx Deployment
apiVersion: extension/v1beta1
kind: Deployment
metadata:
  name: my nginx deployment
spec:
  replicas: 2
  template:
    metadata:
      name: my deployment
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        resources:
          requrests:
            cpu: 50m
        ports:
        - containerPort: 80
Copy the code
  • Write a service

Write a service named mynginx-svc

apiVersion: v1
kind: Service
metadata:
  name: mynginx-svc
spec:
  ports:
  - port: 80
  selector:
    app: nginx
Copy the code
  • Write a hpa

Hpa sets the minimum number of copies to 2, the maximum number to 5, and the CPU to less than 70%, referencing my Nginx Deployment

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: mynginx-hpa
spec:
  scaletargetRef:
    apiversion: app/v1beta1
    kind: deployment
    name: my nginx deployment
  minReplicas: 2
  maxReplicas: 5
  targetCPUUtillizationPercentage: 70
Copy the code

Today is here, learning, if there is a deviation, please correct

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~