In this lesson, you will learn how to transition from using the unreliable latest tag to specific, immutable image tags to ensure your deployments are predictable and repeatable.
The Problem with latest
When you define a Kubernetes Deployment, you specify a container image. A common beginner mistake is to use the latest tag. While it sounds convenient, it is a "moving target." If you pull my-app:latest today, it might be different from the one you pull tomorrow. This lack of immutability makes it impossible to know exactly which version of your code is running, and more importantly, it breaks the ability to perform reliable rollbacks.
Establishing the Best Practice
To achieve predictable deployments, every build in your CI/CD pipeline should generate a unique, immutable identifier for the container image. Instead of latest, use semantic versioning (e.g., v1.2.3) or the Git commit SHA (e.g., a1b2c3d).
When you trigger a deployment, your pipeline should update the Kubernetes manifest to reflect this specific tag. Here is how your deployment.yaml should look:
spec:
containers:
- name: my-app
# Use a specific version, not 'latest'
image: my-registry/my-app:v1.2.3
Why This Matters for Kubernetes
Kubernetes manages the lifecycle of your application based on the "Desired State." If you tell Kubernetes that your desired state is v1.2.3, it will pull that exact image. If you decide to roll back or scale your application, Kubernetes knows exactly which binary it needs to run.
By using specific tags, you create a "source of truth" in your configuration. If a deployment behaves unexpectedly, you don't have to guess which code version is active; the tag in your Deployment object tells you exactly what is running. This practice also allows you to perform "instant rollbacks" effectively, as Kubernetes can easily switch back to a previous, known-good tag without relying on the state of a registry’s latest pointer.
Key Takeaway: Never use the latest tag for production; always use unique, immutable image tags to ensure your deployments are predictable, verifiable, and easy to roll back.