đŸ¶
Kubernetes

Kubernetes Deployment: Update Image Guide

By Jan on 01/16/2025

Learn how to seamlessly update your Kubernetes Deployment with a new container image using different strategies for zero-downtime deployments.

Kubernetes Deployment: Update Image Guide

Table of Contents

Introduction

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.

Step-by-Step Guide

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.

Code Example

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.

Additional Notes

  • Image Pull Policy: By default, Kubernetes will try to pull the new image from your container registry. If you're using an image tagged latest, ensure your image pull policy is set to Always or IfNotPresent to avoid using a cached image.
  • Rollout Strategies: Kubernetes offers different rollout strategies beyond the default rolling update. You can use kubectl rollout history, kubectl rollout undo, or kubectl set image --image-pull-policy for more control over the update process.
  • Blue-Green and Canary Deployments: For more complex update scenarios, consider using blue-green deployments or canary deployments to minimize downtime and risk.
  • Resource Limits: Ensure your deployment has sufficient resource limits (CPU, memory) to handle the image update process. Insufficient resources can lead to pod evictions or deployment failures.
  • Health Checks: Utilize liveness and readiness probes to ensure your application is healthy after the image update. This helps Kubernetes identify and replace unhealthy pods.
  • Automated Image Updates: Tools like Flux and ArgoCD can automate image updates based on changes in your container registry, simplifying continuous delivery pipelines.
  • Security Considerations: Always verify the source and integrity of new images before deploying them to your cluster. Use image scanning tools to detect vulnerabilities.
  • kubectl Alternatives: While 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.
  • Troubleshooting: If the update fails, check the pod logs (kubectl logs <pod-name>) and events (kubectl get events) for error messages.

Summary

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.

Conclusion

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.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
đŸ€źClickbait