# Monitor EMQX cluster by Prometheus and Grafana

# Task Target

Deploy EMQX Exporter (opens new window) and monitor EMQX cluster by Prometheus and Grafana.

# Deploy Prometheus and Grafana

Prometheus' deployment documentation can refer to Prometheus (opens new window) Grafana' deployment documentation can refer to Grafana (opens new window)

# Deploy EMQX Cluster

Here are the relevant configurations for EMQX Custom Resource. You can choose the corresponding APIVersion based on the version of EMQX you wish to deploy. For specific compatibility relationships, please refer to EMQX Operator Compatibility:

# Create API secret

emqx-exporter and Prometheus will pull metrics from EMQX dashboard API, so you need to sign in to dashboard to create an API secret.

Note that it is different to create a secret between EMQX 5 and EMQX 4.4 on the dashboard.

# Deploy EMQX Exporter (opens new window)

The emqx-exporter is designed to expose partial metrics that are not included in the EMQX Prometheus API. It is compatible with EMQX 4.4 and EMQX 5, both open-source and enterprise.

apiVersion: v1
kind: Service
metadata:
  labels:
    app: emqx-exporter
  name: emqx-exporter-service
spec:
  ports:
    - name: metrics
      port: 8085
      targetPort: metrics
  selector:
    app: emqx-exporter
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: emqx-exporter
  labels:
    app: emqx-exporter
spec:
  selector:
    matchLabels:
      app: emqx-exporter
  replicas: 1
  template:
    metadata:
      labels:
        app: emqx-exporter
    spec:
      securityContext:
        runAsUser: 1000
      containers:
        - name: exporter
          image: emqx-exporter:latest
          imagePullPolicy: IfNotPresent
          args:
            # "emqx-dashboard-service-name" is the service name that creating by operator for exposing 18083 port
            - --emqx.nodes=${emqx-dashboard-service-name}:18083
            - --emqx.auth-username=${paste_your_new_api_key_here}
            - --emqx.auth-password=${paste_your_new_secret_here}
          securityContext:
            allowPrivilegeEscalation: false
            runAsNonRoot: true
          ports:
            - containerPort: 8085
              name: metrics
              protocol: TCP
          resources:
            limits:
              cpu: 100m
              memory: 100Mi
            requests:
              cpu: 100m
              memory: 20Mi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

Set the arg "--emqx.nodes" to the service name that creating by operator for exposing 18083 port. Check out the service name by call kubectl get svc.

Save the above content as emqx-exporter.yaml, replace --emqx.auth-username and --emqx.auth-password with your new creating API secret, then execute the following command to deploy the emqx-exporter:

kubectl apply -f emqx-exporter.yaml
1

Check the status of emqx-exporter pod。

$ kubectl get po -l="app=emqx-exporter"

NAME      STATUS   AGE
emqx-exporter-856564c95-j4q5v   Running  8m33s
1
2
3
4

# Configure Prometheus Monitor

Prometheus-operator uses PodMonitor (opens new window) and ServiceMonitor (opens new window) CRD to define how to monitor a set of pods or services dynamically.

Save the above content as monitor.yaml and execute the following command:

$ kubectl apply -f monitor.yaml
1

# View EMQX Indicators on Prometheus

Open the Prometheus interface, switch to the Graph page, and enter emqx to display as shown in the following figure:

Switch to the Status -> Targets page, the following figure is displayed, and you can see all monitored EMQX Pod information in the cluster:

# Import Grafana Templates

Import all dashboard templates (opens new window). Open the main dashboard EMQX and enjoy yourself!