1. Cloud resource optimization
Pick the right instance type and size, with the goal of avoiding overprovisioning your nodes. Make sure that the instance you choose addresses the CPU, memory, and network requirements of your application.
2. Pod resource configuration
Set realistic requests and limits for CPU and memory. Check out this guide to learn how to do that.
3. Pod affinity and anti-affinity
Make good use of labels, and the Kubernetes scheduler will make sure that pods with matching labels or label sets are co-located on the same node or scheduled to different nodes. More details on this topic here.
You can also specify these rules within the affinity section using the podAffinity and podAntiAffinity fields in the pod spec.
- Pod affinity assumes that a pod can run on a specific node if there is already a pod meeting particular conditions.
- Pod anti-affinity prevents pods from running on the same node as pods matching particular criteria.
4. Node affinity
You can set how pods get matched to nodes with labels that specify to which nodes Kube-scheduler should schedule your pods.
You can specify that by adding the .spec.affinity.nodeAffinity field in your pod.
Remember that if you specify nodeSelector and nodeAffinity, both need to be met for the pod to be scheduled.
There are two types of node affinity:
- requiredDuringSchedulingIgnoredDuringExecution – the scheduler will only schedule the pod if the node meets the rule.
- preferredDuringSchedulingIgnoredDuringExecution– the scheduler will try to find a node matching the rule, but it will still schedule the pod even if it doesn’t find anything suitable.
This is quite a lot of work. But it’s definitely worth it since a third of the cloud capacity teams get goes to waste.