For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/kubernetes-statefulset-has-no-pod-anti-affinity.md.
A documentation index is available at /llms.txt.
StatefulSets should define a podAntiAffinity policy to prevent scheduling multiple Pods from the same StatefulSet onto the same node.
The spec.template.spec.affinity.podAntiAffinity field must be set with either preferredDuringSchedulingIgnoredDuringExecution or requiredDuringSchedulingIgnoredDuringExecution. Each term must use topologyKey: kubernetes.io/hostname with a label selector that matches the Pod template labels.
This rule applies when spec.replicas is greater than 2.