Topic outline

  • Scaling our Apps horizontally

    The Horizontal Pod Autoscaler (HPA)

    With HPA you can scale your services up or down depending on CPU, Memory or Custom Metrics. This can be done through creating and managing HPAs with kubectl or HPA manifest definitions.


    In the following we're going to deploy a sample app and see how HPA works. We deploy first the hpa-sample app which is given a 200 millicpu CPU limit which serves on port 80 and create a HPA resource for the hpa-sample deployment and targets 50 percent CPU utilization for the deployment, with a minimum of one pod and a maximum of ten pods. When the average CPU load is below 50 percent, the autoscaler tries to reduce the number of pods in the deployment, to a minimum of one. When the load is greater than 50 percent, the autoscaler tries to increase the number of pods in the deployment, up to a maximum of ten. After the HPA is created, we run a busybox container and do a loop call to the hpa-example service to see how our pods are autoscaling.

    # Create a deployment
    k run hpa-example --image=k8s.gcr.io/hpa-example --requests=cpu=100m --limits=cpu=200m --expose --port=80
    # create HPA based on CPU usage
    kubectl autoscale deployment hpa-example --dry-run --cpu-percent=50 --min=1 --max=10 -o yaml > hpa-example.yaml
    cat hpa-example.yaml
    k create -f hpa-example.yaml
    # In another terminal run
    k run -i --tty generate-load --image=busybox /bin/sh
    # Inside the above container run a loop bash command to stress the CPU
    while true; do wget -q -O- http://hpa-example.default.svc.cluster.local; done
    # Check HPA Status
    k get hpa
    # Check the pods
    k get pods

    • HPA with Custom Metrics

      This is a nice WIP:
      kubectl create ns monitoring
      kn monitoring
      helm install stable/prometheus-operator --name promo --version=8.13.0
      k create -f promo-ingress.yaml
      k get ing
      # NAME HOSTS ADDRESS PORTS AGE
      # prometheus-ingress prom.kubernauts.local 192.168.64.23 80 28m
      # add to /etc/hosts
      # 192.168.64.23 prom.kubernauts.local
      open http://prom.kubernauts.local/
      helm install --name prometheus-adapter --set logLevel=1 stable/prometheus-adapter --set prometheus.url="http://prom.kubernauts.local",prometheus.port="80"
      # wait for few minutes
      kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1
      # {"kind":"APIResourceList","apiVersion":"v1","groupVersion":"custom.metrics.k8s.io/v1beta1","resources":[]}