IBM Cloud Global

Cloud Global

Our mission is to provide clients with an online user community of industry peers and IBM experts, to exchange tips and tricks, best practices, and product knowledge. We hope the information you find here helps you maximize the value of your IBM Cloud solutions.

 View Only

Horizontal Pod Autoscaling in IKS and ROKS with KEDA and IBM Cloud Monitoring

By Carlos Tolon posted Thu June 08, 2023 09:28 AM

  

Crafting the appropriate number of replicas in Kubernetes and OpenShift to manage fluctuating workloads, with variable incoming requests or tasks, can be a challenging task.

This blog will guide you on utilizing Kubernetes Event-driven Autoscaling (KEDA) with IBM Cloud Monitoring in order to define your application's maximum and minimum replica counts based on metrics.

What is KEDA?

KEDA is a lightweight component that works with native Kubernetes Horizontal Pod Autoscaler extending its functionality and performing its operations with four main components:

    • Scalers: provides KEDA with different custom metrics (e.g. Prometheus) to help detect if a deployment should be activated or deactivated [see list of available integrations].

    • keda-operator-metrics-apiserver: acts as a proxy for the different scalers and exposes metrics for the corresponding created HPA.

    • keda-operator: controls the operation of the scaling system

    • keda-admission-webhook: automatically validates the changes keda-operator has performed to prevent misconfiguration and enforce best practices. 

The KEDA operator interacts with specific CRDs (triggerauthentications and/or clustertriggerauthentication, scaledobjects and scaledjobs).

    • ScaledObject: defines how KEDA should scale your application and what trigger(s) to use. This object is configured with information from your workload, how to scale it and which Scalers to use combined with triggers.

      • It includes references to TriggerAuthentication or ClusterTriggerAuthentication which contains the authentication details or secrets to monitor the configured scale. 

    • ScaledJob: defines how KEDA should scale your job application and what trigger(s) to use

The diagram below shows how KEDA interacts with external sources to trigger the HPA to scale up and down your application. 

Deploying KEDA with Helm

You can deploy KEDA in Kubernetes and OpenShift with 3 simple steps:

    1. Add Helm repository

helm repo add kedacore https://kedacore.github.io/charts

    1. Update Helm repositories


helm repo update

    1. Install keda Helm chart


helm install keda kedacore/keda --namespace keda  --create-namespace

Scaling a web application connecting KEDA to IBM Cloud Monitoring

First, let’s deploy an example application based on Nginx with 2 replicas:

kubectl apply -f webapp.yaml -n webapp


apiVersion: apps/v1

kind: Deployment

metadata:

  name: webapp

spec:

  replicas: 2

  selector:

    matchLabels:

      app: nginx

  template:

    metadata:

      labels:

        app: nginx

    spec:

      containers:

      - name: nginx

        image: nginx:alpine-slim

        ports:

        - containerPort: 80

          name: web

        resources:

          requests:

            cpu: 100m

            memory: 100Mi

          limits:

            cpu: 100m

            memory: 100Mi

 

KEDA requires managing the authentication via TriggerAuthentication or ClusterTriggerAuthentication that references the IBM Cloud Monitoring API Token. See how to get the IBM Cloud Monitoring API Token in this document.

You just need to create a secret with your IBM Cloud Monitoring API Token.

kubectl create secret generic keda-prom-secret --from-literal=bearerToken=<API_KEY> -n webapp

Reference this secret in a new TriggerAuthentication object you can create in the application namespace:

kubectl apply -f triggerauthentication.yaml -n webapp

apiVersion: keda.sh/v1alpha1

kind: TriggerAuthentication

metadata:

  name: keda-prom-creds

spec:

  secretTargetRef:

  - parameter: bearerToken

    name: keda-prom-secret

    key: bearerToken

Finally, define how you want to manage the automatic scale of your webapp creating the ScaledObject. In this case, we’ll be scaling our application when CPU usage of all replicas is in average above 80%:

Note: the serverAddress parameter would depend on the IBM Cloud Monitoring instance region

kubectl apply -f scaledobject.yaml -n webapp


apiVersion: keda.sh/v1alpha1

kind: ScaledObject

metadata:

  name: nginx-scale

spec:

  scaleTargetRef:

    kind: Deployment

    name: webapp

  minReplicaCount: 2

  maxReplicaCount: 5

  cooldownPeriod: 30

  pollingInterval: 30

  triggers:

  - type: prometheus

    metadata:

      serverAddress: https://<region>.monitoring.cloud.ibm.com/prometheus

      metricName: sysdig_container_cpu_cores_used_percent

      query: |

        avg(sysdig_container_cpu_cores_used_percent{kube_namespace_name="webapp", kube_workload_name="webapp"})

      threshold: "80"

      authModes: "bearer"

    authenticationRef:

      name: keda-prom-creds

 

Notice the metricName parameter. This is a custom name you set for receiving the value from the query. Keda gets the result of the query and creates the sysdig_container_cpu_cores_used_percent metric with it. Then, it uses this metric to trigger the escalation.

You can use any available metric when using KEDA and IBM Cloud Monitoring, system metrics for resource utilization (CPU or memory) but also custom metrics coming from your application (pending requests, latency, etc). 

Learn more about IBM Cloud Monitoring

In this article, you learned how easy it is to deploy an Horizontal Pod Autoscaler with Keda that is triggered by metrics from IBM Cloud Monitoring. Click this link to start using IBM Cloud Monitoring today!

0 comments
25 views

Permalink