[Write-Up: K8s HPA based on RabbitMQ queues with KEDA]

There is a task to track custom metrics to use HPA (Horizontal pod autoscaler) to increase the number of pods as the metric increases. In this specific case, it is necessary to monitor RabbitMQ queues and, as requests in the queue increase, increase the number of pods proportionally.

Requirements:

From requirements, we only need to install KEDA, which can be installed via helm chart or in any convenient way from the Deploying KEDA documentation.

Deploying KEDA objects

#yaml
apiVersion: v1
kind: Secret
metadata:
  name: keda-rabbitmq-secret
data:
  AMQP_URL: YW1xcDovL3VzZXI6cGFzc3dvcmRAcmFiYml0bXEtY2x1c3Rlcjo1NjcyCg== # The connection string that you created.

KEDA object for TriggerAuthentication

#yaml
apiVersion: keda.sh/v1alpha1
kind: TriggerAuthentication
metadata:
  name: keda-trigger-auth-rabbitmq-conn
  namespace: default
spec:
  secretTargetRef:
    - parameter: host
      name: keda-rabbitmq-secret # keda-rabbitmq-secret is the Secret that you created in the preceding step. 
      key: AMQP_URL # key name from data section

Let's deploy these objects to the k8s cluster

#cmd
kubectl apply -f secret.yaml
kubectl apply -f rabbitmq-trigger-auth.yaml

Next you will need a ScaledObject object, more information about the parameters can be found in the RabbitMQ Queue documentation

#yaml
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: rabbitmq-scaledobject
  namespace: default
spec:
  scaleTargetRef:
    name: application-deployment # Name of application deployment.
  maxReplicaCount: 10
  minReplicaCount: 1
  triggers:
  - type: rabbitmq
    metadata:
      protocol: amqp
      queueName: application-queue # Name of RabbitMQ queue.
      mode: QueueLength # QueueLength or MessageRate
      value: "20" # message backlog or publish/sec. target per instance
      metricName: custom-testqueue 
    authenticationRef:
      name: keda-trigger-auth-rabbitmq-conn # TriggerAuthentication is the object that you created in the preceding step.

Deploy this object to the k8s cluster and check the creation of HPA

#cmd
// Deploy a ScaledObject. 
kubectl apply -f ScaledObject.yaml   

scaledobject.keda.sh/rabbitmq-scaledobject created

// Query the status of the ScaledObject. 
kubectl get ScaledObject

NAME                    SCALETARGETKIND      SCALETARGETNAME   MIN   MAX   TRIGGERS   AUTHENTICATION                    READY   ACTIVE   FALLBACK   AGE
rabbitmq-scaledobject   apps/v1.Deployment   application-deployment        1     10    rabbitmq   keda-trigger-auth-rabbitmq-conn   True    False    False      17s

// Check whether the HPA is deployed to scale the application. 
kubectl get hpa

NAME                             REFERENCE               TARGETS      MINPODS   MAXPODS   REPLICAS   AGE
keda-hpa-rabbitmq-scaledobject   Deployment/application-deployment   0/20 (avg)   1         10        2          2m35s

As a result, with the help of KEDA and RabbitMQ Queue trigger, we were able to scale the application based on the length of the queue in RabbitMQ.