PART 6: HANDS-ON LABS
Lab 1: Configure HPA with CPU Metrics
bash
# Deploy a workload
kubectl create deployment web --image=nginx:1.25-alpine --replicas=2
# Set resource requests
kubectl set resources deployment web --requests=cpu=100m,memory=128Mi
# Create HPA
kubectl autoscale deployment web --min=2 --max=10 --cpu-percent=70
# Verify
kubectl get hpa
kubectl describe hpa web
# Generate load
kubectl run load --image=busybox --restart=Never -- \
sh -c "while true; do wget -q -O- http://web; done"
# Watch scaling
kubectl get hpa web -w
# Cleanup
kubectl delete pod load
kubectl delete hpa web
kubectl delete deployment web
Lab 2: Tune HPA Behavior Policies
bash
kubectl create deployment web --image=nginx:1.25-alpine --replicas=2
kubectl set resources deployment web --requests=cpu=100m
cat <<'EOF' | kubectl apply -f -
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
behavior:
scaleUp:
stabilizationWindowSeconds: 0
policies:
- type: Percent
value: 100
periodSeconds: 15
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 25
periodSeconds: 60
EOF
# Test
kubectl get hpa web-hpa -w
# Cleanup
kubectl delete hpa web-hpa
kubectl delete deployment web
Lab 3: VPA in Recommendation Mode
bash
# Install VPA
kubectl apply -f https://github.com/kubernetes/autoscaler/raw/master/vertical-pod-autoscaler/deploy/vpa-v1-crd-gen.yaml
# Deploy a workload
kubectl create deployment web --image=nginx:1.25-alpine
# Create VPA
cat <<'EOF' | kubectl apply -f -
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: web-vpa
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: web
updatePolicy:
updateMode: "Off"
EOF
# Wait for recommendations
sleep 60
kubectl describe vpa web-vpa
# Look at Recommendation section
# Cleanup
kubectl delete vpa web-vpa
kubectl delete deployment web
Lab 4: KEDA with a Message Queue
Prerequisites: KEDA installed, a message queue (RabbitMQ, Kafka) available.
bash
# Install KEDA
helm repo add kedacore https://kedacore.github.io/charts
helm install keda kedacore/keda -n keda --create-namespace
# Deploy a consumer
kubectl create deployment consumer --image=myconsumer:1.0
# Create ScaledObject
cat <<'EOF' | kubectl apply -f -
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: consumer-scaler
spec:
scaleTargetRef:
name: consumer
minReplicaCount: 0
maxReplicaCount: 20
triggers:
- type: rabbitmq
metadata:
protocol: amqp
queueName: orders
mode: QueueLength
value: "5"
authenticationRef:
name: rabbitmq-auth
EOF
# Watch scaling
kubectl get hpa -w
# Cleanup
kubectl delete scaledobject consumer-scaler
kubectl delete deployment consumer
Lab 5: Deploy Karpenter and Observe Consolidation
Prerequisites: AWS EKS cluster, Karpenter installed.
bash
# Create a NodePool
cat <<'EOF' | kubectl apply -f -
apiVersion: karpenter.sh/v1
kind: NodePool
metadata:
name: default
spec:
template:
spec:
requirements:
- key: karpenter.sh/capacity-type
operator: In
values: ["spot", "on-demand"]
- key: karpenter.k8s.aws/instance-family
operator: In
values: ["t3", "t3a", "t2"]
limits:
cpu: 100
memory: 100Gi
disruption:
consolidationPolicy: WhenUnderutilized
expireAfter: 720h
EOF
# Deploy workloads
kubectl create deployment web --image=nginx --replicas=5
# Watch nodes
kubectl get nodes -w
# Scale down and observe consolidation
kubectl scale deployment web --replicas=1
kubectl get nodes -w
# Karpenter should consolidate
# Cleanup
kubectl delete deployment web
kubectl delete nodepool default
Lab 6: Spot Instance Node Pool with Fallback
yaml
# NodePool with spot priority and on-demand fallback
apiVersion: karpenter.sh/v1
kind: NodePool
metadata:
name: spot-first
spec:
template:
spec:
requirements:
- key: karpenter.sh/capacity-type
operator: In
values: ["spot", "on-demand"]
- key: karpenter.k8s.aws/instance-family
operator: In
values: ["t3", "t3a", "m5", "m5a"]
limits:
cpu: 200
memory: 400Gi
disruption:
consolidationPolicy: WhenUnderutilized
yaml
# Workload with spot toleration
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 10
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
tolerations:
- key: karpenter.sh/capacity-type
operator: Equal
value: "spot"
effect: NoSchedule
containers:
- name: web
image: nginx:1.25-alpine
Lab 7: Bin-Packing with MostAllocated Scoring
bash
# Patch the scheduler config (on control plane node)
sudo tee /etc/kubernetes/scheduler-config.yaml <<'EOF'
apiVersion: kubescheduler.config.k8s.io/v1
kind: KubeSchedulerConfiguration
profiles:
- schedulerName: default-scheduler
pluginConfig:
- name: NodeResourcesFit
args:
scoringStrategy:
type: MostAllocated
resources:
- name: cpu
weight: 1
- name: memory
weight: 1
EOF
# Update kube-scheduler static pod to use --config
# (Edit /etc/kubernetes/manifests/kube-scheduler.yaml)
# Test
kubectl create deployment web --image=nginx --replicas=10
kubectl get pods -o wide
# Pods should pack onto fewer nodes
# Cleanup
kubectl delete deployment web
Lab 8: Install Kubecost and Analyze Costs
bash
# Install Kubecost
helm repo add kubecost https://kubecost.github.io/cost-analyzer/
helm install kubecost kubecost/cost-analyzer \
--namespace kubecost --create-namespace
# Port-forward the dashboard
kubectl port-forward -n kubecost svc/kubecost-cost-analyzer 9090:9090
# Open http://localhost:9090
# Analyze:
# - Cost by namespace
# - Cost by deployment
# - Savings opportunities
# - Idle resources
# Cleanup
helm uninstall kubecost -n kubecost
kubectl delete namespace kubecost
Lab 9: End-to-End Cost Optimization
bash
kubectl create namespace cost-lab
# Deploy with right-sized requests
kubectl create deployment web --image=nginx:1.25-alpine -n cost-lab --replicas=2
kubectl set resources deployment web -n cost-lab \
--requests=cpu=100m,memory=128Mi \
--limits=cpu=500m,memory=256Mi
# HPA
cat <<'EOF' | kubectl apply -n cost-lab -f -
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
EOF
# PDB
cat <<'EOF' | kubectl apply -n cost-lab -f -
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: web-pdb
spec:
minAvailable: 1
selector:
matchLabels:
app: web
EOF
# Verify
kubectl get all,hpa,pdb -n cost-lab
# Cleanup
kubectl delete namespace cost-lab
1:1 copy of the class Google Doc, code in code blocks.