今天分享一下二进制部署prometheus+grafana的全过程,先看一下效果图 准备一台虚拟机 将prometheus相关服务都安装在/data/目录下面,最好/data是一块单独的磁盘,易于扩容 下载地址: 版本信息: 解压安装包 创建prometheus用户 授予prometheus目录权限 给prometheus创建systemd服务 重载服务 启动prometheus并设置开机自启动 检查状态 访问prometheus 解压安装包 解压的内容复制到/data/alertmanager目录 更改alertmanager权限 给alertmanager创建systemd服务 启动alertmanager 查看alertmanager状态 将alertmanager加入prometheus。 增加触发器配置文件 检查配置 一定要检测通过再进行重启prometheus 重启prometheus 访问alertmanager: http://10.0.0.104:9093 解压安装包 解压的内容复制到/data/node_exporter目录 修改权限 给node_exporter创建systemd服务 启动node_exporter 查看状态 访问地址: http://10.0.0.104:9100/metrics 重载prometheus 登录prometheus查看-node_exporter是否起来了 解压安装包 将解压内容移动到/data/grafana 更改grafana目录权限 给grafana创建systemd服务 启动grafana 查看状态 访问grafana http://10.0.0.104:3000 默认用户名/密码:admin/admin 从Grafana官网导入符合要求的仪表盘 https://grafana.com/grafana/dashboards 在grafana右上角处点击Import dashboard, 导入id号或json文件,在grafana官网可以直接获取。 生产环境示例:Linux主机详情和Linux主机列表” Linux主机详情的dashboard ID:12633 Linux主机列表的dashboard ID:12632 本次分享结束,如果帮到你,可以点赞关注一下 往期精彩文章:下面开始进入部署过程:
1 安装前准备
1.1 主机环境
IP地址 操作系统 配置 10.0.0.104 Rocky Linux 9.4 4核4G,100G磁盘 1.2 规划安装目录
mkdir -p /data/{prometheus,grafana,alertmanager,node_exporter}
1.3 下载安装包
cd /data# 下载prometheuswget https://github.com/prometheus/prometheus/releases/download/v2.53.4/prometheus-2.53.4.linux-amd64.tar.gz# 下载grafanawget https://dl.grafana.com/enterprise/release/grafana-enterprise-11.5.3.linux-amd64.tar.gz# 下载altermanagerwget https://github.com/prometheus/alertmanager/releases/download/v0.28.1/alertmanager-0.28.1.linux-amd64.tar.gz# 下载node_exporterwget https://github.com/prometheus/node_exporter/releases/download/v1.9.0/node_exporter-1.9.0.linux-amd64.tar.gz
2 安装prometheus相关服务
2.1 安装Prometheus
[root@localhost ~]# cd /data[root@localhost data]# tar -xvf prometheus-2.53.4.linux-amd64.tar.gz [root@localhost data]# mv prometheus-2.53.4.linux-amd64/* /data/prometheus
useradd -M -s /sbin/nologin prometheus
chown -R prometheus.prometheus /data/prometheus
cat >> /etc/systemd/system/prometheus.service [Unit]Description=Prometheus ServerDocumentation=https://prometheus.io/docs/introduction/overviewAfter=network.target[Service]Type=simpleUser=prometheusGroup=prometheusRestart=on-failureExecStart=/data/prometheus/prometheus --config.file=/data/prometheus/prometheus.yml --storage.tsdb.path=/data/prometheus/data --storage.tsdb.retention.time=15d --web.enable-lifecycle[Install]WantedBy=multi-user.targetEOF
systemctl daemon-reload
systemctl enable --now prometheus.service
systemctl status prometheus.service
prometheus http://10.0.0.104:9090 无用户无密码 监控指标 http://10.0.0.104:9090metrics 2.2 安装alertmanager
tar -xvf alertmanager-0.28.1.linux-amd64.tar.gz
mv /data/alertmanager-0.28.1.linux-amd64/* /data/alertmanager
chown -R prometheus.prometheus /data/alertmanager
cat >> /etc/systemd/system/alertmanager.service [Unit]Desciption=Alert Managerwants=network-online.targetAfter=network-online.target[Service]Type=simpleUser=prometheusGroup=prometheusExecStart=/data/alertmanager/alertmanager --config.file=/data/alertmanager/alertmanager.yml --storage.path=/data/alertmanager/dataRestart=always[Install]WantedBy=multi-user.targetEOF
# 重载服务systemctl daemon-reload# 启动并设置开机自启动systemctl enable --now alertmanager.service
systemctl status alertmanger
vi /data/prometheus/prometheus.yml# Alertmanager configurationalerting: alertmanagers: - static_configs: - targets: # 根据实际填写alertmanager的IP地址 - 10.0.0.104:9093# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.rule_files:# 根据实际名修改文件名,可以有多个规则文件 - "/data/alertmanger/rule/alert.yml"
# 新建存放告警文件目录mkdir /data/alertmanager/rulechown -R prometheus.prometheus /data/alertmanager# 编辑配置文件vim /data/alertmanager/rule/alert.yml groups: - name: 主机状态监控 rules: - alert: 主机宕机 expr: up == 0 for: 1m labels: severity: critical annotations: summary: "{{ $labels.instance }} 主机宕机,请尽快处理" description: "{{ $labels.instance }} 已经宕机超过 1 分钟。请检查服务状态。"
cd /data/prometheus/[root@localhost prometheus]# ./promtool check config prometheus.ymlChecking prometheus.yml SUCCESS: 1 rule files found SUCCESS: prometheus.yml is valid prometheus config file syntaxChecking /data/alertmanager/rule/alert.yml SUCCESS: 1 rules found
systemctl restart prometheus
2.3 安装node_exporter
tar -xvf node_exporter-1.9.0.linux-amd64.tar.gz
mv node_exporter-1.9.0.linux-amd64/* /data/node_exporter
chown prometheus.prometheus -R /data/node_exporter
cat >> /etc/systemd/system/node_exporter.service [Unit]Description=node_exporterDocumentation=https://prometheus.io/After=network.target[Service]User=prometheusGroup=prometheusExecStart=/data/node_exporter/node_exporterRestart=on-failure[Install]WantedBy=multi-user.targetEOF
systemctl daemon-reloadsystemctl enable --now node_exporter.service
systemctl status node_exporter
2.4 配置prometheus
vi /data/prometheus/prometheus.yml# 在尾部添加一个job_name,可以添加多个targets- job_name: "node_exporter" static_configs: - targets: ["10.0.0.104:9100"] labels: instance: 10.0.0.104服务器
# 重启前检查配置是否正确./promtool check config prometheus.yml# 平滑加载curl -X POST http://10.0.0.104:9090/-/reload# 或者直接重启systemctl restart prometheus
2.5 安装Grafana
tar -xvf grafana-enterprise-11.5.3.linux-amd64.tar.gz
mv grafana-v11.5.3/* /data/grafana
chown -R prometheus.prometheus /data/grafana
cat >> /etc/systemd/system/grafana-server.service [Unit]Description=Grafana serverDocumetation=http://dosc.grafana.org[Service]Type=simpleUser=prometheusGroup=prometheusRestart=on-failureExecStart=/data/grafana/bin/grafana-server --config=/data/grafana/conf/defaults.ini --homepath=/data/grafana[Install]WantedBy=multi-user.targetEOF
# 重载系统服务systemctl daemon-reload# 启动并设置开机自启动systemctl enable --now grafana-server.service
systemctl status grafana-server.service
2.6 grafana对接prometheus
部署一套完整的prometheus+grafana监控系统详细过程
发布于 2025-04-05 12 次阅读