Ensuring high availability, auto-scaling, and managing updates for your Pods.
A ReplicaSet in Kubernetes is a controller that ensures a specified number of pod replicas are running at any given time. It is used to maintain a stable set of replica Pods running in the cluster, even if some Pods fail or are deleted.
The core job of a ReplicaSet is to constantly monitor the cluster and compare the desired state (e.g., "I want 3 Pods running") with the actual state. If a Pod crashes and the count drops to 2, the ReplicaSet immediately starts a new one to bring the count back to 3. Conversely, if there are somehow 4 Pods running (perhaps due to manual intervention or a network partition healing), the ReplicaSet will terminate the excess Pod to ensure exactly 3 are running.
Let's not worry about deployments yet. Let's just create a replicaset that starts 3 pods. Notice that we do not need to write a separate manifest to start the Pods! The ReplicaSet manifest contains a template section that defines exactly how to create the Pods it manages.
rs.ymlapiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80ReplicaSet ensures a fixed number of Pods are always running.
app=nginx.app=nginx.ReplicaSet watches labels, not image or name.
It keeps the number of Pods with label app=nginx equal to replicas.
kubectl apply -f rs.yml$ -f stands for the filename flag
kubectl get rskubectl get podskubectl delete pod nginx-replicaset-7zp2v
kubectl get podsapp=nginxkubectl run nginx-pod --image=nginx --labels="app=nginx"rs already has 3 podsIf we want to increase the number of pods from 3 to 5, we don't need to run messy commands to start new containers manually. All we have to do is edit the rs.yml file and change the replica count:
replicas: 5Then, simply run the apply command again (kubectl apply -f rs.yml). The ReplicaSet will automatically detect the desired state has changed to 5, calculate that it needs 2 more pods, and start the rest of the 2 pods for us!
kubectl delete rs nginx-replicasetNote the naming convention of the pods. The pods are named after the replicaset followed by a unique id (for eg nginx-replicaset-vj42z).
A Deployment in Kubernetes is a higher-level abstraction that manages a set of Pods and provides declarative updates to them. It offers features like scaling, rolling updates, and rollback capabilities, making it easier to manage the lifecycle of applications.
Even though the ReplicaSet (RS) is what does Pod management, the Deployment is what does ReplicaSet management.
A Deployment ensures that there is a smooth rollout. If the new image fails for some reason, the Deployment ensures the old ReplicaSet is maintained, preventing downtime and allowing for easy rollbacks.
When you update a Deployment (e.g., by changing the container image from v1 to v2), it doesn't just kill all existing Pods and start new ones simultaneously. That would cause downtime. Instead, a Deployment performs a Rolling Update.
It creates a brand new ReplicaSet for the new version and gradually scales it up, while simultaneously scaling down the old ReplicaSet. This ensures that new Pods are ready to serve traffic before the old ones are terminated. If there is any issue or bug in the new code, the old Pods will keep running, preventing downtime for the application, and we can easily rollback the latest change to fix the issue.
If there happens to be an issue or bug in your new code, your old Pods will keep running until the new ones are fully healthy, ensuring no downtime for your application. If a deployment fails, or if you discover a bug after rolling out, you can easily rollback to the previous stable change to fix the issue on your own time.
| Feature | ReplicaSet | Deployment |
|---|---|---|
| Primary Goal | Ensures a specific number of Pod replicas are running. | Manages ReplicaSets and provides declarative updates for Pods. |
| Use Case | Rarely used directly in modern Kubernetes environments. | The standard, recommended way to deploy stateless applications. |
| Updates / Rollouts | No built-in support for rolling updates. Old pods must be deleted manually for new template changes to apply. | Fully supports rolling updates with zero downtime. Gradually replaces old Pods with new ones. |
| Rollbacks | Not supported natively. | Maintains rollout history, allowing instant rollbacks to previous stable versions. |
| Abstraction Level | Directly governs Pods. | Governs ReplicaSets (which in turn govern Pods). |
Lets create a deployment that starts 3 pods
deployment.ymlapiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80kubectl apply -f deployment.ymlkubectl get deploymentkubectl get rskubectl get podkubectl delete pod nginx-deployment-576c6b7b6-b6kgkkubectl get pods