The affinity
spec, particularly the podAntiAffinity
section in Kubernetes, is used to control how pods are scheduled relative to each other
What is Pod Anti-Affinity?
Pod Anti-Affinity is a Kubernetes scheduling feature that prevents pods with specific labels from being placed on the same node or a set of nodes. It’s used to increase application availability and resilience by spreading out similar pods across different nodes.
Why is it needed?
This feature is critical for high-availability setups, fault tolerance, and load balancing. By ensuring that similar application pods are not hosted on the same physical or virtual machine, it reduces the risk of a single point of failure. This is particularly important for stateful applications or applications that require high availability.
How is it used?
requiredDuringSchedulingIgnoredDuringExecution
: This rule must be met for a pod to be scheduled onto a node. However, if labels on a node change after pod scheduling, it doesn’t affect the placement.
labelSelector
: This is used to select pods with certain labels. In your example, it selects pods with the label app
equal to nginx-controller
.
operator: In
: This specifies the operation to use when matching labels. In
means the label must equal one of the specified values.
values
: The values to match for the given key. In your case, it’s nginx-controller
.
topologyKey
: This specifies the key for the node label that the system uses to denote such a topology domain. In your case, it’s kubernetes.io/hostname
, which means the rule applies to the hostname, effectively spreading pods across different hosts.
==> Bạn có thể hiểu đơn giản như thế này:
Bạn đang cần phân phối đều các pod trên các node => vậy làm sao k8s phân biết các node khác nhau
=> Bạn bảo k8s nhìn vào kubernetes.io/hostname
trên con node sẽ thấy giá trị nó khác nhau.
Nếu bạn muốn hiều nhiều loại hơn thì xem video bên dưới