Monitoring Horizontal Pod Autoscaling in Kubernetes
Fix Apache Issue
1. Kubernetes Resources #
Especially in production Kubernetes clusters, CPU and memory are precious resources. Cluster operators normally require you to specify the CPU and memory your Pod needs so the scheduler can place it on a Node with sufficient capacity.
You do this by adding a resources section to the container specification:
resources:
limits: # Maximum amount of CPU & memory for peak use
cpu: "8" # Maximum of 8 CPU cores allowed at peak use
memory: "4Mi" # Maximum allowed 4 MiB of memory
requests: # Expected amount of CPU & memory for normal use
cpu: "6" # Requesting 6 CPU cores
memory: "2Mi" # Requesting 2 MiB of memoryMore information can be found here: Resource Management for Pods and Containers
If a container exceeds its memory limit, Kubernetes terminates and restarts it (OOMKilled) to protect other workloads on the Node.
If there is not enough unallocated CPU or memory on any Node to satisfy a Pod’s requests, the scheduler leaves the Pod in Pending state until capacity becomes available.
2. Fix the CPU Request #
Before making changes, check Alerts & Detectors in Splunk Observability Cloud to see which detector has fired for the apache namespace (you can also see this in Kubernetes Navigator and Log Observer).
Edit ~/workshop/k3s/apache-hpa.yaml to reduce the CPU resources:
vim ~/workshop/k3s/apache-hpa.yamlFind the resources section and set the CPU limit to 1 and the CPU request to 0.5:
resources:
limits:
cpu: "1"
memory: "4Mi"
requests:
cpu: "0.5"
memory: "2Mi"Save the file (press Esc, then :wq!).
Delete and recreate the StatefulSet so Kubernetes schedules apache-hpa-0 with the new CPU request (a StatefulSet’s RollingUpdate controller will not replace a pod that is stuck in Pending):
kubectl delete -f ~/workshop/k3s/apache-hpa.yaml -n apache
kubectl apply -f ~/workshop/k3s/apache-hpa.yaml -n apacheValidate the changes have been applied:
kubectl describe statefulset apache-hpa -n apacheWatch the pod status in your terminal (kubectl get pods -n apache) and in Splunk Observability Cloud.
Quick check
After fixing the CPU request, why does the apache-hpa-0 pod fail to stay healthy?
4Mi memory limit (OOMKilled), so it restarts repeatedly and Apache metrics drop out.3. Fix the Memory Limit #
Edit the StatefulSet live to give Apache enough memory to run under load:
kubectl edit statefulset apache-hpa -n apacheUpdate the resources section to match:
resources:
limits:
cpu: "1"
memory: 32Mi
requests:
cpu: 500m
memory: 16MiSave and exit (Esc, then :wq!). Because apache-hpa-0 is stuck in CrashLoopBackOff, delete the pod so the StatefulSet immediately recreates it with the updated memory limits:
kubectl delete pod apache-hpa-0 -n apacheVerify the pod is Running and healthy:
kubectl get pods -n apache
kubectl describe statefulset apache-hpa -n apacheConfirm in the Apache web servers (OTel) Navigator (filter by k8s.cluster.name = <your_cluster>) that metrics are now flowing steadily.
