本文承接Prometheus+Grafana+jvm监控Tomcat进行实验。 注意:本文不在阐述MySQL安装过程
一、实验所需包 1.mysqld_exporter-0.12.1.linux-amd64.tar.gz
二、配置步骤 实验机器共两台: 1.node-3 192.168.200.223 (prometheus服务器) 2.node-2 192.168.200.222 (mysql服务器)
#node-3 操作 #修改prometheus配置文件 [root@node-3 ~]# cd /srv/program/prometheus-2.20.1.linux-amd64/ [root@node-3 prometheus-2.20.1.linux-amd64]# vim prometheus.yml #此配置文件承接上文所讲,可只看新增 # my global config global: scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute. # scrape_timeout is set to the global default (10s). # Alertmanager configuration alerting: alertmanagers: - static_configs: - targets: # - alertmanager:9093 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files: # - "first_rules.yml" # - "second_rules.yml" # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs: # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config. - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['192.168.200.223:9090'] - job_name: 'linux' static_configs: - targets: ['192.168.200.223:9100'] - targets: ['192.168.200.221:9101'] #注意 node-2必须设置instance和node-2的mysql一致 - targets: ['192.168.200.222:9101'] labels: instance: db1 - job_name: 'nginx' static_configs: - targets: ['192.168.200.223:9913'] labels: instance: web1 - job_name: 'java' file_sd_configs: - files: ['/srv/program/prometheus-2.20.1.linux-amd64/tomcat.yml'] refresh_interval: 15s - job_name: 'mysql' static_configs: - targets: ['192.168.200.222:9104'] labels: instance: db1 :wq #保存并退出 #将prometheus整合到systemctl启动 [root@node-3 system]# vim /etc/systemd/system/prometheus.service [Unit] Description=prometheus After=network.target [Service] Type=simple User=prometheus ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus/data Restart=on-failure [Install] WantedBy=multi-user.target :wq #保存并退出 [root@node-3 system]# systemctl restart prometheus [root@node-3 system]# systemctl status prometheus ● prometheus.service - prometheus Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor preset: disabled) Active: active (running) since 三 2020-10-21 16:54:25 CST; 42s ago Main PID: 18871 (prometheus) CGroup: /system.slice/prometheus.service └─18871 /usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus/data 10月 21 16:54:25 node-3 prometheus[18871]: level=info ts=2020-10-21T08:54:25.217Z caller=head.go:713 component=tsdb msg="WAL segment loaded" segment=725 maxSegment=728 10月 21 16:54:25 node-3 prometheus[18871]: level=info ts=2020-10-21T08:54:25.248Z caller=head.go:713 component=tsdb msg="WAL segment loaded" segment=726 maxSegment=728 10月 21 16:54:25 node-3 prometheus[18871]: level=info ts=2020-10-21T08:54:25.305Z caller=head.go:713 component=tsdb msg="WAL segment loaded" segment=727 maxSegment=728 10月 21 16:54:25 node-3 prometheus[18871]: level=info ts=2020-10-21T08:54:25.305Z caller=head.go:713 component=tsdb msg="WAL segment loaded" segment=728 maxSegment=728 10月 21 16:54:25 node-3 prometheus[18871]: level=info ts=2020-10-21T08:54:25.305Z caller=head.go:716 component=tsdb msg="WAL replay completed" checkpoint_replay_duration=21.070714ms wal_replay_duration=16...on=189.558152ms 10月 21 16:54:25 node-3 prometheus[18871]: level=info ts=2020-10-21T08:54:25.313Z caller=main.go:700 fs_type=XFS_SUPER_MAGIC 10月 21 16:54:25 node-3 prometheus[18871]: level=info ts=2020-10-21T08:54:25.313Z caller=main.go:701 msg="TSDB started" 10月 21 16:54:25 node-3 prometheus[18871]: level=info ts=2020-10-21T08:54:25.314Z caller=main.go:805 msg="Loading configuration file" filename=/usr/local/prometheus/prometheus.yml 10月 21 16:54:25 node-3 prometheus[18871]: level=info ts=2020-10-21T08:54:25.317Z caller=main.go:833 msg="Completed loading of configuration file" filename=/usr/local/prometheus/prometheus.yml 10月 21 16:54:25 node-3 prometheus[18871]: level=info ts=2020-10-21T08:54:25.317Z caller=main.go:652 msg="Server is ready to receive web requests." Hint: Some lines were ellipsized, use -l to show in full. [root@node-3 system]# #node-2 操作 #安装mysqld_exporter [root@node-2 ~]# tar xvf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/ [root@node-2 ~]# cd /usr/local/mysqld_exporter-0.12.1.linux-amd64/ [root@node-2 mysqld_exporter-0.12.1.linux-amd64]# ls LICENSE mysqld_exporter nohup.out NOTICE [root@node-2 mysqld_exporter-0.12.1.linux-amd64]# vim .my.cnf [client] user=mysql_monitor password=mysql_monitor :wq #保存并退出 #进入数据库添加mysql_monitor用户 [root@node-2 mysqld_exporter-0.12.1.linux-amd64]# mysql -uroot -p123456 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 8698 Server version: 5.7.17-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> GRANT REPLICATION CLIENT,PROCESS ON *.* TO 'mysql_monitor'@'localhost' identified by 'mysql_monitor'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT SELECT ON *.* TO 'mysql_monitor'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye #运行mysqld_exporter [root@node-2 mysqld_exporter-0.12.1.linux-amd64]# nohup /usr/local/mysqld_exporter-0.10.0.linux-amd64/mysqld_exporter --config.my-cnf="/usr/local/mysqld_exporter-0.10.0.linux-amd64/.my.cnf" & [root@node-2 mysqld_exporter-0.12.1.linux-amd64]# cd [root@node-2 ~]# netstat -ntlupa |grep 9104 tcp6 0 0 :::9104 :::* LISTEN 11812/mysqld_export tcp6 0 0 192.168.200.222:9104 192.168.200.223:26172 ESTABLISHED 11812/mysqld_export [root@node-2 ~]#检验节点状态: 访问http://192.168.200.223:9090/targets mysql一栏state中显示up则节点部署成功! 最后导入grafana仪表盘 1.点击“进口” 2.输入 “7362” 3.点击“加载” 后如下图配置 4.配置完毕点击“进口”
四、效果展示