In Kubernetes, both Deployments and ReplicaSets are pivotal concepts that help manage the lifecycle of your applications by ensuring that a specified number of pod replicas are running at any given time. Understanding the differences between a Deployment and a ReplicaSet is crucial for effective Kubernetes management.
ReplicaSet – maintain a stable set of replica Pods running at any given time
A ReplicaSet’s primary purpose is to maintain a stable set of replica Pods running at any given time. It is often used to guarantee the availability of a specified number of identical Pods.
- Key Characteristics:
- Ensures that a specified number of pod replicas are running.
- Offers a basic mechanism for pod replication and scaling.
- Does not support rolling updates; any updates to the pod template require manual intervention or deletion of existing pods to adopt the new template.
- Use Case:
- When you need to ensure a specific number of pods are always running and are not concerned with rolling updates or rollback capabilities.
Deployment – manage the creation and scaling of ReplicaSets and provide the ability to roll out updates
A Deployment provides declarative updates for Pods and ReplicaSets. You describe a desired state in a Deployment, and the Deployment controller changes the actual state to the desired state at a controlled rate.
Deployments manage the creation and scaling of ReplicaSets and provide the ability to roll out updates and rollbacks, making them more versatile than ReplicaSets.
Get Your Free Linux training!
Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!- Key Characteristics:
- Manages the deployment and scaling of a set of Pods and is backed by a ReplicaSet.
- Supports declarative updates for Pods and ReplicaSets, allowing for easy scaling, updates, and rollbacks.
- Enables you to describe the desired state of your application, and the Deployment controller works to maintain this state.
- Facilitates rolling updates to Pods without downtime by incrementally updating pod instances with new ones.
- Use Case:
- Ideal for most application deployment scenarios where you want to maintain high availability, perform rolling updates, and have rollback capabilities.
To further elucidate the differences between Deployments and ReplicaSets in Kubernetes, let’s explore some practical examples and relevant kubectl commands that demonstrate how to work with each resource.
ReplicaSet Example in Kubernetes
Here’s a simple example of a ReplicaSet definition that ensures three replicas of an nginx pod are running:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: example-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
To create this ReplicaSet, save the above YAML to a file named example-replicaset.yaml
and run:
kubectl create -f example-replicaset.yaml
To list ReplicaSets:
root@controlplane:~$ kubectl get rs
NAME DESIRED CURRENT READY AGE
example-replicaset 3 3 3 84s
#kubectl get all
NAME READY STATUS RESTARTS AGE
pod/example-replicaset-2bmv7 1/1 Running 0 10s
pod/example-replicaset-jd24q 1/1 Running 0 10s
pod/example-replicaset-qzhcz 1/1 Running 0 10s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 443/TCP 3m35s
NAME DESIRED CURRENT READY AGE
replicaset.apps/example-replicaset 3 3 3 10s
To scale the ReplicaSet:
kubectl scale rs example-replicaset --replicas=5
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/example-replicaset-2bmv7 1/1 Running 0 5m22s
pod/example-replicaset-7tk5n 1/1 Running 0 11s
pod/example-replicaset-jd24q 1/1 Running 0 5m22s
pod/example-replicaset-qzhcz 1/1 Running 0 5m22s
pod/example-replicaset-tr9rj 1/1 Running 0 11s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 443/TCP 8m47s
NAME DESIRED CURRENT READY AGE
replicaset.apps/example-replicaset 5 5 5 5m22s
To delete the ReplicaSet:
kubectl delete rs example-replicaset
Deployment Example in Kubernetes
Here’s an example of a Deployment that uses a ReplicaSet behind the scenes to manage three replicas of an nginx pod. It also allows for rolling updates:
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
To create this Deployment, save the above YAML to a file named example-deployment.yaml
and run:
kubectl create -f example-deployment.yaml
To list Deployments:
kubectl get deployments
root@controlplane:~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
example-deployment-65d99844c9-5xzkd 1/1 Running 0 5s
example-deployment-65d99844c9-9sg5h 1/1 Running 0 7s
example-deployment-65d99844c9-sm5g6 1/1 Running 0 9s
To update the Deployment (for example, to update the nginx image version):
kubectl set image deployment/example-deployment nginx=nginx:1.16.1
This command updates the nginx container image to version 1.16.1, triggering a rolling update.
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 101s default-scheduler Successfully assigned default/example-deployment-65d99844c9-zg9ks to node01
Normal Pulling 101s kubelet Pulling image "nginx:1.16.1"
Normal Pulled 95s kubelet Successfully pulled image "nginx:1.16.1" in 5.379s (5.379s including waiting)
Normal Created 95s kubelet Created container nginx
Normal Started 95s kubelet Started container nginx
To scale the Deployment:
kubectl scale deployment example-deployment --replicas=5
root@controlplane:~$ kubectl get pods
NAME READY STATUS RESTARTS AGE
example-deployment-65d99844c9-4s6nn 1/1 Running 0 4s
example-deployment-65d99844c9-5xzkd 1/1 Running 0 5m2s
example-deployment-65d99844c9-9sg5h 1/1 Running 0 5m4s
example-deployment-65d99844c9-mrzpl 1/1 Running 0 4s
example-deployment-65d99844c9-sm5g6 1/1 Running 0 5m6s
To undo the last Deployment (rollback):
kubectl rollout undo deployment example-deployment
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 116s default-scheduler Successfully assigned default/example-deployment-7c79c4bf97-4bsfj to node01
Normal Pulling 115s kubelet Pulling image "nginx:latest"
Normal Pulled 113s kubelet Successfully pulled image "nginx:latest" in 859ms (1.785s including waiting)
Normal Created 113s kubelet Created container nginx
Normal Started 113s kubelet Started container nginx
To delete the Deployment:
kubectl delete deployment example-deployment
Key Takeaways
- ReplicaSets are great for ensuring a specific number of pod replicas are running but lack the ability to handle rolling updates and rollbacks.
- Deployments manage ReplicaSets and provide additional features like rolling updates, rollbacks, and scaling, making them more suited for most application deployment scenarios.
- Using kubectl commands, you can manage the lifecycle of both Deployments and ReplicaSets, including creation, updates, scaling, and deletion.
These examples and commands showcase the fundamental differences in managing pods with Deployments and ReplicaSets in Kubernetes, highlighting the scenarios where each is most beneficial.