|
| 1 | +# K8s pod autoscaling with KEDA |
| 2 | + |
| 3 | +Kubernetes, a powerful container orchestration platform, revolutionized the way applications are deployed and managed. However, scaling applications to meet fluctuating workloads can be a complex task. KEDA, a Kubernetes-based Event-Driven Autoscaler, provides a simple yet effective solution to automatically scale Kubernetes Pods based on various metrics, including resource utilization, custom metrics, and external events. |
| 4 | + |
| 5 | +## Goal |
| 6 | + |
| 7 | +To install and configure KEDA on an EKS Cluster created on the [**binbash Leverage**](https://leverage.binbash.co/) way. |
| 8 | + |
| 9 | +!!! Note |
| 10 | + To read more on how to create the EKS Cluster on the [**binbash Leverage**](https://leverage.binbash.co/) way, read [here](./k8s.md). |
| 11 | + |
| 12 | +**Note** for the following example we will be using a Kedacore plugin called [http-add-on](https://github.com/kedacore/http-add-on/). |
| 13 | + |
| 14 | +!!! Note |
| 15 | + To lear more about KEDA read [the official site](https://keda.sh/docs/2.15/). |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +### Assumptions |
| 20 | + |
| 21 | +We are assuming the [**binbash Leverage**](https://leverage.binbash.co/) [Landing Zone](https://leverage.binbash.co/try-leverage/) is deployed, an account called `apps-devstg` was created and region `us-east-1` is being used. In any case you can adapt these examples to other scenarios. |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +To install KEDA, just enable it in the components layer [here](https://github.com/binbashar/le-tf-infra-aws/tree/master/apps-devstg/us-east-1/k8s-eks/k8s-components). |
| 30 | + |
| 31 | +Note `enable_keda` has to be enabled and, for the next example, also enable `enable_keda_http_add_on`. |
| 32 | + |
| 33 | +To read more on how to enable components see [here](./k8s.md#eks). |
| 34 | + |
| 35 | +## Giving it a try! |
| 36 | + |
| 37 | +Now, let's create an example so we can show how KEDA Works |
| 38 | + |
| 39 | +We will deploy a simple NGINX server. |
| 40 | + |
| 41 | +These are the manifests for NGINX. |
| 42 | + |
| 43 | +Let's create a namespace: |
| 44 | + |
| 45 | +```yaml |
| 46 | +apiVersion: v1 |
| 47 | +kind: Namespace |
| 48 | +metadata: |
| 49 | + name: demoapps |
| 50 | + labels: |
| 51 | + name: demoapps |
| 52 | +``` |
| 53 | +
|
| 54 | +This is the `nginx.yaml`: |
| 55 | + |
| 56 | +```yaml |
| 57 | +apiVersion: apps/v1 |
| 58 | +kind: Deployment |
| 59 | +metadata: |
| 60 | + name: nginx-deployment |
| 61 | + namespace: demoapps |
| 62 | + labels: |
| 63 | + app: nginx |
| 64 | +spec: |
| 65 | + replicas: 1 |
| 66 | + selector: |
| 67 | + matchLabels: |
| 68 | + app: nginx |
| 69 | + template: |
| 70 | + metadata: |
| 71 | + labels: |
| 72 | + app: nginx |
| 73 | + spec: |
| 74 | + containers: |
| 75 | + - name: nginx-container |
| 76 | + image: nginx:latest |
| 77 | +``` |
| 78 | + |
| 79 | +And this is the `service.yaml`: |
| 80 | + |
| 81 | +```yaml |
| 82 | +apiVersion: v1 |
| 83 | +kind: Service |
| 84 | +metadata: |
| 85 | + name: nginx-svc |
| 86 | + namespace: demoapps |
| 87 | +spec: |
| 88 | + type: NodePort |
| 89 | + selector: |
| 90 | + app: nginx |
| 91 | + ports: |
| 92 | + - protocol: TCP |
| 93 | + port: 80 |
| 94 | + targetPort: 80 |
| 95 | +``` |
| 96 | + |
| 97 | +Deploy the resources using `kubectl`. |
| 98 | + |
| 99 | +!!! Info |
| 100 | + Note you can use `kubectl` through [**binbash Leverage**](https://leverage.binbash.co/), for more info read [here](../../leverage-cli/reference/kubectl/). |
| 101 | + |
| 102 | +These are the deployed resources: |
| 103 | + |
| 104 | +```shell |
| 105 | +NAME READY STATUS RESTARTS AGE |
| 106 | +pod/nginx-deployment-5bb85d69d8-g997n 1/1 Running 0 55s |
| 107 | +
|
| 108 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 109 | +service/nginx-svc NodePort 10.100.222.129 <none> 80:30414/TCP 54s |
| 110 | +
|
| 111 | +NAME READY UP-TO-DATE AVAILABLE AGE |
| 112 | +deployment.apps/nginx-deployment 1/1 1 1 56s |
| 113 | +
|
| 114 | +NAME DESIRED CURRENT READY AGE |
| 115 | +replicaset.apps/nginx-deployment-5bb85d69d8 1 1 1 56s |
| 116 | +``` |
| 117 | + |
| 118 | +To try it, create a port-forward to the service and hit it from your browser. |
| 119 | + |
| 120 | +```shell |
| 121 | +kubectl port-forward -n demoapps svc/nginx-svc 8080:80 |
| 122 | +``` |
| 123 | + |
| 124 | +Try it! |
| 125 | + |
| 126 | +```shell |
| 127 | +curl localhost:8080 |
| 128 | +``` |
| 129 | + |
| 130 | +Now, it has no horizontal autoscaling tool (HPA), so it won't scale. I.e. it always will have one pod (as per the manifests). |
| 131 | + |
| 132 | +Let's create then a KEDA autoscaler! |
| 133 | + |
| 134 | +This is the manifest: |
| 135 | + |
| 136 | +```yaml |
| 137 | +apiVersion: http.keda.sh/v1alpha1 |
| 138 | +kind: HTTPScaledObject |
| 139 | +metadata: |
| 140 | + name: nginx-scaledobject |
| 141 | + namespace: demoapps |
| 142 | +spec: |
| 143 | + hosts: |
| 144 | + - "thehostname.internal" |
| 145 | + targetPendingRequests: 100 |
| 146 | + scaleTargetRef: |
| 147 | + deployment: nginx-deployment |
| 148 | + service: nginx-svc |
| 149 | + port: 80 |
| 150 | + replicas: |
| 151 | + min: 0 |
| 152 | + max: 10 |
| 153 | +``` |
| 154 | + |
| 155 | +It can be seen an HPA and a custom resource were created: |
| 156 | + |
| 157 | + |
| 158 | +```shell |
| 159 | +NAME REFERENCE TARGETS MINPODS MAXPODS REPLICAS AGE |
| 160 | +horizontalpodautoscaler.autoscaling/keda-hpa-nginx-scaledobject Deployment/nginx-deployment <unknown>/100 (avg) 1 10 0 15s |
| 161 | +
|
| 162 | +NAME TARGETWORKLOAD TARGETSERVICE MINREPLICAS MAXREPLICAS AGE ACTIVE |
| 163 | +nginx-scaledobject apps/v1/Deployment/nginx-deployment nginx-svc:80 0 10 52s |
| 164 | +``` |
| 165 | + |
| 166 | +Note in the HPA no replicas are in place, i.e. no pods for our app. Now if you try: |
| 167 | + |
| 168 | +```shell |
| 169 | +kubectl port-forward -n demoapps svc/nginx-svc 8080:80 |
| 170 | +``` |
| 171 | + |
| 172 | +...it will fail, since no pod are available to answer the service. |
| 173 | + |
| 174 | +Instead we have to hit a KEDA intercepter, that will route the traffic using the Hosts in the `HTTPScaledObject` object. |
| 175 | + |
| 176 | +We've set `thehostname.internal` as the name, so let's port-forward the intercepter... |
| 177 | + |
| 178 | +```shell |
| 179 | +kubectl port-forward -n keda svc/keda-add-ons-http-interceptor-proxy 8080:8080 |
| 180 | +``` |
| 181 | + |
| 182 | +...and hit it with the Host header set: |
| 183 | + |
| 184 | +```shell |
| 185 | +curl localhost:8080 -H "Host: thehostname.internal" |
| 186 | +``` |
| 187 | + |
| 188 | +If you check the HPA now it will have at least one replica. |
| 189 | + |
| 190 | +!!! Note |
| 191 | + Note the first query will have a delay since the pod has to be created. |
| 192 | + |
| 193 | +Then if you cancel the port-forward and wait for a while, the deployment will be scaled-down to zero again. |
| 194 | + |
| 195 | +Voilà! |
| 196 | + |
| 197 | +!!! Note |
| 198 | + There are other ways to configure KEDA, e.g. using Prometheus metrics, read more [here](https://keda.sh/docs/2.15/concepts/). |
| 199 | + |
| 200 | +## Final thoughts |
| 201 | + |
| 202 | +Given the scale-to-zero feature for pods, KEDA is a great match to Karpenter! |
0 commit comments