Kubernetes: How to update a deployment with a new image version

Updated: January 31, 2024 By: Guest Contributor Post a comment

Overview

Kubernetes, the container orchestration tool, allows for seamless updates to running deployments. When updating a deployment, you generally want to change its pod template to a new container image version. This tutorial will guide you through updating a deployment in Kubernetes to use a new image version.

Understanding Deployment Updates

When you update a Deployment, Kubernetes applies updates in a controlled way. By default, it gradually replaces instances of the old version of the pod with the new one, ensuring that the application remains available during the process.

Prerequisites

  • Kubernetes cluster setup
  • kubectl command-line tool installed and configured
  • A running Deployment to update

Step-by-Step Guide

1. Check the Current Deployment

First, ensure you have a deployment currently running:

kubectl get deployments

This will list the available Deployments in the current namespace.

2. Update the Deployment Image

To update the image used by the deployment:

kubectl set image deployment/<deployment_name> <container_name>=<new_image>:<tag>

Replace <deployment_name> with the name of your deployment, <container_name> with the name of the container within the pod that you’re updating, and <new_image>:<tag> with the new image version.

For example:

kubectl set image deployment/my-app my-container=my-app:v2

3. Use a YAML File Update

If you prefer to use a declarative approach:

kubectl apply -f deployment.yml

Make sure the YAML file reflects the new image version.

4. Verify the Update

To check the status of the rollout:

kubectl rollout status deployment/<deployment_name>

This will provide feedback on the progress of the update.

5. Rolling Back an Update

If something went wrong:

kubectl rollout undo deployment/<deployment_name>

This will revert the Deployment to the previous known state.

Advanced Update Strategies

You can define different update strategies, like Recreate or RollingUpdate, in your Deployment’s manifest. The RollingUpdate strategy is the default and replaces pods gradually with new ones.

Updating Multiple Containers

If your Deployment has multiple containers and you need to update all at once:

kubectl set image deployment/<deployment_name> <container_name1>=<new_image1>:<tag1> <container_name2>=<new_image2>:<tag2>

Conclusion

Updating your Kubernetes deployment with a new image version keeps your applications up to date and secure. Understanding the update process and its mechanisms is crucial for managing your Kubernetes resources effectively.