Kubernetes kubectl apply does not update pods when using latest tag
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Understanding why `kubectl apply` Does Not Update Pods with the "latest" Tag
In the realm of container orchestration, Kubernetes is widely recognized for its powerful and efficient management of containerized applications. However, there are some nuances and behaviors that can surprise even seasoned Kubernetes users. One such behavior involves the use of image tags, specifically the "latest" tag, and the behavior of `kubectl apply` when it comes to updating Pods.
The Challenge with the "latest" Tag
When deploying applications with Kubernetes, the `kubectl apply` command is commonly used to apply configurations to the desired state of the cluster. However, a common issue arises when users expect `kubectl apply` to automatically update Pods to the "latest" image when an image tag is set to "latest". This behavior does not occur as expected due to how Kubernetes handles image versioning and pod management.
Why `kubectl apply` Does Not Update Pods with "latest" Tag
1. Image Caching
Docker, the underlying container runtime used by Kubernetes, caches images locally. When you specify an image with the "latest" tag, Docker might cache an older version of the image with the same tag name and use it, not pulling the newer image unless explicitly told to do so.
2. Kubernetes Pod Spec and Immutable Config
When `kubectl apply` is used, it applies changes based on the difference in the configuration file and the current state of the cluster. Kubernetes does not track changes to images that are tagged as "latest" since the tag itself has not changed. As a result, there is no perceived change in the image specification when running `kubectl apply`.
3. Deployments Need Explicit Changes
Kubernetes Deployments, which manage the life cycle of Pods, require a change in the Deployment's specification to trigger a rollout of new Pods. If only the image's underlying content has changed without a change in the tag or another spec property, it will not result in a new rollout.
Best Practices to Ensure Updates
- Use Immutable Tags: Instead of using the "latest" tag, use versioned or uniquely identifiable tags for images. This makes it easier for Kubernetes to detect changes and update Pods accordingly.
- Manual Image Pull: Manually pull the latest image on worker nodes to ensure the cache is up-to-date using:
- name: my-app-container
- Environment Configuration Discrepancies: Always ensure that your local, staging, and production environments are using the same configuration setups to avoid discrepancies, especially relevant when dealing with "latest" tags.
- CI/CD Pipeline Integration: Incorporate image versioning strategies within your Continuous Integration/Continuous Deployment (CI/CD) pipelines to ensure a consistent and traceable deployment process.
Related reading
- Kubernetes kubectl bash completion with alias
- Kubernetes kustomize command giving error when we specify base manifest files in kustomization.yaml file under resources section
- Kubernetes Let's Encrypt cert-manager Error secret not found
- Kubernetes list all pods and its nodes
- Kubernetes Liveness Probe Logging
- Kubernetes livenessProbe restarting vs destroying of the pod
- kubernetes list all running pods name
- Kubernetes log location in pod

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.