How to Update Deployment in Kubernetes: Step-by-Step Guide
To update a deployment in Kubernetes, use the
kubectl apply -f command with an updated deployment YAML file or use kubectl set image to change the container image directly. Kubernetes will then perform a rolling update to replace old pods with new ones without downtime.Syntax
There are two common ways to update a deployment in Kubernetes:
- Using a YAML file: Update the deployment manifest file and run
kubectl apply -f deployment.yaml. - Using kubectl set image: Change the container image directly with
kubectl set image deployment/DEPLOYMENT_NAME CONTAINER_NAME=NEW_IMAGE.
Kubernetes will then perform a rolling update, replacing pods gradually to avoid downtime.
bash
kubectl apply -f deployment.yaml kubectl set image deployment/my-deployment my-container=my-image:tag
Example
This example shows how to update the container image of a deployment named nginx-deployment from nginx:1.14.2 to nginx:1.16.1 using kubectl set image. Kubernetes will roll out the new pods with the updated image.
bash
kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
kubectl rollout status deployment/nginx-deploymentOutput
deployment "nginx-deployment" image updated
Waiting for deployment "nginx-deployment" rollout to finish: 2 of 3 updated replicas are available...
deployment "nginx-deployment" successfully rolled out
Common Pitfalls
Common mistakes when updating deployments include:
- Not specifying the correct container name in
kubectl set image, causing no update. - Forgetting to check rollout status, missing failed updates.
- Using
kubectl applywith an incorrect or incomplete YAML file, which can cause unintended changes. - Not having proper permissions to update the deployment.
Always verify the deployment status after updating to ensure the update succeeded.
bash
kubectl set image deployment/nginx-deployment wrong-container=nginx:1.16.1 # This will not update the image because the container name is incorrect # Correct way: kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
Quick Reference
| Command | Description |
|---|---|
| kubectl apply -f deployment.yaml | Update deployment using updated YAML manifest |
| kubectl set image deployment/DEPLOYMENT_NAME CONTAINER_NAME=NEW_IMAGE | Update container image directly |
| kubectl rollout status deployment/DEPLOYMENT_NAME | Check rollout status of deployment |
| kubectl rollout undo deployment/DEPLOYMENT_NAME | Rollback to previous deployment version |
Key Takeaways
Use kubectl apply with updated YAML or kubectl set image to update deployments.
Kubernetes performs rolling updates to avoid downtime during deployment changes.
Always check rollout status to confirm the update succeeded.
Specify the correct container name when updating images with kubectl set image.
Use kubectl rollout undo to rollback if the update causes issues.