Skip to content

Create a replicaset

Course: Kubernetes Part 1 - chapter 14

Let’s not worry about deployments, lets just create a replicaset that starts 3 pods

  • Create rs.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
  • Apply the manifest
kubectl apply -f rs.yml
  • Get the rs details
kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-replicaset 3 3 3 23s
  • Check the pods
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-replicaset-7zp2v 1/1 Running 0 35s
nginx-replicaset-q264f 1/1 Running 0 35s
nginx-replicaset-vj42z 1/1 Running 0 35s
  • Try deleting a pod and check if it self heals
kubectl delete pod nginx-replicaset-7zp2v
kubectl get pods
  • Try adding a pod with the app=nginx
kubectl run nginx-pod --image=nginx --labels="app=nginx"
  • Ensure it gets terminated immedietely because the rs already has 3 pods
  • Delete the replicaset
kubectl delete rs nginx-deployment-576c6b7b6

💡 Note the naming convention of the pods. The pods are named after the replicaset followed by a unique id (for eg nginx-replicaset-vj42z)