This page is under regular updates. Please check back later for more content.
Replica Set

Replica Set

Overview

  • Replica set is a next generation replication Controller
  • Only supports equality based selector whereas the replica set supports set based selector that is filtering according to set of values.
  • Replica set is used by other objects like deployment rather than the replication controller.
  • There is no major changes in the manifest file except the kind and the apiVersion
keyReplication ControllerReplica Set
kindReplicationControllerReplicaSet
apiVersionv1apps/v1

Example of Replica Set

k8s_example_8.yml
kind: ReplicaSet
apiVersion: apps/v1
metadata:
  name: example-8
spec:
  replicas: 2
  selector:
    matchExpressions:
      - key: myname
        operator: In
        values: [xander, billa, kubernetes]
      - key: env
        operator: NotIn
        values: [prod, dev]
  template:
    metadata:
      name: container0
      labels:
        myname: xander
    spec:
      containers:
        - name: c00
          image: ubuntu
          command: ["/bin/bash", "-c", "while true; do echo Hello-World; sleep 5 ; done"]

Create a replicaset

kubectl apply -f k8s_example_8.yml
Output
controlplane $ kubectl apply -f k8s_example_8.yml 
replicaset.apps/example-8 created

Verify the rs and pods

kubectl get rs
kubectl get pods
Output
controlplane $ kubectl get rs
NAME        DESIRED   CURRENT   READY   AGE
example-8   5         5         5       72s

For more details

kubectl describe rs example-8
Output
controlplane $ kubectl describe rs example-8
.
.
.
Replicas:     5 current / 5 desired
Pods Status:  5 Running / 0 Waiting / 0 Succeeded / 0 Failed
.
.
.
Events:
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  3m32s  replicaset-controller  Created pod: example-8-wjtkc
  Normal  SuccessfulCreate  3m32s  replicaset-controller  Created pod: example-8-w8qnb

Verify the pods

kubectl get pods
Output
controlplane $ kubectl get pods
NAME              READY   STATUS    RESTARTS   AGE
example-8-w8qnb   1/1     Running   0          4m11s
example-8-wjtkc   1/1     Running   0          4m12s

Note the labels

kubectl get pods --show-labels
Output
controlplane $ kubectl get pods --show-labels
NAME              READY   STATUS    RESTARTS   AGE     LABELS
example-8-w8qnb   1/1     Running   0          4m31s   myname=xander
example-8-wjtkc   1/1     Running   0          4m32s   myname=xander

Tweak the number of desired replica

kubectl scale rs --replicas=2 example-8
Output
controlplane $ kubectl scale rs --replicas=2 example-8   
replicaset.apps/example-8 scaled
controlplane $ kubectl get pods
NAME              READY   STATUS    RESTARTS   AGE
example-8-w8qnb   1/1     Running   0          12m
example-8-wjtkc   1/1     Running   0          12m

Verify the rs

kubectl get rs
Output
controlplane $ kubectl get rs
NAME        DESIRED   CURRENT   READY   AGE
example-8   2         2         2       13m

To delete replica set

kubectl delete rs example-8
Output
controlplane $ kubectl delete rs example-8
replicaset.apps "example-8" deleted