五、非云原生监控mysql-Exporter
将使用 MySQL 作为一个测试用例,演示如何使用 Exporter 监控非云原生应用。
一、部署测试用例
首先部署 MySQL 至 Kubernetes 集群中,直接配置 MySQL 的权限即可:
[root@k8s-master01 ~]#kubectl create deploy mysql --image=registry.cn-beijing.aliyuncs.com/dotbalo/mysql:5.7.23
deployment.apps/mysql created
# 注意,此时的mysql还是不正常的
[root@k8s-master01 prometheus]#kubectl get po
NAME READY STATUS RESTARTS AGE
mysql-79df95c755-c7n4x 0/1 Error 2 (27s ago) 109s
[root@k8s-master01 prometheus]#kubectl describe po mysql-79df95c755-c7n4x
......
Warning BackOff 3s (x3 over 21s) kubelet Back-off restarting failed container mysql in pod mysql-79df95c755-c7n4x_default(f76c1432-aa94-4c5d-864f-cefd5dd20a38)
# 设置密码,设置密码后正常
[root@k8s-master01 ~]# kubectl set env deploy/mysql MYSQL_ROOT_PASSWORD=mysql
deployment.apps/mysql env updated
# 查看是否正常
[root@k8s-master01 ~]#kubectl get po -l app=mysql
NAME READY STATUS RESTARTS AGE
mysql-7799b54cc6-xs9hx 1/1 Running 0 101s
创建 Service 暴露 MySQL:
[root@k8s-master01 ~]#kubectl expose deploy mysql --port 3306
service/mysql exposed
[root@k8s-master01 ~]#kubectl get svc -l app=mysql
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP 10.96.104.14 <none> 3306/TCP 27s
检查 Service 是否可用:
[root@k8s-master01 ~]#telnet 10.96.104.14 3306
Trying 10.96.104.14...
Connected to 10.96.104.14.
Escape character is '^]'.
J
5.7.23
=m#a:Pe]qU?M}I
#smysql_native_passwordConnection closed by foreign host.
登录 MySQL,创建 Exporter 所需的用户和权限(如果已经有需要监控的 MySQL,可以直接执行此步骤即可):
[root@k8s-master01 ~]#kubectl exec -it mysql-7799b54cc6-xs9hx -- bash
root@mysql-7799b54cc6-xs9hx:/# mysql -uroot -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 3
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