How do I force Kubernetes to re-pull an image?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Kubernetes allows you to orchestrate containerized applications, but there are times when you must deal with stale images. If you need to ensure that your Kubernetes cluster is using the latest version of a container image, you might need to force Kubernetes to re-pull an image. This can be particularly crucial for new releases, updated patches, or security fixes. This article provides a detailed explanation of how to achieve this in Kubernetes, focusing on technical details and examples.
Understanding How Kubernetes Handles Images
Before diving into how to force Kubernetes to re-pull an image, it is helpful to understand how Kubernetes handles images. When a Kubernetes Pod is deployed, it pulls the specified container images from a registry. These images are then locally cached on the nodes to speed up subsequent launches of the same image.
Image Pull Policies
Kubernetes uses image pull policies to determine when to fetch a new image from a registry. These policies include:
Always: Always pull the image from the registry when a Pod is started.IfNotPresent: Pull the image only if it is not already available locally.Never: Never pull the image; use the one available locally.
The imagePullPolicy is an attribute specified in the Pod definition file. If unspecified, Kubernetes sets the imagePullPolicy to IfNotPresent for tagged images and to Always for images with the latest tag.
Forcing Kubernetes to Re-Pull an Image
There are several methods to force Kubernetes to re-fetch an image from the registry:
1. Update the imagePullPolicy
One straightforward approach is to set the imagePullPolicy to Always in your Pod's YAML configuration. This ensures that Kubernetes fetches the image every time the Pod is started.
2. Change the Image Tag
Another method is to deploy a new version of your application with a different image tag. This makes Kubernetes treat it as a new image, triggering a new pull.
3. Manually Delete the Image from Nodes
In some scenarios, you might want to delete an image from the nodes to force Kubernetes to pull it again. This can be done using the docker or containerd command, depending on the container runtime used.
For Docker:
For Containerd:
4. Use kubectl rollout restart
If you are working with Deployments, restarting the pods can be a simple solution. Execute the following command to restart all pods in the deployment:
This triggers the Pods to restart, and with the appropriate imagePullPolicy, the images will be re-pulled.
Key Points
Here’s a summary of methods and considerations for forcing Kubernetes to re-pull an image:
| Method | Description |
Update imagePullPolicy | Set to Always to guarantee re-pulling image |
| Change Image Tag | Change to a new tag to force Kubernetes to fetch anew |
| Manually Delete Image | Use CLI commands to remove image from node cache |
Use kubectl rollout restart | Restart pods in a Deployment to trigger image re-pull |
Additional Considerations
- Network Bandwidth: Frequent re-pulling can strain bandwidth resources. Consider using caching solutions or local registries if re-pulls are needed frequently.
- Image Versioning: Leverage semantic versioning of tags to ensure clarity about which version is running in your environments.
- CI/CD Pipelines: Integrate automatic updates and image re-pulling within continuous integration and delivery pipelines for smoother deployments.
Conclusion
Forcing Kubernetes to re-pull an image can be crucial for ensuring your applications are running the correct version of a container image, particularly during rapid development cycles or emergency fixes. By adjusting imagePullPolicy, using unique image tags, removing cached images, or leveraging rollout restarts, you can enforce desired behaviors according to your use case. Understanding these mechanisms will help you manage Kubernetes deployments effectively and keep your applications up-to-date.
Related reading
- How do I get logs from all pods of a Kubernetes replication controller?
- How Do I Get Skaffold And Helm Charts To Work With A Local Image Repository?
- How do I kill microk8s kubernetes?
- How do I load properties from a Kubernetes configmap into my Spring Boot application using Spring Cloud?
- How do I get into a Docker container's shell?
- How do I make a comment in a Dockerfile?
- How do I initialize the whitelist for Apache-Zookeeper?
- How do I keep track of the time the CPU is used vs the GPUs for deep learning?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.