Introduction
In Kubernetes, a rolling update is a deployment strategy used to update applications running as pods in a controlled and gradual manner. This approach ensures minimal disruption to the application's availability by gradually replacing old instances with new ones.
How Rolling Updates Work
Deployment Definition
Rolling updates are typically managed through Kubernetes Deployments, which provide declarative updates to applications. Here's an example of a simple Deployment definition:
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:v1 ports: - containerPort: 80
This Deployment specifies three replicas (
replicas: 3
) of a container (my-app-container
) running themy-app:v1
image.Updating the Deployment
To perform a rolling update, you update the Deployment's
spec.template.spec.containers[0].image
field to point to a new version of your application's image (my-app:v2
, for example). Kubernetes automatically manages the update process:Pod Creation: Kubernetes creates new pods with the updated image (
my-app:v2
).Pod Termination: Kubernetes gradually terminates old pods running the previous version (
my-app:v1
).Replica Control: It ensures that the specified number of replicas (
replicas: 3
) is maintained throughout the update process.
Update Strategy
Kubernetes manages the rolling update process using a configurable strategy defined in the Deployment's
spec.strategy
field. By default, it uses a rolling update strategy (RollingUpdate
) which ensures that a specified number of pods (or a percentage) are updated at a time.spec: strategy: type: RollingUpdate rollingUpdate: maxUnavailable: 1 maxSurge: 1
maxUnavailable: Specifies the maximum number or percentage of pods that can be unavailable during the update process. Setting
maxUnavailable: 1
ensures that only one pod is taken down at a time.maxSurge: Specifies the maximum number or percentage of new pods that can be created above the desired number of pods. Setting
maxSurge: 1
allows one additional pod beyond the desired count to be created temporarily during the update.
Benefits of Rolling Updates
Zero Downtime: Ensures continuous availability of the application by gradually updating pods.
Controlled Process: Kubernetes manages the update process, ensuring consistency and reliability.
Rollback Capability: Provides the ability to rollback to a previous version quickly if issues arise during the update.
Considerations
Health Checks: Define readiness and liveness probes in your Deployment to ensure Kubernetes only considers pods ready for traffic.
Resource Requirements: Ensure nodes have sufficient resources to handle both old and new pods during the update.
Conclusion
Rolling updates in Kubernetes are a fundamental feature for maintaining application availability while updating containerized applications. By leveraging Kubernetes Deployments and update strategies, administrators can ensure smooth and controlled updates, minimizing disruption and providing a reliable mechanism for managing application lifecycle in dynamic environments.