Skip to content

ReplicaSet

ReplicaSet is the next-generation Replication Controller. ReplicaSet supports the new set-based selector requirements whereas a Replication Controller only supports equality-based selector requirements.

Replica Sets are declared in essentially the same way as Replication Controllers, except that they have more options for the selector

Create a ReplicaSet

$ cat rs.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # this replicas value is default
  # modify it according to your case
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
    matchExpressions:
      - {key: tier, operator: In, values: [frontend, development]}
      - {key: tier, operator: NotIn, values: [production]}
  template:
    metadata:
      labels:
        app: guestbook
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google_samples/gb-frontend:v3
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
          # If your cluster config does not include a dns service, then to
          # instead access environment variables to find service host
          # info, comment out the 'value: dns' line above, and uncomment the
          # line below.
          # value: env
        ports:
        - containerPort: 80
In this case, we’re looking at two different conditions:

  1. The tier label must be frontend or development
  2. The tier label (if it exists) must not be production
$ kubectl create -f rs.yaml
replicaset "frontend" created

$ kubectl get pods
NAME                                   READY     STATUS              RESTARTS   AGE
frontend-pwl99                         0/1       ContainerCreating   0          8s
frontend-tp7w2                         0/1       ContainerCreating   0          8s
frontend-vs8pf                         0/1       ContainerCreating   0          8s

$ kubectl get rs
NAME                             DESIRED   CURRENT   READY     AGE
frontend                         3         3         3         56s

$ kubectl describe rs/frontend
Name:         frontend
Namespace:    default
Selector:     tier=frontend,tier in (frontend)
Labels:       app=guestbook
              tier=frontend
Annotations:  <none>
Replicas:     3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=guestbook
           tier=frontend
  Containers:
   php-redis:
    Image:  gcr.io/google_samples/gb-frontend:v3
    Port:   80/TCP
    Requests:
      cpu:     100m
      memory:  100Mi
    Environment:
      GET_HOSTS_FROM:  dns
    Mounts:            <none>
  Volumes:             <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  1m    replicaset-controller  Created pod: frontend-pwl99
  Normal  SuccessfulCreate  1m    replicaset-controller  Created pod: frontend-vs8pf
  Normal  SuccessfulCreate  1m    replicaset-controller  Created pod: frontend-tp7w2

Delete ReplicaSet

$ kubectl delete rs frontend
replicaset "frontend" deleted

$ kubectl get pods
NAME                                   READY     STATUS        RESTARTS   AGE
frontend-pwl99                         0/1       Terminating   0          10m
frontend-tp7w2                         0/1       Terminating   0          10m