Performing rolling updates in Kubernetes allows you to update your application without causing downtime. Here’s how you can do it:
Step 1: Create or Apply a Deployment
First, you need to have a Deployment in place. If you don’t already have one, create or apply a deployment YAML file. For example:
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:tag
ports:
- containerPort: 80Apply the deployment using:
kubectl apply -f my-deployment.yamlStep 2: Update the Image Version
Update the image version in your deployment file or use the set image command:
kubectl set image deployment/my-deployment my-container=my-new-image:tagStep 3: Monitor the Update Progress
Monitor the update progress using:
kubectl get pods -l app=my-appYou’ll see a mix of old and new pods while the update is in progress.
Step 4: Check the Deployment Status
Once the update is complete, you’ll see that all pods are running the new version:
kubectl get pods -l app=my-appOptional: Rollback
If something goes wrong during the update, you can perform a rollback to the previous version:
kubectl rollout undo deployment/my-deploymentThis will revert the deployment to the previous version.
Optional: Pause and Resume
You can also pause and resume a deployment if you want to halt the update process temporarily:
kubectl rollout pause deployment/my-deployment
kubectl rollout resume deployment/my-deploymentKeep in mind that this is a basic example. Depending on your specific requirements, you may need to handle more complex scenarios, such as database migrations or application-specific procedures during updates. Always refer to the official Kubernetes documentation for best practices and advanced configurations.
