Prometheus部署与使用
1. 部署
Prometheus依赖于go语言环境,需要首先安装go环境
停止ipv6:
编辑文件/etc/sysctl.conf
vi /etc/sysctl.conf
添加下面的行:
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
1.1 安装部署go环境
yum -y install go
或者 去官网下载go的安装包
下载解压即可使用(需配置环境变量GOROOT,即go程序所在的文件夹)
建立工作目录。官方建议放在 /home/go 下,创建三个目录:bin(编译后可的执行文件的存放路径)、pkg(编译包时,生成的.a文件的存放路径)、src(源码路径,一般我们的工程就创建在src
下面)
mkdir -p /home/go/bin /home/go/pkg /home/go/src
vi /etc/profile
增加
export GOPATH=/home/go
export PATH=$PATH:$GOPATH/bin
source /etc/profile
查看go环境配置信息
go version
go env
1.2 部署Prometheus
下载地址: https://prometheus.io/download/
node_exporter #服务端、客户端都部署
prometheus #服务端部署
mkdir /prometheus
tar xf prometheus-2.29.1.linux-amd64.tar.gz -C /prometheus
tar xvf node_exporter-1.2.2.linux-amd64.tar.gz -C /prometheus/
mv prometheus-2.29.1.linux-amd64 prometheus
mv node_exporter-1.2.2.linux-amd64 node_exporter
ln -s /prometheus/prometheus/prometheus /usr/bin/prometheus
ln -s /prometheus/node_exporter/node_exporter /usr/bin/node_exporter
注意:如果需要使用其他用户启动如prometheus,可以创建用户
groupadd prometheus
useradd -g prometheus -s /sbin/nologin prometheus
创建运行目录
mkdir -p /prometheus/data
chown -R prometheus:prometheus /prometheus/data
设置开机启动
touch /usr/lib/systemd/system/prometheus.service
chown prometheus:prometheus /usr/lib/systemd/system/prometheus.service
vi /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
[Service]
Type=simple
User=prometheus
ExecStart=/prometheus/prometheus/prometheus --config.file=/prometheus/prometheus/prometheus.yml --storage.tsdb.path=/prometheus/data --web.listen-address=:9090
Restart=on-failure
[Install]
WantedBy=multi-user.target
启动 node_exporter
node_exporter &
或者设置开机自启
vi /usr/lib/systemd/system/node_exporter.service
输入下面内容,配置node_exporter.service
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
[Service]
Type=simple
User=prometheus
ExecStart=/usr/bin/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
修改配置文件并启动 prometheus,
注意 :
不要直接用 Tab 键,yaml、yml 文件不能有制表符,一个一个按空格来。
在prometheus.yml中新增如下配置监控mysql1主机
- job_name: "mysql1"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["192.168.32.101:9100"]
labels:
alias: linux-mysql1
/usr/bin/prometheus --config.file=/prometheus/prometheus/prometheus.yml &
或者:
systemctl restart prometheus
或者
curl -XPOST localhost:9090/-/reload
systemctl start node_exporter
1.3 安装grafana
下载网址: Download Grafana | Grafana Labs
wget https://dl.grafana.com/oss/release/grafana-8.1.1-1.x86_64.rpm
yum install grafana-8.1.1-1.x86_64.rpm
配置文件位置:/etc/grafana/grafana.ini
暂时保持默认
grafana配置 - shhnwangjian - 博客园 (cnblogs.com)
启动grafana:systemctl start grafana-server
下载模板:
git clone https://github.com/percona/grafana-dashboards.git
# 模板文件拷贝到对应文件夹
cp /backup/grafana-dashboards/dashboards/* /etc/grafana/provisioning/dashboards
vi /usr/share/grafana/conf/defaults.ini
[dashboards.json] # 自定义dashboards放入这个路径中,可以在页面上直接显示并使用它
enabled = true # 是否开启
path = /etc/grafana/provisioning/dashboards # 路径
配置piechart
git clone https://github.com/grafana/piechart-panel.git --branch release-1.3.8
mv piechart-panel /var/lib/grafana/plugins/grafana-piechart-panel
配置文件中增加:/etc/grafana/grafana.ini
[plugin.piechart]
path = /var/lib/grafana/plugins/grafana-piechart-panel
systemctl start grafana-server
2. 使用
2.1 监控linux主机
普罗米修斯 监控_普罗米修斯监控实例_weixin_39710966的博客-CSDN博客
2.2 监控mysqld
参考文章: Prometheus 进阶 - 奇妙的 Linux 世界 (hi-linux.com)
安装mysql_exporter,解压即可用
vim /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld_exporter
[Service]
Type=simple
Restart=on-failure
RrestartSec=5
ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf
[Install]
WantedBy=multi-user.target
增加配置文件:
vi /usr/local/mysqld_exporter/.my.cnf
[client]
user=mysqld_exporter
password=PrometheusPwd
创建用户:
GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysqld_exporter'@'%' identified by 'PrometheusPwd';
GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'mysqld_exporter'@'localhost' identified by 'PrometheusPwd';
GRANT SELECT ON performance_schema.* TO 'mysqld_exporter'@'localhost';
flush privileges;
开启mysqld_exporter systemctl start mysqld_exporter
3. 遇到的问题
3.1 Grafana之 “Request Origin is not authorized” 问题解决
在grafana配置文件中修改以下配置vim /etc/grafana/grafana.ini[server]
domain = 192.168.220.129
#这个设置是root_url的一部分,当你通过浏览器访问grafana时的公开的domian名称,默认是localhost
enforce_domain = true
#如果主机的header不匹配domian,则跳转到一个正确的domain上,默认是false
root_url = %(protocol)s://%(domain)s:%(http_port)s/
#这是一个web上访问grafana的全路径url,默认是%(protocol)s://%(domain)s:%(http_port)s/
[security]
admin_user = admin
admin_password = Sdwf12345+
[auth.basic] enabled = true
#当设置为true,则http api开启基本认证
实际操作中,只设置了domain和enforce_domain就没有提示了
3.2 Grafana提示缺少插件问题
缺少什么安装什么,安装完成之后重启服务
grafana-cli plugins install grafana-polystat-panel
systemctl restart grafana-server
3.3 Prometheus提示 Template variable service failed Datasource named Metrics was not found
就是数据源里边没有配置叫Metrics的数据源。
3.4 Prometheus提示时间差异
Warning: Error fetching server time: Detected 37.111000061035156 seconds time difference between your browser and the server. Prometheus relies on accurate time and time drift might cause unexpected query results.
可以使用ntpdate同步时间,如果没有ntp服务
ntpdate time3.aliyun.com
yum install ntp -y
评论区