I am trying to test the theory of deployment controllers fighting over pods when creating two Deployments with exactly the same selector.
Here's my deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
spec:
replicas: 3
selector:
matchLabels:
app: app
template:
metadata:
labels:
app: app
spec:
containers:
- name: busybox-container
image: busybox
command: ["/bin/sh"]
args: ["-c", "sleep 10m"]
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 8080
deployment1.yaml only differs with metadata.name field having the value deployment-1 and spec.replicas being set to 1.
I create the deployments back to back as such:
❯ kubectl create -f deployment.yaml; kubectl create -f deployment1.yaml
deployment.apps/deployment created
deployment.apps/deployment-1 created
❯ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/deployment-1-7d95ff89fc-p6fg7 1/1 Running 0 18s
pod/deployment-7d95ff89fc-fvsvd 1/1 Running 0 18s
pod/deployment-7d95ff89fc-ktdq9 1/1 Running 0 18s
pod/deployment-7d95ff89fc-xnw42 1/1 Running 0 18s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/deployment 3/3 3 3 18s
deployment.apps/deployment-1 1/1 1 1 18s
NAME DESIRED CURRENT READY AGE
replicaset.apps/deployment-1-7d95ff89fc 1 1 1 18s
replicaset.apps/deployment-7d95ff89fc 3 3 3 18s
I expect the second deployment to start interfering with the first deployment since the second deployment only wants 1 replica whereas the first deployment wants 3. I expect to see containers being deleted and recreated, but all is smooth sailing and I have 4 pods.
According to the Kubernetes documentation on a Deployment selector property, there should be interference.
If you have multiple controllers that have overlapping selectors, the controllers will fight with each other and won't behave correctly.
Now a similar question has been asked before and it names OwnerReferences as the reason for lack of interference, and the Owners and Dependents does state:
Owner references help different parts of Kubernetes avoid interfering with objects they don’t control.
Still the Owner/Dependent seems more concerned with garbage collection than with driving desired state. So then is the Deployment documentation incorrect/outdated? I thought a primary reason for selectors/labels was that controllers could pick up existing resources already in the system that were created outside of these higher resources.
I'm using the Kubernetes installation that comes installed w/ Docker Desktop for MacOS, version 1.22.4.