Kubernetes is a powerful platform for managing containerized applications, and one of its core features is the ability to schedule pods across a cluster. While basic scheduling strategies are sufficient for many use cases, advanced scheduling allows you to optimize resource utilization, ensure high availability, and meet specific business requirements.
In this tutorial, we will explore advanced scheduling strategies in Kubernetes, including node affinity, pod affinity/anti-affinity, taints and tolerations, and custom resources. These features provide fine-grained control over how pods are scheduled within a cluster.
Node affinity allows you to specify that certain pods should be scheduled on nodes with specific labels. This is useful for scenarios where you want to ensure that your application runs on nodes with certain characteristics, such as specific hardware or geographic location.
There are two types of node affinity:
Pod affinity and anti-affinity allow you to specify that certain pods should be scheduled in relation to other pods. This is useful for scenarios where you want to ensure that your application runs on nodes with or without specific pods, such as collocating services or spreading load.
There are also two types of pod affinity/anti-affinity:
Taints and tolerations allow you to repel certain pods from nodes or attract them to specific nodes. This is useful for scenarios where you want to isolate critical workloads or ensure that certain types of workloads run on dedicated nodes.
Custom resources allow you to define your own scheduling strategies using custom logic. This is useful for scenarios where you have specific requirements that cannot be met with the built-in scheduling features.
Let's create a pod that requires nodes with a specific label, such as gpu=true.
apiVersion: v1
kind: Pod
metadata:
name: gpu-pod
spec:
containers:
- name: cuda-container
image: nvidia/cuda:9.0-base
resources:
limits:
nvidia.com/gpu: 2 # requesting 2 GPUs
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: gpu
operator: In
values:
- "true"
pod/database-pod created pod/web-app-pod created
Let's taint a node to repel all pods except those that tolerate the taint.
pod/critical-pod created
Creating custom resources for advanced scheduling is more complex and typically involves defining a custom resource definition (CRD) and implementing custom logic. This example will not cover the full implementation but will give you an idea of how to start.
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: schedulingstrategies.example.com
spec:
group: example.com
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
strategy:
type: string
scope: Namespaced
names:
plural: schedulingstrategies
singular: schedulingstrategy
kind: SchedulingStrategy
shortNames:
- ss
schedulingstrategy.example.com/custom-strategy created
In this tutorial, we explored advanced scheduling strategies in Kubernetes, including node affinity, pod affinity/anti-affinity, taints and tolerations, and custom resources. These features provide powerful tools for optimizing your Kubernetes cluster.
Next, you might want to explore "Kubernetes Advanced Network Policy Management Tools" to further enhance the security and isolation of your applications within the cluster.