Learn how to seamlessly update your Kubernetes Deployment with a new container image using different strategies for zero-downtime deployments.
In Kubernetes, a Deployment is a resource that manages the deployment and scaling of your applications. When you need to update your application with a new image, Kubernetes provides a straightforward way to do so.
To update a Kubernetes Deployment with a new image, you can use the kubectl set image
command.
For example, to update the nginx
container image in a Deployment named my-deployment
to nginx:1.9.1
, you would run:
kubectl set image deployment/my-deployment nginx=nginx:1.9.1
This command tells Kubernetes to update the Deployment named my-deployment
, specifically targeting the container named nginx
, and set its image to nginx:1.9.1
.
Kubernetes will then perform a rolling update, gradually replacing the Pods in the Deployment with new Pods using the updated image.
This code demonstrates updating a Kubernetes Deployment named my-deployment with a new Nginx image version. It shows how to create a Deployment with an initial Nginx image, verify the Deployment status and image version, update the Nginx image using the kubectl set image command, and verify the updated image version after the rolling update.
This example demonstrates updating a Kubernetes Deployment named my-deployment
with a new Nginx image version.
1. Create a Deployment:
First, create a Deployment named my-deployment
with the initial Nginx image nginx:1.9.0
. You can use a YAML file like this:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.9.0
ports:
- containerPort: 80
Apply the YAML file:
kubectl apply -f deployment.yaml
2. Verify the Deployment:
Check the Deployment status and the image version:
kubectl get deployment my-deployment
kubectl get pods -l app=nginx -o wide | grep image
You should see the Deployment running with 3 replicas and the Nginx image version nginx:1.9.0
.
3. Update the Image:
Now, update the Nginx image to nginx:1.9.1
using the kubectl set image
command:
kubectl set image deployment/my-deployment nginx=nginx:1.9.1
4. Verify the Update:
Kubernetes will perform a rolling update. Monitor the rollout progress:
kubectl rollout status deployment/my-deployment
Once the rollout is complete, verify the updated image version:
kubectl get pods -l app=nginx -o wide | grep image
You should now see the Pods running with the updated image nginx:1.9.1
.
This example demonstrates a simple image update. You can use the same command to update images for other containers in your Deployments. Remember to adjust the Deployment name, container name, and image tag accordingly.
latest
, ensure your image pull policy is set to Always
or IfNotPresent
to avoid using a cached image.kubectl rollout history
, kubectl rollout undo
, or kubectl set image --image-pull-policy
for more control over the update process.kubectl
is the standard CLI tool, you can also update deployments using the Kubernetes Dashboard or various infrastructure-as-code tools like Terraform and Pulumi.kubectl logs <pod-name>
) and events (kubectl get events
) for error messages.Feature | Description |
---|---|
Command: | kubectl set image |
Purpose: | Updates the image of a container within a Kubernetes Deployment. |
Syntax: | kubectl set image deployment/<DEPLOYMENT_NAME> <CONTAINER_NAME>=<IMAGE_NAME>:<TAG> |
Example: |
kubectl set image deployment/my-deployment nginx=nginx:1.9.1 updates the nginx container in the my-deployment Deployment to use the nginx:1.9.1 image. |
Update Process: | Kubernetes performs a rolling update, gradually replacing existing Pods with new ones using the updated image. |
Updating container images in Kubernetes Deployments is a fundamental aspect of managing and deploying applications in a Kubernetes environment. The kubectl set image
command provides a simple and efficient way to trigger rolling updates, ensuring minimal downtime and application availability. By understanding the concepts of Deployments, image versions, and rollout strategies, developers and administrators can effectively manage application updates in Kubernetes, leveraging its features for seamless and reliable deployments.