ReplicaSets

A ReplicaSet’s purpose is to run a specified number of pods at any given time.

While ReplicaSets can be used independently, today it’s mainly used by Deployments as a mechanism to orchestrate pod creation, deletion, and updates.

When you use Deployments you don’t have to worry about managing the ReplicaSets that they create. Deployments own and manage their ReplicaSets.

Therefore, we recommend using Deployments instead of directly using ReplicaSets, unless you require custom update orchestration or don’t require updates at all.

Here is the configuration of ReplicaSets

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: webapp
  labels:
    app: webapp
    tier: frontend
spec:
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: webapp
        image: learninghub/website:2.0

Creating ReplicaSet

$ kubectl apply -f replicaSet.yaml 
replicaset.apps "webapp" created
$ kubectl get rs
NAME      DESIRED   CURRENT   READY     AGE
webapp    3         3         3         49s
$ kubectl get pods
NAME           READY     STATUS    RESTARTS   AGE
webapp-jcs7v   1/1       Running   0          32s
webapp-kpl9h   1/1       Running   0          32s
webapp-rxwr5   1/1       Running   0          32s

Scaling a ReplicaSet

$ kubectl scale  --replicas=5 rs webapp
replicaset.extensions "webapp" scaled
$ kubectl get pods
NAME           READY     STATUS    RESTARTS   AGE
webapp-96klr   1/1       Running   0          8s
webapp-d5hv5   1/1       Running   0          8s
webapp-jcs7v   1/1       Running   0          5m
webapp-kpl9h   1/1       Running   0          5m
webapp-rxwr5   1/1       Running   0          5m

ReplicaSet as a Horizontal Pod Autoscaler Target

A ReplicaSet can also be a target for Horizontal Pod Autoscalers (HPA). That is, a ReplicaSet can be auto-scaled by an HPA.

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: webapp-scaler
spec:
  scaleTargetRef:
    kind: ReplicaSet
    name: webapp
  minReplicas: 3
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50

Delete a ReplicaSet

$ kubectl delete rs/webapp
replicaset.extensions "webapp" deleted

You can delete a ReplicaSet without affecting any of its Pods using kubectl delete with the –cascade=false option.

$ kubectl delete rs/webapp --cascade=false
Advertisements