- ReplicaSet ensures that a specified number of pod replicas are running at any given time.
- It is recommended to use Deployments instead of directly using ReplicaSets, as they help manage ReplicaSets and provide declarative updates to Pods.
show
kubectl get replicasets
# OR
kubectl get rs
Create a replica set named replica-set-demo
using a pod named ngnix using a nginx image and labeled as tier=frontend
with a single replica.
show
cat << EOF > replica-set-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replica-set-demo
spec:
replicas: 1
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: nginx
image: nginx
EOF
kubectl apply -f replica-set-demo.yaml
show
kubectl scale replicaset replica-set-demo --replicas=2
OR
Edit the replica set definition file replica-set-demo.yaml
and apply kubectl apply -f replica-set-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replica-set-demo
spec:
replicas: 2 # update this
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: nginx
image: nginx
EOF
show
kubectl scale replicaset replica-set-demo --replicas=1
OR
Edit the replica set definition file replica-set-demo.yaml
and apply kubectl apply -f replica-set-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replica-set-demo
spec:
replicas: 1 # update this
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: nginx
image: nginx
EOF
apiVersion: v1
kind: ReplicaSet
metadata:
name: replicaset-1
spec:
replicas: 1
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: nginx
image: nginx
show
Update the version and apply again.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicaset-2
spec:
replicas: 1
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: nginx
spec:
containers:
- name: nginx
image: nginx
show
The replica set selector field tier: frontend
does not match the pod labels tier: nginx
. Correct either of them and reapply.
kubectl delete replicaset replica-set-demo replicaset-1 replicaset-2
rm replica-set-demo.yaml