Pod Controller Overview
Introduction
Pods are the smallest management unit of kubernetes. In kubernetes, pods can be divided into two categories according to how they are created.
- Autonomous pods: Pods created directly by kubernetes. Such pods are not available after deletion and will not be rebuilt
- Controller-created pods: Pods created by kubernetes through the controller, which are automatically rebuilt after deletion.
Controller
Pod controller is the middle layer for managing pods. After using Pod controller, you just need to tell Pod controller how many and what kind of Pods you want, and it will create Pods that meet the conditions and make sure each Pod resource is in the target state that the user expects. If a Pod resource fails in operation, it will reschedule the Pods based on the specified policy.
Categories
In kubernetes, there are many types of pod controllers, each with its own suitable scenario, the common ones are the following.
- ReplicationController: A more primitive pod controller that has been deprecated and replaced by ReplicaSet
- ReplicaSet: Ensure that the number of replicas has been maintained at the desired value, and support the number of pod expansion and contraction, image version upgrade
- Deployment: control Pods by controlling ReplicaSet, and support rolling upgrade, rollback version
- Horizontal Pod Autoscaler: can automatically adjust the number of Pods horizontally according to the cluster load, automatic scaling
- DaemonSet: It runs on the specified Node in the cluster and only runs one copy, generally used for daemon type tasks
- Job: It creates a pod that exits as soon as it completes its task, without restarting or rebuilding, and is used to perform one-time tasks.
- Cronjob: It creates a Pod that is responsible for periodic task control and does not need to run continuously in the background.
- StatefulSet: manage stateful applications
ReplicaSet(RS)
Overview
The main role of ReplicaSet is to keep a certain number of pods up and running, it will continuously listen to the running status of these pods, and restart or rebuild them once they fail. It also supports scaling up and scaling down the number of pods and scaling up and down the image version.
Resource List
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
apiVersion: apps/v1 # Version number
kind: ReplicaSet # type
metadata:
name:
namespace:
labels:
spec:
replicas: <Number of copies>
selector: # selector, which specifies which pods are managed by this controller
matchLabels: # Labels Matching Rules
matchExpressions: # Expressions Matching Rules
- {key: <lableskey>, operator: <Matching method>, values: <lablesvalue>}
template: # template, when the number of copies is insufficient, the pod copies will be created according to the following template
metadata:
labels:
spec:
containers:
- name:
image:
ports:
- containerPort:
|
replicas: Specify the number of replicas, which is actually the number of pods created by the current rs, the default is 1
selector: selector, its role is to establish the association between pod controller and pod, using the Label Selector mechanism. Define the label on the pod template and the selector on the controller to indicate which pods can be managed by the current controller
template: template is the template used by the current controller to create pods, which is actually the definition of the pods learned in the previous chapter
Create RS
Create the Pc-Replicaset.yaml file with the following contents.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: pc-replicaset
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: docker.io/library/nginx:1.23.1
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# Create rs
[root@master yaml]# kubectl create -f Pc-Replicaset.yaml
replicaset.apps/pc-replicaset created
# View rs
# DESIRED:desired number of copies
# CURRENT:current number of replicas
# READY:Number of replicas ready for service
[root@master yaml]# kubectl get rs pc-replicaset -n default -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-replicaset 3 3 3 2m41s nginx docker.io/library/nginx:1.23.1 app=nginx-pod
# View the pods created by the current controller
# The name of the pod created by the controller is a random code with -xxxxx spliced after the controller name
[root@master yaml]# kubectl get pod -n default
NAME READY STATUS RESTARTS AGE
pc-replicaset-fvjg2 1/1 Running 0 3m53s
pc-replicaset-lzfc2 1/1 Running 0 3m53s
pc-replicaset-q7hrm 1/1 Running 0 3m53s
|
Expansion and reduction of capacity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# Edit the configuration online
# Just modify spce.replicas to 6
[root@master yaml]# kubectl edit rs pc-replicaset -n default
replicaset.apps/pc-replicaset edited
# Check the number of Pods
[root@master yaml]# kubectl get pod -n default
NAME READY STATUS RESTARTS AGE
pc-replicaset-49zl6 1/1 Running 0 67s
pc-replicaset-5ngpl 1/1 Running 0 67s
pc-replicaset-fvjg2 1/1 Running 0 6m45s
pc-replicaset-lzfc2 1/1 Running 0 6m45s
pc-replicaset-pnstd 1/1 Running 0 67s
pc-replicaset-q7hrm 1/1 Running 0 6m45s
# Use the command
# Use scale to achieve expansion and contraction replicas for the number of expansion and contraction
[root@master yaml]# kubectl scale rs pc-replicaset --replicas=2 -n default
replicaset.apps/pc-replicaset scaled
# Check the number of Pods
[root@master yaml]# kubectl get pod -n default|grep pc
NAME READY STATUS RESTARTS AGE
pc-replicaset-fvjg2 1/1 Running 0 8m39s
pc-replicaset-q7hrm 1/1 Running 0 8m39s
|
image Upgrade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# Online editing configuration
# Modify spce.template.spec.containers.image to docker.io/library/nginx:latest
[root@master yaml]# kubectl edit rs pc-replicaset -n default
replicaset.apps/pc-replicaset edited
# Check rs status
# The image version has changed
[root@master yaml]# kubectl get rs -n default -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-replicaset 2 2 2 14m nginx docker.io/library/nginx:latest app=nginx-pod
# Use the command
# kubectl set image rs rs name container=image version -n namespace
[root@master yaml]# kubectl set image rs pc-replicaset nginx=docker.io/library/nginx:1.23.1 -n default
replicaset.apps/pc-replicaset image updated
# Check again
# The image version has changed
[root@master yaml]# kubectl get rs -n default -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
pc-replicaset 2 2 2 17m nginx docker.io/library/nginx:1.23.1 app=nginx-pod
|
Delete RS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# Using the kubectl delete command will delete this RS and the Pods it manages
# Before kubernetes deletes the RS, it will adjust the RS replicasclear to 0 and wait for all the Pods to be deleted before performing the deletion of the RS object
[root@master yaml]# kubectl delete rs pc-replicaset -n default
replicaset.apps "pc-replicaset" deleted
[root@master yaml]# kubectl get pod -n default -o wide
No resources found in default namespace.
# If you wish to just delete the RS object (keep the Pod), you can add the --cascade=false option when using the kubectl delete command (not recommended).
[root@master yaml]# kubectl delete rs pc-replicaset -n default --cascade=false
replicaset.apps "pc-replicaset" deleted
[root@master yaml]# kubectl get pods -n dev
NAME READY STATUS RESTARTS AGE
pc-replicaset-cl82j 1/1 Running 0 75s
pc-replicaset-dslhb 1/1 Running 0 75s
# You can also use yaml to delete directly (recommended)
[root@master yaml]# kubectl delete -f Pc-Replicaset.yaml
replicaset.apps "pc-replicaset" deleted
|