{"id":15127496,"url":"https://github.com/learnk8s/spring-boot-k8s-hpa","last_synced_at":"2025-04-06T04:15:15.198Z","repository":{"id":42562895,"uuid":"133850662","full_name":"learnk8s/spring-boot-k8s-hpa","owner":"learnk8s","description":"Autoscaling Spring Boot with the Horizontal Pod Autoscaler and custom metrics on Kubernetes","archived":false,"fork":false,"pushed_at":"2022-03-31T18:43:24.000Z","size":63,"stargazers_count":323,"open_issues_count":4,"forks_count":136,"subscribers_count":33,"default_branch":"master","last_synced_at":"2025-03-30T03:08:45.729Z","etag":null,"topics":["autoscaling","docker-container","kubernetes","spring-boot"],"latest_commit_sha":null,"homepage":"https://learnk8s.io/blog/scaling-spring-boot-microservices","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/learnk8s.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-17T18:04:55.000Z","updated_at":"2025-03-27T08:31:55.000Z","dependencies_parsed_at":"2022-08-25T18:40:59.856Z","dependency_job_id":null,"html_url":"https://github.com/learnk8s/spring-boot-k8s-hpa","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/learnk8s%2Fspring-boot-k8s-hpa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/learnk8s%2Fspring-boot-k8s-hpa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/learnk8s%2Fspring-boot-k8s-hpa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/learnk8s%2Fspring-boot-k8s-hpa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/learnk8s","download_url":"https://codeload.github.com/learnk8s/spring-boot-k8s-hpa/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247430963,"owners_count":20937875,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autoscaling","docker-container","kubernetes","spring-boot"],"created_at":"2024-09-26T02:04:25.627Z","updated_at":"2025-04-06T04:15:15.171Z","avatar_url":"https://github.com/learnk8s.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Autoscaling Spring Boot with the Horizontal Pod Autoscaler and custom metrics on Kubernetes\n\n## Prerequisites\n\nYou should have minikube installed.\n\nYou should start minikube with at least 4GB of RAM:\n\n```bash\nminikube start \\\n  --memory 4096 \\\n  --extra-config=controller-manager.horizontal-pod-autoscaler-upscale-delay=1m \\\n  --extra-config=controller-manager.horizontal-pod-autoscaler-downscale-delay=2m \\\n  --extra-config=controller-manager.horizontal-pod-autoscaler-sync-period=10s\n```\n\n\u003e 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.\n\nYou should install `jq` — a lightweight and flexible command-line JSON processor.\n\nYou can find more [info about `jq` on the official website](https://github.com/stedolan/jq).\n\n## Installing Custom Metrics Api\n\nDeploy the Metrics Server in the `kube-system` namespace:\n\n```bash\nkubectl create -f monitoring/metrics-server\n```\n\nAfter one minute the metric-server starts reporting CPU and memory usage for nodes and pods.\n\nView nodes metrics:\n\n```bash\nkubectl get --raw \"/apis/metrics.k8s.io/v1beta1/nodes\" | jq .\n```\n\nView pods metrics:\n\n```bash\nkubectl get --raw \"/apis/metrics.k8s.io/v1beta1/pods\" | jq .\n```\n\nCreate the monitoring namespace:\n\n```bash\nkubectl create -f monitoring/namespaces.yaml\n```\n\nDeploy Prometheus v2 in the monitoring namespace:\n\n```bash\nkubectl create -f monitoring/prometheus\n```\n\nDeploy the Prometheus custom metrics API adapter:\n\n```bash\nkubectl create -f monitoring/custom-metrics-api\n```\n\nList the custom metrics provided by Prometheus:\n\n```bash\nkubectl get --raw \"/apis/custom.metrics.k8s.io/v1beta1\" | jq .\n```\n\n## Package the application\n\nYou package the application as a container with:\n\n```bash\neval $(minikube docker-env)\ndocker build -t spring-boot-hpa .\n```\n\n## Deploying the application\n\nDeploy the application in Kubernetes with:\n\n```bash\nkubectl create -f kube/deployment\n```\n\nYou can visit the application at http://minkube_ip:32000\n\n\u003e (Find the minikube ip address via `minikube ip`)\n\nYou can post messages to the queue by via http://minkube_ip:32000/submit?quantity=2\n\nYou should be able to see the number of pending messages from http://minkube_ip:32000/metrics and from the custom metrics endpoint:\n\n```bash\nkubectl get --raw \"/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/messages\" | jq .\n```\n\n## Autoscaling workers\n\nYou 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:\n\n```bash\nkubectl create -f kube/hpa.yaml\n```\n\nYou can send more traffic to the application with:\n\n```bash\nwhile true; do curl -d \"quantity=1\" -X POST http://minkube_ip:32000/submit ; sleep 4; done\n```\n\nWhen the application can't cope with the number of incoming messages, the autoscaler increases the number of pods in 3 minute intervals.\n\nYou may need to wait three minutes before you can see more pods joining the deployment with:\n\n```bash\nkubectl get pods\n```\n\nThe autoscaler will remove pods from the deployment every 5 minutes.\n\nYou can inspect the event and triggers in the HPA with:\n\n```bash\nkubectl get hpa spring-boot-hpa\n```\n\n## Notes\n\nThe configuration for metrics and metrics server is configured to run on minikube only.\n\n**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.**\n\nAlso, there are secrets checked in the repository to deploy the Prometheus adapter.\n\n**In production, you should generate your own secrets and (possibly) not check them into version control.**\n\nIf you wish to run metrics and custom metrics server in production, you should check out the following resources:\n\n- [Metrics server](https://github.com/kubernetes-sigs/metrics-server)\n- [How to install Prometheus and the Promtheus Adapter](https://github.com/DirectXMan12/k8s-prometheus-adapter/blob/master/docs/walkthrough.md)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flearnk8s%2Fspring-boot-k8s-hpa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flearnk8s%2Fspring-boot-k8s-hpa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flearnk8s%2Fspring-boot-k8s-hpa/lists"}