In Kubernetes, ReplicaController uses label selectors to specify which pod(s) it will operate on.
Now, my question is, if multiple ReplicaControllers have overlapping label selectors, what will be the behavior of the Kubernetes cluster? Or is it considered some kind of error and should be avoided?
For example, I have two ReplicationControllers described as below.
rc1.yaml:
apiVersion: v1
kind: ReplicationController
metadata:
name: frontend
spec:
replicas: 3
selector:
app: frontend
team: payment
rc2.yaml:
apiVersion: v1
kind: ReplicationController
metadata:
name: backend
spec:
replicas: 2
selector:
app: backend
team: payment
You can see that they both have the label selector team=payment
, but one specifies a replica count of 3 while another of 2.
Any explanations or references will be appreciated. Thanks.
The Kubernetes docs state the following about overlapping selectors (either from other pods, replication controllers or jobs):
Also you should not normally create any pods whose labels match this selector, either directly, with another ReplicationController, or with another controller such as Job. If you do so, the ReplicationController thinks that it created the other pods. Kubernetes does not stop you from doing this. If you do end up with multiple controllers that have overlapping selectors, you will have to manage the deletion yourself
In the docs about Deployments there is another statement that confirms the above:
If you have multiple controllers that have overlapping selectors, the controllers will fight with each other and won’t behave correctly.
So, in summary, you should try to avoid overlapping selectors for replication controllers, replica sets (the next-generation replication controller which I would advise you to use instead of replication controllers) and deployments because it might probably lead to severe problems as confirmed by this issue.