0
0
Kubernetesdevops~5 mins

ReplicaSet definition in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
A ReplicaSet in Kubernetes makes sure a specific number of copies of a pod are running at all times. It helps keep your app available by replacing pods if they fail or get deleted.
When you want to keep a fixed number of identical pods running to handle user requests.
When you need to automatically replace pods that crash or are deleted.
When you want to scale the number of pods up or down easily.
When you want to manage pods without manually restarting them.
When you want to ensure high availability of your application.
Config File - replicaset.yaml
replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: example-replicaset
  labels:
    app: example-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: example-app
  template:
    metadata:
      labels:
        app: example-app
    spec:
      containers:
      - name: example-container
        image: nginx:1.23.3
        ports:
        - containerPort: 80

apiVersion: Specifies the Kubernetes API version.

kind: Defines this resource as a ReplicaSet.

metadata: Contains the name and labels to identify the ReplicaSet.

spec.replicas: Number of pod copies to run.

spec.selector: Matches pods with these labels to manage.

spec.template: Defines the pod template used to create pods, including container image and ports.

Commands
This command creates the ReplicaSet in the Kubernetes cluster using the configuration file. It tells Kubernetes to start managing 3 pods with the specified settings.
Terminal
kubectl apply -f replicaset.yaml
Expected OutputExpected
replicaset.apps/example-replicaset created
This command lists all ReplicaSets in the current namespace to verify that the ReplicaSet was created and shows how many pods are ready.
Terminal
kubectl get replicasets
Expected OutputExpected
NAME DESIRED CURRENT READY AGE example-replicaset 3 3 3 10s
This command lists all pods with the label app=example-app to check that the ReplicaSet created the pods correctly.
Terminal
kubectl get pods -l app=example-app
Expected OutputExpected
NAME READY STATUS RESTARTS AGE example-replicaset-abc123 1/1 Running 0 15s example-replicaset-def456 1/1 Running 0 15s example-replicaset-ghi789 1/1 Running 0 15s
-l - Filter pods by label
This command shows detailed information about the ReplicaSet, including events and pod status, to understand how it is managing pods.
Terminal
kubectl describe replicaset example-replicaset
Expected OutputExpected
Name: example-replicaset Namespace: default Selector: app=example-app Labels: app=example-app Replicas: 3 current / 3 desired Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 1m replicaset-controller Created pod: example-replicaset-abc123 Normal SuccessfulCreate 1m replicaset-controller Created pod: example-replicaset-def456 Normal SuccessfulCreate 1m replicaset-controller Created pod: example-replicaset-ghi789
Key Concept

If you remember nothing else from this pattern, remember: a ReplicaSet keeps the exact number of pod copies running by creating or deleting pods automatically.

Common Mistakes
Not matching the selector labels with the pod template labels
The ReplicaSet won't manage any pods if the selector does not match the pod labels, so no pods will be created or controlled.
Ensure the selector.matchLabels exactly matches the labels in spec.template.metadata.labels.
Setting replicas to zero unintentionally
This causes the ReplicaSet to delete all pods, making the app unavailable.
Set replicas to the desired number of pods you want running, usually at least 1.
Summary
Create a ReplicaSet with a YAML file defining the desired number of pod copies and pod template.
Apply the ReplicaSet configuration using kubectl apply to start managing pods.
Verify the ReplicaSet and pods with kubectl get and kubectl describe commands.
Ensure selector labels match pod labels so the ReplicaSet can manage pods correctly.