Performing Rolling Updates in Kubernetes
A rolling update in Kubernetes is a deployment strategy that allows you to update your application without downtime. It gradually replaces the old version of your application with the new version, ensuring that some instances of the application are always available to handle requests.
How Rolling Updates Work
When you perform a rolling update, Kubernetes updates a few pods at a time, based on the specified update strategy. This allows for a smooth transition from the old version to the new version. If any issues arise during the update, Kubernetes can roll back to the previous version automatically.
Sample Deployment Configuration for Rolling Updates
Below is a sample YAML configuration for a Deployment that supports rolling updates:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-image:v1
ports:
- containerPort: 80
Explanation of the Deployment Configuration
- apiVersion: Specifies the version of the Kubernetes API for the Deployment.
- kind: Indicates that this resource is a Deployment.
- metadata: Contains data that helps uniquely identify the Deployment, including its name.
- spec: Defines the desired state of the Deployment.
- replicas: Specifies the number of pod replicas to run.
- selector: A label query that identifies the pods managed by this Deployment.
- strategy: Defines the strategy used to replace old pods with new ones.
- type: Set to
RollingUpdate
to enable rolling updates. - rollingUpdate: Specifies the parameters for the rolling update.
- maxUnavailable: The maximum number of pods that can be unavailable during the update. In this case, 1 pod can be unavailable.
- maxSurge: The maximum number of pods that can be created above the desired number of replicas. Here, 1 additional pod can be created during the update.
- template: Describes the pods that will be created, including metadata and container specifications.
Performing a Rolling Update
To perform a rolling update, you can update the image of your application in the Deployment configuration. For example, if you want to update the image to version 2, you can modify the Deployment YAML as follows:
image: my-image:v2
After modifying the YAML file, apply the changes using the following command:
kubectl apply -f deployment.yaml
You can monitor the status of the rolling update by running:
kubectl rollout status deployment/my-app
If you encounter any issues during the update, you can roll back to the previous version using:
kubectl rollout undo deployment/my-app
Conclusion
Rolling updates in Kubernetes provide a seamless way to update applications without downtime. By gradually replacing old pods with new ones, Kubernetes ensures that your application remains available to users. This strategy, combined with the ability to roll back if necessary, makes rolling updates a powerful feature for managing application deployments.