Rolling updates in Kubernetes is a way to update the application in our Kubernetes cluster with little downtime. This method slowly changes the app’s instances to new versions. It makes sure that some instances stay available while we update. By using rolling updates, we keep our application running and apply updates smoothly.
In this article, we will look at how to do rolling updates in Kubernetes. We will talk about what rolling updates are, why they matter, how to set up deployments, important settings, how to monitor updates, and what happens during the update. We will also explain how to go back if an update fails, real-life examples, and how to test our rolling update plan. Here’s what we will cover:
- How Can I Perform Rolling Updates in Kubernetes?
- What is a Rolling Update in Kubernetes?
- How to Configure a Deployment for Rolling Updates?
- What are the Key Parameters for Rolling Updates?
- How Do I Monitor Rolling Updates in Kubernetes?
- What Happens During a Rolling Update?
- How to Rollback a Failed Rolling Update?
- What are Real-World Use Cases for Rolling Updates in Kubernetes?
- How to Test Your Rolling Update Strategy?
- Frequently Asked Questions
For more information on Kubernetes, we suggest these articles: What is Kubernetes and How Does it Simplify Container Management?, Why Should I Use Kubernetes for My Applications?, and What are Kubernetes Deployments and How Do I Use Them?. These articles will help us better understand how to deploy and manage apps in Kubernetes.
What is a Rolling Update in Kubernetes?
A rolling update in Kubernetes is a way to update our applications without any downtime. It slowly changes the old versions of the application to new versions. This happens while making sure some parts are always running. This way, we can keep things working smoothly when we switch from old to new versions.
Key Features of Rolling Updates:
- Zero Downtime: Users do not see any downtime when we update.
- Gradual Replacement: We update Pods step by step. This way, part of the application is always available.
- Rollback Ability: If we find a problem, Kubernetes can quickly go back to the last working version.
Example of a Rolling Update Configuration:
In our deployment YAML, we can set a rolling update strategy like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: my-app:latest
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
Parameters Explained:
- maxUnavailable: This is the most Pods that can be down during the update. In our example, only one Pod can be down.
- maxSurge: This is the most Pods that can be added above the number we want during the update. Here, we can add one extra Pod.
For more information about Kubernetes deployments, please check what are Kubernetes deployments and how do I use them.
How to Configure a Deployment for Rolling Updates?
To setup a deployment for rolling updates in Kubernetes, we need to define the deployment strategy in the YAML file. By default, Kubernetes uses the rolling update strategy for deployments. This way, we can update our application without stopping it.
Here is a simple example of a deployment configuration that uses rolling updates:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: my-app-image:latest
ports:
- containerPort: 80
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
Key Elements of the Configuration:
- replicas: This tells how many pod replicas we want to run.
- selector: This helps to find the set of pods that the deployment manages.
- template: This describes the pod details, like the container image and ports.
- strategy: This tells which strategy we use for
updates.
- type: We set this to
RollingUpdate
to use rolling updates. - maxUnavailable: This is the number of pods that can
be not available during the update. For example, it can be
1
. - maxSurge: This is the number of pods that can be
created above the needed number during the update. For example, it can
be
1
.
- type: We set this to
Applying the Configuration:
We can apply the configuration with this command:
kubectl apply -f deployment.yaml
To see the status of our deployment and its updates, we can use:
kubectl rollout status deployment/my-app
When we configure our deployment like this, it helps us to update easily with little downtime. This way, we keep our service running while we add new changes. For more details on Kubernetes deployments, we can check this article.
What are the Key Parameters for Rolling Updates?
In Kubernetes, rolling updates help us update our application without any downtime. We do this by slowly replacing parts of the old version with the new version. There are key parameters that we need to know for rolling updates in a Kubernetes deployment. These include:
maxUnavailable: This parameter tells us the most number of pods that can be not working during the update. We can set it as a specific number or a percentage. For example, if we set it to
1
, at least one pod must always be working.strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 1
maxSurge: This shows the most number of pods that can be made above the expected number of pods during the update. Like before, we can set it as a number or a percentage. For example, if we set it to
25%
, Kubernetes can create extra pods up to 25% of what we want.strategy: type: RollingUpdate rollingUpdate: maxSurge: 25%
progressDeadlineSeconds: This parameter tells us how long to wait in seconds for the update to finish before saying it has failed. It helps make sure our deployment does not get stuck for a long time.
progressDeadlineSeconds: 600
revisionHistoryLimit: This shows how many old Replicasets we keep for a deployment. The default is 10, but we can change it based on how many we need for rolling back.
revisionHistoryLimit: 5
minReadySeconds: This is the least number of seconds a new pod should be ready without any problems to be counted as available. This helps keep the application stable after we put out a new version.
minReadySeconds: 10
We can set these parameters in the deployment YAML file under the
spec
section. Here is an example of a full deployment
configuration that uses these parameters:
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-deployment
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 25%
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
containers:
- name: example-container
image: example-image:latest
By changing these key parameters for rolling updates, we can control how our deployments work. This helps us move between application versions smoothly while reducing downtime and keeping our service running.
How Do We Monitor Rolling Updates in Kubernetes?
Monitoring rolling updates in Kubernetes is important. We need to check that deployments go well and that applications stay available. Here are some simple ways to monitor rolling updates:
kubectl rollout status: This command helps us see the status of the deployment during a rolling update.
kubectl rollout status deployment/<deployment-name>
Events: Kubernetes makes events that show us what is happening with the deployment. We can use this command to see events for the deployment:
kubectl get events --sort-by='.metadata.creationTimestamp'
Pod Health Checks: We must set up readiness and liveness probes for our applications. This way, traffic only goes to pods that are ready. Here is an example YAML setup:
readinessProbe: httpGet: path: /health port: 8080 initialDelaySeconds: 5 periodSeconds: 10
Logs: We can use
kubectl logs
to check the logs of new pods during the update. This helps us find problems quickly.kubectl logs deployment/<deployment-name>
Metrics Server: We should deploy the Kubernetes Metrics Server. It helps us check how much CPU and memory our pods use during the update.
Prometheus and Grafana: We can set up Prometheus for monitoring and Grafana for showing data. We can make dashboards that show metrics like pod availability, response times, and error rates.
Deployment Strategy Configuration: In our deployment setup, we can set
maxSurge
andmaxUnavailable
values. This controls how many pods we can update at once. It helps us balance load and monitor updates.strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 maxUnavailable: 1
Health Checks with Prometheus: We can use Prometheus to get metrics from our application’s health endpoints. This helps us check health during updates.
Visualize with KubeDash or Kiali: We can use tools like KubeDash or Kiali to see a graphical view of the deployment status and health during the update.
These methods will help us monitor rolling updates in Kubernetes well. This way, we can make sure everything goes smoothly and find problems fast. For more details on setting up deployments, we can check this article on Kubernetes deployments.
What Happens During a Rolling Update?
During a rolling update in Kubernetes, the update happens slowly without stopping the application. This keeps the application available while we put in new versions. Here is what happens step by step:
Update Configuration: When we put a new configuration, like a new container image, into a Deployment, Kubernetes starts the rolling update.
Pod Creation: Kubernetes makes new Pods with the updated configuration. The new Pods start while the old Pods are still running.
Readiness Check: Each new Pod needs to pass its readiness checks before we send traffic to it. This makes sure only healthy Pods handle requests.
Scaling Up: The number of replicas goes up for a short time. This way, both old and new Pods run together during the change.
Traffic Shift: After the new Pods are ready, Kubernetes slowly moves traffic from the old Pods to the new Pods.
Scaling Down: As more new Pods are ready, we stop old Pods one by one. This keeps going until all old Pods are replaced.
Completion: The rolling update finishes when all old Pods are gone and all new Pods are ready to take requests.
We can set up the rolling update strategy in our Deployment specification. Here is an example of a Deployment configuration with rolling update settings:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:v2
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
In this configuration, maxUnavailable
means at least 2
Pods should stay available during the update. maxSurge
lets
us create one extra Pod beyond the desired count. This keeps everything
running smoothly with no downtime.
Kubernetes makes it easier for us to handle rolling updates. It helps us deploy changes without problems. For more information on how to set up your Kubernetes environment for good deployments, check out how to deploy a Kubernetes cluster.
How to Rollback a Failed Rolling Update?
In Kubernetes, when a rolling update fails, we can easily go back to
the last stable version of our application. We do this using the
kubectl rollout
command. This command helps us manage the
history of our deployment.
To rollback a failed rolling update, we follow these simple steps:
Check the Deployment History: First, we need to look at the history of our deployment to find the revisions.
kubectl rollout history deployment/<deployment-name>
We replace
<deployment-name>
with the name of our deployment.Rollback to the Previous Revision: Next, we use the
kubectl rollout undo
command to go back to the last successful version.kubectl rollout undo deployment/<deployment-name>
Rollback to a Specific Revision: If we want to rollback to a specific version, we can give the revision number.
kubectl rollout undo deployment/<deployment-name> --to-revision=<revision-number>
Verify Rollback: After we rollback, we need to check the status of our deployment to make sure it went back successfully.
kubectl rollout status deployment/<deployment-name>
Inspect the Current State: We can also describe the deployment to see its current state and check if the rollback was successful.
kubectl describe deployment/<deployment-name>
With these commands, we can easily manage and rollback failed rolling updates in Kubernetes. This way, our applications stay stable and run well. For more about managing Kubernetes deployments, check out what are Kubernetes deployments and how do I use them.
What are Real-World Use Cases for Rolling Updates in Kubernetes?
Rolling updates in Kubernetes is a important way to deploy applications. It helps to keep the application running without any downtime. Here are some real-world examples:
Microservices Architecture: In a microservices setup, we can update services one by one. With rolling updates, our team can deploy updates to some services without affecting the whole application. This is great for our continuous delivery process.
Web Applications: For web applications that need to be always available, rolling updates let us do updates step by step. This lowers the chance of downtime. It makes sure users can always access the application, even when we are updating it.
A/B Testing: We can use rolling updates for A/B testing. This means we can show different versions of an application to different groups of users. It helps us compare performance and user experience before we fully launch a new version.
Feature Flags: When we want to add new features, we can use rolling updates with feature flags. This way, we can control which users see the new features. It allows us to test safely in live environments without affecting everyone at once.
Security Patches: If there is a security problem, rolling updates let us quickly apply patches across all nodes. This keeps our services running. It is very important for compliance and managing risks in enterprise applications.
Gradual Rollout of Major Changes: For applications that are changing a lot, rolling updates allow us to deploy changes slowly. This helps us watch how the changes affect users. We can fix problems early before they affect many users.
Cloud-Native Applications: Many cloud-native applications use Kubernetes. Rolling updates make sure new container images are deployed without downtime. This fits well with the cloud-native ideas of being strong and able to grow.
Legacy Application Modernization: When we modernize old applications, rolling updates let us make changes step by step. Our team can slowly move parts to microservices. This way, the old system can still work while we make changes.
To use these examples well, we need to set up our Kubernetes deployments for rolling updates. Here is a sample deployment configuration:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: my-app-image:latest
This configuration sets a rolling update strategy. It allows one pod to be down during updates. Also, it lets us create one extra pod beyond the number we want. This keeps our application available.
For more information on Kubernetes deployments, you can check what are Kubernetes deployments and how do I use them.
How to Test Your Rolling Update Strategy?
Testing our rolling update strategy in Kubernetes is very important. It helps us make sure our application updates without downtime. Here are some simple steps and methods to test our rolling update strategy:
- Simulate Updates in a Staging Environment:
- Before we update the production, we should make a staging
environment that looks like our production setup. We can deploy our
application and simulate rolling updates.
- We use the same Kubernetes settings, like Deployments and Services.
- Before we update the production, we should make a staging
environment that looks like our production setup. We can deploy our
application and simulate rolling updates.
- Use
kubectl
to Apply Updates:We can update the container image version in our Deployment file. For example:
apiVersion: apps/v1 kind: Deployment metadata: name: my-app spec: replicas: 3 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-app-container image: my-app:2.0 # Updated image version
Then we apply the update using:
kubectl apply -f deployment.yaml
- Monitor the Update Process:
We can use
kubectl rollout status
to see how the rolling update is going:kubectl rollout status deployment/my-app
- Check Application Health:
- We need to make sure that readiness and liveness probes are set up right. This checks our application’s health. It helps to confirm that new pods are ready before we send traffic to them.
- Load Testing:
- We should do load testing while the rolling update happens. This shows us how the application acts under real traffic. We can use tools like JMeter or Locust for this.
- Rollback Testing:
We can check the rollback by deploying a bad image or setting. We need to see if we can quickly go back to the last good version:
kubectl rollout undo deployment/my-app
- Logging and Metrics:
- We should set up logging and monitoring tools like Prometheus and Grafana. They help us collect data during the rolling update. We need to check logs to find errors or performance problems.
- User Acceptance Testing (UAT):
- We can ask users to test the application after the update. This helps us get feedback on how it works and performs.
By following these steps, we can make sure our rolling update strategy is strong and reduces risks when we deploy. For more details about Kubernetes deployments, we can check out Kubernetes Deployments.
Frequently Asked Questions
What is a Rolling Update in Kubernetes?
A rolling update in Kubernetes helps us update applications without stopping them. We can update Pods step by step with new images or settings. Kubernetes makes sure part of our application stays available while we update. This method is important for keeping our service running during updates. That’s why many Kubernetes users like to use rolling updates for managing upgrades.
How do I configure a Deployment for Rolling Updates?
To set up a Kubernetes Deployment for rolling updates, we need to
change the strategy
field in our Deployment YAML file. By
default, Kubernetes uses a RollingUpdate strategy. We can add settings
like maxUnavailable
and maxSurge
to decide how
many Pods we update at the same time. For example:
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
This setup helps us keep problems to a minimum during the rolling update.
How can I monitor Rolling Updates in Kubernetes?
We can monitor rolling updates in Kubernetes using tools like
kubectl
or dashboards like Kubernetes Dashboard or
Prometheus. To check the status of our Deployment, we can run
kubectl rollout status deployment/<deployment-name>
.
This command gives us live updates on how our rolling update is going
and helps us find any problems that might happen during the update.
What happens if a Rolling Update fails?
If a rolling update fails, Kubernetes stops the update to avoid more
problems. We can check the status of our Deployment by using
kubectl get deployment
to see what is happening. If needed,
we can go back to the previous version with the command
kubectl rollout undo deployment/<deployment-name>
.
This way, our application stays stable.
How do I test my Rolling Update strategy in Kubernetes?
We can test our rolling update strategy in Kubernetes by using a staging environment that looks like our production setup. We can try a deployment with a test version of our application. During the update, we should watch performance metrics and user experience to make sure everything works well. For more detailed information, we can use tools like Prometheus to check the health of our application during the update.
For more details on Kubernetes concepts and best practices, we can read articles like What are Kubernetes Deployments and How Do I Use Them? or How Do I Scale Applications Using Kubernetes Deployments?.