Scrape RabbitMQ Metrics With Prometheus in Kubernetes
… without all the complicated fuzz around it. This is probably only interesting to you if you’re running a Kubernetes cluster with something like k3s and want some monitoring and metrics from RabbitMQ without installing extra Kubernetes PlugIns and all these things.
I’m running the kube-prometheus-stack from the prometheus-community github repository. They have a helm chart available to make installing easy.
install kube-prometheus-stack
I’m just going with the default things here:
kubectl create ns monitoring
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
helm install -n monitoring monitoring prometheus-community/kube-prometheus-stack
RabbitMQ ServiceMonitor
Now, to make Prometheus scrape the metrics from RabbitMQ, first make sure that you’re running the RabbitMQ management container image, right now this would be: 3.9-management. The releases are available on Docker Hub.
Next, let’s create a ServiceMonitor. This will tell Prometheus where to look for things to get data from. Create the following file rabbitmq_servicemonitor.yaml:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
app: rabbitmq
release: monitoring
name: rabbitmq
namespace: monitoring
spec:
endpoints:
- interval: 15s
port: metrics
path: /metrics
namespaceSelector:
matchNames:
- default
selector:
matchLabels:
app: rabbitmq
release: monitoring
The important bits in here are:
- namespaceSelector: Where should Prometheus look for RabbitMQ. My RabbitMQ installation is in the default namespace
- the matchLabels and labels where it says release. They need to match to Prometheus, I first thought this has something to do with the actual version (it might still have? Who knows… this is all rocket science) but this needs to match the name of the installation we did with helm earlier. And this was called monitoring
Create a RabbitMQ service to expose metrics
Last thing, you need to create an additional service/port to expose the metrics to Prometheus. Chances are, that you have a service already configured for RabbitMQ, so other services can communicate with it. Anyway, here’s an example service which has both things in there. RabbitMQ itself and the metrics endpoint for Prometheus.
apiVersion: v1
kind: Service
metadata:
labels:
release: monitoring # put this in here
app: rabbitmq
name: rabbitmq
spec:
ports:
- port: 5672
targetPort: 5672
name: rabbitmq
- port: 15692 # put this in here
targetPort: 15692 # put this in here
name: metrics # put this in here
selector:
app: rabbitmq
type: NodePort
If you have a service already configured, just update the changes I commented with # put this in here above. These are the important bits again. The release, and that you have a name configured for the port.
I hope this helps a few folks who just want a basic setup (if you can even call this basic). Otherwise, it’s for my future self, so I can find this again when I’m doing a multi-hour google search again in the future.