CodeBlanks Logo Icon CodeBlanks

Metrics server

Learn that the metrics server is required for HPA.

Metrics server

The HPA gets CPU and memory metrics from metrics-server; metrics-server in turn collects them from the kubelet on each node. If it isn't installed, the HPA can do nothing and kubectl top doesn't work:

$ kubectl top pods
error: Metrics API not available

minikube addons enable metrics-server
# or
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

$ kubectl top pods -l app=api
NAME                  CPU(cores)   MEMORY(bytes)
api-7f8c9d6b5-abcde   142m         180Mi

Testing under load

To see the HPA, artificial load is generated and watched with -w. Because of the metric collection and decision intervals, the reaction can take a minute or two:

kubectl run yuk --image=busybox:1.36 --restart=Never -- \
  sh -c "while true; do wget -q -O- http://api:8000/; done"
kubectl get hpa api -w
NAME   TARGETS         REPLICAS
api    cpu: 38%/70%    2
api    cpu: 156%/70%   2
api    cpu: 156%/70%   5
api    cpu: 64%/70%    5
kubectl delete pod yuk

Practice A load test

Write three commands: enable the metrics-server add-on in Minikube; show the resource usage of the pods labelled app=api; watch the api HPA live.

Starter code

terminal
# buraya yaz

Sample solution

terminal
minikube addons enable metrics-server
kubectl top pods -l app=api
kubectl get hpa api -w

Expected output

🌟  The 'metrics-server' addon is enabled
NAME                  CPU(cores)   MEMORY(bytes)
api-7f8c9d6b5-abcde   312m         190Mi
NAME   REFERENCE        TARGETS         MINPODS   MAXPODS   REPLICAS
api    Deployment/api   cpu: 156%/70%   2         10        2
api    Deployment/api   cpu: 156%/70%   2         10        5

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is needed to scale on application metrics like request count?

  • It can't be scaled
  • A custom metrics provider like the Prometheus Adapter or KEDA Correct answer
  • Only metrics-server
  • Bigger nodes

Explanation: metrics-server only provides CPU and memory.

2 Show the pods' current resource usage.

Question
kubectl ____ pods -l app=api
Answer
kubectl top pods -l app=api

Explanation: kubectl top reads metrics-server data.

3 Match.

Term Meaning
metrics-server CPU/memory metrics
Metrics API not available No metrics-server
KEDA Event-driven scaling
hpa -w Watch the scaling

All lessons in Advanced