Investigate KEDA to automatically scale consumer pods based on queue depth

Open
#377 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
38/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
helm, java, kubernetes, rabbitmq

Research direction

Start by inspecting the repository's Helm chart and consumer Deployment templates, then compare the work needed to enable the Kubernetes Metrics API with installing KEDA. The issue does not name chart paths or queue details, so first identify how the consumer and RabbitMQ are configured. Done means documenting which approach is simpler and, if KEDA is chosen, outlining the chart and cluster changes required.

Written by the indexing model from the issue text.

Description

K8s

At the time of writing, the NCEAS K8s clusters do not have the Kubernetes Metrics Server available, so the standard Kubernetes Horizontal Pod Autoscaler (HPA) won't work because it relies on the Metrics API (CPU/memory metrics).

KEDA (Kubernetes Event-driven Autoscaling - website | GitHub) could be used to automatically scale consumer pods based on RabbitMQ queue depth, without relying on the Metrics API. This looks pretty simple (Google Gemini instructions below. Add salt). Worth doing only if this is simpler/quicker than making the Metrics API available.


Google Gemini

To automatically scale your Helm chart's consumer pods based on RabbitMQ queue depth without relying on the Kubernetes Metrics Server, the standard Kubernetes Horizontal Pod Autoscaler (HPA) won't work out of the box because it relies on the Metrics API (CPU/memory metrics).

Instead, the standard solution in the Kubernetes ecosystem is to use KEDA (Kubernetes Event-driven Autoscaling). KEDA acts as the Control Loop described in the article:

  • Desired state: Target queue depth per pod (e.g., 5 messages per consumer).
  • Sensor: KEDA checking RabbitMQ's API directly via its built-in RabbitMQ scaler.
  • Control Element: Adjusting your Deployment's spec.replicas.

Here is the straightforward step-by-step way to apply this concept to your existing Helm chart:


Step 1: Install KEDA on your Cluster

Since KEDA queries RabbitMQ directly, it does not require the Kubernetes Metrics Server API. Install KEDA once in your cluster using Helm:

helm repo add kedacore https://kedacore.github.io/charts
helm repo update
helm install keda kedacore/keda --namespace keda --create-namespace


Step 2: Define a ScaledObject in Your Helm Chart

In your Helm chart's templates/ folder, add a new file named scaledobject.yaml. This resource tells KEDA how to monitor your RabbitMQ queue and control your consumer deployment.

templates/scaledobject.yaml:

apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
  name: {{ include "mychart.fullname" . }}-autoscaler
  labels:
    {{- include "mychart.labels" . | nindent 4 }}
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: {{ include "mychart.fullname" . }}-consumer # Name of your consumer Deployment
  minReplicaCount: {{ .Values.autoscaling.minReplicas | default 1 }}
  maxReplicaCount: {{ .Values.autoscaling.maxReplicas | default 10 }}
  cooldownPeriod: 300 # Seconds to wait before scaling down to 0 or minReplicas
  triggers:
    - type: rabbitmq
      metadata:
        protocol: amqp
        queueName: {{ .Values.rabbitmq.queueName | quote }}
        mode: QueueLength # Triggers on total unprocessed messages in the queue
        value: {{ .Values.autoscaling.targetQueueLength | quote }} # Target messages per pod
      authenticationRef:
        name: keda-rabbitmq-auth


Step 3: Configure Authentication for KEDA

KEDA needs credentials to inspect your RabbitMQ queue length via the RabbitMQ API/AMQP protocol. Add a trigger authentication object alongside your ScaledObject:

templates/keda-auth.yaml:

apiVersion: keda.sh/v1alpha1
kind: TriggerAuthentication
metadata:
  name: keda-rabbitmq-auth
spec:
  secretTargetRef:
    - parameter: host
      name: {{ .Values.rabbitmq.secretName }} # Name of your existing Kubernetes Secret
      key: {{ .Values.rabbitmq.secretHostKey | default "RABBITMQ_URI" }} # e.g., amqp://user:[email protected]:5672/vhost


Step 4: Configure values.yaml

Update your chart's values.yaml to make the scaling behavior configurable:

autoscaling:
  enabled: true
  minReplicas: 1
  maxReplicaCount: 10
  targetQueueLength: "5" # ScaledObject will aim for 1 pod per 5 queued messages

rabbitmq:
  queueName: "task_queue"
  secretName: "rabbitmq-credentials"
  secretHostKey: "AMQP_URL"


Step 5: Update Your Deployment Template

Ensure your consumer Deployment template (templates/deployment.yaml) does not hardcode a replicas count if autoscaling is enabled, or allow KEDA to take over replica management.

In templates/deployment.yaml:

spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      ...


How the Control Loop Operates in Practice

Once deployed, KEDA executes the loop continuously:

  1. Observe (Sensor): KEDA periodically checks RabbitMQ for queueName. If 25 messages are sitting in the queue and targetQueueLength is 5, it calculates that 5 pods are required ($25 / 5 = 5$).
  2. Compare with Desired State: If you currently have 1 pod running, the state is out of balance.
  3. Act (Control Element): KEDA updates spec.replicas on your consumer Deployment to 5.
  4. Scale to Zero (Optional): If minReplicaCount is set to 0, KEDA will spin down all pods when the queue drops to 0 messages, saving cluster resources entirely until a new message arrives.
Dominant language
Java
Stars
0
Forks
2
Avg merge
6d 6h
Merged PRs (30d)
3

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from DataONEorg/dataone-indexer

All issues in DataONEorg/dataone-indexer

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.