PART 6: HANDS-ON LABS
Lab 1: Node Selectors and Node Affinity
Goal: Practice node selection.
bash
# Label a node
kubectl get nodes
NODE=$(kubectl get nodes -o jsonpath='{.items[0].metadata.name}')
kubectl label nodes $NODE disktype=ssd
# Pod with node selector
kubectl run selector-pod --image=nginx --overrides='
{
"spec": {
"nodeSelector": {"disktype": "ssd"},
"containers": [{"name": "nginx", "image": "nginx"}]
}
}'
kubectl get pod selector-pod -o wide
kubectl delete pod selector-pod
# Pod with node affinity (required)
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: affinity-pod
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: disktype
operator: In
values: [ssd, nvme]
containers:
- name: nginx
image: nginx
EOF
kubectl get pod affinity-pod -o wide
kubectl delete pod affinity-pod
# Preferred affinity
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: preferred-pod
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: disktype
operator: In
values: [ssd]
containers:
- name: nginx
image: nginx
EOF
kubectl get pod preferred-pod -o wide
kubectl delete pod preferred-pod
# Cleanup
kubectl label nodes $NODE disktype-
Lab 2: Pod Affinity and Anti-Affinity
Goal: Practice pod co-location and spreading.
bash
# Create a "cache" Pod
kubectl run cache --image=redis:7-alpine --labels=app=cache
# Pod with pod affinity (co-locate with cache)
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: affinity-to-cache
spec:
affinity:
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: cache
topologyKey: kubernetes.io/hostname
containers:
- name: app
image: nginx
EOF
# Both should be on the same node
kubectl get pods -o wide
kubectl delete pod affinity-to-cache
# Pod anti-affinity
kubectl create deployment web --image=nginx --replicas=3
kubectl patch deployment web --type=merge -p='
{
"spec": {
"template": {
"spec": {
"affinity": {
"podAntiAffinity": {
"preferredDuringSchedulingIgnoredDuringExecution": [{
"weight": 100,
"podAffinityTerm": {
"labelSelector": {"matchLabels": {"app": "web"}},
"topologyKey": "kubernetes.io/hostname"
}
}]
}
}
}
}
}
}'
kubectl get pods -l app=web -o wide
# Should be on different nodes if possible
# Cleanup
kubectl delete deployment web
kubectl delete pod cache
Lab 3: Taints and Tolerations
Goal: Practice taints and tolerations.
bash
# Get a node
NODE=$(kubectl get nodes -o jsonpath='{.items[0].metadata.name}')
# Taint the node
kubectl taint nodes $NODE dedicated=gpu:NoSchedule
# Pod without toleration (should fail)
kubectl run no-tol --image=nginx
sleep 10
kubectl get pod no-tol
# Should stay Pending
# Check why
kubectl describe pod no-tol | grep -A3 Events
# Pod with toleration (should schedule)
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: with-tol
spec:
tolerations:
- key: dedicated
operator: Equal
value: gpu
effect: NoSchedule
containers:
- name: nginx
image: nginx
EOF
sleep 10
kubectl get pod with-tol
# Cleanup
kubectl delete pod no-tol with-tol
kubectl taint nodes $NODE dedicated-
Lab 4: Priority Classes and Preemption
Goal: Demonstrate preemption.
bash
# Create priority classes
kubectl apply -f - <<'EOF'
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: low-priority
value: 100
---
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: high-priority
value: 1000
EOF
# Create a low-priority Pod (hog resources)
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: low-pod
spec:
priorityClassName: low-priority
containers:
- name: hog
image: nginx
resources:
requests:
cpu: "500m"
memory: "500Mi"
EOF
# Create a high-priority Pod
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: high-pod
spec:
priorityClassName: high-priority
containers:
- name: app
image: nginx
resources:
requests:
cpu: "500m"
memory: "500Mi"
EOF
# Watch the low-pod get preempted if resources are tight
# Cleanup
kubectl delete pod low-pod high-pod
kubectl delete priorityclass low-priority high-priority
Lab 5: Topology Spread Constraints
Goal: Distribute Pods across nodes.
bash
# Create a Deployment with topology spread
kubectl apply -f - <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: spread-app
spec:
replicas: 4
selector:
matchLabels:
app: spread-app
template:
metadata:
labels:
app: spread-app
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: spread-app
containers:
- name: app
image: nginx
EOF
# Verify spread
kubectl get pods -l app=spread-app -o wide
# Should be evenly distributed across nodes
# Cleanup
kubectl delete deployment spread-app
Lab 6: Custom Scheduler Plugin
Goal: Understand the Scheduler Framework.
Note: Writing a plugin requires Go and the Kubernetes scheduler framework. This lab is conceptual.
go
// Simple Go plugin outline
package myscheduler
import (
"k8s.io/kubernetes/pkg/scheduler/framework"
)
type MyPlugin struct{}
func (p *MyPlugin) Name() string {
return "MyPlugin"
}
func (p *MyPlugin) Score(
ctx context.Context,
state *framework.CycleState,
pod *v1.Pod,
nodeName string,
) (int64, *framework.Status) {
// Custom scoring logic
return 50, nil
}
// New initializes the plugin
func New(
plArgs runtime.Object,
h framework.Handle,
) (framework.Plugin, error) {
return &MyPlugin{}, nil
}
Register the plugin in KubeSchedulerConfiguration.
Lab 7: Debug a Pending Pod
Goal: Diagnose scheduling failures.
bash
# Create a Pod with impossible resource requirements
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: impossible
spec:
containers:
- name: app
image: nginx
resources:
requests:
cpu: "1000"
memory: "1Ti"
EOF
sleep 10
kubectl get pod impossible
# Should be Pending
# Diagnose
kubectl describe pod impossible | grep -A10 Events
# Look for FailedScheduling events
# Check node resources
kubectl describe nodes | grep -A5 "Allocated resources"
# Try to fix
kubectl delete pod impossible
# Try with a bad node selector
kubectl apply -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
name: bad-selector
spec:
nodeSelector:
nonexistent: "true"
containers:
- name: app
image: nginx
EOF
sleep 10
kubectl describe pod bad-selector | grep -A5 Events
kubectl delete pod bad-selector
Lab 8: Descheduler in Action
Goal: Rebalance a cluster with the descheduler.
bash
# Create imbalanced workload
kubectl create deployment imbalanced --image=nginx --replicas=6
# Force all replicas onto one node (anti-affinity off)
kubectl patch deployment imbalanced --type=json -p='[
{"op":"add","path":"/spec/template/spec/nodeSelector","value":{"kubernetes.io/hostname":"<node-1-name>"}}
]'
kubectl get pods -l app=imbalanced -o wide
# All on same node
# Install descheduler (simplified - requires more setup in production)
# kubectl apply -f https://raw.githubusercontent.com/kubernetes-sigs/descheduler/master/kubernetes/base/rbac.yaml
# ... (full descheduler install)
# In production, descheduler would:
# 1. Detect imbalance
# 2. Evict Pods violating policies
# 3. Scheduler re-places them
# Cleanup
kubectl delete deployment imbalanced
Lab 9: Complete Scheduling Strategy
Goal: Build a complete scheduling strategy for a production workload.
bash
# Create namespaces
kubectl create namespace production
# Label nodes
kubectl label nodes --all environment=production
# Taint a "spot" node
# kubectl taint nodes <spot-node> lifecycle=spot:NoSchedule
# Deploy a highly-available workload
kubectl apply -n production -f - <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 6
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
# Priority for production
priorityClassName: system-cluster-critical
# Spread across zones
topologySpreadConstraints:
- maxSkew: 1
topologyKey: topology.kubernetes.io/zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
app: web
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: ScheduleAnyway
labelSelector:
matchLabels:
app: web
# Prefer SSD nodes
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 100
preference:
matchExpressions:
- key: environment
operator: In
values: [production]
containers:
- name: web
image: nginx:1.25-alpine
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 256Mi
readinessProbe:
httpGet:
path: /
port: 80
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: web-pdb
spec:
minAvailable: 4
selector:
matchLabels:
app: web
EOF
# Verify
kubectl get pods -n production -o wide
kubectl get pdb -n production
# Cleanup
kubectl delete namespace production
1:1 copy of the class Google Doc, code in code blocks.