In this lesson, we will explore advanced scheduling features and resource management techniques in Kubernetes. Understanding these concepts will help you optimize the performance and resource utilization of your applications in a Kubernetes environment.
Kubernetes scheduling is the process of selecting a suitable node for a pod to run on. While the default scheduler handles basic scheduling needs, advanced features allow for more sophisticated scheduling decisions:
Affinity rules allow you to specify that certain pods should be scheduled on the same node or on different nodes. This is useful for co-locating services that need to communicate frequently or isolating services for performance reasons.
Node Affinity
Node affinity allows you to constrain which nodes your pod is eligible to be scheduled based on labels on the nodes.
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: my-container
image: my-image
Pod Anti-Affinity
Pod anti-affinity allows you to specify that certain pods should not be scheduled on the same node.
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- my-app
topologyKey: "kubernetes.io/hostname"
containers:
- name: my-container
image: my-image
Taints and tolerations are mechanisms that allow a node to repel a set of pods unless those pods have matching tolerations.
Taint a Node
kubectl taint nodes my-node key=value:NoSchedule
Add Toleration to Pod
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
tolerations:
- key: "key"
operator: "Equal"
value: "value"
effect: "NoSchedule"
containers:
- name: my-container
image: my-image
Kubernetes allows you to manage resources effectively using requests and limits:
Resource requests and limits allow you to define the minimum and maximum resources that a container can use.
Define Requests and Limits
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: my-container
image: my-image
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Horizontal Pod Autoscaler (HPA) automatically scales the number of pods in a deployment based on observed CPU utilization or other select metrics.
Create HPA
kubectl autoscale deployment my-app --cpu-percent=50 --min=1 --max=10
Best Practice: Always set resource requests and limits to ensure that your applications have the resources they need without overwhelming the nodes.
Common Mistake: Forgetting to define tolerations when using taints can lead to pods not being scheduled on nodes where they are needed.
Create a Pod with Node Affinity: Modify the provided Pod YAML to add node affinity that requires the pod to run on nodes labeled with disktype: ssd.
Implement Taints and Tolerations: Taint a node with a key-value pair and create a Pod YAML that includes the toleration for this taint.
Set Resource Requests and Limits: Update an existing deployment to include resource requests for CPU and memory, ensuring it has a limit as well.