ReplicaSets - Sandeep-K-Khandelwal/CKAD GitHub Wiki

ReplicationController and ReplicaSet monitor the PODs in the cluster and make sure the desired number of PODs are always up and running. ReplicationController and ReplicaSet spin up a new POD if one of the existing POD terminates and make sure the desired number of PODs are always running.

ReplicationController is an older technology while ReplicaSet is a new one. They both do the same task with a minimal difference.

ReplicationController

  • kubectl create -f rc-definition.yml - Create Replication controller based on the definition file
  • kubectl get replicationcontrollers - Get the list of Replication controllers
  • kubectl delete replicationcontroller <replication_controller_name> - Delete the specified Replication controller. It also delete the underlying Pods
  • kubectl describe replicationcontroller <replication_controller_name> - Get details about the specified Replication controller
apiVersion: v1
kind: ReplicationController
metadata:
  name: myapp-rc
  labels:
    app: myapp
    type: frontend
spec:
  replicas: 3
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myap
        type: frontend
    spec:
      containers:
      - name: nginx
        image: nginx

ReplicaSet - We need to specify selector for ReplicaSet. The selector is optional for the ReplicationController

  • kubectl create -f rs-definition.yml - Create ReplicaSet based on the definition file
  • kubectl get replicasets - Get the list of ReplicaSets
  • kubectl delete replicaset <replication_controller_name> - Delete the specified ReplicaSet. It also deletes the underlying Pods
  • kubectl describe replicaset <replication_controller_name> - Get details about the specified ReplicaSet
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  labels:
    app: myapp
    type: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      type: frontend
  template:
    metadata:
      labels:
        type: frontend
        app: myapp
    spec:
      containers:
      - name: nginx
        image: nginx

Scale ReplicaSet -

  1. Update the definition file used to create the ReplicaSet earlier and use the command - kubectl apply -f <rs-definition.yml>
  2. kubectl scale --replicas=<new number of relicas> -f <rs-definition.yml>
  3. kubectl scale --replicas=<new number of relicas> replicaset <replica_set_name>