• Stars
    star
    296
  • Rank 139,992 (Top 3 %)
  • Language
    Java
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Autoscaling Spring Boot with the Horizontal Pod Autoscaler and custom metrics on Kubernetes

Autoscaling Spring Boot with the Horizontal Pod Autoscaler and custom metrics on Kubernetes

Prerequisites

You should have minikube installed.

You should start minikube with at least 4GB of RAM:

minikube start \
  --memory 4096 \
  --extra-config=controller-manager.horizontal-pod-autoscaler-upscale-delay=1m \
  --extra-config=controller-manager.horizontal-pod-autoscaler-downscale-delay=2m \
  --extra-config=controller-manager.horizontal-pod-autoscaler-sync-period=10s

If you're using a pre-existing minikube instance, you can resize the VM by destroying it an recreating it. Just adding the --memory 4096 won't have any effect.

You should install jq β€” a lightweight and flexible command-line JSON processor.

You can find more info about jq on the official website.

Installing Custom Metrics Api

Deploy the Metrics Server in the kube-system namespace:

kubectl create -f monitoring/metrics-server

After one minute the metric-server starts reporting CPU and memory usage for nodes and pods.

View nodes metrics:

kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes" | jq .

View pods metrics:

kubectl get --raw "/apis/metrics.k8s.io/v1beta1/pods" | jq .

Create the monitoring namespace:

kubectl create -f monitoring/namespaces.yaml

Deploy Prometheus v2 in the monitoring namespace:

kubectl create -f monitoring/prometheus

Deploy the Prometheus custom metrics API adapter:

kubectl create -f monitoring/custom-metrics-api

List the custom metrics provided by Prometheus:

kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1" | jq .

Package the application

You package the application as a container with:

eval $(minikube docker-env)
docker build -t spring-boot-hpa .

Deploying the application

Deploy the application in Kubernetes with:

kubectl create -f kube/deployment

You can visit the application at http://minkube_ip:32000

(Find the minikube ip address via minikube ip)

You can post messages to the queue by via http://minkube_ip:32000/submit?quantity=2

You should be able to see the number of pending messages from http://minkube_ip:32000/metrics and from the custom metrics endpoint:

kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/messages" | jq .

Autoscaling workers

You can scale the application in proportion to the number of messages in the queue with the Horizontal Pod Autoscaler. You can deploy the HPA with:

kubectl create -f kube/hpa.yaml

You can send more traffic to the application with:

while true; do curl -d "quantity=1" -X POST http://minkube_ip:32000/submit ; sleep 4; done

When the application can't cope with the number of incoming messages, the autoscaler increases the number of pods in 3 minute intervals.

You may need to wait three minutes before you can see more pods joining the deployment with:

kubectl get pods

The autoscaler will remove pods from the deployment every 5 minutes.

You can inspect the event and triggers in the HPA with:

kubectl get hpa spring-boot-hpa

Notes

The configuration for metrics and metrics server is configured to run on minikube only.

You won't be able to run the same YAML files for metrics and custom metrics server on your cluster or EKS, GKE, AKS, etc.

Also, there are secrets checked in the repository to deploy the Prometheus adapter.

In production, you should generate your own secrets and (possibly) not check them into version control.

If you wish to run metrics and custom metrics server in production, you should check out the following resources:

More Repositories

1

xlskubectl

xlskubectl β€” a spreadsheet to control your Kubernetes cluster
JavaScript
1,218
star
2

kubernetes-production-best-practices

A checklist of Kubernetes best practices to help you release to production
959
star
3

free-kubernetes

List of Free Trials of Managed Kubernetes Services
629
star
4

k8bit

A tiny Kubernetes dashboard
JavaScript
126
star
5

templating-kubernetes

Templating Kubernetes resources with *real* code
C#
103
star
6

laravel-kubernetes-demo

A demo application in Laravel to illustrate deploying to Kubernetes
PHP
97
star
7

knote-js

Code for the Knote course with Node.js
JavaScript
88
star
8

learnk8s.io

TypeScript
74
star
9

distributed-tensorflow-on-k8s

Python
48
star
10

multi-cluster

Creating and connecting multiple Kubernetes clusters using Terraform, Karmada & Istio
HTML
48
star
11

knote-java

Simple Spring Boot app to take notes
Java
48
star
12

kubernetes-instance-calculator-data

A collection of scripts to retrieve instance types and pricing from AWS, GCP and Azure.
JavaScript
44
star
13

kubernetes-challenge

Kubernetes challenge
JavaScript
42
star
14

terraform-aks

Getting started with Terraform and AKS
HCL
27
star
15

bash-ingress

Shell
27
star
16

scale-to-zero

JavaScript
24
star
17

kubernetes-resource-inspector

Scripts for getting the reserved resources
Shell
22
star
18

terraform-eks

Terraform script to spin up EKS cluster
HCL
17
star
19

first-steps

The material for the Kubernetes first steps course.
HTML
16
star
20

advanced-networking

"Advanced networking - Pod networking and CNI plugins" course resources
Shell
13
star
21

proactive-cluster-autoscaler

JavaScript
13
star
22

autoscaling

Example code for the Autoscaling module in the Learnk8s Academy
Go
9
star
23

authentication

Kubernetes LDAP authentication with the Webhook Token authentication plugin
Shell
8
star
24

service-catalog-aws

A step-by-step guide on how to set up the Service Catalogue in Amazon Web Services (AWS)
Shell
6
star
25

research

Curated Kubernetes research
4
star
26

kubeflow-pipelines-demo

Jupyter Notebook
3
star
27

docker-hello-world

Node.js "Hello world"
JavaScript
3
star
28

kube-hello-world

JavaScript
2
star
29

Telegram

Learnk8s Telegram group β€” Code of conduct
2
star
30

jenkins-x-demo

CSS
2
star
31

resources

Resources for Learnk8s tutorials
1
star