title: 1.4.二进制安装prometheus
order: 4
icon: lightbulb
主机名 | IP地址 | 配置 | 系统 | 说明 |
localhost | 192.168.11.61 | 2核4g | Ubuntu 20.04 | Prometheus版本:2.37.6 LTS,grafana版本:9.4.3,alertmanager版本:0.25,node-exporter版本:1.5.0 |
1、环境准备
克隆虚拟机
通过链接克隆一台新的虚拟机,并把ip更为192.168.11.61
二、从官网手动安装(和通过gitee下载安装二选一)
Prometheus 采集、存储数据
Grafana 用于图表展示
alertmanager 用于接收 Prometheus 发送的告警信息
node-exporter 用于收集操作系统和硬件信息的metrics
1、安装Prometheus
官网下载地址,获取最新下载URL.
#切换到root用户
sudo -i
#下载prometheus二进制压缩包
wget https://github.com/prometheus/prometheus/releases/download/v2.37.6/prometheus-2.37.6.linux-amd64.tar.gz
#解压
tar xf prometheus-2.37.6.linux-amd64.tar.gz
#查看解压后的文件名
ls -l
mkdir /opt/prometheus -p
#移动解压后的文件名到/opt/,并改名prometheus
mv prometheus-2.37.6.linux-amd64/ /opt/prometheus/prometheus
创建一个专门的prometheus
用户:
useradd -M -s /usr/sbin/nologin prometheus
更改prometheus
用户的文件夹权限:
chown prometheus:prometheus -R /opt/prometheus
创建 systemd 服务
cat > /etc/systemd/system/prometheus.service << "EOF"
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network-online.target
[Service]
Type=simple
User=prometheus
Group=prometheus
Restart=on-failure
ExecStart=/opt/prometheus/prometheus/prometheus \
--config.file=/opt/prometheus/prometheus/prometheus.yml \
--storage.tsdb.path=/opt/prometheus/prometheus/data \
--storage.tsdb.retention.time=60d \
--web.enable-lifecycle
[Install]
WantedBy=multi-user.target
EOF
配置参数解释:
通过/opt/prometheus/prometheus -h
查看帮助详情
--config.file=/opt/prometheus/prometheus/prometheus.yml #主配置文件
--storage.tsdb.path=/opt/prometheus/prometheus/data #数据库存储目录
--web.console.libraries=/opt/prometheus/prometheus/console_libraries #指定控制台库目录路径
--web.console.templates=/opt/prometheus/prometheus/consoles #指定控制台模版目录路径
--storage.tsdb.retention=60d #指明数据保留天数,默认15天
--web.enable-lifecycle #热加载
启动 Prometheus
systemctl daemon-reload
systemctl start prometheus.service
加入到开机自启动
systemctl enable prometheus.service
检查
systemctl status prometheus.service
查看 Prometheus 的日志以进行故障排除:
journalctl -u prometheus.service -f
访问地址
应用 | 访问地址 | 备注 |
prometheus | 无用户和密码 | |
监控指标 |
2、安装alertmanager
下载alertmanager
官网下载地址,获取最新下载URL.
- 注 在root用户下执行
下载alertmanager二进制压缩包
wget https://github.com/prometheus/alertmanager/releases/download/v0.25.0/alertmanager-0.25.0.linux-amd64.tar.gz
#解压
tar xf alertmanager-0.25.0.linux-amd64.tar.gz
#查看解压后的文件名
ls -l
#移动解压后的文件名到/opt/,并改名为alertmanager
mv alertmanager-0.25.0.linux-amd64 /opt/prometheus/alertmanager
更改alertmanager
文件夹权限:
chown prometheus:prometheus -R /opt/prometheus/alertmanager
创建 systemd 服务
cat >/etc/systemd/system/alertmanager.service << "EOF"
[Unit]
Description=Alert Manager
Wants=network-online.target
After=network-online.target
[Service]
Type=simple
User=prometheus
Group=prometheus
ExecStart=/opt/prometheus/alertmanager/alertmanager \
--config.file=/opt/prometheus/alertmanager/alertmanager.yml \
--storage.path=/opt/prometheus/alertmanager/data
Restart=always
[Install]
WantedBy=multi-user.target
EOF
启动 alertmanager
systemctl daemon-reload
systemctl start alertmanager.service
加入到开机自启动
systemctl enable alertmanager.service
检查
systemctl status alertmanager.service
修改prometheus配置
加入alertmanager
vim /opt/prometheus/prometheus/prometheus.yml
alerting:
alertmanagers:
- static_configs:
- targets:
#根据实际填写alertmanager的地址
- localhost:9093
rule_files:
#根据实际名修改文件名
- "alert.yml"
# 搜刮配置
scrape_configs:
- job_name: 'alertmanager'
scrape_interval: 15s
static_configs:
- targets: ['alertmanager:9093']
增加触发器配置文件
cat > /opt/prometheus/prometheus/alert.yml <<"EOF"
groups:
- name: Prometheus alert
rules:
# 对任何实例超过30秒无法联系的情况发出警报
- alert: 服务告警
expr: up == 0
for: 30s
labels:
severity: critical
annotations:
summary: "服务异常,实例:{{ $labels.instance }}"
description: "{{ $labels.job }} 服务已关闭"
EOF
检查配置
cd /opt/prometheus/prometheus/
./promtool check config prometheus.yml
重启prometheus或重新加载配置文件(二选一)
#重启
systemctl restart prometheus
或:
#重载,需要--web.enable-lifecycle配置
curl -X POST http://localhost:9090/-/reload
访问地址
应用 | 访问地址 | 备注 |
alertmanager | 无用户和密码 |
检查
3、安装grafana
官网下载地址:https://grafana.com/grafana/download
二进制安装
cd ~
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-9.4.3.linux-amd64.tar.gz
tar -zxvf grafana-enterprise-9.4.3.linux-amd64.tar.gz
ls -l
mv grafana-9.4.3/ /opt/prometheus/grafana
更改grafana
文件夹权限:
chown prometheus:prometheus -R /opt/prometheus
创建 systemd 服务
cat >/etc/systemd/system/grafana-server.service<<"EOF"
[Unit]
Description=Grafana server
Documentation=http://docs.grafana.org
[Service]
Type=simple
User=prometheus
Group=prometheus
Restart=on-failure
ExecStart=/opt/prometheus/grafana/bin/grafana-server \
--config=/opt/prometheus/grafana/conf/defaults.ini \
--homepath=/opt/prometheus/grafana
[Install]
WantedBy=multi-user.target
EOF
启动 alertmanager
systemctl daemon-reload
systemctl start grafana-server.service
加入到开机自启动
systemctl enable grafana-server.service
检查
systemctl status grafana-server.service
检查日志
journalctl -u grafana-server.service -f
web访问地址
应用 | 访问地址 | 备注 |
grafana | 默认:admin/admin |
4、安装node_exporter
官网下载地址:https://prometheus.io/download/
wget https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz
tar xvf node_exporter-1.5.0.linux-amd64.tar.gz
ls -l
mv node_exporter-1.5.0.linux-amd64 /opt/prometheus/node_exporter
更改node_exporter
文件夹权限:
chown prometheus:prometheus -R /opt/prometheus/node_exporter
创建 systemd 服务
cat > /etc/systemd/system/node_exporter.service <<"EOF"
[Unit]
Description=node_exporter
Documentation=https://prometheus.io/
After=network.target
[Service]
User=prometheus
Group=prometheus
ExecStart=/opt/prometheus/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
启动 node_exporter
systemctl daemon-reload
systemctl start node_exporter.service
加入到开机自启动
systemctl enable node_exporter.service
检查
systemctl status node_exporter.service
检查日志
journalctl -u node_exporter.service -f
web访问地址
应用 | 访问地址 | 备注 |
node-exporter | 无用户和密码 |
修改prometheus配置
prometheus服务器操作
cat >> /opt/prometheus/prometheus/prometheus.yml <<"EOF"
# 再scrape_configs这行下面添加如下配置:
#node-exporter配置
- job_name: 'node-exporter'
scrape_interval: 15s
static_configs:
- targets: ['localhost:9100']
labels:
instance: Prometheus服务器
EOF
重载prometheus
curl -X POST http://localhost:9090/-/reload
Prometheus web上检查
三、通过gitee下载安装(和从官网手动安装二选一)
官网提供的下载地址都放在github上,国内下载很慢。我把他们都下载好了放在了我的gitee代码仓库
通过git clone下载
cd /opt
git clone https://gitee.com/linge365/prometheus.git
cd prometheus
#移动systemd 服务到/etc/systemd/system/目录下
mv *.service /etc/systemd/system/
#检查
ls -l /etc/systemd/system/
创建一个专门的prometheus
用户:
useradd -M -s /usr/sbin/nologin prometheus
更改prometheus
用户的文件夹权限:
chown prometheus:prometheus -R /opt/prometheus
启动
systemctl daemon-reload
systemctl start prometheus.service
systemctl start grafana-server.service
systemctl start node_exporter.service
systemctl start alertmanager.service
设置开机自启
systemctl enable prometheus.service
systemctl enable grafana-server.service
systemctl enable node_exporter.service
systemctl enable alertmanager.service
检查
systemctl status node_exporter.service
systemctl status prometheus.service
systemctl status grafana-server.service
systemctl status alertmanager.service
四、使用grafana展示prometheus的图形
1、登陆grafana
用户名:admin
密码: admin
第一次登陆,需要修改密码,填入新的密码后,再submit(提交)如下图:
2、创建 Prometheus 数据源
在 Grafana 中创建 Prometheus 数据源:
单击边栏中的“齿轮”,打开“配置”菜单。
单击“数据源”。
单击“添加数据源”。
选择“Prometheus”作为类型。
设置适当的 Prometheus 服务器 URL(例如,
http://localhost:9090
)单击“保存并测试”以保存新的数据源。
如下图:
点击Prometheus
url填入:http://localhost:9090
注:localhost表示本机
3、创建仪表盘
手动创建仪表盘
略
从 Grafana.com 导入仪表板
https://grafana.com/grafana/dashboards/
打开grafana的dashboards官网,在搜索栏输入node exporter回车,点击下载量大的dashboards如下图:
在拷贝id
打开grafana web控制台–点击dashbord图标–在点import导入–粘贴之前复制的id–在点load加载
name填写“服务器监控”(根据实际修改),选择“prometheus”–在点import导入
导入dashbord完成后,如下图:
评论区