[email protected] ~ / yum y install mariadb server [email protected] ~ / systemctl enable mariadb now [email protected] ~ / mys...">

当前位置:网站首页>prometheus学习4Grafana监控mysql&blackbox了解

prometheus学习4Grafana监控mysql&blackbox了解

2022-08-11 05:58:00 daydayup9527

Grafana部署及基本使用

MySQL主从复制

一、MySQL的主从安装配置

[[email protected] ~]# yum -y install mariadb-server 
[[email protected] ~]# systemctl enable mariadb --now
[[email protected] ~]# mysql_secure_installation # 设置密码
enter
Set root password? [Y/n] y
[[email protected] ~]# mysql -uroot -p123456
  • 修改主库配置
[[email protected] ~]# cat /etc/my.cnf|grep -v '#'|grep -v '^$'
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
binlog_format          = row
log_bin                = /data/logbin/mysql-bin
server-id              = 11
log-basename           = master
[mysqld_safe]
skip-name-resolve
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
[[email protected] ~]# systemctl restart mariadb.service
  • 从库配置
[[email protected] ~]# cat /etc/my.cnf|grep -v '#'|grep -v '^$'
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
server_id          = 12
read_only          = ON
relay_log          = relay-log
relay_log_index    = relay-log.index
datadir            = /var/lib/mysql
socket             = /var/lib/mysql/mysql.sock
skip-name-resolve
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
[[email protected] ~]# systemctl restart mariadb.service
  • 主库新建复制用户
-- 登录mysql
[root@master ~]# mysql -uroot -p123456
create user 'mysqld_exporter'@'%' identified by 'ezdevops';
grant all privileges on *.* to 'mysqld_exporter'@'%';
#grant process, replication client, select on *.* to 'mysqld_exporter'@'%';
flush privileges;
select Host,User from mysql.user;
+-----------+-----------------+
| %         | mysqld_exporter |
show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |      712 |              |                  |
+-------------------+----------+--------------+------------------+

#slave exporter 账户创建

create user 'mysqld_exporter'@'%' identified by 'ezdevops';
grant all privileges on *.* to 'mysqld_exporter'@'%';
flush privileges;
select Host,User from mysql.user;
  • 从库复制主库
-- 指定主库复制
CHANGE MASTER TO
MASTER_HOST='192.168.1.11',
MASTER_USER='mysqld_exporter',
MASTER_PASSWORD='ezdevops',
MASTER_PORT=3306,
MASTER_LOG_FILE='master-bin.000001',
MASTER_LOG_POS=712,
MASTER_CONNECT_RETRY=10;

-- 启动slave
START SLAVE;
-- 查看slave信息
SHOW SLAVE STATUS\G
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
-- 查看进程信息
SHOW PROCESSLIST;
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+----------+
| Id | User        | Host      | db   | Command | Time | State                                                                       | Info             | Progress |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+----------+
| 11 | root        | localhost | NULL | Query   |    0 | NULL                                                                        | SHOW PROCESSLIST |    0.000 |
| 12 | system user |           | NULL | Connect |   85 | Waiting for master to send event                                            | NULL             |    0.000 |
| 13 | system user |           | NULL | Connect |   85 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL             |    0.000 |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+----------+

验证

[[email protected] ~]# mysql -uroot -p123456
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
[[email protected] ~]# mysql -uroot -p123456
MariaDB [(none)]> create database testdb;
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| testdb             |
+--------------------+
[[email protected] ~]# mysql -uroot -p123456
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| testdb             |
+--------------------+

二、配置mysql_exporter

exporter官网

监控master

