In this lesson, we will explore advanced scheduling techniques and resource management in Kubernetes. Understanding how to effectively manage resources and schedule workloads is crucial for optimizing application performance and ensuring efficient use of cluster resources.
The Kubernetes Scheduler is responsible for assigning Pods to Nodes based on resource availability and scheduling policies. It considers various factors, including resource requests, constraints, and affinity rules.
You can create a custom scheduler if the default behavior does not meet your requirements. Here’s a basic example of a custom scheduler configuration:
apiVersion: v1
kind: ConfigMap
metadata:
name: custom-scheduler-config
data:
scheduler.conf: |
apiVersion: scheduling.k8s.io/v1
kind: Scheduler
name: custom-scheduler
... # additional configuration
Node Affinity allows you to constrain which nodes your Pods are eligible to be scheduled based on labels on the nodes. Taints and tolerations are mechanisms that allow nodes to repel certain Pods unless they tolerate the taint.
Here’s an example of a Pod specification using node affinity:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: my-container
image: my-image:latest
To apply a taint to a node:
kubectl taint nodes my-node key=value:NoSchedule
And here’s how to add a toleration to a Pod:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
tolerations:
- key: "key"
operator: "Equal"
value: "value"
effect: "NoSchedule"
containers:
- name: my-container
image: my-image:latest
Resource requests and limits allow you to specify the minimum and maximum amount of CPU and memory that a container can use. This helps the scheduler make better decisions and ensures that your applications do not consume more resources than intended.
Here’s a Pod configuration that specifies resource requests and limits:
apiVersion: v1
kind: Pod
metadata:
name: resource-limits-pod
spec:
containers:
- name: my-container
image: my-image:latest
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Kubernetes classifies Pods into three QoS classes based on their resource requests and limits: - Guaranteed: If limits and requests are set and equal. - Burstable: If limits are set, but requests are lower than limits. - BestEffort: If no requests or limits are set.
| QoS Class | Requests | Limits |
|---|---|---|
| Guaranteed | Equal | Equal |
| Burstable | Lower | Higher |
| BestEffort | None | None |
Best Practice: Always set resource requests and limits to prevent resource contention. Common Mistake: Forgetting to use tolerations when applying taints to nodes can lead to Pods not being scheduled.
Best Practice: Use node affinity to schedule Pods on specific nodes that meet certain criteria. Common Mistake: Overloading nodes with too many Pods can lead to performance degradation.
In this lesson, we covered advanced scheduling techniques and resource management in Kubernetes. Understanding these concepts is essential for optimizing your applications and ensuring efficient resource usage in your Kubernetes cluster.
zone=us-west on one of your nodes:
bash
kubectl label nodes <node-name> zone=us-westzone=us-west.100Mi memory and 200m CPU.200Mi memory and 400m CPU.