I want to tightly couple the two pods to run on the same node so that even if one pod is accidentally deleted, it gets rescheduled to the same node where another app resides. This should be the same for both apps.
Suppose, I have two pods P1 and P2. Then if the pod P1 is running on the node N1 then the pod P2 also should run on the same node N1.
I have implemented this using this manifest.
apiVersion: apps/v1
kind: Deployment
metadata:
name: first-app
spec:
replicas: 1
selector:
matchLabels:
app: first-app
template:
metadata:
labels:
app: first-app
spec:
containers:
- name: first-app
imagePullPolicy: IfNotPresent
image: image1
nodeSelector:
nodegroup: etl
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: "app"
operator: In
values:
- first-app
and for the second app
apiVersion: apps/v1
kind: Deployment
metadata:
name: second-app
spec:
replicas: 1
selector:
matchLabels:
app: second-app
template:
metadata:
labels:
app: second-app
spec:
containers:
- name: second-app
imagePullPolicy: IfNotPresent
image: image1
nodeSelector:
nodegroup: etl
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: "app"
operator: In
values:
- second-app
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: "app"
operator: In
values:
- first-app
topologyKey: "kubernetes.io/hostname"
By this, second-app will always be assigned to the same node where the first-node is running. Now, when both the apps are running, first-app is accidentally removed from the node and assigned to another node, the second node should also evict from the node and assigned to another node where the first app runs.
Another solution might be, when the first-app is out from the node, it gets scheduled to same node where the second-app is running and vice-versa.
But I am not sure how to implement this?