To help students practice the concept of Pod Priority and PriorityClasses in Kubernetes, we can create a hands-on lab where students will create and manage PriorityClass objects and assign them to pods. This lab will demonstrate how pod priority affects scheduling and eviction behavior in resource-constrained environments.
Here’s a step-by-step guide for the lab:
- A working Kubernetes cluster
kubectlCLI tool installed and configured to interact with your cluster.
In this step, students will create different PriorityClass resources to assign priorities to pods.
Task:
- Create three different PriorityClasses with varying priority values:
- high-priority: A high priority value for critical workloads.
- medium-priority: A moderate priority value.
- low-priority: A low priority value for non-essential workloads.
Instructions:
- Create a YAML file
priority-classes.yaml:
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: high-priority
value: 1000000
globalDefault: false
description: "High priority pods for critical workloads"
---
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: medium-priority
value: 500000
globalDefault: false
description: "Medium priority pods"
---
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: low-priority
value: 100000
globalDefault: false
description: "Low priority pods"- Apply the PriorityClass resources to the cluster:
kubectl apply -f priority-classes.yaml- Verify the PriorityClasses have been created:
kubectl get priorityclassThis should list the three PriorityClasses (high-priority, medium-priority, low-priority).
Now, students will create pods that use the PriorityClass resources they just created.
Task:
- Create three pods:
- A high-priority pod.
- A medium-priority pod.
- A low-priority pod.
Instructions:
- Create a YAML file
pods-with-priority.yaml:
apiVersion: v1
kind: Pod
metadata:
name: high-priority-pod
spec:
priorityClassName: high-priority
containers:
- name: nginx
image: nginx
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
---
apiVersion: v1
kind: Pod
metadata:
name: medium-priority-pod
spec:
priorityClassName: medium-priority
containers:
- name: nginx
image: nginx
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
---
apiVersion: v1
kind: Pod
metadata:
name: low-priority-pod
spec:
priorityClassName: low-priority
containers:
- name: nginx
image: nginx
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"- Apply the pod configuration:
kubectl apply -f pods-with-priority.yaml- Verify that the pods have been created:
kubectl get podsThe output will show that the pods high-priority-pod, medium-priority-pod, and low-priority-pod are running.
Next, students will simulate resource pressure on the cluster by running resource-intensive pods and observing how Kubernetes evicts lower-priority pods.
Task:
- Simulate resource pressure by creating BestEffort pods that consume a lot of resources and trigger the eviction process.
Instructions:
- Create a YAML file
best-effort-pods.yamlfor resource-hogging pods:
apiVersion: v1
kind: Pod
metadata:
name: resource-hogger-pod-1
spec:
containers:
- name: stress-test
image: polinux/stress
resources:
requests:
memory: "1Gi"
cpu: "500m"
limits:
memory: "2Gi"
cpu: "1"
---
apiVersion: v1
kind: Pod
metadata:
name: resource-hogger-pod-2
spec:
containers:
- name: stress-test
image: polinux/stress
resources:
requests:
memory: "1Gi"
cpu: "500m"
limits:
memory: "2Gi"
cpu: "1"- Apply the resource-hogging pods to the cluster:
kubectl apply -f best-effort-pods.yaml- Monitor the pod eviction process by checking the pod status:
kubectl get pods- Pods with lower priority, like the
low-priority-pod, may be evicted first to make room for higher-priority pods when the node reaches its resource limits.
-
Verify Pod Evictions:
- Use
kubectl describe pod <pod-name>to check the reason for eviction, e.g.,:
kubectl describe pod low-priority-pod
The Event section will show eviction details like:
Evicted: The node was under pressure, and pod was evicted - Use
Once the practice is complete, students should clean up the resources they’ve created to keep the cluster clean.
Task:
- Delete the created pods and PriorityClasses.
Instructions:
- Delete the created pods:
kubectl delete pod high-priority-pod medium-priority-pod low-priority-pod- Delete the resource-hogging pods:
kubectl delete pod resource-hogger-pod-1 resource-hogger-pod-2- Delete the PriorityClasses:
kubectl delete priorityclass high-priority medium-priority low-priorityIn this lab, students practiced:
- Creating and managing PriorityClass resources.
- Assigning different PriorityClasses to pods.
- Simulating node resource pressure to observe how Kubernetes handles pod evictions.
- Observing how higher-priority pods are less likely to be evicted than lower-priority pods under pressure.