Sling Academy
Home/DevOps/Kubernetes: How to update a deployment with a new image version

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

Last updated: January 31, 2024

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.

Next Article: Understanding kubectl port-forward (with examples)

Previous Article: VolumeMount user group and file permissions in Kubernetes: Explained with examples

Series: Kubernetes Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide