├── .eksctlignore ├── .gitignore ├── LICENSE ├── README.md ├── demo ├── loadtester │ ├── deployment.yaml │ └── service.yaml ├── namespace.yaml └── podinfo │ ├── deployment.yaml │ ├── hpa.yaml │ └── service.yaml ├── docs └── fargate-eks-hpa.png └── monitoring-system ├── metrics-server.yaml ├── namespace.yaml ├── prometheus-adapter.yaml └── prometheus.yaml /.eksctlignore: -------------------------------------------------------------------------------- 1 | # exclude CI 2 | .circleci/ 3 | .github/ 4 | docs/ -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, build with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # eks-hpa-profile 2 | 3 | This repo is an [eksctl GitOps profile](https://eksctl.io/usage/experimental/gitops-flux/) 4 | for configuring HPA with metrics provided by Prometheus to automatically scale pods running on EKS on Fargate. 5 | 6 | ![](docs/fargate-eks-hpa.png) 7 | 8 | ### Create an EKS cluster 9 | 10 | Create an EKS cluster with two EC2 managed nodes and a Fargate profile: 11 | 12 | ```sh 13 | cat << EOF | eksctl create cluster -f - 14 | apiVersion: eksctl.io/v1alpha5 15 | kind: ClusterConfig 16 | metadata: 17 | name: eks-fargate-hpa 18 | region: eu-west-1 19 | 20 | managedNodeGroups: 21 | - name: default 22 | instanceType: m5.large 23 | desiredCapacity: 2 24 | volumeSize: 120 25 | iam: 26 | withAddonPolicies: 27 | appMesh: true 28 | albIngress: true 29 | 30 | fargateProfiles: 31 | - name: default 32 | selectors: 33 | - namespace: demo 34 | labels: 35 | scheduler: fargate 36 | EOF 37 | ``` 38 | 39 | You'll use the managed nodes for cluster add-ons(CoreDNS, KubeProxy) and for the HPA metrics add-ons: 40 | * [Prometheus](https://github.com/stefanprodan/eks-hpa-profile/blob/master/monitoring-system/prometheus.yaml) - scrapes pods and stores metrics 41 | * [Prometheus metrics adapter](https://github.com/stefanprodan/eks-hpa-profile/blob/master/monitoring-system/prometheus-adapter.yaml) - queries Prometheus and exposes metrics for the Kubernetes custom metrics API 42 | * [Metrics server](https://github.com/stefanprodan/eks-hpa-profile/blob/master/monitoring-system/metrics-server.yaml) - collects pods CPU and memory usage and exposes metrics for the Kubernetes resource metrics API 43 | 44 | You'll use Fargate for the demo application [podinfo](https://github.com/stefanprodan/eks-hpa-profile/tree/master/demo/podinfo), 45 | note that only the pods deployed in the `demo` namespace with a `scheduler: fargate` label will be running on Fargate. 46 | 47 | ### Create a GitHub repository 48 | 49 | To configure HPA for Fargate you'll be using an eksctl GitOps [profile](https://eksctl.io/gitops-quickstart/setup-gitops/). 50 | A Profile allows you to create a specific Kubernetes application platform tailored for a specific use case. 51 | 52 | Create a GitHub [repository](https://github.com/new) and clone it locally. 53 | Replace `GH_USER/GH_REPO` value with your GitHub username and new repo. 54 | Use these variables to clone your repo and setup GitOps for your cluster. 55 | 56 | ```sh 57 | export GH_USER=YOUR_GITHUB_USERNAME 58 | export GH_REPO=YOUR_GITHUB_REPOSITORY 59 | 60 | git clone https://github.com/${GH_USER}/${GH_REPO} 61 | cd ${GH_REPO} 62 | ``` 63 | 64 | Run the eksctl repo command: 65 | 66 | ```sh 67 | export EKSCTL_EXPERIMENTAL=true 68 | 69 | eksctl enable repo \ 70 | --cluster=eks-fargate-hpa \ 71 | --region=eu-west-1 \ 72 | --git-url=git@github.com:${GH_USER}/${GH_REPO} \ 73 | --git-user=fluxcd \ 74 | --git-email=${GH_USER}@users.noreply.github.com 75 | ``` 76 | 77 | The command `eksctl enable repo` takes an existing EKS cluster and an empty repository 78 | and sets up a GitOps pipeline. 79 | 80 | After the command finishes installing [FluxCD](https://github.com/fluxcd/flux) and [Helm Operator](https://github.com/fluxcd/flux), 81 | you will be asked to add Flux's deploy key to your GitHub repository. 82 | 83 | Copy the public key and create a deploy key with write access on your GitHub repository. 84 | Go to `Settings > Deploy keys` click on `Add deploy key`, check `Allow write access`, 85 | paste the Flux public key and click `Add key`. 86 | 87 | Once that is done, Flux will be able to pick up changes in the repository and deploy them to the cluster. 88 | 89 | ### Install the metrics add-ons 90 | 91 | Run the eksctl profile command: 92 | 93 | ```sh 94 | eksctl enable profile \ 95 | --profile-source=https://github.com/stefanprodan/eks-hpa-profile \ 96 | --cluster=eks-fargate-hpa \ 97 | --region=eu-west-1 \ 98 | --git-url=git@github.com:${GH_USER}/${GH_REPO} \ 99 | --git-user=fluxcd \ 100 | --git-email=${GH_USER}@users.noreply.github.com 101 | ``` 102 | 103 | The command `eksctl enable profile` adds the HPA metrics add-ons and 104 | the demo app manifests to the configured repository. 105 | 106 | Sync your local repository with: 107 | 108 | ```sh 109 | git pull origin master 110 | ``` 111 | 112 | Install [fluxctl](https://github.com/fluxcd/flux/releases): 113 | 114 | ```sh 115 | curl -sL https://fluxcd.io/install | sh 116 | export PATH="$PATH:$HOME/.fluxcd/bin" 117 | ``` 118 | 119 | Run the fluxctl sync command to apply the manifests on your cluster: 120 | 121 | ```sh 122 | fluxctl sync --k8s-fwd-ns flux 123 | ``` 124 | 125 | Flux does a git-cluster reconciliation every five minutes, 126 | the above command can be used to speed up the synchronization. 127 | 128 | List the installed components: 129 | 130 | ``` 131 | $ kubectl -n monitoring-system get helmreleases 132 | 133 | NAME RELEASE STATUS 134 | metrics-server metrics-server DEPLOYED 135 | prometheus prometheus DEPLOYED 136 | prometheus-adapter prometheus-adapter DEPLOYED 137 | ``` 138 | 139 | ### Install podinfo 140 | 141 | You'll use a Go web app named [podinfo](https://github.com/stefanprodan/podinfo) to try out HPA. 142 | The app is instrumented with Prometheus and exposes the `http_requests_total` [counter](https://prometheus.io/docs/concepts/metric_types/#counter). 143 | The HPA controller will scale the pods running on Fargate based on the number of requests per second. 144 | 145 | Install podinfo by setting `fluxcd.io/ignore` to `false` in base/demo/namespace.yaml: 146 | 147 | ```sh 148 | cat << EOF | tee base/demo/namespace.yaml 149 | apiVersion: v1 150 | kind: Namespace 151 | metadata: 152 | name: demo 153 | annotations: 154 | fluxcd.io/ignore: "false" 155 | EOF 156 | ``` 157 | 158 | Apply changes via git: 159 | 160 | ```sh 161 | git add -A && \ 162 | git commit -m "init demo" && \ 163 | git push origin master && \ 164 | fluxctl sync --k8s-fwd-ns flux 165 | ``` 166 | 167 | Wait for Fargate to schedule and start podinfo: 168 | 169 | ```sh 170 | watch kubectl -n demo get po -l scheduler=fargate 171 | ``` 172 | 173 | When podinfo starts, Prometheus will scrape the metrics endpoint and the Prometheus adapter will export the HTTP 174 | requests per second metrics to the Kubernetes custom metrics API: 175 | 176 | ``` 177 | $ watch kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1 | jq . 178 | 179 | { 180 | "kind": "APIResourceList", 181 | "apiVersion": "v1", 182 | "groupVersion": "custom.metrics.k8s.io/v1beta1", 183 | "resources": [ 184 | { 185 | "name": "namespaces/http_requests_per_second", 186 | "singularName": "", 187 | "namespaced": false, 188 | "kind": "MetricValueList", 189 | "verbs": [ 190 | "get" 191 | ] 192 | }, 193 | { 194 | "name": "pods/http_requests_per_second", 195 | "singularName": "", 196 | "namespaced": true, 197 | "kind": "MetricValueList", 198 | "verbs": [ 199 | "get" 200 | ] 201 | } 202 | ] 203 | } 204 | ``` 205 | 206 | ### Configure autoscaling based on HTTP traffic 207 | 208 | To configure auto-scaling you can set up a HPA definition that uses the `http_requests_per_second` metric. 209 | The HPA manifest is in `base/demo/podinfo.hpa.yaml` and it's set to scale up podinfo 210 | when the average req/sec per pod goes over 10: 211 | 212 | ```yaml 213 | apiVersion: autoscaling/v2beta1 214 | kind: HorizontalPodAutoscaler 215 | metadata: 216 | name: podinfo 217 | namespace: demo 218 | spec: 219 | scaleTargetRef: 220 | apiVersion: apps/v1 221 | kind: Deployment 222 | name: podinfo 223 | minReplicas: 1 224 | maxReplicas: 10 225 | metrics: 226 | - type: Pods 227 | pods: 228 | metricName: http_requests_per_second 229 | targetAverageValue: 10 230 | ``` 231 | 232 | Note that the podinfo [deployment manifest](https://github.com/stefanprodan/eks-hpa-profile/blob/master/demo/podinfo/deployment.yaml) 233 | has no replicas defined in `deployment.spec.replicas`, the HPA controller updates the number of replicas based on the metric average value. 234 | 235 | Once the metric is available to the metrics API, the HPA controller will display the current value: 236 | 237 | ``` 238 | $ watch kubectl -n demo get hpa 239 | 240 | NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE 241 | podinfo Deployment/podinfo 200m/10 1 10 1 8m58s 242 | ``` 243 | 244 | The m in `200m` represents milli-units, 200m means 0.2 req/sec. 245 | The traffic is generated by Prometheus that scrapes the `/metrics` endpoint every five seconds. 246 | 247 | Exec into the loadtester pod with: 248 | 249 | ```sh 250 | kubectl -n demo exec -it loadtester-xxxx-xxxx 251 | ``` 252 | 253 | Generate traffic with `hey`: 254 | 255 | ```sh 256 | hey -z 10m -c 5 -q 5 -disable-keepalive http://podinfo.demo 257 | ``` 258 | 259 | After a few minutes the HPA begins to scale up the deployment: 260 | 261 | ``` 262 | $ kubectl -n demo describe hpa podinfo 263 | 264 | Events: 265 | Type Reason Age From Message 266 | ---- ------ ---- ---- ------- 267 | Normal SuccessfulRescale 2m horizontal-pod-autoscaler New size: 3; reason: pods metric http_requests_per_second above target 268 | ``` 269 | 270 | After the load tests finishes, the HPA down scales the deployment to it's initial replicas: 271 | 272 | ``` 273 | Events: 274 | Type Reason Age From Message 275 | ---- ------ ---- ---- ------- 276 | Normal SuccessfulRescale 21s horizontal-pod-autoscaler New size: 1; reason: All metrics below target 277 | ``` 278 | 279 | You may have noticed that the autoscaler doesn't react immediately to usage spikes. 280 | By default the metrics sync happens once every 30 seconds and scaling up/down can only happen 281 | if there was no rescaling within the last 3-5 minutes. 282 | In this way, the HPA prevents rapid execution of conflicting decisions. 283 | 284 | ### Configure autoscaling based on CPU usage 285 | 286 | For workloads that aren't instrumented with Prometheus, you can use the Kubernetes 287 | [metrics server](https://github.com/stefanprodan/eks-hpa-profile/blob/master/monitoring-system/metrics-server.yaml) 288 | and configure auto-scaling based on CPU and/or memory usage. 289 | 290 | Update the HPA manifest by replacing the HTTP metric with CPU average utilization: 291 | 292 | ```sh 293 | cat << EOF | tee base/demo/podinfo/hpa.yaml 294 | apiVersion: autoscaling/v2beta1 295 | kind: HorizontalPodAutoscaler 296 | metadata: 297 | name: podinfo 298 | namespace: demo 299 | spec: 300 | scaleTargetRef: 301 | apiVersion: apps/v1 302 | kind: Deployment 303 | name: podinfo 304 | minReplicas: 1 305 | maxReplicas: 10 306 | metrics: 307 | - type: Resource 308 | resource: 309 | name: cpu 310 | targetAverageUtilization: 90 311 | EOF 312 | ``` 313 | 314 | Apply changes via git: 315 | 316 | ```sh 317 | git add -A && \ 318 | git commit -m "cpu hpa" && \ 319 | git push origin master && \ 320 | fluxctl sync --k8s-fwd-ns flux 321 | ``` 322 | 323 | Run a load test to increase CPU usage above 90% to trigger the HPA: 324 | 325 | ```sh 326 | hey -z 10m -c 5 -q 5 -m POST -d 'test' -disable-keepalive http://podinfo.demo/token 327 | ``` 328 | 329 | The Kubernetes Metrics Server is a cluster-wide aggregator of resource usage data, 330 | it collects CPU and memory usage for nodes and pods by pooling data from the `kubernetes.summary_api`. 331 | The summary API is a memory-efficient API for passing data from Kubelet to the metrics server. 332 | 333 | ### Configure autoscaling based on App Mesh traffic 334 | 335 | One of the advantages of using a service mesh like App Mesh is the builtin monitoring capability. 336 | You don’t have to instrument your web apps in order to monitor the L7 traffic. 337 | 338 | The Envoy sidecar used by App Mesh exposes a counter `envoy_cluster_upstream_rq`, you can configure the 339 | [Prometheus adapter](https://github.com/stefanprodan/eks-hpa-profile/blob/master/monitoring-system/prometheus-adapter.yaml) 340 | to transform this metric into req/sec rate with: 341 | 342 | ```yaml 343 | apiVersion: helm.fluxcd.io/v1 344 | kind: HelmRelease 345 | metadata: 346 | name: prometheus-adapter 347 | namespace: monitoring-system 348 | spec: 349 | releaseName: prometheus-adapter 350 | chart: 351 | repository: https://kubernetes-charts.storage.googleapis.com/ 352 | name: prometheus-adapter 353 | version: 1.4.0 354 | values: 355 | prometheus: 356 | url: http://prometheus.monitoring-system 357 | port: 9090 358 | rules: 359 | default: false 360 | custom: 361 | - seriesQuery: 'envoy_cluster_upstream_rq{kubernetes_namespace!="",kubernetes_pod_name!=""}' 362 | resources: 363 | overrides: 364 | kubernetes_namespace: {resource: "namespace"} 365 | kubernetes_pod_name: {resource: "pod"} 366 | name: 367 | matches: "envoy_cluster_upstream_rq" 368 | as: "appmesh_requests_per_second" 369 | metricsQuery: 'sum(rate(<<.Series>>{<<.LabelMatchers>>}[1m])) by (<<.GroupBy>>)' 370 | ``` 371 | 372 | Now you can use the `appmesh_requests_per_second` metric in the HPA definition: 373 | 374 | ```yaml 375 | apiVersion: autoscaling/v2beta1 376 | kind: HorizontalPodAutoscaler 377 | metadata: 378 | name: podinfo 379 | namespace: demo 380 | spec: 381 | scaleTargetRef: 382 | apiVersion: apps/v1 383 | kind: Deployment 384 | name: podinfo 385 | minReplicas: 1 386 | maxReplicas: 10 387 | metrics: 388 | - type: Pods 389 | pods: 390 | metricName: appmesh_requests_per_second 391 | targetAverageValue: 10 392 | ``` 393 | 394 | -------------------------------------------------------------------------------- /demo/loadtester/deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: loadtester 5 | namespace: demo 6 | labels: 7 | app: loadtester 8 | spec: 9 | selector: 10 | matchLabels: 11 | app: loadtester 12 | template: 13 | metadata: 14 | labels: 15 | app: loadtester 16 | annotations: 17 | prometheus.io/scrape: "true" 18 | prometheus.io/port: "8080" 19 | spec: 20 | containers: 21 | - name: loadtester 22 | image: weaveworks/flagger-loadtester:0.12.0 23 | imagePullPolicy: IfNotPresent 24 | ports: 25 | - name: http 26 | containerPort: 8080 27 | command: 28 | - ./loadtester 29 | - -port=8080 30 | - -log-level=info 31 | - -timeout=1h 32 | livenessProbe: 33 | exec: 34 | command: 35 | - wget 36 | - --quiet 37 | - --tries=1 38 | - --timeout=4 39 | - --spider 40 | - http://localhost:8080/healthz 41 | timeoutSeconds: 5 42 | readinessProbe: 43 | exec: 44 | command: 45 | - wget 46 | - --quiet 47 | - --tries=1 48 | - --timeout=4 49 | - --spider 50 | - http://localhost:8080/healthz 51 | timeoutSeconds: 5 52 | resources: 53 | requests: 54 | memory: 128Mi 55 | cpu: 100m 56 | -------------------------------------------------------------------------------- /demo/loadtester/service.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: loadtester 5 | namespace: demo 6 | labels: 7 | app: loadtester 8 | spec: 9 | type: ClusterIP 10 | selector: 11 | app: loadtester 12 | ports: 13 | - name: http 14 | port: 80 15 | protocol: TCP 16 | targetPort: http 17 | -------------------------------------------------------------------------------- /demo/namespace.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Namespace 3 | metadata: 4 | name: demo 5 | annotations: 6 | fluxcd.io/ignore: "true" -------------------------------------------------------------------------------- /demo/podinfo/deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: podinfo 5 | namespace: demo 6 | labels: 7 | app: podinfo 8 | spec: 9 | minReadySeconds: 1 10 | revisionHistoryLimit: 5 11 | strategy: 12 | rollingUpdate: 13 | maxUnavailable: 0 14 | type: RollingUpdate 15 | selector: 16 | matchLabels: 17 | app: podinfo 18 | template: 19 | metadata: 20 | annotations: 21 | prometheus.io/scrape: "true" 22 | prometheus.io/port: "9797" 23 | labels: 24 | app: podinfo 25 | scheduler: fargate 26 | spec: 27 | containers: 28 | - name: podinfod 29 | image: stefanprodan/podinfo:3.1.0 30 | imagePullPolicy: IfNotPresent 31 | ports: 32 | - name: http 33 | containerPort: 9898 34 | protocol: TCP 35 | - name: http-metrics 36 | containerPort: 9797 37 | protocol: TCP 38 | - name: grpc 39 | containerPort: 9999 40 | protocol: TCP 41 | command: 42 | - ./podinfo 43 | - --port=9898 44 | - --port-metrics=9797 45 | - --grpc-port=9999 46 | - --grpc-service-name=podinfo 47 | - --level=info 48 | env: 49 | - name: PODINFO_UI_COLOR 50 | value: "#34577c" 51 | livenessProbe: 52 | exec: 53 | command: 54 | - podcli 55 | - check 56 | - http 57 | - localhost:9898/healthz 58 | initialDelaySeconds: 5 59 | timeoutSeconds: 5 60 | readinessProbe: 61 | exec: 62 | command: 63 | - podcli 64 | - check 65 | - http 66 | - localhost:9898/readyz 67 | initialDelaySeconds: 5 68 | timeoutSeconds: 5 69 | resources: 70 | requests: 71 | cpu: 100m 72 | memory: 64Mi 73 | -------------------------------------------------------------------------------- /demo/podinfo/hpa.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: autoscaling/v2beta1 2 | kind: HorizontalPodAutoscaler 3 | metadata: 4 | name: podinfo 5 | namespace: demo 6 | spec: 7 | scaleTargetRef: 8 | apiVersion: apps/v1 9 | kind: Deployment 10 | name: podinfo 11 | minReplicas: 1 12 | maxReplicas: 10 13 | metrics: 14 | - type: Pods 15 | pods: 16 | metricName: http_requests_per_second 17 | targetAverageValue: 10 18 | -------------------------------------------------------------------------------- /demo/podinfo/service.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: podinfo 5 | namespace: demo 6 | labels: 7 | app: podinfo 8 | spec: 9 | type: ClusterIP 10 | selector: 11 | app: podinfo 12 | ports: 13 | - name: http 14 | port: 80 15 | protocol: TCP 16 | targetPort: http 17 | -------------------------------------------------------------------------------- /docs/fargate-eks-hpa.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stefanprodan/eks-hpa-profile/3f944697378175c3afe3591e03def3e7738c66ca/docs/fargate-eks-hpa.png -------------------------------------------------------------------------------- /monitoring-system/metrics-server.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: helm.fluxcd.io/v1 2 | kind: HelmRelease 3 | metadata: 4 | name: metrics-server 5 | namespace: monitoring-system 6 | spec: 7 | releaseName: metrics-server 8 | chart: 9 | repository: https://charts.helm.sh/stable 10 | name: metrics-server 11 | version: 2.8.8 12 | values: 13 | args: 14 | - --kubelet-preferred-address-types=InternalIP 15 | -------------------------------------------------------------------------------- /monitoring-system/namespace.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Namespace 3 | metadata: 4 | name: monitoring-system 5 | labels: 6 | appmesh.k8s.aws/sidecarInjectorWebhook: disabled 7 | -------------------------------------------------------------------------------- /monitoring-system/prometheus-adapter.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: helm.fluxcd.io/v1 2 | kind: HelmRelease 3 | metadata: 4 | name: prometheus-adapter 5 | namespace: monitoring-system 6 | spec: 7 | releaseName: prometheus-adapter 8 | chart: 9 | repository: https://charts.helm.sh/stable 10 | name: prometheus-adapter 11 | version: 1.4.0 12 | values: 13 | prometheus: 14 | url: http://prometheus.monitoring-system 15 | port: 9090 16 | rules: 17 | default: false 18 | custom: 19 | - seriesQuery: 'http_requests_total{kubernetes_namespace!="",kubernetes_pod_name!=""}' 20 | resources: 21 | overrides: 22 | kubernetes_namespace: {resource: "namespace"} 23 | kubernetes_pod_name: {resource: "pod"} 24 | name: 25 | matches: "^(.*)_total" 26 | as: "${1}_per_second" 27 | metricsQuery: 'sum(rate(<<.Series>>{<<.LabelMatchers>>}[1m])) by (<<.GroupBy>>)' 28 | - seriesQuery: 'envoy_cluster_upstream_rq{kubernetes_namespace!="",kubernetes_pod_name!=""}' 29 | resources: 30 | overrides: 31 | kubernetes_namespace: {resource: "namespace"} 32 | kubernetes_pod_name: {resource: "pod"} 33 | name: 34 | matches: "envoy_cluster_upstream_rq" 35 | as: "appmesh_requests_per_second" 36 | metricsQuery: 'sum(rate(<<.Series>>{<<.LabelMatchers>>}[1m])) by (<<.GroupBy>>)' 37 | -------------------------------------------------------------------------------- /monitoring-system/prometheus.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: helm.fluxcd.io/v1 2 | kind: HelmRelease 3 | metadata: 4 | name: prometheus 5 | namespace: monitoring-system 6 | spec: 7 | releaseName: prometheus 8 | chart: 9 | git: https://github.com/aws/eks-charts 10 | ref: master 11 | path: stable/appmesh-prometheus 12 | values: 13 | retention: 2h 14 | fullnameOverride: prometheus 15 | --------------------------------------------------------------------------------