<p>In a Deployment, under what circumstances would the matchLabels in the selector not precisely match the template metadata labels? If they didn't match, any pod created wouldn't match the selector, and I'd imagine K8s would go on creating new pods until every node is full. If that's true, why does K8s want us to specify the same labels twice? Either I'm missing something or this violates the DRY principle.</p>
<p>The only thing I can think of would be creating a Deployment with matchLabels &quot;key: A&quot; &amp; &quot;key: B&quot; that simultaneously puts existing/un-owned pods that have label &quot;key: A&quot; into the Deployment while at the same time any new pods get label &quot;key: B&quot;. But even then, it feels like any label in the template metadata should automatically be in the selector matchLabels.</p>
<p>K8s docs give the following example:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
</code></pre>

<p><code>...In a Deployment, under what circumstances would the matchLabels in the selector not precisely match the template metadata labels?</code></p>
<p>Example when doing <a href="https://kubernetes.io/docs/concepts/cluster-administration/manage-deployment/#canary-deployments" rel="nofollow noreferrer">canary deployment</a>.</p>
<p><code>...If they didn't match, any pod created wouldn't match the selector, and I'd imagine K8s would go on creating new pods until every node is full.</code></p>
<p>Your deployment will not proceed, it will fail with error message <code>&quot;selector&quot; does not match template &quot;labels&quot;</code>. No pod will be created.</p>
<p><code>...it feels like any label in the template metadata should automatically be in the selector matchLabels.</code></p>
<p>Labels under template.metadata are used for many purposes and not only for deployment, example labels added by CNI pertain to IP on the fly. Labels meant for selector should be minimum and specific.</p>
