How Does Deployment Work?

You define the desired state of your Deployment in a YAML file, and Kubernetes takes the necessary steps to achieve that state.

The Deployment object doesn't manage Pods directly. Instead, it uses ReplicaSets to create and manage Pods, ensuring the desired state is reached.

Deployments use labels to identify and manage Pods (through ReplicaSets).

The selector field within the spec section specifies matchLabels, which the deployment controller uses to find ReplicaSets that match the desired Pod configuration. These ReplicaSets are then responsible for creating and managing the actual Pods.

So, what happens if a different pod has the same label as the deployment pods?

Each pod within a deployment has a unique label named "pod-template-hash" with a unique hash appended to it. It is added by the deployment controller during its creation.

This hash is derived from the pod template specification and serves as a unique identifier for that specific deployment revision.

So even if multiple pods use the same general labels (like app: myapp), the pod-template-hash keeps them clearly grouped by version.

This ensures the ReplicaSet only tracks the right pods and nothing else bymistake.

The following image shows how the template hash is used.


Complete and Continue