Schedule Pods Using Required Pod Affinity
Scenario
You have an existing Deployment named db-app running in the db namespace with 3 replicas using the mysql image. The pods are labeled with app=db.
Create a new Deployment named web-app in the web namespace using the image httpd:trixie with 2 replicas, and ensure that the pods are labeled with app=web-app.
Configure the Deployment so that its pods are scheduled only on nodes where pods with the label app=db are already running by using required pod affinity with the topologyKey set to kubernetes.io/hostname.
Do not modify the existing db-app Deployment. Ensure the web-app pods remain in the Running state and verify that they are scheduled on the same nodes as the db-app pods.
Do not use taints, tolerations, or node selectors. Use only Pod Affinity to control pod scheduling.
Hint
You need to add affinity.podAffinity.requiredDuringSchedulingIgnoredDuringExecution to the web-app Deployment spec. Use topologyKey: kubernetes.io/hostname and match the label app=db to co-locate with db-app pods.