Replication Controller of Kubernetes study notes (7)

The concept of Replication Controller

Replication Controller, referred to as "RC", is a controller that manages the number of pods in kubernetes. It ensures that a specified number of pods are running at any time.

The role of Replication Controller

1. The main role of Replication Controller is to ensure that the number of Pod copies in the Kubernetes cluster always meets expectations. It is able to monitor the status of pods and create, update and delete pods as needed. Replication Controller specifies the number of Pods to run through the number of Pod copies. When the number of Pod copies is less than the specified number, Replication Controller will automatically create a new Pod copy. Conversely, if the number of Pod copies is more than the specified number, Replication Controller will automatically delete redundant Pod copy.

2. Tag selector

The Replication Controller uses label selectors to determine which Pods it manages. It selects pods that match it based on label selectors, and manages the number of replicas for those pods. Label selector is a very important concept in Kubernetes. It can group Pods so that Replication Controller can manage specific Pods.

3. Rolling update

Replication Controller supports rolling update, that is, the Pod version can be updated gradually without replacing all Pods at once. Rolling update can ensure that the application will not be down or shut down during the update, thereby ensuring the availability of the application.

4. Elastic expansion

The Replication Controller can dynamically adjust the number of copies of the Pod according to the load of the Pod to adapt to the change of the load. Elastic scaling can improve the performance and availability of applications to better meet user needs.

5. Monitoring and self-healing

Replication Controller can monitor the status of pods and automatically create new pods when pods fail or are deleted, so as to ensure that the number of copies of pods always meets expectations. Through monitoring and self-healing, Replication Controller can improve application availability and stability.

Working principle of Replication Controller

A ReplicaSet is defined by a set of fields, including a selector to identify the set of available Pods, a value to indicate the number of replicas that should be maintained, and a value to specify that new Pods should be created to satisfy the replica set. The Pod template to use when counting conditions, etc. Each ReplicaSet realizes its existence value by creating and deleting Pods as needed to make the number of replicas reach the expected value. When a ReplicaSet needs to create a new Pod, it uses the provided Pod template.

ReplicaSets are connected to satellite Pods via the metadata.ownerReferences field on the Pod, which gives the current object's owner resources. The Pod obtained by the ReplicaSet contains the identification information of the owner ReplicaSet in its ownerReferences field. It is through this connection that the ReplicaSet knows the state of the collection of Pods it maintains and plans its operational behavior accordingly.

A ReplicaSet uses its selector to identify the set of Pods to obtain. If a Pod has no OwnerReference or its OwnerReference is not a controller, and it matches the selector of a ReplicaSet, the Pod is immediately acquired by this ReplicaSet.

Use of Replication Controller

Example:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # 按你的实际情况修改副本数
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google_samples/gb-frontend:v3
Create rc resources based on the above sample files
kubectl apply -f demo_rc.yaml

View the created rc resources

kubectl get rs

# 输出结果如下
NAME       DESIRED   CURRENT   READY   AGE
frontend   3           3         3      6s

View details of rc resources

kubectl describe rs/frontend

# 输出结果如下
Name:         frontend
Namespace:    default
Selector:     tier=frontend
Labels:       app=guestbook
              tier=frontend
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {
    
    "apiVersion":"apps/v1","kind":"ReplicaSet","metadata":{
    
    "annotations":{
    
    },"labels":{
    
    "app":"guestbook","tier":"frontend"},"name":"frontend",...
Replicas:     3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  tier=frontend
  Containers:
   php-redis:
    Image:        gcr.io/google_samples/gb-frontend:v3
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  117s  replicaset-controller  Created pod: frontend-wtsmm
  Normal  SuccessfulCreate  116s  replicaset-controller  Created pod: frontend-b2zdv
  Normal  SuccessfulCreate  116s  replicaset-controller  Created pod: frontend-vcmts

View pods associated with rc

kubectl get pods

# 输出结果如下
NAME             READY   STATUS    RESTARTS   AGE
frontend-b2zdv   1/1     Running   0          6m36s
frontend-vcmts   1/1     Running   0          6m36s
frontend-wtsmm   1/1     Running   0          6m36s

Summarize

Currently, Replication Controller is generally used in combination with Deployment to manage pods. In short, the Replication Controller is a very important component in Kubernetes, which provides a reliable operating environment for the application and ensures that there are enough Pod copies running at any time. Learning Replication Controller requires an in-depth understanding of concepts such as Pod, label selector, rolling update, elastic scaling, and monitoring self-healing. In practice, you can operate in conjunction with specific applications, so as to better grasp the use of Replication Controller.

Guess you like

Origin blog.csdn.net/Habo_/article/details/129111639