Rolling Updates are Kubernetes' default and most common strategy for updating applications without downtime. Instead of taking down all instances of your old application version and then bringing up all instances of the new one (a "recreate" strategy), rolling updates gradually replace old pods with new ones. This ensures that a minimum number of application instances are always available to serve traffic, providing a seamless experience for users. For a DevOps Engineer, mastering rolling updates is fundamental for implementing robust CI/CD pipelines that deliver high availability and continuous service delivery even during frequent updates.
Kubernetes orchestrates rolling updates through its Deployment controller. When you update the image tag or other pod spec details in your Deployment manifest, Kubernetes starts creating new pods with the updated configuration. Crucially, it waits for these new pods to become "ready" (as defined by your readiness probes) before terminating an equivalent number of old pods. This process repeats iteratively until all old pods are replaced by new ones. You control the pace and risk tolerance of this rollout using two key parameters within the strategy.rollingUpdate section: maxUnavailable (maximum number of pods that can be unavailable during the update) and maxSurge (maximum number of pods that can be created above the desired replica count).
The primary advantage of rolling updates is the zero-downtime deployment, which is critical for production environments. It also allows for easy rollbacks to a previous stable version if issues are detected, as Kubernetes retains a history of your deployments. While generally slower than a full "recreate" strategy, this gradual approach significantly reduces the risk of service interruption. For successful rolling updates, proper implementation of readiness and liveness probes in your application is non-negotiable, ensuring Kubernetes only directs traffic to healthy, fully initialized new pods and removes unhealthy old ones effectively. Understanding how to configure maxUnavailable and maxSurge is key to balancing update speed with service availability and resource usage.
Key Takeaways
- Enables zero-downtime application updates by gradually replacing pods.
- Kubernetes Deployment controller manages the update process automatically.
- Configured using
maxUnavailableandmaxSurgeparameters in the Deployment strategy. - Relies heavily on application readiness probes to determine pod health.
- Allows for easy rollbacks to previous stable versions.
Code Example
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 25% # Allows 25% of replicas to be unavailable during update
maxSurge: 25% # Allows 25% more replicas than desired temporarily
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app-container
image: my-registry/my-app:v2.0 # The new image version to deploy
ports:
- containerPort: 80How this code works
This code defines a Kubernetes Deployment called my-app-deployment, responsible for managing an application. Its primary job is to ensure three copies (replicas: 3) of my-app are always running, and crucially, to update the application to a new image: my-registry/my-app:v2.0 without interruption. The strategy field, set to type: RollingUpdate, dictates this process. Instead of taking the entire application offline to apply changes, Kubernetes will update pods gradually, maintaining continuous availability.
The rollingUpdate section fine-tunes this zero-downtime strategy. maxUnavailable: 25% means that during the update, no more than 25% of the total replicas (in this case, one out of three pods) can be out of service at any given time. Complementing this, maxSurge: 25% allows Kubernetes to temporarily create up to 25% more pods than desired (one extra pod here). This ensures new application versions are fully launched and serving traffic before older versions are removed. A subtle point for beginners is that RollingUpdate is the default deployment strategy for Kubernetes Deployments, and maxUnavailable: 25% and maxSurge: 25% are also the default values if not specified, making these lines explicit for clarity and control over this common behavior.