In the Kubernetes Slack an interesting question was posed:
Hi, can a pod belong to 2 workloads? For example, can a pod belong both the a workload and to the control plane workload?
My initial reaction was that, while a Pod can belong to two (or three, or more) services, it cannot belong to two workloads (Deployments for example). I put my theory to the test by creating initially a pod with some labels
apiVersion: v1
kind: Pod
metadata:
name: caddy
labels:
apache: ok
nginx: ok
spec:
containers:
- name: caddy
image: caddy
ports:
- name: http
containerPort: 80
- name: https
containerPort: 443
Sure enough the pod was created
% kubectl get pod
NAME READY STATUS RESTARTS AGE
caddy 1/1 Running 0 2s
Next I created a replicaSet whose pods have a label that the above (caddy) pod has also.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
nginx: ok
template:
metadata:
labels:
app: nginx
nginx: ok
spec:
containers:
- name: nginx
image: bitnami/nginx
ports:
- containerPort: 8080
Since the original pod and the replicaSet share a common label (nginx: ok), the pod is assimilated in the replicaSet and it launches one additional pod only:
% kubectl get pod
NAME READY STATUS RESTARTS AGE
caddy 1/1 Running 0 2m52s
nginx-lmmbk 1/1 Running 0 3s
We can now ask Kubernetes to create an identical replicaSet that launches apache instead of nginx and has the apache: ok label set.
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: apache
labels:
app: apache
spec:
replicas: 2
selector:
matchLabels:
apache: ok
template:
metadata:
labels:
app: apache
apache: ok
spec:
containers:
- name: apache
image: bitnami/apache
ports:
- containerPort: 8080
If a pod can be shared among workloads, then it should start a single apache pod. Does it?
% kubectl get pod
NAME READY STATUS RESTARTS AGE
apache-8fwdz 1/1 Running 0 4s
apache-9xwhd 1/1 Running 0 4s
caddy 1/1 Running 0 5m17s
nginx-lmmbk 1/1 Running 0 2m28
As you can see, it starts two apache pods and the pods carrying the apache-ok label are three:
% kubectl get pod -l apache=ok
NAME READY STATUS RESTARTS AGE
apache-8fwdz 1/1 Running 0 6m20s
apache-9xwhd 1/1 Running 0 6m20s
caddy 1/1 Running 0 11m
% kubectl get rs
NAME DESIRED CURRENT READY AGE
apache 2 2 2 6m21s
nginx 2 2 2 8m45s
So there you have it, a Pod cannot be shared among workloads.