Overview
Official website address:/zh-cn/docs/concepts/workloads/controllers/replicaset/
ReplicaSet
Abbreviationrs
, its main function andReplicationController
Likewise, ensure that a certain number of pods run normally, it will continuously monitor the running status of these pods. Once the pod fails, it will be restarted or rebuilt. It also supports scaling and mirrored versions of pods.
Compared with ReplicationController, ReplicaSet is lighter and more powerful, and supports regular matching pods.
If you want to learn ReplicationController, you can view this article:ReplicationController resources for K8s newbies series
The role of ReplicaSet
-
Replica Management: The core function of ReplicaSet is to ensure that there are a specified number of Pod replicas running at any time. If a pod terminates unexpectedly, ReplicaSet will automatically create a new pod to replace it; if there are too many pods, it will delete the excess pods.
-
Cluster expansion and reduction: Users can easily expand or reduce applications by modifying the number of replicas in the ReplicaSet.
-
Flexible tag selector
-
Collection selector: Unlike ReplicationController that only supports equation selectors, ReplicaSet supports more flexible collection selectors, such as in, notin, exists, etc. This allows ReplicaSet to select the pods to be managed more accurately, improving the flexibility and expressiveness of tag selection.
-
Dynamic Match Pods: Through the tag selector, ReplicaSet can dynamically match Pods that meet the criteria. Even after the ReplicaSet is created, these Pods will be included in the management scope of the ReplicaSet as long as new Pods are created and their labels are in line with the ReplicaSet selector.
-
Detailed explanation of ReplicaSet resource configuration file
Can be passedkubectl explain rs
View the fields required to create a ReplicaSet
Example:
[root@node01 ~]# kubectl explain rs
KIND: ReplicaSet
VERSION: apps/v1
DESCRIPTION:
ReplicaSet ensures that a specified number of pod replicas are running at
any given time.
# The content in the fileds field has been intercepted
FIELDS:
apiVersion <string>
kind <string>
metadata <Object>
spec <Object>
status <Object>
Through the above, we can find that the resource file that defines ReplicaSet is the same as the Pod, and also requires fields such as apiVersion, kind, metadata, spec, etc.
However, there are three fields in the spec field, which should be noted, namely replicas, selector, and template. as follows:
[root@node01 ~]# kubectl explain
KIND: ReplicaSet
VERSION: apps/v1
RESOURCE: spec <Object>
DESCRIPTION:
Spec defines the specification of the desired behavior of the ReplicaSet.
More info:
https://git./community/contributors/devel/sig-architecture/#spec-and-status
ReplicaSetSpec is the specification of a ReplicaSet.
FIELDS:
minReadySeconds <integer>
# minReadySeconds specifies a time threshold that is considered available only if the time when the Pod remains ready reaches or exceeds this threshold, and no containers within the Pod crash during this period.
replicas <integer>
# Define the number of copies of the Pod
selector <map[string]string>
# Tag selector, here specifies the tag defined by Pod
template <Object>
# Define Pod templates
Create a ReplicaSet
Here we create three Pod copies
Example:
# Define resource files
[root@node01 ~/rs]# cat
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-nginx-1
namespace: default
labels:
app: rs-nginx
spec:
# Define the number of copies of the Pod
replicas: 3
# Tag selector, here is consistent with Pod's Label
selector:
# Match the Pod's Label
matchLabels:
app: nginx
# Define the Pod template, just define two fields: metadata and spec
template:
metadata:
name: pod-nginx
labels:
app: nginx
spec:
restartPolicy: Always
containers:
- name: nginx
image: nginx:latest
# Create a ReplicaSet
[root@node01 ~/rs]# kubectl apply -f
View the corresponding ReplicaSet and Pod
View ReplicaSet
[root@node01 ~/rs]# kubectl get rs
NAME DESIRED CURRENT READY AGE
rs-nginx-1 3 3 3 93s
View Pod
[root@node01 ~/rs]# kubectl get po | grep rs-nginx
rs-nginx-1-6rb9p 1/1 Running 0 2m
rs-nginx-1-ssdhv 1/1 Running 0 2m
rs-nginx-1-vzn2x 1/1 Running 0 2m
Verify Replica Management of ReplicaSet
The core function of ReplicaSet is to ensure that there are a specified number of Pod replicas running at any time. If a pod terminates unexpectedly, ReplicaSet will automatically create a new pod to replace it; if there are too many pods, it will delete the excess pods.
What happens to Replicaset when we delete the created Pod?
# Delete all pods created above
[root@node01 ~/rs]# kubectl delete po rs-nginx-1-6rb9p rs-nginx-1-ssdhv rs-nginx-1-vzn2x
pod "rs-nginx-1-6rb9p" deleted
pod "rs-nginx-1-ssdhv" deleted
pod "rs-nginx-1-vzn2x" deleted
# Check rs and find that the number of copies is still 3
[root@node01 ~/rs]# kubectl get rs
NAME DESIRED CURRENT READY AGE
rs-nginx-1 3 3 3 5m17s
# Check the pod and find out that there are 3 corresponding pods. Through the AGE field, it is found that it is newly created.
[root@node01 ~/rs]# kubectl get po | grep rs-nginx
NAME READY STATUS RESTARTS AGE
rs-nginx-1-6h7fv 1/1 Running 0 22s
rs-nginx-1-fhkm9 1/1 Running 0 22s
rs-nginx-1-l5njb 1/1 Running 0 22s
Verify the scaling capacity of Replicaset
Verify the capacity expansion
Let's adjust the number of copies of rs to 5 to see what happens?
[root@node01 ~/rs]# cat
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-nginx-1
namespace: default
labels:
app: rs-nginx
spec:
# Change the number of copies to 5
replicas: 5
selector:
matchLabels:
app: nginx
template:
metadata:
name: pod-nginx
labels:
app: nginx
spec:
restartPolicy: Always
containers:
- name: nginx
image: nginx:latest
# Reapply it
[root@node01 ~/rs]# kubectl apply -f
/rs-nginx-1 configured
Check out the changes in the Pod:
[root@node01 ~/rs]# kubectl get po | grep rs-nginx
NAME READY STATUS RESTARTS AGE
rs-nginx-1-6h7fv 1/1 Running 0 4m17s
rs-nginx-1-fhkm9 1/1 Running 0 4m26s
rs-nginx-1-l5njb 1/1 Running 0 4m26s
rs-nginx-1-t75z7 1/1 Running 0 8s
rs-nginx-1-tdvbr 1/1 Running 0 8s
Through the above, it was found that the number of pod copies changed from 3 to 5. Looking at the AGE field, I found that there are two pods in a newly created state.
Verify the size reduction
Similarly, we adjust the number of copies to 1 to see what happens?
[root@node01 ~/rs]# cat
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-nginx-1
namespace: default
labels:
app: rs-nginx
spec:
# Adjust the number of pods to 1
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
name: pod-nginx
labels:
app: nginx
spec:
restartPolicy: Always
containers:
- name: nginx
image: nginx:latest
# Reapply it
[root@node01 ~/rs]# kubectl apply -f
/rs-nginx-1 configured
Check it out:
# View rs
[root@node01 ~/rs]# kubectl get rs
NAME DESIRED CURRENT READY AGE
rs-nginx-1 1 1 1 11m
# View Pod
[root@node01 ~/rs]# kubectl get po | grep rs-nginx
rs-nginx-1-fhkm9 1/1 Running 0 6m33s
Through the above findings, the number of Pods has been reduced from 5 to 1
Replicaset's tag selector
Replicaset's tag selector is divided into two categories: one is the equation selector and the other is the set selector.
Equation selector
The equation selector filters labels by =, == (the two have the same meanings) or !=. It passesTo achieve it.
Example:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-nginx-1
namespace: default
labels:
app: rs-nginx
spec:
replicas: 1
selector:
# Equation selector
matchLabels:
app: nginx
template:
metadata:
name: pod-nginx
labels:
app: nginx
spec:
restartPolicy: Always
containers:
- name: nginx
image: nginx:latest
Collection selector
The collection selector uses in, notin, exists, and DoesNotExist to filter tags.
- in: Used to select the pod with the label value in the specified set.
- notin: Used to select Pods whose label value is not in the specified set.
- exists: Used to select a Pod with the specified tag, regardless of the value of the tag.
- DoesNotExist: Used to select Pods that do not have the specified tag, regardless of the value of the tag.
It passesTo achieve
Example:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset-set
spec:
replicas: 3
selector:
# Collection selector
matchExpressions:
# key: specify the key of the Label, operator: represents the operator, value: represents the value of the Label
- {key: app, operator: In, values: [nginx]}
- {key: env, operator: NotIn, values: [development, testing]}
template:
metadata:
labels:
app: nginx
env: production
spec:
containers:
- name: nginx
image: nginx:1.14.2
Ports:
- containerPort: 80
Manage Replicaset
View Replicaset
grammar:
kubectl get rs <rs-name> -n <namespace-name>
Example:
[root@node01 ~/rs]# kubectl get rs
NAME DESIRED CURRENT READY AGE
rs-nginx-1 1 1 1 25m
Modify Replicaset
In two ways, one is throughkubectl edit rc <rc-name>
To modify it, a similarvim
, just modify its corresponding value, and finallywq
Save to apply your configuration.
Another way is to modify the corresponding resource file and finally use itkubectl apply -f <file>
Just do it.
Delete Replicaset
grammar:
kubectl delete rc <rc-name> -n <namespace-name>