Replication Controller vs ReplicaSets vs Deployments in k8s (Kubernetes)
Replication Controller
In kubernetes, Replication Controller is an older concept that has already been replaced by Replica Sets and Deployments. However, it's good to understand its features and how they relate to managing pods. It can be created easily using json or yaml manifest files or by using kubectl
commands. It is a core component in kubernetes that is responsible for maintaining a stable set of replicas of pods at any given time. it makes sure that a specified number of pods are always running within a cluster. If any pod dies or is deleted, the ReplicationController automatically creates a new pod to replace it, thereby maintaining the desired state.
Key Features of a ReplicationController:
Replication: The main function of ReplicationController is to make sure that a specified number of pod are running all the time. It is mentioned in the configuration file.
Self-healing: If the pod dies or gets deleted, It heals itself and creates a new one and makes sure the desired number of pods are always running.
Scaling: The desired number of pods can be scaled up (increased) or scaled down (decreased) at any time.
Pod template: The replication controller uses a pod template to configure the pods it manages. It includes labels, containers, and some other things as well. All pods created by the ReplicationController will match this template.
Label selector: ReplicationControllers used a concept of labels and selectors to identify the pods it manages. Only pods that matches the labels get selected.
Rolling updates: While limited compared to modern tools like Replica Sets and Deployments, a ReplicationController can facilitate rolling updates by gradually replacing old pods with new ones based on the updated pod template.
How does it work:
When a ReplicationController is created, you provide a template and specify the desired number of replicas.
Then the controller makes sure that the desired number of pods are running all the time.
If the number of running pods drops below the desired number, then the new pods are created to meet the difference.
If more pods are detected than the desired count, then they are terminated.
Let's create an example ReplicationController using:
- Imperative way (using
kubectl
commands):
kubectl create rc nginx-controller --image=nginx:latest --replicas=3 --port=80
- Declarative way (using manifest file):
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-controller
spec:
replicas: 3
selector:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
ReplicaSets
It is also a resource that ensure desired number of pods are always running, similar to ReplicationController but with more advanced features. Just like ReplicationController it can be created using json or yaml manifest files or using kubectl
commands.
Key Features of a ReplicaSet:
Replication: Just like ReplicationController, it makes sure that a specified number of desired replicas of pods are always running creating new pods when needed and deleting excess pods.
Scaling: ReplicaSets can be manually be scaled up (increased) or scaled down (decreased) using
kubectl
commands if needed or it can also be achieved by making the changes to the configuration files.Self-healing: If the pods that are running are less than the desired count (dead or deleted), the new pods are created. If the number of pods that are running are more than the desired count, they are deleted.
Pod template: A ReplicaSet uses a pod template to define the configuration of the pods it manages. This template includes details like container images, labels, and resource requests/limits. All pods created by the ReplicaSet will adhere to this template.
Label selector: ReplicaSets use label selectors to manage pods. The selector defines which pods the ReplicaSet should manage based on their labels. This feature allows the ReplicaSet to control a specific set of pods within the cluster.
Adption of separately created pods: One feature that distinguishes ReplicaSets from ReplicationControllers is their ability to adopt existing pods that match their label selector. This is useful when you want to bring existing pods under the management of a new ReplicaSets.
Rolling Updates (via Deployments): Although ReplicaSets themselves do not directly manage rolling updates, they are often managed by Deployments, which do. Deployments allow for seamless rolling updates by creating new ReplicaSets with updated pod templates, and then gradually phasing out the old ReplicaSets.
Integration with Deployments: ReplicaSets are commonly used in conjunction with Deployments. A Deployment automatically creates and manages ReplicaSets, making it easier to perform updates, rollbacks, and scaling operations. While you can use a ReplicaSet directly, using Deployments is typically more flexible and recommended for most use cases.
How does it work:
A ReplicaSet is defined with a pod template and a specified number of replicas.
The ReplicaSet controller continuously monitors the cluster to ensure the correct number of pods is running.
If the number of running pods drops below the desired count, the ReplicaSet creates new pods.
If there are more pods than desired, the ReplicaSet deletes the excess pods.
ReplicaSets can adopt orphaned pods if their labels match the selector, allowing for flexible management of pods.
Let's create an example ReplicaSet using:
- Imperative way (using
kubectl
commands):
kubectl create rs nginx-replicaset --image=nginx:latest --replicas=3 --port=80 --dry-run=client -o yaml | kubectl apply -f -
- Declarative way (using manifest file):
apiVersion: 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: 80
Deployments:
A deployment in Kubernetes is a higher-level of abstraction that manages a group of pods and the underlying ReplicaSets to ensure the desired state of an application. Deployments are the preferred way to manage stateless applications in Kubernetes, offering advanced features for rolling updates, rollbacks, and scaling.
Key Features of a Deployment:
Declarative Updates: Deployments allow you to declare the desired state of your application, including the number of replicas, the container images to use, and other configurations. Kubernetes takes care of transitioning from the current state to the desired state.
Rolling Updates: One of the most powerful features of a Deployment is its ability to perform rolling updates. This means that when you update your application (e.g., changing the container image), the Deployment will gradually replace the old pods with new ones, ensuring zero or minimal downtime.
Rollbacks: If something goes wrong during an update, Deployments allow you to easily roll back to a previous stable version. This is done by reverting to an earlier ReplicaSet that the Deployment manages.
Scaling: Deployments allow you to scale the number of replicas up or down easily. This can be done manually or automatically through Horizontal Pod Autoscalers, which adjust the number of replicas based on metrics like CPU or memory usage.
Self-healing: Like ReplicaSets and ReplicationControllers, Deployments ensure that the specified number of replicas are running at all times. If a pod fails, the Deployment controller will automatically replace it.
Pod Template: The Deployment specifies a pod template that defines the configuration of the pods it manages, including container images, labels, resource requests/limits, and environment variables. All pods created by the Deployment will match this template.
Multiple ReplicaSets: While a Deployment typically manages a single active ReplicaSet, it can manage multiple ReplicaSets during the course of updates. Older ReplicaSets are retained to facilitate rollbacks.
History and Versioning: Deployments keep track of the versions of your application. Each update to the Deployment creates a new ReplicaSet, allowing Kubernetes to maintain a history of changes and enabling rollbacks if needed.
Paused Deployments: You can pause a Deployment during an update, which allows you to make changes to the Deployment’s configuration without triggering a new rollout. Once ready, you can resume the Deployment to apply all changes at once.
Blue-Green and Canary Deployments: While these are not out-of-the-box features, Deployments can be used to implement advanced deployment strategies like blue-green or canary deployments, often with the help of additional Kubernetes resources or third-party tools.
How does it work:
Create/Update Deployment: When you create or update a Deployment, you specify the desired state, including the number of replicas and the pod template.
Deployment Controller: The Deployment controller continuously monitors the state of the Deployment. If the actual state differs from the desired state, the controller takes action to bring them into alignment.
Rolling Updates: During an update, the Deployment gradually creates new pods with the updated configuration while terminating the old pods. This process is controlled by parameters like
maxUnavailable
andmaxSurge
to ensure service availability.Rollbacks: If an update fails or causes issues, you can rollback to a previous state with a single command. The Deployment will then revert to the last known good ReplicaSet.
Use Cases of Deployments:
Application updates: Deployments are ideal for updating applications with minimal disruption. Rolling updates and rollbacks ensure that you can update your app safely and revert changes if needed.
Scaling Applications: Deployments make it easy to scale your application by increasing or decreasing the number of replicas.
Continuous Delivery: Deployments fit well into CI/CD pipelines, allowing automated updates to be rolled out safely.
Let's create an example Deployment using:
- Imperative way (using
kubectl
commands:
kubectl create deployment nginx-deployment --image=nginx:latest --replicas=3 --port=80
- Declarative way (using manifest file):
apiVersion: 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: 80
Summary:
ReplicationController is the most basic form of replication and scaling in Kubernetes but is largely deprecated in favor of ReplicaSets and Deployments.
ReplicaSet improves upon the ReplicationController by adding features like the adoption of orphaned pods but is typically not used on its own. It is the foundation for managing pods, but it's mostly managed by a Deployment.
Deployment is the most powerful and flexible resource, offering advanced features like rolling updates, rollbacks, and version management. It is the preferred choice for managing applications in modern Kubernetes environments.
Conclusion:
For modern Kubernetes workflows, Deployments are the go-to resource as they offer the most comprehensive features for managing application lifecycles. ReplicaSets serve as a key component within Deployments, while ReplicationControllers are generally considered obsolete.
Subscribe to my newsletter
Read articles from Syed Mahmood Ali directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by