Why don't two Kubernetes ReplicaSets with same selector conflict with each other?

1/13/2019

Replica Set 1

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  labels:
    app: nginx
  name: rs-1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
        version: 1.7.1
    spec:
      containers:
      - image: nginx:1.7.1
        name: nginx-1
      restartPolicy: Always

Replica Set 2

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  labels:
    app: nginx
  name: rs-2
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
        version: 1.7.9
    spec:
      containers:
      - image: nginx:1.7.9
        name: nginx-1
      restartPolicy: Always

When I create these two ReplicaSets, one ignores the pods created by the other.

C02T30K2GTFM:ask erkanerol$ kubectl get pods --show-labels
NAME         READY   STATUS    RESTARTS   AGE     LABELS
rs-1-996cz   1/1     Running   0          5m13s   app=nginx,version=1.7.1
rs-1-ktv9z   1/1     Running   0          5m13s   app=nginx,version=1.7.1
rs-1-w7sbg   1/1     Running   0          5m13s   app=nginx,version=1.7.1
rs-2-2z8rb   1/1     Running   0          4m26s   app=nginx,version=1.7.9
rs-2-5c56s   1/1     Running   0          4m26s   app=nginx,version=1.7.9
rs-2-hls9p   1/1     Running   0          4m26s   app=nginx,version=1.7.9

As far as I understand from the documentation, if there are enough pods which match a replicaset's selector, it shouldn't create new pods. Why is this happenning? Is it using ownerReferences?

-- Erkan Erol
kubernetes
replicaset

3 Answers

1/13/2019

That's because two replica sets have two different .metadata.name values hence they both have their own isolated resources. The same behavior will be available even with deployment sets. Assuming that you name the two with different values, the two deployment sets would also spin up isolated pods with same labels.

-- Mukesh Sharma
Source: StackOverflow

1/13/2019

Labels are key/value pairs attached to the objects such as pods,deployment etc. Labels are used to identify and group the kubernetes resources.

According to the official documentation of kubernetes,

Unlike names and UUID labels do not provide uniqueness. In general, we expect many objects to carry the same labels.

Labels are not for uniqueness, labels are used to identify the group of objects which are related in some way so that you can list or watch those objects.

Let's take the example you mentioned in your question, that have two replicasets with 3 replica's each. Both replica's represent the labels app: nginx and version:1.7.9 or version:1.7.1

Now if you want to identify all the pods having labels app=nginx you can run following command:

kubectl get pods -l app=nginx

It will show you all 6 pods.

Now, If you want to identify the pods which have app=nginx as well as specific version of that nginx then you need to run following command:

kubectl get pods -l app=nginx,version=1.7.1

Now it will show you only three pods which have both the labels.

For more information read official docs on labels here

-- Prafull Ladha
Source: StackOverflow