Overview
- ReplicaSet is a process of monitoring pods, when pod is down, it will startup a new one.
- ReplicaSet identify which pods to monitor by labels.
- RelicatSet monitor and keep the pod number as described in yaml file or command line setting. In order word, if part of the pods is down, ReplicaSet will automatically start new pods to keep the pod number as described in ReplicaSet yaml.
- According to #2 and #3, suppose that there is a number of pods running as described in ReplicaSet yaml. We start a new pod with the same label described in RelipcaSet in some way, (e.g. start a new pod by pod.yaml), the newly created pod will be terminated since ReplicaSet will keep the pod number by labels described in ReplicaSet yaml.
Yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: my-rc
labels:
app: myapp
type: front-end
spec:
# RelicaSet - template - pod description
# RelicaSet - replicas - pod number
# RelicaSet - selector - pod finder
replicas: 3
selector: # match the pod label @1
matchLabels:
type: front-end
template:
metadata:
labels: # pod label @1
type: front-end
spec:
containers:
- name: ngx-container
image: nginx
Test
kubectl apply -f replicaset.yaml
kubectl describe pod ngx-container
kubectl apply -f replicaset.yaml # change replicas from 3 to 5.
kubectl get pods
kubectl delete -f replicaset.yaml