Prometheus 监控非云原生应用
这里使用 MySQL 作为一个测试用例,演示如何使用 Exporter 监控非云原生应用。
部署测试用例
首先部署 MySQL 至 Kubernetes 集群中,如果存在现有的 MySQL 数据库,则跳过这一步(此处略)
1
2
3
4
5
6
7
8
9# 部署 MySQL deployment
kubectl create deploy mysql --image=registry.cn-beijing.aliyuncs.com/dotbalo/mysql:5.7.23
# 设置 MySQL root 密码
kubectl set env deploy/mysql MYSQL_ROOT_PASSWORD=mysql
deployment.apps/mysql env updated
# 查看 Pod 是否正常
kubectl get po -l app=mysql
配置监控 MySQL
创建 MySQL Service (可选)
由于这里 MySQL 数据库是运行在 k8s 集群外,所以需要配置 Service 代理外部的 MySQL 服务,创建 mysql-svc.yaml 文件,文件内容如下
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
29apiVersion: v1
kind: Endpoints
metadata:
labels:
app: mysql-prom
name: mysql-prom
namespace: public
subsets:
- addresses:
- ip: 192.168.55.62
ports:
- name: https-metrics
port: 3306
protocol: TCP
apiVersion: v1
kind: Service
metadata:
labels:
app: mysql-prom
name: mysql-prom
namespace: public
spec:
ports:
- name: https-metrics
port: 3306
protocol: TCP
targetPort: 3306
type: ClusterIP创建该资源,并查看该 Service 的 ClusterIP
1
2
3
4
5
6
7
8
9
10# kubectl create ns public # 创建 public 命名空间
namespace/public created
# kubectl create -f mysql-svc.yaml
endpoints/mysql-prom created
service/mysql-prom created
# kubectl get svc -n public
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql-prom ClusterIP 10.107.223.139 <none> 3306/TCP 7s通过 ClusterIP 测试 Service 是否可用
1
2
3
4# telnet 10.107.223.139 3306
Trying 10.107.223.139...
Connected to 10.107.223.139.
Escape character is '^]'.
创建 MySQL Exporter
登录 MySQL,创建监控所需要的用户以及授权
1
2
3SQL> GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysqld_exporter'@'localhost' identified by '12345678';
SQL> GRANT SELECT ON performance_schema.* TO 'mysqld_exporter'@'localhost';
SQL> flush privileges;配置 MySQL Exporter 采集 MySQL 监控指标,创建 mysql-exporter.yaml 文件,内容如下
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
41apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-exporter
namespace: monitoring
spec:
replicas: 1
selector:
matchLabels:
k8s-app: mysql-exporter
template:
metadata:
labels:
k8s-app: mysql-exporter
spec:
containers:
- name: mysql-exporter
image: registry.cn-beijing.aliyuncs.com/dotbalo/mysqld-exporter
env:
- name: DATA_SOURCE_NAME
value: "mysqld_exporter:123456@(mysql-prom.public:3306)/"
# value: "mysqld_exporter:123456@(192.168.55.62:3306)/" # 外部的 MySQL 服务器填写方式
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9104
apiVersion: v1
kind: Service
metadata:
name: mysql-exporter
namespace: monitoring
labels:
k8s-app: mysql-exporter
spec:
type: ClusterIP
selector:
k8s-app: mysql-exporter
ports:
- name: api
port: 9104
protocol: TCP注意 DATA_SOURCE_NAME 的配置,需要将
mysqld_exporter:123456@(mysql-prom.public:3306)/
改成自己的实际配置,格式如下USERNAME:PASSWORD@(MYSQL_HOST_ADDRESS:MYSQL_PORT)/
创建 Exporter
1
2
3# kubectl create -f mysql-exporter.yaml
deployment.apps/mysql-exporter created
service/mysql-exporter created
创建 ServiceMonitor
创建 mysql-servicemonitor.yaml 文件,内容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: mysql-exporter
namespace: monitoring
labels:
k8s-app: mysql-exporter
namespace: monitoring
spec:
jobLabel: k8s-app
endpoints:
- interval: 30s
port: api
scheme: http
selector:
matchLabels:
k8s-app: mysql-exporter
namespaceSelector:
matchNames:
- monitoring创建 ServiceMonitor 对象
1
2# kubectl create -f mysql-servicemonitor.yaml
servicemonitor.monitoring.coreos.com/mysql-exporter created此时在 Prometheus web 界面可以查看到该监控
打开 Grafana web,选择导入面板,面板地址为
https://grafana.com/grafana/dashboards/6239
。