Skip to content

ReplicaSet

A ReplicaSet is a Kubernetes API object used to maintain a stable set of replica Pods running at any given time^[Kubernetes基本概念.md]. It is fundamentally a combination of a defined number of replicas and a Pod template^[Kubernetes基本概念.md].

Structure

The definition of a ReplicaSet consists of three main components^[Kubernetes基本概念.md]:

  • Replica Count: The desired number of Pods (e.g., spec.replicas: 3).
  • Selector: A label selector that identifies which Pods fall under the ReplicaSet's scope (e.g., matching the label app: nginx).
  • Pod Template: A template that defines the Pod specification to be run when the number of replicas does not match the desired state.

Usage

Horizontal Scaling

Horizontal scaling, or expansion and contraction, is achieved by simply modifying the replicas value in the ReplicaSet specification^[Kubernetes基本概念.md]. For instance, changing the value from 3 to 4 scales the application up, while changing it from 4 to 3 scales it down^[Kubernetes基本概念.md].

Rolling Updates

The ReplicaSet is the underlying mechanism that enables "rolling updates" for Kubernetes Deployments^[Kubernetes基本概念.md]. When a Deployment's Pod template is updated (e.g., changing a container image), the system manages the transition by creating a new ReplicaSet with the updated template and progressively scaling the old ReplicaSet down while scaling the new one up^[Kubernetes基本概念.md].

Sources

  • Kubernetes基本概念.md