Can a Deployment controller control Pods that weren't created by it?

Viewed 74

Say I have a pod YAML such as:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.19.1   

And a Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template: 
    metadata: 
      labels: 
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
        ports:
        - containerPort: 80

Now I first create the Pod:

$ kubectl apply -f pod.yaml

And only then the Deployment:

$ kubectl apply -f deployment.yaml

I thought that, since the pod.yaml metadata includes a app: nginx selector, the Deployment controller will only create 2 nginx:1.17.1 pods, but I see that all 3 are created. Why is that?

1 Answers

In addition to creating the app: nginx label, Deployment controller also added the pod-template-hash label for each pod that was created.

If we check labels for running pods, we can see pod-template-hash=5d5dd5dd49 label for my-deployment pods:

$ kubectl get pods --show-labels
NAME    READY   STATUS   RESTARTS   AGE     LABELS
my-deployment-5d5dd5dd49-9tbcx 1/1 Running 0 55s app=nginx,pod-template-hash=5d5dd5dd49
my-deployment-5d5dd5dd49-b88f4 1/1 Running 0 55s app=nginx,pod-template-hash=5d5dd5dd49
my-deployment-5d5dd5dd49-x7n8q 1/1 Running 0 55s app=nginx,pod-template-hash=5d5dd5dd49
nginx                          1/1 Running 0 62s app=nginx

According to the official documentation:

The pod-template-hash label ensures that child ReplicaSets of a Deployment do not overlap. It is generated by hashing the PodTemplate of the ReplicaSet and using the resulting hash as the label value that is added to the ReplicaSet selector, Pod template labels, and in any existing Pods that the ReplicaSet might have.

This is why the changes from the Deployment didn't apply to a single pod with only the app: nginx label.

Related