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.yamlcommand to create thelabeled-pod. We have assignedS1label to this pod.Once the pod is created, execute the
kubectl get pods -o widecommand to check the pod details, specifically the pod's name and the node starting withk3d-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 labelS1is running on this node.Now execute the
kubectl apply -f anti-affinity-pod.yamlcommand to createreq-anti-affinity-pod. Once the pod is created, check the pods again usingkubectl get pods -o widecommand. You'll see two pods running on different nodes. Now apply theanti-affinity-pod-1.yamlfile 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 aus-east-1alabel.
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:latestFree Resources