Replicaset

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.

Creating Replicasets

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 )

Deployment

It manages bunch of replicasets.

image.png

Creating Deployment

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