Kubernetes
kubectl apply
pods
latest tag
container deployment

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.

Practice system design

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

  1. 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.
  2. 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
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design