Inter-pod anti-affinity demo

Inter-pod anti-affinity is a concept used in Kubernetes to add constraints on pods during scheduling. It is very useful when we want to introduce high availability and fault tolerance in our application. Anti-affinity rule says that the pod defining this affinity rule should not run on a node with key “X” if that node already has a pod with the label “Y.”

Let’s say we want to create a pod to test an application, and we already have a pod running an application in the us-east-1 region. We add anti-affinity rules to this pod so that it can not be scheduled on a node in the region us-east-1 if the node has a running pod labeled “production.”

Types of anti-affinity

There are two types of inter-pod anti-affinity. The first is "hard," and the second is "soft." The "hard" type requires the rules to be fulfilled, while the "soft" type shows the preference for fulfilling the rules.

  • requiredDuringSchedulingIgnoredDuringExecution – The "hard" type tells the scheduler to check the rules only during scheduling and ignore them during execution.

  • preferredDuringSchedulingIgnoredDuringExecution – The "soft" type tells the scheduler to only check the rules preferences during scheduling.

Demo

In the widget below, we are creating a Kubernetes cluster with two worker nodes. We have two Yaml files labeled-pod.yaml and anti-affinity-pod.yaml. We first create a labeled-pod, add a label to the node on which the labeled-pod is running. After that, we execute the anti-affinity-pod.yaml file to create a req-anti-affinity-pod pod. This new pod will not run on the node on which labeled-pod is running.

Follow the steps below to execute the pods.

  • Click the "Run" button and wait until the cluster is created and the terminal is ready.

Note: Wait for 2–3 minutes once the terminal is ready and then execute the following commands.

  • Execute the kubectl apply -f labeled-pod.yaml command to create the labeled-pod. We have assigned S1 label to this pod.

  • Once the pod is created, execute the kubectl get pods -o wide command to check the pod details, specifically the pod's name and the node starting with k3d-edu-affinity-.

  • Then execute the kubectl label node <node name of the labeled-pod> topology.kubernetes.io/zone=us-east-1a. Now, the label is added to the node, and a pod with a label S1 is running on this node.

  • Now execute the kubectl apply -f anti-affinity-pod.yaml command to create req-anti-affinity-pod. Once the pod is created, check the pods again using kubectl get pods -o wide command. You'll see two pods running on different nodes. Now apply the anti-affinity-pod-1.yaml file to create a new pod and check its node. You'll see that both the pods with anti-affinity rules are running on a different node than the node having a us-east-1a label.

apiVersion: v1
kind: Pod
metadata:
  name: req-anti-affinity-pod-1
spec:
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: security
            operator: In
            values:
            - S1
        topologyKey: topology.kubernetes.io/zone
  containers:
  - name: nginx
    image: nginx:latest

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved