Replicaset is a controller that ensures that specified no. of pod replicas are running at any given time. It will autoheal / restart if any pod dies.
rs.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset # name of replicaset
spec:
replicas: 3
selector: # how does replicaset knows which are my pods ? ( here whichever has lable set to apps: nginx ) # based on this u know how many replicas of this replicaset are up whether to increase/decrease
matchLabels:
apps: nginx
template: # how does pod looks like whose replicaset are supposed to be created/ started
metadata:
labels:
apps: nginx # Pod that's getting started will has this label # Should be unique to the pod of this replicaset
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
kubectl apply -f rs.yml
kubectl get replicaset # This will return replicaset
kubectl get pods # Pods are automatically started
kubectl delete pod nginx-replicaset-vdstl # Even if u delete one of the pods, replicaset will start another pod in order to maintain 3 replicas
# Deleting replicaset
kubectl delete rs nginx-replicaset # ( replicaset_name )
It manages bunch of replicasets.
An application needs to be redeployed again and again. Whenever a new update happens, it starts 1 pod of new type after the newly started pod is healthy it stops 1 pod of old type . This way it drains old pods and starts newer gradually. That’s called rolling updates
At the time of rolling update, If new pod fails to start it still is running older pods, it will not drain until newer pods are up & ready
It provides scaling , rollback to older version. .

Deployment orchestrates replicaset which in turn orchestrates pods.
deployment.yml
# Same as rs.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment # name of replicaset
spec:
replicas: 3
selector: # how does replicaset knows which are my pods ? ( here whichever has lable set to apps: nginx ) # based on this u know how many replicas of this replicaset are up whether to increase/decrease
matchLabels:
apps: nginx
template: # how does pod looks like whose replicaset are supposed to be created/ started
metadata:
labels:
apps: nginx # Pod that's getting started will has this label # Should be unique to the pod of this replicaset
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
kubectl apply -f deployment.yml
kubectl get deployment # This will return deployment that u created
kubectl get replicaset # replicaset will automatically be created
kubectl get pods # replicaset will start pods
kubectl rollout history deployment/nginx-deployment