Kubernetes: ReplicaSets

Claire Lee
4 min readMar 1, 2023

--

In Kubernetes, a ReplicaSet is responsible for maintaining the desired number of replicas of a pod to ensure high availability and load balancing in handling traffic. This is achieved by creating or removing pod replicas as necessary. In the ReplicaSet YAML file, the labels specified in spec.selector.matchLabels field and spec.template.metadata.labels should match to ensure proper management of the pod replicas. Mismatched label sets in both fields may cause errors, leading to unexpected behavior and functionality issues in the ReplicaSet

Kubernetes: ReplicaSets

Analogy Kubernetes to Restaurant Chef Overview

Kubernetes: Understanding Kubernetes Architecture through a Restaurant Chef’s Analogy

Analogy Kubernetes to Restaurant Chef

ReplicaSets(Line cooks)

ReplicaSet in Kubernetes is like a team of line cooks in a restaurant.

ReplicaSets vs. Line Cooks

1. High availability

ReplicaSet provides high availability to our application by ensuring that a specific number of identical replicas of a Pod are always running . If a Pod fails, ReplicaSet will automatically replace it with a new one to maintain the desired number of replicas.

In a restaurant, line cooks work together to prepare multiple orders of the same dish to ensure all customers are served in a timely manner. If a customer returns a dish, line cooks can quickly re-cook and replace it.

2. Load balancing

Web traffic can vary for our application. ReplicaSet can create or remove Pods as needed to ensure that our application can handle any amount of traffic.

In a busy restaurant, the number of orders can fluctuate throughout the day. Line cooks can adjust the amount of ingredients based on the number of orders.

Short Name: rs

$ kubectl api-resources
NAME SHORTNAMES APIVERSION NAMESPACED KIND
replicasets rs apps/v1 true ReplicaSet

ReplicaSet with YAML

  • .spec.selector.matchLabels field: specify a set of labels that are used to identify the Pods controlled by the ReplicaSet.
  • .spec.template field: define the configuration of new Pods created by the ReplicaSet.

It’s crucial to ensure that the labels specified in the .spec.selector.matchLabels field match those in the spec.template.metadata.labels field of the Pod template. This is because when the ReplicaSet creates new Pods, it adds the labels from the spec.template.metadata.labels field to them. The spec.selector.matchLabels field then ensures that only the Pods with the matching labels are controlled by the ReplicaSet. This is an essential mechanism for managing and scaling the Pods in a Kubernetes cluster.

ReplicaSet with YAML

pod.yaml

apiVersion: v1
kind: Pod
metadata:
name: <pod_name>
labels:
<key1>: <value1>
<key2>: <value2>
:
:
<keyN>: <valueN>
spec:
containers:
- name: <container1_name>
image: <image>
- name: <container2_name>
image: <image>

replicaSet.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: <replicaSet_name>
labels:
<key1>: <value1>
<key2>: <value2>
:
:
<keyM>: <valueM>
spec:
replicas: <number_of_replicas>
selector:
matchLabels:
<key1>: <value1>
<key2>: <value2>
:
:
<keyN>: <valueN>
template:
# The pod template start here
metadata:
name: <pod_name>
labels:
<key1>: <value1>
<key2>: <value2>
:
:
<keyN>: <valueN>
spec:
containers:
- name: <container1_name>
image: <image>
- name: <container2_name>
image: <image>
# The pod template ends here

Commands

replicaSet commands
  1. Retrieve information about the ReplicaSet API object
$ kubectl explain replicaset

analogy: Ask the head chef for information about the team of line cooks

2. Create a ReplicaSet based on the configuration file

$ kubectl create -f <replicaSet_file_name>.yaml

analogy: Head chef instructs the team of line cooks to prepare a certain dish

3. Display information about the ReplicaSet in the current namespace

$ kubectl get replicaset

analogy: Display information of line cooks currently working in the kitchen

4. Delete a specified ReplicaSet

$ kubectl delete replicaset <replicaSet_name>

analogy: Remove a certain line cook from the team

5. Update a specified ReplicaSet based on the configuration file provided

$ kubectl replace -f <replicaSet_file_name>.yaml

analogy: Revise a recipe with updated ingredients

6. Scale the specified ReplicaSet to the desired number of replicas

$ kubectl scale --replicas=<number_of_replicas> -f <replicaSet_file_name>.yaml

analogy: Add or remove line cooks to match the demand for a certain dish

7. Scale the specified ReplicaSet to the desired number of replicas (replicas in replicaSet configuration file is not modified)

$ kubectl scale --replicas=<number_of_replicas> replcaset <replicaSet_name>

analogy: Add or remove line cooks to match the demand for a certain dish

--

--

Claire Lee
Claire Lee

No responses yet