Kubernetes Horizontal Pod Autoscaling with custom metrics API

Table of Contents

Introduction

This is a second article in a series dedicated to installing and configuring Kubernetes Horizontal Pod Autoscaling:

  1. Horizontal Pod Autoscaling with metrics API
  2. Horizontal Pod Autoscaling with custom metrics API

In the first article I gave an overview of Kubernetes HPA and showed how to configure autoscaling with metrics API server running locally with minikube. Metrics API and HPA allows to trigger scaling events based on application CPU and Memory usage.

In this article, we will continue exploring Horizontal Pod Autoscaling and this time we will use custom metrics API.

Source code for the demo app and kubernetes objects can be found here.

Overview of HPA with custom metrics api

Configuring HPA with custom metrics api

Requirements

  • Kubernetes cluster with installed:
    • Prometheus operator
    • Prometheus adapter
  • Kubectl
  • Helm

Starting Kubernetes cluster and installing kubectl with helm

  1. To start kubernetes cluster and install kubectl please refer to the first article.
  2. Helm is a package manager for Kubernetes, it allows to manage complex application deployments with templates. Please follow the official instructions to install helm cli. Check if you have successfully installed helm:
$ helm version
version.BuildInfo{Version:"v3.7.1", GitCommit:"1d11fcb5d3f3bf00dbe6fe31b8412839a96b3dc4", GitTreeState:"clean", GoVersion:"go1.17.2"}

Installing Prometheus server

We will be using official helm chart for kube prometheus stack.

First we need to add helm repository:

$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
"prometheus-community" has been added to your repositories
$ helm repo update
...Successfully got an update from the "prometheus-community" chart repository
Update Complete. ⎈Happy Helming!⎈

Prometheus stack helm chart installs prometheus operator (Kubernetes native deployment of prometheus), prometheus Alertmanager, grafana and for other components for scraping kubernetes metrics. We don’t need to use Alertmanager in the scope of this article, so we will disable in on helm install command. We will be installing in prometheus resources in a dedicated namespace prometheus-resources.

$ kubectl create ns prometheus-resources
$ helm search repo prometheus-community/kube-prometheus-stack --versions
$ helm -n prometheus-resources install kube-prometheus-stack prometheus-community/kube-prometheus-stack --version=44.3.0 --set alertmanager.enabled=false

Instrumenting your application with custom metrics endpoint

Configuring HPA with custom metrics of your app

Configuring HPA with ingress controller on your application ingress

HPA in action

HPA configurations

Summary

Ilya Mochalov
Ilya Mochalov
DevOps and Platform Engineering

IT professional living in Shanghai, China