-
Notifications
You must be signed in to change notification settings - Fork 40
15_HPA_Lab
deepak gupta edited this page Feb 9, 2022
·
6 revisions
In this lab we will use HPA to ensure that k8s will take care of scaling and de-scaling of pods of your MS on the basis of CPU & Memory usage :
- Enable autoscaling for Attendance MS with a threshold of 50% avg CPU usage of all instances.
By the end of this lab, you will have to be able to deploy your microservices in such a fashion that they can scale up & down on the basis of CPU/memory usages.
We have to restart minikube to enable some config for HPA
minikube start --extra-config=controller-manager.horizontal-pod-autoscaler-upscale-delay=1m --extra-config=controller-manager.horizontal-pod-autoscaler-downscale-delay=1m --extra-config=controller-manager.horizontal-pod-autoscaler-sync-period=10s --extra-config=controller-manager.horizontal-pod-autoscaler-downscale-stabilization=1m
minikube addons enable metrics-server
So firstly, we have to give resources and limits because HPA uses them to calculate usage and then scale it.
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-apache-<name>
spec:
selector:
matchLabels:
run: php-apache-<name>
replicas: 1
template:
metadata:
labels:
run: php-apache-<name>
spec:
containers:
- name: php-apache
image: k8s.gcr.io/hpa-example
ports:
- containerPort: 80
resources:
limits:
cpu: 500m
requests:
cpu: 200m
kubectl apply -f hpa.yaml
---
apiVersion: v1
kind: Service
metadata:
name: php-apache-<name>
labels:
run: php-apache-<name>
spec:
ports:
- port: 80
selector:
run: php-apache
kubectl apply -f hpa-service.yaml
Create an HPA manifest for attendance application
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: php-apache-<name>
spec:
maxReplicas: 10
minReplicas: 1
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-apache-<name>
targetCPUUtilizationPercentage: 50
kubectl apply -f php-hpa.yaml
Validate it by
kubectl get hpa -w
Open a new tab and run a pod with the name load-generator
kubectl run -it --rm load-generator --image=busybox /bin/sh
Run this command:-
while true; do wget -q -O- http://php-apache-<name>; done
And wait for 2 to 3 minutes and you will have something like this:-