tar -xvf mysqld_exporter-0.13.0.linux-amd64.tar.gz
rm -rf /usr/local/mysqld_exporter*
mv ./mysqld_exporter-0.13.0.linux-amd64/ /usr/local/
ln -sv /usr/local/mysqld_exporter-0.13.0.linux-amd64 /usr/local/mysqld_exporter
mkdir /etc/mysqld_exporter &>/dev/null
[[email protected] ~]# cat /etc/my_master.cnf
[client]
host=192.168.1.11 
port=3306 
user=mysqld_exporter 
password=ezdevops
cat <<EOF >/usr/lib/systemd/system/mysqld_exporter_master.service
[Unit]
Description=Mysqld Exporter Master
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter \
--config.my-cnf=/etc/my_master.cnf \
--collect.info_schema.processlist \
--collect.info_schema.innodb_tablespaces \
--collect.info_schema.innodb_metrics  \
--collect.perf_schema.tableiowaits \
--collect.perf_schema.indexiowaits \
--collect.perf_schema.tablelocks \
--collect.engine_innodb_status \
--collect.perf_schema.file_events \
--collect.binlog_size \
--collect.info_schema.clientstats \
--collect.perf_schema.eventswaits \
--web.listen-address=0.0.0.0:9105
Restart=always

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl stop mysqld_exporter_master.service
systemctl start mysqld_exporter_master.service
systemctl restart mysqld_exporter_master.service

http://192.168.1.12:9105/metrics   #看不到上千条数据的话,确认下my_master.cnf账户密码是否正确

监控slave

tar -xvf mysqld_exporter-0.13.0.linux-amd64.tar.gz
rm -rf /usr/local/mysqld_exporter*
mv ./mysqld_exporter-0.13.0.linux-amd64/ /usr/local/
ln -sv /usr/local/mysqld_exporter-0.13.0.linux-amd64 /usr/local/mysqld_exporter
mkdir /etc/mysqld_exporter &>/dev/null
[[email protected] ~]# cat /etc/my_slave.cnf
[client]
host=192.168.1.12 
port=3306
user=mysqld_exporter
password=ezdevops
cat <<EOF >/usr/lib/systemd/system/mysqld_exporter_slave.service
[Unit]
Description=Mysqld Exporter Master
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter \
--config.my-cnf=/etc/my_slave.cnf \
--collect.info_schema.processlist \
--collect.info_schema.innodb_tablespaces \
--collect.info_schema.innodb_metrics  \
--collect.perf_schema.tableiowaits \
--collect.perf_schema.indexiowaits \
--collect.perf_schema.tablelocks \
--collect.engine_innodb_status \
--collect.perf_schema.file_events \
--collect.binlog_size \
--collect.info_schema.clientstats \
--collect.perf_schema.eventswaits \
--web.listen-address=0.0.0.0:9104
Restart=always

