上一篇文章我們演示了如何《在 S3 備份恢復(fù) RadonDB MySQL 集群數(shù)據(jù)》,本文將演示在 KubeSphere[1] 中使用 Prometheus[2] + Grafana[3] 構(gòu)建 MySQL 監(jiān)控平臺,開啟所需監(jiān)控指標(biāo)。
背景
Prometheus 基于文本的暴露格式,已經(jīng)成為云原生監(jiān)控領(lǐng)域事實上的標(biāo)準(zhǔn)格式。
RadonDB MySQL 監(jiān)控引擎基于 Prometheus MySQLd Exporter[5] 定義。通過 mysqld-exporter 抓取 RadonDB MySQL 服務(wù)指標(biāo),再通過接入第三方應(yīng)用平臺實現(xiàn)監(jiān)控指標(biāo)可視化。
準(zhǔn)備工作
- 已準(zhǔn)備可用 Kubernetes 或 KubeSphere 集群
- 已部署 RadonDB MySQL 集群 《部署文檔》
- RadonDB MySQL Kubernetes 版本 2.1.0+
部署步驟
步驟 1: 配置 serviceMonitor
serviceMonitor 開啟后將自動綁定 mysqld_exporter 與 Prometheus。
serviceMonitor 參數(shù)包含如下字段:
serviceMonitor:
enabled: true
## Additional labels for the serviceMonitor. Useful if you have multiple prometheus operators running to select only specific ServiceMonitors
# additionalLabels:
# prometheus: prom-internal
interval: 10s
scrapeTimeout: 3s
# jobLabel:
# targetLabels:
# podTargetLabels:
namespaceSelector:
any: true
selector:
matchLabels:
app.kubernetes.io/managed-by: mysql.radondb.com
app.kubernetes.io/name: mysql
您可以在 charts/mysql-operator/values.yaml 文件中配置 serviceMonitor。
- 新部署 Operator 時,
serviceMonitor.enabled默認(rèn)為 true,表示默認(rèn)開啟。 - 已部署 Operator 2.1.0 以下版本的集群,需重新部署 Operator。
步驟 2: 配置 metricsOpts
metricsOpts 是 CRD mysqlclusters.mysql.radondb.com 中定義 RadonDB MySQL 集群監(jiān)控的參數(shù),可通過配置mysql_v1alpha1_mysqlcluster.yaml 文件中參數(shù)值開啟監(jiān)控服務(wù)。
metricsOpts 參數(shù)包含如下字段:
metricsOpts:
enabled: false
image: prom/mysqld-exporter:v0.12.1
resources:
requests:
cpu: 10m
memory: 32Mi
limits:
cpu: 100m
memory: 128Mi
metricsOpts.enabled 默認(rèn)為 false,需手動設(shè)置為 true。
- 設(shè)置
metricsOpts.enabled為 true,開啟集群監(jiān)控功能; - 設(shè)置資源參數(shù)值,定義監(jiān)控容器資源配額大??;
文件參數(shù)修改完成后,使用如下指令應(yīng)用配置,部署/更新集群回顯信息如下:
$ kubectl apply -f config/sample/mysql_v1alpha1_mysqlcluster.yaml
cluster.mysql.radondb.com/sample created/configured
查看監(jiān)控服務(wù)
通過客戶端查看
您可以通過如下指令查看集群監(jiān)控服務(wù)和 serviceMonitor 信息。
$ kubectl get service,servicemonitor
$ kubectl describe servicemonitor <serviceName>
預(yù)期效果
$ kubectl get service,servicemonitor
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mysql-operator-metrics ClusterIP 10.96.242.205 <none> 8443/TCP 3h25m
service/sample-follower ClusterIP 10.96.2.234 <none> 3306/TCP 21h
service/sample-leader ClusterIP 10.96.30.238 <none> 3306/TCP 21h
service/sample-metrics ClusterIP 10.96.7.222 <none> 9104/TCP 3h24m
service/sample-mysql ClusterIP None <none> 3306/TCP 21h
NAME AGE
servicemonitor.monitoring.coreos.com/demo-mysql-operator 3h25m
$ kubectl describe servicemonitor demo-mysql-operator
Name: test-radondb-mysql-metrics
Namespace: default
Labels: app=test-radondb-mysql
app.kubernetes.io/managed-by=Helm
app.kubernetes.io/vendor=kubesphere
chart=radondb-mysql-1.0.0
heritage=Helm
release=test
Annotations: kubesphere.io/creator: admin
API Version: monitoring.coreos.com/v1
Kind: ServiceMonitor
......
Spec:
Endpoints:
Interval: 1m
Path: /metrics
Port: metrics
Scheme: http
Scrape Timeout: 10s
......
在 KubeSphere 平臺查看
在 KubeSphere 企業(yè)空間部署的 RadonDB MySQL Operator 和集群,開啟監(jiān)控后,可在如下頁面查看監(jiān)控服務(wù)狀態(tài)。
- 在項目空間應(yīng)用負載下的服務(wù)頁面,點擊
<集群名稱>-metrics,可查看監(jiān)控服務(wù)信息。

- 在項目空間應(yīng)用負載下的容器組頁面,點擊一個容器的名稱,可查看該容器中
metrics資源狀態(tài)。

查看監(jiān)控內(nèi)容
通過 KubeSphere 自定義監(jiān)控
KubeSphere 的監(jiān)控引擎基于 Prometheus 和 Prometheus Operator。使用 KubeSphere 的自定義監(jiān)控功能支持以可視化的形式監(jiān)控 RadonDB MySQL 指標(biāo)。
- 在集群同一項目中,選擇 監(jiān)控告警 -> 自定義監(jiān)控 -> 創(chuàng)建;
- 在對話框中,選擇 MySQL 模版,并繼續(xù)配置監(jiān)控模版;

- 點擊 保存模版,即新創(chuàng)建監(jiān)控面板。

- 新建監(jiān)控面板需等待約十分鐘,即可查看監(jiān)控數(shù)據(jù)。

更多詳情,請查看 KubeSphere 自定義監(jiān)控介紹[6] 和 可視化監(jiān)控[7]。
通過 Prometheus + Grafana 平臺
Grafana 是一個跨平臺、開源的數(shù)據(jù)可視化網(wǎng)絡(luò)應(yīng)用程序平臺。通過 Prometheus + Grafana 平臺查看監(jiān)控基本原理如下:
- 通過 mysql_exporter 獲取 RadonDB MySQL 服務(wù)監(jiān)控數(shù)據(jù).
- 通過 node_exporter 獲得 RadonDB MySQL 服務(wù)器的監(jiān)控數(shù)據(jù)。
- 將監(jiān)控數(shù)據(jù)傳到 Prometheus 后,通過配置數(shù)據(jù)源,最終在 Grafana 呈現(xiàn)豐富的監(jiān)控數(shù)據(jù)圖表和警告。

更多 Grafana 可視化監(jiān)控使用說明,請參見 Grafana Dashboards[8]。
參考引用
- KubeSphere:https://kubesphere.com.cn
- Prometheus:https://prometheus.io/
- Grafana:https://grafana.org/
- RadonDB MySQL Kubernetes 2.1.3:https://radondb.com/projects/mysql/
- Prometheus MySQLd Exporter:https://github.com/prometheus/mysqld_exporter
- 自定義監(jiān)控:https://kubesphere.io/zh/docs/project-user-guide/custom-application-monitoring/introduction/
- 可視化監(jiān)控:https://kubesphere.io/zh/docs/project-user-guide/custom-application-monitoring/visualization/overview/
- Grafana Dashboards:https://grafana.com/docs/grafana/latest/dashboards/