Exercise
- Install metrics server with the –kubelet-insecure-tls flag and wait for it to work fine.
Note: it can be installed from https://luc.run/metrics-server.yaml
-
Create the specification of a Deployment named www with a single replica based on nginx
-
Modify the specification adding 100Mi memory and 50m cpu request and create the Deployment.
-
Expose the Pod with a Service named www
-
Create a HorizontalPodAutoscaller with a target CPU of 50% and with a minimum of 3 and a maximum of 10 replicas
-
Run a stress Pod (command below) and verify the number of replicas are increased
k run ab -ti --rm --restart='Never' --image=lucj/ab -- -n 100000 -c 50 http://www/
- Delete the HPA, the Deployment and the Service
Documentation
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
Solution
- Install metrics server with the –kubelet-insecure-tls flag and wait for it to work fine.
Installation:
k apply -f https://luc.run/metrics-server.yaml
Metrics is running fine when it returns nodes / pods metrics:
k top node
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
master 119m 5% 1242Mi 65%
worker1 33m 1% 962Mi 51%
worker2 48m 2% 987Mi 52%
k top pod
NAME CPU(cores) MEMORY(bytes)
mongo 5m 68Mi
nginx 0m 3Mi
- Create the specification of a Deployment named www with a single replica based on nginx
k create deploy www --image=nginx:1.20 --dry-run=client -o yaml > deploy.yaml
- Modify the specification adding 100Mi memory and 50m cpu request and create the Deployment.
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: www
name: www
spec:
replicas: 1
selector:
matchLabels:
app: www
template:
metadata:
labels:
app: www
spec:
containers:
- image: nginx:1.20
name: nginx
resources:
requests:
memory: 100Mi
cpu: 50m
Creation of the Deployment
k apply -f deploy.yaml
- Expose the Pod with a Service named www
k expose deploy/www --name=www --port=80
- Create a HorizontalPodAutoscaller with a target CPU of 50% and with a minimum of 3 and a maximum of 10 replicas
k autoscale deploy www --min=3 --max=10 --cpu-percent=50
- Run a stress Pod and verify the number of replicas are increased
k run ab -ti --rm --restart='Never' --image=lucj/ab -- -n 100000 -c 50 http://www/
See the evolution of the number of replicas:
k get hpa -w
- Delete the HPA, the Deployment and the Service
k delete deploy/www svc/www hpa/www