{"id":3509,"date":"2022-11-04T05:31:43","date_gmt":"2022-11-04T05:31:43","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=3509"},"modified":"2023-08-04T13:26:57","modified_gmt":"2023-08-04T13:26:57","slug":"horizontal-pod-autoscaler","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/horizontal-pod-autoscaler","title":{"rendered":"Horizontal pod autoscaler in Kubernetes"},"content":{"rendered":"<p>The objective of a <strong>HorizontalPodAutoscaler<\/strong> in Kubernetes is to<strong> automatically scale the workload<\/strong> to match demand by updating a workload resource such as a Deployment or StatefulSet. <strong>When a load increases, more Pods are deployed<\/strong>, which is referred to as horizontal scaling.\u00a0The<strong> HorizontalPodAutoscaler<\/strong> informs the workload resource (the Deployment, StatefulSet, or other similar resources) to <strong>scale back down if the load drops and the number of Pods is more than the defined minimum<\/strong>.<\/p>\n<p>For using horizontal pod autoscaler in <a href=\"https:\/\/www.checkmateq.com\/kubernetes\">Kubernetes<\/a> first we will need to install the Kubernetes metric server which queries the resource usages of pods and nodes like CPU and memory utilization.<\/p>\n<h3>For installing the metric server in EKS\u00a0 use the following commands.<\/h3>\n<pre>kubectl apply -f https:\/\/github.com\/kubernetes-sigs\/metrics-server\/releases\/latest\/download\/components.yaml\r\n\r\nkubectl get deployment metrics-server -n kube-system\r\n<\/pre>\n<p>If you&#8217;re using any local Kubernetes cluster like minikube then you will need to add the\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 <strong>&#8211;kubelet-insecure-tls<\/strong> argument in the container property in the metrics server deployment manifest file.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-3514\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/hire-android-app-developers-300x98.png\" alt=\"\" width=\"701\" height=\"229\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/hire-android-app-developers-300x98.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/hire-android-app-developers-768x252.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/hire-android-app-developers.png 973w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><br \/>\nUse the following command to verify that the metrics server deployment is running.<\/p>\n<pre>kubectl get deployment metrics-server -n kube-system\r\n<\/pre>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-3516\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-gcp-cloud-services-300x23.png\" alt=\"\" width=\"704\" height=\"54\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-gcp-cloud-services-300x23.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-gcp-cloud-services-1024x78.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-gcp-cloud-services-768x58.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-gcp-cloud-services-1536x116.png 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-gcp-cloud-services-1200x91.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-gcp-cloud-services.png 1678w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-3519\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Hire-full-stack-development-engineer-300x109.png\" alt=\"\" width=\"727\" height=\"264\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Hire-full-stack-development-engineer-300x109.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Hire-full-stack-development-engineer-768x280.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Hire-full-stack-development-engineer.png 1039w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now we will deploy an Nginx web server using Kubernetes deployment. To use the autoscaler we will need to define the resource requests. Requests define the minimum amount of resources required by pods.<\/p>\n<pre><code>\r\napiVersion: apps\/v1\r\nkind: Deployment\r\nmetadata:\r\n  name: nginx-deploy\r\nspec:\r\n   replicas: 2\r\n   selector:\r\n     matchLabels:\r\n       app: web\r\n   template:\r\n       metadata:\r\n         labels:\r\n           app: web\r\n       spec:\r\n          containers:\r\n          - name: nginx\r\n            image: nginx:1.23\r\n            ports:\r\n             - containerPort: 80    \r\n            resources:\r\n               requests:\r\n                  memory: 200Mi\r\n                  cpu: 100m      \r\n---\r\n\r\napiVersion: v1\r\nkind: Service\r\nmetadata:\r\n   name: nginx-service\r\nspec:\r\n  selector:\r\n    app: web\r\n  type: NodePort\r\n  ports:\r\n  - name: http\r\n    port: 80\r\n    nodePort: 30080\r\n     \r\n\r\n<\/code><\/pre>\n<p>Next, deploy the horizontal pod autoscaler manifest file. Here we have defined that if CPU usage increase above 50, it will scale up the replicas.<\/p>\n<pre><code>\r\napiVersion: autoscaling\/v2\r\nkind: HorizontalPodAutoscaler\r\nmetadata:\r\n  name: nginx-hpa\r\nspec:\r\n  scaleTargetRef:\r\n    apiVersion: apps\/v1\r\n    kind: Deployment\r\n    name: nginx-deploy\r\n  minReplicas: 1\r\n  maxReplicas: 10\r\n  metrics:\r\n  - type: Resource\r\n    resource:\r\n      name: cpu\r\n      target:\r\n        type: Utilization\r\n        averageUtilization: 50\r\n  behavior:\r\n    scaleDown:\r\n      stabilizationWindowSeconds: 60\r\n      policies:\r\n      - type: Pods\r\n        value: 1\r\n        periodSeconds: 60\r\n\r\n    scaleUp:\r\n      stabilizationWindowSeconds: 0\r\n      policies:\r\n      - type: Pods\r\n        value: 2\r\n        periodSeconds: 15\r\n  \r\n<\/code><\/pre>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-3521\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Hire-Golang-developer-300x68.png\" alt=\"\" width=\"737\" height=\"167\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Hire-Golang-developer-300x68.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Hire-Golang-developer-1024x234.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Hire-Golang-developer-768x175.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Hire-Golang-developer-1200x274.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Hire-Golang-developer.png 1420w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now for testing the autoscaler, we will use a busybox container deployment. It will call the Nginx pods in a loop and hence increase the CPU load.<\/p>\n<pre><code>\r\napiVersion: apps\/v1\r\nkind: Deployment\r\nmetadata:\r\n  name: nginx-load\r\n  labels:\r\n    app: nginx-load\r\nspec:\r\n  replicas: 1\r\n  selector:\r\n    matchLabels:\r\n      app: nginx-load\r\n  template:\r\n    metadata:\r\n      name: nginx-load\r\n      labels:\r\n        app: nginx-load\r\n    spec:\r\n       containers:\r\n       - name: busybox\r\n         image: busybox\r\n         command:\r\n         - \/bin\/sh\r\n         - -c\r\n         - \"while true; do wget -q -O- nginx-service; done\"\r\n<\/code><\/pre>\n<p>After deploying the busybox pod we will see that CPU utilization will increase. In our case it has increased up to 80% which is above 50% threshold that we defined.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-3522\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-offshore-it-service-300x103.png\" alt=\"\" width=\"702\" height=\"241\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-offshore-it-service-300x103.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-offshore-it-service-1024x352.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-offshore-it-service-768x264.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-offshore-it-service-1200x413.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-offshore-it-service.png 1294w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>After some time, autoscaler will notice the increased CPU utilization and increase the number of replicas up to 3, thus bringing down the CPU utilization.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-3526\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/hire-software-development-Team-300x109.png\" alt=\"\" width=\"729\" height=\"265\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/hire-software-development-Team-300x109.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/hire-software-development-Team-768x278.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/hire-software-development-Team-1200x434.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/hire-software-development-Team.png 1288w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now we will delete the busy box deployment and we will see that in some time number of replicas will scale down as well.<\/p>\n<p><img loading=\"lazy\" class=\"alignnone wp-image-3528\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-game-development-company-300x76.png\" alt=\"\" width=\"699\" height=\"177\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-game-development-company-300x76.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-game-development-company-1024x258.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-game-development-company-768x194.png 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-game-development-company-1200x303.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/11\/Checkmate-game-development-company.png 1428w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p><strong>Author Details<\/strong><\/p>\n<p>This post is written by Amit Kumar, Engineering Director, Checkmate Global Technologies.\u00a0 <a href=\"https:\/\/www.checkmateq.com\/\">Please contact<\/a> him for if you have anything related to <a href=\"https:\/\/www.checkmateq.com\/aws-cloud\">cloud infrastructure<\/a> to be discussed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The objective of a HorizontalPodAutoscaler in Kubernetes is to automatically scale the workload to match demand by updating a workload resource such as a Deployment or StatefulSet. When a load increases, more Pods are deployed, which is referred to as horizontal scaling.\u00a0The HorizontalPodAutoscaler informs the workload resource (the Deployment, StatefulSet, or other similar resources) to &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/horizontal-pod-autoscaler\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Horizontal pod autoscaler in Kubernetes&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":3553,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[4,3,2,69,68,7],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/3509"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=3509"}],"version-history":[{"count":15,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/3509\/revisions"}],"predecessor-version":[{"id":4215,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/3509\/revisions\/4215"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/3553"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=3509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=3509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=3509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}