主机ip | 主机名 | 角色 |
---|---|---|
192.168.88.67 | server67 | Prometheus |
192.168.88.68 | server68 | agent |
192.168.88.69 | server69 | grafana |
解释:
-
Prometheus Server: 用于收集和存储时间序列数据。
-
Client Library: 客户端库,检测应用程序代码,当Prometheus抓取实例的HTTP端点时,客户端库会将所有跟踪的metrics指标的当前状态发送到prometheus server端。
-
Exporters: prometheus支持多种exporter,通过exporter可以采集metrics数据,然后发送到prometheus server端,所有向promtheus server提供监控数据的程序都可以被称为exporter
-
Grafana:监控仪表盘,可视化监控数据
1.修改/etc/hosts文件
在所有的主机执行 #cat >>/etc/hosts<< EOF 192.168.88.67 server67 192.168.88.68 server68 192.168.88.69 server69 EOF
2.同步时间
在所有的主机执行 #yum install ntpdate -y #systemctl enable ntpd #systemctl start ntpd #ntpdate cn.pool.ntp.org
3. server67安装prometheus
[root@server67 ~]# wget https://github.com/prometheus/prometheus/releases/download/v2.5.0/prometheus-2.5.0.linux-amd64.tar.gz [root@server67 ~]# ll 总用量 35304 -rw-------. 1 root root 1455 11月 22 04:51 anaconda-ks.cfg -rw-r--r-- 1 root root 36142430 12月 8 2021 prometheus-2.5.0.linux-amd64.tar.gz -rw-r--r-- 1 root root 981 11月 2 10:20 yum.sh [root@server67 ~]# tar xf prometheus-2.5.0.linux-amd64.tar.gz -C /usr/local [root@server67 ~]# mv /usr/local/prometheus-2.5.0.linux-amd64/ /usr/local/prometheus [root@server67 ~]# mkdir /etc/prometheus [root@server67 ~]# cp /usr/local/prometheus/prometheus.yml /etc/prometheus/ [root@server67 ~]# nohup /usr/local/prometheus/prometheus --config.file="/etc/prometheus/prometheus.yml" & [root@server67 ~]# netstat -pantul | grep 9090 tcp 0 0 127.0.0.1:41224 127.0.0.1:9090 ESTABLISHED 2410/prometheus tcp6 0 0 :::9090 :::* LISTEN 2410/prometheus tcp6 0 0 ::1:36244 ::1:9090 ESTABLISHED 2410/prometheus tcp6 0 0 ::1:9090 ::1:36244 ESTABLISHED 2410/prometheus tcp6 0 0 127.0.0.1:9090 127.0.0.1:41224 ESTABLISHED 2410/prometheus
4. server68安装node_exporter
[root@server68 ~]# wget https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz [root@server68 ~]# tar xf node_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local/ [root@server68 ~]# mv /usr/local/node_exporter-0.16.0.linux-amd64/ /usr/local/node_exporter [root@server68 ~]# nohup /usr/local/node_exporter/node_exporter & [root@server68 ~]# netstat -pantul | grep 9100 #监控程序已经在运行 tcp6 0 0 :::9100 :::* LISTEN 2347/node_exporter
5.server67修改配置文件,添加监控的机器
[root@server67 ~]# vim /etc/prometheus/prometheus.yml [root@server67 ~]# tail -10 /etc/prometheus/prometheus.yml # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['localhost:9090'] - job_name: 'agent' static_configs: - targets: ['192.168.88.68:9100'] [root@server67 ~]# ps -aux | grep prometheus root 2410 0.0 3.9 145464 39296 pts/0 Sl 11:04 0:00 /usr/local/prometheus/prometheus --config.file=/etc/prometheus/prometheus.yml root 2426 0.0 0.0 112664 972 pts/0 S+ 11:19 0:00 grep --color=auto prometheus [root@server67 ~]# kill -HUP 2410 ##动态更新进程 通过浏览器查验:http://192.168.88.67:9090/targets
6.在server68添加mysql监控
[root@server68 ~]# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.11.0/mysqld_exporter-0.11.0.linux-amd64.tar.gz [root@server68 ~]# tar xf mysqld_exporter-0.11.0.linux-amd64.tar.gz -C /usr/local/ [root@server68 ~]# mv /usr/local/mysqld_exporter-0.11.0.linux-amd64/ /usr/local/mysqld_exporter [root@server68 ~]# systemctl start mariadb [root@server68 ~]# systemctl enable mariadb Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service. [root@server68 ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] y ... Success! By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB! [root@server68 ~]# vim /usr/local/mysqld_exporter/.my.cnf [root@server68 ~]# cat /usr/local/mysqld_exporter/.my.cnf ##创建一个数据库配置文件,写上连接的用户名与密码 [client] user=root password=123456 [root@server68 ~]# nohup /usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf & [2] 5284 [root@server68 ~]# netstat -pantul | grep 9104 tcp6 0 0 :::9104 :::* LISTEN 5284/mysqld_exporte 修改server67的配置文件 [root@server67 ~]# tail -10 /etc/prometheus/prometheus.yml - job_name: 'agent' static_configs: - targets: ['192.168.88.68:9100'] - job_name: 'mysql' static_configs: - targets: ['192.168.88.68:9104'] [root@server67 ~]# ps -aux | grep prometheus root 2410 0.0 4.1 145464 41796 pts/0 Sl 11:04 0:00 /usr/local/prometheus/prometheus --config.file=/etc/prometheus/prometheus.yml root 5003 0.0 0.0 112664 976 pts/0 R+ 11:32 0:00 grep --color=auto prometheus [root@server67 ~]# kill -HUP 2410 通过浏览器查验:http://192.168.88.67:9090/targets
7.server69安装grafana
[root@server69 ~]# wget https://dl.grafana.com/oss/release/grafana-5.3.4-1.x86_64.rpm [root@server69 ~]# yum install urw-fonts [root@server69 ~]# rpm -ivh grafana-5.3.4-1.x86_64.rpm [root@server69 ~]# systemctl start grafana-server [root@server69 ~]# systemctl enable grafana-server [root@server69 ~]# systemctl status grafana-server ● grafana-server.service - Grafana instance Loaded: loaded (/usr/lib/systemd/system/grafana-server.service; enabled; vendor preset: disabled) Active: active (running) since 三 2022-11-23 11:41:59 CST; 7s ago Docs: http://docs.grafana.org Main PID: 5031 (grafana-server) CGroup: /system.slice/grafana-server.service └─5031 /usr/sbin/grafana-server --config=/etc/grafana/grafana.ini --pidfile=/var/run/grafana/grafana-server.pid cfg:defa... [root@server69 ~]# netstat -pantul | grep 3000 tcp6 0 0 :::3000 :::* LISTEN 5031/grafana-server
浏览器查看192.168.88.69:3000 默认账户密码为:admin admin
1.点击Add data source
2.点击import
3.导入MySQL_Overview.json
Prometheus和Grafana结合监控MySQL服务性能成功