Challenge, Easy,  on  Kubernetes

Kubernetes Pod Scheduling: Pod Affinity and Anti-Affinity

Pod affinity pulls pods together onto the same node. Pod anti-affinity pushes them apart. Both use topologyKey to define the granularity of placement.

Verify the cluster - three nodes are available:

kubectl get nodes

Task 1 - Pod Affinity

A cache pod is already running. Deploy an app pod that must be scheduled on the same node using required pod affinity.

Steps:

  • Create pod named app, image nginx:alpine
  • requiredDuringSchedulingIgnoredDuringExecution
  • labelSelector.matchLabels: app: cache
  • topologyKey: kubernetes.io/hostname
kubectl get pod cache app -o wide
Hint: Pod affinity structure
affinity:
  podAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - labelSelector:
        matchLabels:
          <label-key>: <label-value>
      topologyKey: kubernetes.io/hostname

Task 2 - Pod Anti-Affinity

Deploy a web Deployment where no two replicas land on the same node.

Steps:

  • Create Deployment named web, image nginx:alpine, 3 replicas, label app: web
  • requiredDuringSchedulingIgnoredDuringExecution anti-affinity to itself
  • topologyKey: kubernetes.io/hostname
kubectl get pods -l app=web -o wide
Hint: Pod anti-affinity structure
affinity:
  podAntiAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
    - labelSelector:
        matchLabels:
          <label-key>: <label-value>
      topologyKey: kubernetes.io/hostname

This challenge is part of the Kubernetes Pod Scheduling skill path.