[K8s] How to enable Horizontal Pod Autoscaling based on RabbitMQ queue metrics

This guide outlines the process of setting up Horizontal Pod Autoscaling (HPA) for the Scanning service based on RabbitMQ queue size metrics. This configuration automatically scales the number of scanning pods up or down in response to the number of messages in the scan queue.

This documentation provides a general implementation approach that will need to be adapted to your specific environment. The exact commands, configurations, and values presented here should be reviewed and modified according to your organization's specific Kubernetes infrastructure, network architecture, and operational requirements.

Prerequisites

  • Kubernetes cluster with HPA support

  • If using an external RabbitMQ instance:

    • RabbitMQ version 3.8.0 or higher
    • RabbitMQ Prometheus plugin must be enabled
    • Port 15692 must be accessible for metrics scraping
    • The RabbitMQ management interface must be available
  • Basic understanding of Kubernetes and Prometheus

1. Install Prometheus (if not already deployed)

If Prometheus is not already deployed in your cluster, install it using Helm:`

Bash
Copy

2. Configure Prometheus to Scrape RabbitMQ Metrics

Add the following scrape configuration to your Prometheus ConfigMap to collect metrics from RabbitMQ:

YAML
Copy

Apply the updated ConfigMap and restart Prometheus to apply the changes.

Note for External RabbitMQ: If using an external RabbitMQ instance, you'll need to modify the scrape configuration to target your external instance instead of using Kubernetes service discovery.

3. Install Prometheus Adapter (if not already deployed)

The Prometheus Adapter is required to expose Prometheus metrics to the Kubernetes metrics API:

Please update prometheus.url and prometheus.port according to your environment

Bash
Copy

4. Configure Prometheus Adapter

Update the Prometheus Adapter ConfigMap to expose the RabbitMQ queue metric:

YAML
Copy

This configuration:

  • Queries metrics with name "rabbitmq_queue_messages_ready"
  • Associates metrics with Kubernetes namespaces
  • Renames the metric to "rabbitmq_scan_queue_messages"
  • Filters for scan queues with Low, Medium, and High priorities
  • Sums the total number of messages across these queues

After modifying the ConfigMap, restart the adapter:

Bash
Copy

5. Verify the Custom Metric

Check that the metric is properly exposed to the Kubernetes API:

Bash
Copy

You should see "rabbitmq_scan_ queue_messages" listed in the output.

Bash
Copy

To get the metric value:

Bash
Copy

You should see the actual value of the queue size:

Bash
Copy

6. Create a Horizontal Pod Autoscaler

Create an HPA resource that uses the custom metric to scale the Scanning service:

YAML
Copy

Apply the HPA:

Bash
Copy

7. Monitor the HPA

Check the status of your HPA:

Bash
Copy

Scaling Behavior

  • When the total number of messages in the scan queues exceeds 5000, the HPA will scale up the number of scanning pods (up to the maximum of 10).
  • When the number of messages decreases, the HPA will gradually scale down the number of pods.

Troubleshooting

If the HPA isn't working as expected:

  1. Verify Prometheus is collecting RabbitMQ metrics:
Bash
Copy

Then visit http://localhost:9090 and query:

rabbitmq_queue_messages_ready{queue=~"object_ready_for_scan_queue_(Low|Medium|High)"}

  1. Check that the Prometheus Adapter can access the metrics:
Bash
Copy
  1. Verify the custom metric is available:
Bash
Copy
  1. Ensure the HPA is targeting the correct deployment and namespace.
Type to search, ESC to discard
Type to search, ESC to discard
Type to search, ESC to discard