Share the love

Here are the general steps to use Kubernetes-based Event Driven Autoscaler (KEDA) to autoscale a microservice in AKS based on the number of HTTP requests:

  1. Install KEDA on your AKS cluster by following the steps provided blog post Use Message Rate to Autoscale Microservice in AKS.
  2. Create a ScaledObject resource in your Kubernetes cluster to define the scaling behavior of your microservice. In the ScaledObject resource, you need to specify the deployment that you want to scale, the polling interval, and the scaling triggers.
  3. For the scaling triggers, you will use the prometheus type, which allows you to scale based on metrics from a Prometheus server.
  4. Configure Prometheus to scrape metrics from your microservice, including the number of HTTP requests.
  5. In the ScaledObject resource, specify the Prometheus query that you want to use to determine the number of HTTP requests and the threshold values for scaling up and scaling down.
  6. Deploy the ScaledObject resource and verify that it is working as expected by checking the number of replicas of your microservice deployment and the values of the Prometheus metrics.
  7. You can also create an alert to notify you when the pod is scaling up or down.

Here is an example YAML file that demonstrates how to use KEDA to autoscale a microservice based on the number of HTTP requests:

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: my-microservice-scaledobject
  namespace: my-namespace
spec:
  scaleTargetRef:
    kind: Deployment
    name: my-microservice-deployment
  pollingInterval: 10
  triggers:
  - type: prometheus
    metadata:
      query: 'sum(rate(http_requests{job="my-microservice"}[1m])) > 10'
    scaleDown:
      threshold: 0
    scaleUp:
      threshold: 100

In this example, KEDA is set to monitor the number of HTTP requests for the my-microservice-deployment deployment in the my-namespace namespace. The pollingInterval is set to 10 seconds, and the query parameter is set to retrieve the number of HTTP requests over the last 1 minute. When the number of HTTP requests exceeds 100, KEDA will scale up the number of replicas of the deployment, and when the number of HTTP requests falls below 0, KEDA will scale down the number of replicas.

It’s also important to note that you need to have Prometheus installed and configured to scrape metrics from your microservice. You can also use other metrics providers like Stackdriver, Azure Monitor, InfluxDB, etc.

Install Prometheus and configured to scrape metrics from your microservice

Here are the general steps to install Prometheus and configure it to scrape metrics from your microservice in an AKS cluster:

  • Install Prometheus using Helm by running the following command:
helm install prometheus stable/prometheus
  • Create a ServiceMonitor resource in your Kubernetes cluster to configure Prometheus to scrape metrics from your microservice. In the ServiceMonitor resource, you need to specify the target labels that match the labels of your microservice pods and the scrape interval.
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: my-microservice-servicemonitor
  namespace: my-namespace
spec:
  selector:
    matchLabels:
      app: my-microservice
  namespaceSelector:
    matchNames:
    - my-namespace
  endpoints:
  - path: /metrics
    port: my-microservice

In this example, Prometheus is set to scrape metrics from the my-microservice-servicemonitor service in the my-namespace namespace, which expose metrics on the /metrics endpoint.

  • Deploy the ServiceMonitor resource and verify that it is working as expected by checking the Prometheus server and the metrics endpoint of your microservice.
  • You can use Prometheus UI to check the metrics and test the queries.