Pod Anti Affinity
Create a nginx deployment with 3 pods
Add a ‘podAntiAffinity’ section to the deployment
The goal is to distribute all pod for this deployment across different nodes.
Use the affinity
of this example to write the affinity
section in the deployment.yaml
file:
apiVersion: v1
kind: Pod
metadata:
name: with-pod-antiaffinity
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: security
operator: In
values:
- S1
topologyKey: topology.kubernetes.io/zone
containers:
- name: with-pod-affinity
image: registry.k8s.io/pause:2.0
Note
The topologyKey
value will be kubernetes.io/hostname
because pod will not be on the same nodes.
-
Check that all pods are running on different nodes using:
kubectl get pods -o wide
-
Scale the
Deployment
to five pods and try to understand what’s happening.