Reference Documentation

Design docs, concept definitions, and references for APIs and CLIs.

Edit This Page

Replica Sets

What is a Replica Set?

Replica Set is the next-generation Replication Controller. The only difference between a Replica Set and a Replication Controller right now is the selector support. Replica Set supports the new set-based selector requirements as described in the labels user guide whereas a Replication Controller only supports equality-based selector requirements.

Most kubectl commands that support Replication Controllers also support Replica Sets. One exception is the rolling-update command. If you want the rolling update functionality please consider using Deployments instead. Also, the rolling-update command is imperative whereas Deployments are declarative, so we recommend using Deployments through the rollout command.

While replica sets 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 replica sets that they create. Deployments own and manage their replica sets.

When to use a Replica Set?

A Replica Set ensures that a specified number of pod “replicas” are running at any given time. However, a Deployment is a higher-level concept that manages replica sets and provides declarative updates to pods along with a lot of other useful features. Therefore, we recommend using Deployments instead of directly using Replica Sets, unless you require custom update orchestration or don’t require updates at all.


apiVersion: extensions/v1beta1
kind: ReplicaSet
  name: frontend
  # these labels can be applied automatically
  # from the labels in the pod template if not set
  # labels:
    # app: guestbook
    # tier: frontend
  # this replicas value is default
  # modify it according to your case
  replicas: 3
  # selector can be applied automatically
  # from the labels in the pod template if not set,
  # but we are specifying the selector here to
  # demonstrate its usage.
      tier: frontend
      - {key: tier, operator: In, values: [frontend]}
        app: guestbook
        tier: frontend
      - name: php-redis
            cpu: 100m
            memory: 100Mi
        - 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
        - containerPort: 80

Saving this config into frontend.yaml and submitting it to a Kubernetes cluster should create the defined replica set and the pods that it manages.

$ kubectl create -f frontend.yaml 
replicaset "frontend" created
$ kubectl describe rs/frontend
Name:		frontend
Namespace:	default
Selector:	tier=frontend,tier in (frontend)
Labels:		app=guestbook,tier=frontend
Replicas:	3 current / 3 desired
Pods Status:	3 Running / 0 Waiting / 0 Succeeded / 0 Failed
No volumes.
  FirstSeen	LastSeen	Count	From				SubobjectPath	Type		Reason			Message
  ---------	--------	-----	----				-------------	--------	------			-------
  1m		1m		1	{replicaset-controller }			Normal		SuccessfulCreate	Created pod: frontend-qhloh
  1m		1m		1	{replicaset-controller }			Normal		SuccessfulCreate	Created pod: frontend-dnjpy
  1m		1m		1	{replicaset-controller }			Normal		SuccessfulCreate	Created pod: frontend-9si5l
$ kubectl get pods
frontend-9si5l   1/1       Running   0          1m
frontend-dnjpy   1/1       Running   0          1m
frontend-qhloh   1/1       Running   0          1m

Replica Set as an Horizontal Pod Autoscaler target

A Replica Set can also be a target for Horizontal Pod Autoscalers (HPA), i.e. a Replica Set can be auto-scaled by an HPA. Here is an example HPA targeting the Replica Set we created in the previous example.

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
  name: frontend-scaler
    kind: ReplicaSet
    name: frontend
    subresource: scale
  minReplicas: 3
  maxReplicas: 10
    targetPercentage: 50

Saving this config into hpa-rs.yaml and submitting it to a Kubernetes cluster should create the defined HPA that autoscales the target Replica Set depending on the CPU usage of the replicated pods.

kubectl create -f hpa-rs.yaml

Alternatively, you can just use the kubectl autoscale command to acommplish the same (and it’s easier!)

kubectl autoscale rs frontend