0
0
Kubernetesdevops~10 mins

ReplicaSet definition in Kubernetes - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - ReplicaSet definition
Write ReplicaSet YAML
kubectl apply -f
API Server receives request
ReplicaSet Controller checks desired replicas
Create/Delete Pods to match replicas
Pods running as per ReplicaSet spec
Monitor and maintain pod count
The flow shows how a ReplicaSet YAML is applied, the controller ensures the desired number of pods run, creating or deleting pods as needed.
Execution Sample
Kubernetes
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
This YAML defines a ReplicaSet named 'my-rs' that ensures 3 pods with label 'app: myapp' running nginx container.
Process Table
StepActionEvaluationResult
1kubectl apply -f replicaset.yamlYAML parsed and sent to API ServerReplicaSet object created in cluster
2ReplicaSet Controller reads ReplicaSet specDesired replicas = 3Checks current pods with label 'app: myapp'
3Current pods found = 00 < 3Create 3 pods matching template
4Pods start runningPods Ready status checked3 pods running and ready
5Controller monitors podsIf pod deleted or failsCreates replacement pod to maintain 3
6User deletes one pod manuallyPods now 2Controller creates 1 new pod to restore 3
7User updates replicas to 5Desired replicas = 5Controller creates 2 more pods
8Pods running = 5Desired replicas metStable state maintained
9User deletes ReplicaSetReplicaSet removedPods controlled by ReplicaSet are deleted
10No ReplicaSet presentNo controller managing podsPods remain or are deleted depending on owner references
💡 ReplicaSet ensures pod count matches desired replicas; stops when ReplicaSet is deleted.
Status Tracker
VariableStartAfter Step 3After Step 6After Step 7Final
desired_replicas33355
current_pods03245
ReplicaSet_existsYesYesYesYesNo
Key Moments - 3 Insights
Why does the controller create pods when current pods are less than desired replicas?
Because the ReplicaSet controller constantly compares current pods with desired replicas (see execution_table step 3). If current pods are fewer, it creates new pods to match the desired count.
What happens if a pod managed by ReplicaSet is deleted manually?
The controller detects the missing pod and creates a new one to maintain the desired replicas (see execution_table step 6).
What occurs when the ReplicaSet is deleted?
Deleting the ReplicaSet removes its control over pods, which usually leads to deletion of those pods as well (see execution_table step 9 and 10).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the controller create pods for the first time?
AStep 3
BStep 5
CStep 6
DStep 7
💡 Hint
Check the 'Action' and 'Result' columns in step 3 where pods are created because current pods are less than desired.
According to the variable tracker, what is the number of current pods after step 6?
A3
B2
C4
D5
💡 Hint
Look at the 'current_pods' row under 'After Step 6' column.
If the desired replicas were changed to 4 instead of 5 at step 7, how would the current pods count change after step 7?
AIt would be 3
BIt would be 4
CIt would be 5
DIt would remain 2
💡 Hint
The controller creates pods to match desired replicas; see variable_tracker and execution_table step 7.
Concept Snapshot
ReplicaSet YAML defines desired pod count and selector.
Apply YAML with kubectl to create ReplicaSet.
Controller ensures pods match desired replicas.
Creates or deletes pods automatically.
Deleting ReplicaSet removes pod management.
Full Transcript
A ReplicaSet in Kubernetes is defined by a YAML file specifying the desired number of pod replicas and a selector to identify pods it manages. When you apply this YAML using kubectl, the ReplicaSet object is created in the cluster. The ReplicaSet controller continuously monitors the current number of pods matching the selector. If fewer pods exist than desired, it creates new pods using the pod template. If pods are deleted or fail, the controller replaces them to maintain the desired count. Changing the replicas count in the ReplicaSet spec causes the controller to add or remove pods accordingly. When the ReplicaSet is deleted, it stops managing pods, which are usually deleted as well. This process ensures your application always has the right number of pods running.