[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl start mysqld_exporter_slave.service

http://192.168.1.11:9104/metrics
[[email protected] nodes]# cat /usr/local/prometheus/prometheus.yml
...
  - job_name:  'mysql replication'
    file_sd_configs:
      - files:
        - /etc/prometheus/mysql/*.yaml
        refresh_interval: 5s
...
[[email protected] nodes]# cat /etc/prometheus/mysql/nodes.yaml
- targets:
  - 192.168.1.11:9105
  labels:
    role: master
- targets:
  - 192.168.1.12:9104
  labels:
    role: slave
[[email protected] ~]# systemctl restart prometheus.service
访问http://192.168.1.10:9090/targets

http://192.168.1.11:9105/metrics  UP instance="192.168.1.11:9105"job="scan yaml"role="master" 10.556s ago	8.546ms	
http://192.168.1.12:9104/metrics  UP instance="192.168.1.12:9104"job="scan yaml"role="slave"  13.91s ago	9.004ms	

在这里插入图片描述
配置主从监控面板

在这里插入图片描述
在这里插入图片描述
如果数据没有刷新出来涉及到相关标签变量,有些数据未显示。io sql状态查询需要调整

mysql_slave_status_slave_io_running{channel_name="",connection_name="",master_host="192.168.1.11",master_uuid=""} 1
mysql_slave_status_slave_sql_running{channel_name="",connection_name="",master_host="192.168.1.11",master_uuid=""} 1

三、MySQL自定义查询

下载安装sql_exporter 官网

1、配置启动sql_exporter

[email protected] ~]# tar xf sql_exporter-0.5.linux-amd64.tar.gz
[[email protected] ~]# ls sql_exporter-0.5.linux-amd64
LICENSE  mssql_standard.collector.yml  README.md  sql_exporter  sql_exporter.yml
  • sql_exporter.yml配置文件
global:
  scrape_timeout_offset: 500ms
  min_interval: 0s
  max_connections: 3
  max_idle_connections: 3
target:
  data_source_name: 'mysql://mysqld_exporter:[email protected](192.168.1.11:3306)/ezdevops'
  # mysql://user:[email protected](host:port)/dbname。支持多种数据库,查询配置
  collectors: [collector_user]
collector_files:
  - "*.collector.yml"
  • 定义查询语句,mysql.collector.yml,查询表的记录数量
collector_name: collector_user
metrics:
  - metric_name: mysql_table_count
    type: counter
    help: 'Count Table Authors.'
    values: [count(*)]
    query: | select count(*) from authors;
  • 启动sql_exporter
./sql_exporter --config.file=./sql_exporter.yml  #提示默认启动的9399端口

2、测试数据

CREATE TABLE authors (id INT, name VARCHAR(20), email VARCHAR(20));
INSERT INTO authors (id,name,email) VALUES(1,"user1","book1");
INSERT INTO authors (id,name,email) VALUES(2,"user2","book2");
INSERT INTO authors (id,name,email) VALUES(3,"user3","book3");

四、黑盒监控(black_exporter)

balckexporter提供基于HTTP,ICMP,TCP端口等监控接口,可接入Prometheus后使用。

  • 监控面板预览
    在这里插入图片描述

下载安装blackexporter

[[email protected] ~]# tar xf blackbox_exporter-0.19.0.linux-amd64.tar.gz
[[email protected] ~]# ls blackbox_exporter-0.19.0.linux-amd64
blackbox_exporter  blackbox.yml  LICENSE  NOTICE
[[email protected] ~]# rm -rf /usr/local/blackbox_exporter*
[[email protected] ~]# mv blackbox_exporter-0.19.0.linux-amd64/ /usr/local/
[[email protected] ~]# ln -sv /usr/local/blackbox_exporter-0.19.0.linux-amd64/ /usr/local/blackbox_exporter
[[email protected] ~]# mkdir /etc/blackbox &>/dev/null
  • blackbox.yml配置文件
modules:
  http_2xx:
    prober: http
  http_post_2xx:
    prober: http
    http:
      method: POST
  tcp_connect:
    prober: tcp
  pop3s_banner:
    prober: tcp
    tcp:
      query_response:
      - expect: "^+OK"
      tls: true
      tls_config:
        insecure_skip_verify: false
  ssh_banner:
    prober: tcp
    tcp:
      query_response:
      - expect: "^SSH-2.0-"
      - send: "SSH-2.0-blackbox-ssh-check"
  irc_banner:
    prober: tcp
    tcp:
      query_response:
      - send: "NICK prober"
      - send: "USER prober prober prober :prober"
      - expect: "PING :([^ ]+)"
        send: "PONG ${1}"
      - expect: "^:[^ ]+ 001"
  icmp:
    prober: icmp
  • 启动blackexporter
./blackbox_exporter  --config.file=./blackbox.yml --web.listen-address=:9115
[[email protected] ~]# cat <<EOF >/usr/lib/systemd/system/blackbox_exporter.service
[Unit]
Description=Blackbox Exporter Service
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/blackbox_exporter/blackbox_exporter \
  --config.file=/etc/blackbox/blackbox.yml \
  --web.listen-address=:9115

Restart=always

[Install]
WantedBy=multi-user.target
EOF

[[email protected] ~]# systemctl daemon-reload
[[email protected] ~]# systemctl start blackbox_exporter

在这里插入图片描述
在这里插入图片描述

Prometheus接入blackexporter

  • prometheus.yml配置文件,检测HTTP
[[email protected] ~]# cat /usr/local/prometheus/prometheus.yml
...
  - job_name: "http_check"
    metrics_path: /probe
    params:
      module: [http_2xx]  #使用http模块
    file_sd_configs:
    - refresh_interval: 1m
      files:
      - "/etc/prometheus/blackbox/black-http.yml"
    relabel_configs:
    - source_labels: [__address__]
      target_label: __param_target
    - source_labels: [__param_target]
      target_label: instance
    - target_label: __address__
      replacement: 192.168.1.12:9115
[[email protected] ~]# mkdir -p  /etc/prometheus/blackbox/
[[email protected] ~]# vim /etc/prometheus/blackbox/black-http.yml
- targets:
  - https://www.baidu.com
[[email protected] ~]# systemctl restart prometheus.service

在这里插入图片描述
在这里插入图片描述

  • prometheus.yml配置文件,检测ICMP
  - job_name: "icmp_ping"
    metrics_path: /probe
    params:
      module: [icmp]  # 使用icmp模块
    file_sd_configs:
    - refresh_interval: 10s
      files:
      - "/etc/prometheus/node/black-icmp.yml"
    relabel_configs:
    - source_labels: [__address__]
      regex: (.*)(:80)?
      target_label: __param_target
      replacement: ${
    1}
    - source_labels: [__param_target]
      target_label: instance
    - source_labels: [__param_target]
      regex: (.*)
      target_label: ping
      replacement: ${
    1}
    - source_labels: []
      regex: .*
      target_label: __address__
      replacement: 192.168.0.100:9115
  • prometheus.yml配置文件,检测TCP端口

  - job_name: 'prometheus_port_status'
    metrics_path: /probe
    params:
      module: [tcp_connect]
    static_configs:
      - targets: ['192.168.1.11:3306']
        labels:
          instance: 'port_status'
          group: 'tcp'
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 192.168.0.100:9115

检测目标配置

  • black-http.yml
- targets:
  - https://www.sina.cn
  - https://www.baidu.com
  • black-icmp.yml
- targets: ['192.168.0.1','192.168.0.100']
  labels:
    group: '内网节点'
- targets: ['114.114.114.114','8.8.8.8']
  labels:
    group: '域名服务商'

配置blackexporter面板

ID-7587 import导入这个id即可

使用consul方式注册

  • 注册脚本,添加一个自定义的标签,最后将他转为param_target
curl -X PUT -d '{
    
  "id": "http_2xx",
  "name": "http_200_check",
  "Meta": {
    
		"target": "https://www.sina.cn"
  }
}' http://192.168.1.11:8500/v1/agent/service/register
  • prometheus配置
  - job_name: "http_200_check"
    metrics_path: /probe
    params:
      module: [http_2xx]  #使用http模块
    consul_sd_configs:
       - server: 192.168.1.11:8500
    relabel_configs:
    - source_labels: [__meta_consul_service_metadata_target]
      regex: ""
      action: drop
    - source_labels: [__meta_consul_service_metadata_target]
      target_label: __param_target
    - source_labels: [__meta_consul_service_metadata_target]
      target_label: instance
    - target_label: __address__
      replacement: 192.168.1.12:9115

http://192.168.1.11:8500/v1/agent/service/register


- prometheus配置

```yml
  - job_name: "http_200_check"
    metrics_path: /probe
    params:
      module: [http_2xx]  #使用http模块
    consul_sd_configs:
       - server: 192.168.1.11:8500
    relabel_configs:
    - source_labels: [__meta_consul_service_metadata_target]
      regex: ""
      action: drop
    - source_labels: [__meta_consul_service_metadata_target]
      target_label: __param_target
    - source_labels: [__meta_consul_service_metadata_target]
      target_label: instance
    - target_label: __address__
      replacement: 192.168.1.12:9115
原网站

版权声明
本文为[daydayup9527]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_60092693/article/details/126235366