Directory Structure
Assuming your project root is namedKubernetes-for-Beginners, the layout might look like this:
1. Defining the ReplicaSet
Create the filereplicasets/replicaset.yaml:
The
selector.matchLabels field must exactly match the labels under template.metadata.labels. The labels in metadata.labels on the ReplicaSet itself are not used for Pod selection.pods/nginx.yaml:
2. Deploying the ReplicaSet
From the project root, apply the ReplicaSet manifest:3. ReplicaSet Self-Healing
To observe self-healing, delete one of the Pods:4. Preventing Extra Pods
If you manually create a Pod matching the ReplicaSet’s selector, the controller will delete it to maintain the desired count.The ReplicaSet controller enforces the desired replica count by deleting any excess Pods that match its selector.
5. Scaling the ReplicaSet
5.1 Using kubectl edit
spec.replicas field and adjust it: