1、首先部署MySQL至Kubernetes集群中,直接配置MySQL权限即可
[root@k8s-master01 prometheus]# kubectl create deployment mysql --image=registry.cn-beijing.aliyuncs.com/dotbalo/mysql:5.7.23 [root@k8s-master01 prometheus]# kubectl set env deployment/mysql MYSQL_ROOT_PASSWORD=mysql deployment.apps/mysql env updated
查看Pod是否正常
创建Service暴露MySQL
[root@k8s-master01 prometheus]# kubectl expose deploy mysql --port 3306 service/mysql exposed
[root@k8s-master01 prometheus]# kubectl get svc -l app=mysql
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP 192.168.133.253 <none> 3306/TCP 38s
检查Service是否可用(通过telnet 测试cluster IP的3306端口即可)
授权
对于我们非云原应用监控来说,要想通过Exporter采集到数据库服务的监控数据,还需要登陆MySQL,创建Exporter所需要的用户和权限(如果已经有需要监控的MySQL,可以直接执行此步骤即可)
[root@k8s-master01 prometheus]# kubectl exec -it mysql-69d6f69557-g7xd6 -- bash root@mysql-69d6f69557-g7xd6:/# ls bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@mysql-69d6f69557-g7xd6:/# mysql -u root -pmysql #验证登陆 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or /g. Your MySQL connection id is 5 Server version: 5.7.23 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '/h' for help. Type '/c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> create user 'exporter'@'%' identified by 'exporter' with max_user_connections 3; #创建exporter用户,最大连接数为3 Query OK, 0 rows affected (0.01 sec) mysql> grant process, replication client, select on *.* to 'exporter'@'%'; #授予该用户复制客户端、查看所有库、所有表权限 Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; #刷新权限列表,避免权限没有正常生效 Query OK, 0 rows affected (0.01 sec) mysql> quit;
2、配置MySQL Exporter采集MySQL监控数据
[root@k8s-master01 prometheus]# vim mysql-exporter.yaml
--- apiVersion: 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: "exporter:exporter@(mysql.default:3306)/" #value: "exporter:exporter@(mysql.default:3306)/" #如果对接的是外部mysql数据库,那么该地址可以直接写IP 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
创建exporter以及service的yaml文件
#kubectl apply -f mysql-exporter.yaml
此时上述yaml文件创建完成之后会暴露出一个metrics接口,我们可以通过Service地址,检查是否能正常获取Metrics数据:
[root@k8s-master01 prometheus]# curl 192.168.212.135:9104/metrics
3、 配置ServiceMonitor和Grafana
#vim mysql-am.yaml
apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: mysql-exporter namespace: monitoring labels: k8s-app: mysql-exporter namespace: monitoring spec: jobLabel: k8s-app endpoints: - port: api interval: 30s scheme: http selector: matchLabels: k8s-app: mysql-exporter namespaceSelector: matchNames: - monitoring [root@k8s-master01 prometheus]# kubectl apply -f mysql-am.yaml servicemonitor.monitoring.coreos.com/mysql-exporter created
接下来我们可以在Prometheus Web UI界面看到该监控
4、最后我们将导入Grafana dashboard模版,URL地址:地址:https://grafana.com/grafana/dashboards/6239
原创文章,作者:,如若转载,请注明出处:https://blog.ytso.com/278022.html