1.开启prometheus 插件
部署的是 EMQX 4.4 开源版, 则需要通过 EmqxPlugin CRD 开启emqx_prometheus 插件:
bash
cat << "EOF" | kubectl apply -f -
apiVersion: apps.emqx.io/v1beta4
kind: EmqxPlugin
metadata:
name: emqx-prometheus
spec:
pluginName: emqx_prometheus
selector:
apps.emqx.io/instance: emqx
apps.emqx.io/managed-by: emqx-operator
获取AppID和AppSecret
bash
curl -u {AppID}:{AppSecret} -X GET localhost:18083/api/v4/emqx_prometheus?type=prometheus
2.配置 EMQX 服务的 Kubernetes Service
确保 EMQX 的接口通过一个 Kubernetes Service 暴露。
yaml
kind: Service
apiVersion: v1
metadata:
name: emqx-prometheus
labels:
app: emqx
spec:
ports:
- name: prometheus
protocol: TCP
port: 18083
targetPort: 18083
type: ClusterIP
selector:
apps.emqx.io/instance: emqx
apps.emqx.io/managed-by: emqx-operator
3.创建 ServiceMonitor
yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: emqx-monitor
spec:
endpoints:
- basicAuth:
password:
key: password
name: emqx-basic-auth
username:
key: username
name: emqx-basic-auth
params:
type:
- prometheus
path: /api/v4/emqx_prometheus
port: prometheus
scheme: http
namespaceSelector:
matchNames:
- mes-emqx
selector:
matchLabels:
app: emqx
4.创建 BasicAuth Secret
bash
kubectl create secret generic emqx-basic-auth \
--from-literal=username={AppID} \
--from-literal=password={AppSecret}