<p>I've experienced a surprising behavior when playing around with Kubernetes and I wanted to know if there is any good explanation behind it.</p>
<p>I've noticed that when two Kubernetes deployments are created with the same labels, and with the same <code>spec.selector</code>, the deployments still function correctly, even though using the same selector &quot;should&quot; cause them to be confused regarding which pods is related to each one.</p>
<p>Example configurations which present this -</p>
<p><code>example_deployment.yaml</code></p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
    extra_label: one
spec:
  replicas: 2
  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>example_deployment_2.yaml</code></p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment-2
  labels:
    app: nginx
    extra_label: two
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>I expected the deployments not to work correctly, since they will select pods from each other and assume it is theirs.
The actual result is that the deployments seem to be created correctly, but entering the deployment from <code>k9s</code> returns all of the pods. This is true for both deployments.</p>
<p>Can anyone please shed light regarding why this is happening? Is there additional internal filtering in Kubernetes to to prevent pods which were not really created by the deployment from being associated with it?</p>
<p>I'll note that I've seen this behavior in AWS and have reproduced it in Minikube.</p>

Guy
<p>When you create a K8S Deployment, K8S creates a ReplicaSet to manage the pods, then this ReplicaSet creates the pods based on the number of replicas provided or patched by the hpa. Addition to the provided labels and annotations you provide, the ReplicaSet add <code>ownerReferences</code> which contains its name and uid, so even if you have 4 pods with the same labels, each two pods will have a different <code>ownerReferences</code> used by the ReplicaSet to manage them:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Pod
metadata:
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: &lt;replicaset name&gt;
    uid: &lt;replicaset uid&gt;
...
</code></pre>
