ReplicaSet: Kubernetes' Pod Thermostat

A ReplicaSet is Kubernetes' thermostat for pods, ensuring a specific number of replicas are always running. It replaces crashed pods or removes excess ones to maintain a stable state.
WHY IT EXISTS ReplicaSets exist to solve the fundamental problem of service availability. Without an automated controller, if a pod running your application crashes, it's gone for good until a human intervenes. A ReplicaSet automates this basic self-healing, ensuring a predictable number of instances are always available to serve traffic.
THE MENTAL MODEL Think of a ReplicaSet as a thermostat for your pods. You set a desired state, like "I need 3 pods running at all times." The ReplicaSet controller continuously monitors the cluster. If the pod count drops to 2, it "heats up" by creating a new one. If it sees 4, it "cools down" by terminating one, always working to match the current state to your desired state.
HOW IT WORKS A ReplicaSet definition has three key fields in its spec: replicas (the desired number), a selector (which defines which pods it owns based on their labels), and a template (a blueprint for creating new pods). The Kubernetes control plane runs a loop that compares the count of pods matching the selector against the replicas field and creates or deletes pods to reconcile any difference.
WHEN TO USE IT The only modern, recommended use for a ReplicaSet is to be managed by a higher-level object, almost always a Deployment. A Deployment orchestrates updates by creating and managing ReplicaSets for you. You declare the desired state in the Deployment, and it handles the ReplicaSet lifecycle.
WHEN NOT TO USE IT Do not create and manage ReplicaSets directly for your applications. This is a legacy pattern. If you update the pod template within a standalone ReplicaSet, it will not update your existing pods. You lose the crucial ability to perform rolling updates, which is a primary feature of cloud-native systems. For that, you need a Deployment.
ONE CANONICAL EXAMPLE When you create a Deployment for 3 nginx pods, the Deployment controller creates a ReplicaSet with replicas: 3. If you then update the Deployment to use a new nginx image version, the Deployment creates a new ReplicaSet with the new template and scales it up to 3, while simultaneously scaling the old ReplicaSet down to 0. This is how rolling updates are achieved.
Read the original → kubernetes.io
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.