Kubernetes 101 : Pod Affinity - Pods co-location -
The node affinity:
For example we could use node affinity to schedule resource-hungry pods on nodes that have more RAM or faster CPUs.
The pod affinity:
Pod affinity allows us to have pods that "work" together on the same node, same cloud region, ...
For example we could schedule a "web server pod" and a "database pod" on the same node.
We also have a Yaml file of a "database pod" with pod affinity for the pods labeled "nginx_server":
Pods affinity and anti-affinity:
Pod affinity example:
Below is a Yaml file of an "nginx" pod.
We deploy the "nginx" pod first.
We can create the above pods using the below command:
Remark:
The "topologyKey: kubernetes.io/hostname" means that the pods with "pod affinity" attributes should be on the same host.
Pods affinity and anti-affinity:
The "requiredDuringSchedulingIgnoredDuringExecution" and
"preferredDuringSchedulingIgnoredDuringExecution" are the two types of pod affinity and anti-affinity.
We have a "required" and "preferred" version.
The "topologyKey" instruction:
In our example above, the "topologyKey" specifies that the pods with affinity need to be located on the same node.
Below some examples of topology keys:
- kubernetes.io/hostname
- beta.kubernetes.io/os
- beta.kubernetes.io/arch
- beta.kubernetes.io/instance-type
- -------
Remark:
Whether Pods get scheduled on the same host, region, ..., depends on the values of both the topologyKey and the label selector.
Comments