0

What if deployment selector labels are only subset of template(pod) labels? I've read documents about Labels and Selectors, Deployments. Also googled but not found answer.

When using helm3 to create a new chart by helm create x, the default deployment labels are like this(omitted unrelated parts):

spec:
  selector:
    matchLabels:
      {{- include "x.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "x.selectorLabels" . | nindent 8 }}
    spec:

which means it is highly recommended, that selector labels are same as pod labels. But what if selector labels are only subset of pod labels?

I did two experiments:

  1. Define sigle deployment, and helm install to two different releases
spec:
  replicas: 2
  selector:
    matchLabels:
      foo: bar
  template:
    metadata:
      labels:
        foo: bar
        unique: {{- uuidv4 }}
    spec:
  1. Define two deployments in single chart, and helm install
  • deployment1
metadata:
  name: deployment1
spec:
  replicas: 2
  selector:
    matchLabels:
      foo: bar
  template:
    metadata:
      labels:
        foo: bar
        unique: {{- uuidv4 }}
    spec:
  • deployment2
metadata:
  name: deployment2
spec:
  replicas: 2
  selector:
    matchLabels:
      foo: bar
  template:
    metadata:
      labels:
        foo: bar
        unique: {{- uuidv4 }}
    spec:

My expectations is that since deployment select pods by labels, and not anything else, I think in each case final pods count should be 2, because the pods created by the two deployments share same labels.

Actual result is not. Totally 4 pods created. Seems deployment "knows" pods reference other than labels. And uninstalling one of the releases in case 1 only removed two pods, which proves the idea again.

I checked replicasets, there's a label called pod-template-hash which surely is unique and might explain. But if pod-template-hash is really implicitely used, why do we need explicitely define label selectors that aren't acutally used?

Lei Yang
  • 3,970
  • 6
  • 38
  • 59
  • you are correct the pod-template hash is added to the pods and replicasets and are the reason why 4 pods are created. there's more info on why that's the case here https://stackoverflow.com/questions/69940393/whats-the-exact-reason-a-pod-template-hash-is-added-to-the-name-of-the-replicas – meaningqo Nov 08 '22 at 06:30

0 Answers0