prometheus包
wget https://github.com/prometheus/prometheus/releases/download/v2.25.0/prometheus-2.25.0.linux-amd64.tar.gz
Grafana包
下载地址:
https://grafana.com/grafana/download
https://mirrors.bfsu.edu.cn/grafana/yum/rpm/
mysqld_exporter包
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
禁用防火墙
systemctl stop firewalld
开放端口
firewall-cmd --zone=public --add-port=80/tcp
(永久生效再加上 --permanent)
说明:
–zone 作用域
–add-port=8080/tcp 添加端口,格式为:端口/通讯协议
–permanent #永久生效,没有此参数重启后失效
部署 prometheus
tar xf prometheus-2.35.0.linux-amd64.tar.gz -C /usr/local
cd /usr/local``
mv prometheus-2.35.0.linux-amd64.tar.gz prometheus
配置yml文件(拉取数据地址)
cat /usr/local/prometheus/prometheus.yml | grep -v "^#"
global: #用于prometheus的全局配置,比如采集间隔,抓取超时时间等
scrape_interval: 15s #采集目标主机监控数据的时间间隔,默认为1m
evaluation_interval: 15s #触发告警生成alert的时间间隔,默认是1m
# scrape_timeout is set to the global default (10s).
scrape_timeout: 10s #数据采集超时时间,默认10s
alerting: #用于alertmanager实例的配置,支持静态配置和动态服务发现的机制
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
rule_files: #用于加载告警规则相关的文件路径的配置,可以使用文件名通配机制
# - "first_rules.yml"
# - "second_rules.yml"
scrape_configs: #用于采集时序数据源的配置
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus" #每个被监控实例的集合用job_name命名,支持静态配置(static_configs)和动态服务发现的机制(*_sd_configs)
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs: #静态目标配置,固定从某个target拉取数据
- targets: ["localhost:9090"]
配置系统启动文件,启动 Prometheust
cat > /usr/lib/systemd/system/prometheus.service <<'EOF'
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/usr/local/prometheus/data/ \
--storage.tsdb.retention=15d \
--web.enable-lifecycle
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
[Unit] #服务单元
Description=Prometheus Server #描述
Documentation=https://prometheus.io
After=network.target #依赖关系
[Service]
Type=simple
ExecStart=/usr/local/prometheus/prometheus
–config.file=/usr/local/prometheus/prometheus.yml \ #配置文件
–storage.tsdb.path=/usr/local/prometheus/data/ \ #数据目录
–storage.tsdb.retention=15d \ #保存时间
–web.enable-lifecycle #开启热加载
ExecReload=/bin/kill -HUP $MAINPID #重载
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动
systemctl start prometheus
systemctl enable prometheus
netstat -natp | grep :9090
浏览器访问:http://192.168.141.101:9090 ,访问到 Prometheus 的 Web UI 界面
点击页面的 Status -> Targets,如看到 Target 状态都为 UP,说明 Prometheus 能正常采集到数据
http://192.168.141.101:9090/metrics ,可以看到 Prometheus 采集到自己的指标数据
部署 grafana
#使用yum解决依赖关系 我这边直接上传软件包到opt
yum install -y grafana-7.4.0-1.x86_64.rpm
systemctl start grafana-server
systemctl enable grafana-server
netstat -natp | grep :3000
浏览器访问:http://192.168.109.138:3000 ,默认账号和密码为 admin/admin
部署 mysql_exporter
数据库授权
GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost' IDENTIFIED BY 'expoter12Ssdc3' WITH MAX_USER_CONNECTIONS 3;
mkdir -p /data
将mysqld_exporter安装包上传至/data下
cd /data
tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz
mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter
cd /data/mysqld_exporter
touch my.cnf
echo '
[client]
host = 127.0.0.1
user = exporter
password = expoter12Ssdc3
' >>/data/mysqld_exporter/my.cnf
配置完,测试一下使用该配置文件中的账号密码socket是否可登陆
mysql -uexporter -p'expoter12Ssdc3'
echo '
[Unit]
Description=mysqld_exporter
[Service]
ExecStart=/data/mysqld_exporter/mysqld_exporter --config.my-cnf /data/mysqld_exporter/my.cnf --web.listen-address=0.0.0.0:9104 \
--collect.slave_status \
--collect.binlog_size \
--collect.info_schema.processlist \
--collect.info_schema.innodb_metrics \
--collect.engine_innodb_status \
--collect.perf_schema.file_events \
--collect.perf_schema.replication_group_member_stats
Restart=on-failure
[Install]
WantedBy=multi-user.target
' >>/usr/lib/systemd/system/mysqld-exporter.service
管理方式:
systemctl daemon-reload
systemctl enable mysqld-exporter
systemctl restart mysqld-exporter
systemctl status mysqld-exporter
systemctl stop mysqld-exporter