Kubernetes > ReplicaSets
Pod replicas can be created using ReplicaSets
One Pod template, multiple replicas
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl get rs
No resources found.
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl get pods
No resources found.
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ cat ex6.yml
---
#ReplicaSet is an object that contains replicas of a Pod
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs6
namespace: default
spec:
replicas: 3 #there will be 3 replicas of the same Pod
selector:
matchLabels:
tag: label6 #the replicaSet should pick Pods with this label
template: #the Pod template
metadata: #Pod name need not be defined as replicaset takes care of it
labels:
tag: label6
spec:
containers:
- name: ctr6
image: nginx
...
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl create -f ex6.yml
replicaset.apps/rs6 created
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl get rs
NAME DESIRED CURRENT READY AGE
rs6 3 3 3 1m
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl get pods
NAME READY STATUS RESTARTS AGE
rs6-274lv 1/1 Running 0 11s
rs6-rp6jj 1/1 Running 0 11s
rs6-vnc4r 1/1 Running 0 11s
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl delete pods rs6-274lv
pod "rs6-274lv" deleted
Even when a Pod is deleted, the replicaSet would ensure its replaced by another Pod so that the number of replicas remain the same
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl get pods
NAME READY STATUS RESTARTS AGE
rs6-pzxwd 1/1 Running 0 11s
rs6-rp6jj 1/1 Running 0 1m
rs6-vnc4r 1/1 Running 0 1m
Once the replicaSet is deleted, the Pods are also deleted
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl delete rs rs6
replicaset.extensions "rs6" deleted
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl get pods
NAME READY STATUS RESTARTS AGE
rs6-4krtz 0/1 Terminating 0 1m
rs6-829r6 0/1 Terminating 0 1m
rs6-x58v5 0/1 Terminating 0 1m
networkandcode@cloudshell:~ (kubernetes-cka-224606)$ kubectl get pods
No resources found.
Reference:
https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/
--end-of-post--