In Kubernetes, rolling updates are a way to update a deployment, stateful set, or daemon set in a controlled and gradual manner. Rolling updates update the Pods in a controlled and gradual manner to minimize downtime and disruptions.
The process of rolling updates in Kubernetes involves the following steps:
Update the container image or configuration: The first step in a rolling update is to update the container image or configuration that you want to deploy. This can be done by editing the YAML manifest file for the deployment or stateful set.
Create a new replica set: Once the updated configuration is in place, a new replica set is created with the updated configuration. The new replica set runs alongside the old one, allowing for a controlled transition between the old and new versions of the application.
Scale up the new replica set: The next step is to gradually scale up the new replica set while scaling down the old one. This ensures that the application remains available during the update process.
Monitor the update process: Throughout the rolling update process, Kubernetes monitors the status of the Pods in both the old and new replica sets. If any of the new Pods fail to start or the old Pods fail to terminate, Kubernetes will pause the rolling update and roll back to the previous version of the application.
Complete the update: Once all of the new Pods are running and the old Pods have been terminated, the rolling update is complete and the new version of the application is running.
Advantages of rolling updates in Kubernetes:
Minimize downtime: Rolling updates allow for a controlled and gradual transition between the old and new versions of the application, minimizing downtime and disruptions.
Maintain availability: By running the old and new versions of the application side-by-side, rolling updates ensure that the application remains available during the update process.
Rollback if necessary: Kubernetes monitors the update process and can automatically roll back to the previous version of the application if any issues arise.
Scalability: Rolling updates can be used to scale up or down the number of replicas of the application, allowing for easy horizontal scaling.
Here’s an example of a rolling update in Kubernetes:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image:v1
ports:
- containerPort: 8080
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
\# Update the image to v2
\# to perform a rolling update
\# on the deployment.
\# Use "kubectl apply" to apply
\# the changes to the deployment.
\# kubectl apply -f deployment.yaml
In this example, the YAML manifest file for the deployment is updated to use a new container image, my-image:v2. When the updated manifest file is applied using kubectl apply -f deployment.yaml, Kubernetes will perform a rolling update by creating a new replica set with the updated configuration and gradually scaling it up while scaling down the old replica set.