Prometheus监控Linux主机
目录
一、安装Prometheus
# 获取软件 $ wget https://github.com/prometheus/prometheus/releases/download/v2.20.1/prometheus-2.20.1.linux-amd64.tar.gz $ groupadd prometheus $ useradd -g prometheus -m -d /usr/local/prometheus -s /sbin/nologin prometheus $ tar zxf prometheus-2.24.1.linux-amd64.tar.gz $ mv prometheus-2.24.1.linux-amd64/* /usr/local/prometheus $ sed -i 's/localhost/10.4.7.100/g' /usr/local/prometheus/prometheus.yml # 创建systemd服务 cat <<EOF > /usr/lib/systemd/system/prometheus.service [Unit] Description=prometheus After=network.target [Service] Type=simple User=prometheus ExecStart=/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --storage.tsdb.path=/usr/local/prometheus/data/ --web.enable-lifecycle --storage.tsdb.retention.time=30d Restart=on-failure [Install] WantedBy=multi-user.target EOF # --config.file: 指定配置文件 # --web.enable-lifecycle: 支持通过http请求重载配置 # --storage.tsdb.path: 指定数据存储目录(默认当前目录的的data目录,若不存在则新建) # --storage.tsdb.retention.time: 指定数据保留时间(默认15d) # 创建数据目录 $ mkdir /usr/local/prometheus/data/ # 启动服务 $ systemctl daemon-reload $ systemctl start prometheus $ systemctl status prometheus && systemctl enable prometheus # 确认端口已经被监听 $ ss -lnput | grep 9090 tcp LISTEN 0 1024 :::9090 :::* users:(("prometheus",pid=8257,fd=7))
浏览器访问:
Prometheus管理接口
$ curl http://10.4.7.100:9090/-/healthy Prometheus is Healthy. # 健康检查 $ curl http://10.4.7.100:9090/-/ready Prometheus is Ready. # 检查Prometheus是否启动 $ curl -XPOST http://10.4.7.100:9090/-/reload # 通过web接口重载,启动时需增加选项 --web.enable-lifecycle $ curl -XPUT http://10.4.7.100:9090/-/quit $ curl -XPOST http://10.4.7.100:9090/-/quit # 停止Prometheus # 同样需启动时增加--web.enable-lifecycle选项
二、安装grafana
$ wget https://dl.grafana.com/oss/release/grafana-7.1.4-1.x86_64.rpm $ yum -y install grafana-7.1.4-1.x86_64.rpm $ systemctl daemon-reload $ systemctl enable grafana-server && systemctl start grafana-server $ ss -lnput | grep 3000 tcp LISTEN 0 1024 :::3000 :::* users:(("grafana-server",pid=8843,fd=8))
三、grafana添加Prometheus数据源
访问grafana的3000端口进行登录,默认用户名和密码为: admin/admin。第一次登陆修改更改密码!
四、安装node_exporter
node_exporter用于收集主机运行信息,比如CPU、内存、磁盘等资源使用情况。
各种监控exporter,可以去官网下载
$ wget https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz $ userdel node_exporter $ useradd -g node_exporter -m -d /usr/local/node_exporter -s /sbin/nologin node_exporter $ tar zxf node_exporter-1.0.1.linux-amd64.tar.gz $ mv node_exporter-1.0.1.linux-amd64/* /usr/local/node_exporter/ $ cat > /usr/lib/systemd/system/node_exporter.service <<EOF [Unit] Description=node_exporter After=network.target [Service] Type=simple User=node_exporter ExecStart=/usr/local/node_exporter/node_exporter Restart=on-failure [Install] WantedBy=multi-user.target EOF $ systemctl daemon-reload $ systemctl enable node_exporter && systemctl start node_exporter $ systemctl status node_exporter $ ss -lnput | grep 9100 tcp LISTEN 0 128 [::]:9100 [::]:* users:(("node_exporter",pid=7352,fd=3))
五、调整Prometheus获取主机及应用监控数据
$ $ vim /usr/local/prometheus/prometheus.yml - job_name: 'prometheus' # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ['10.4.7.100:9090'] # 增加以下配置,其中job_name为自定义 - job_name: 'agent1' static_configs: - targets: ['10.4.7.102:9100'] # 重载Prometheus配置文件 $ curl -XPOST http://10.4.7.100:9090/-/reload
确定Prometheus已采集到数据
六、验证
使用Grafana添加图形,便于展示!(监控Linux主机推荐使用9276模板)!
大部分指标都可以获取到,但是极少指标是获取不到的,需要调整模板对应的PromQL语句,目前正在学习阶段,关于Prometheus相关文章持续更新!
监控Linux主机强烈推荐使用1860模板!该模板获取主机的指标更加的详细!
七、Grafana插件扩展之饼图安装
有些时候导入的插件里面有饼图类型,但是Grafana饼图插件默认并没有安装,所以需要自行安装!(强烈推荐在线安装,避免版本的问题)!
官网:https://grafana.com/grafana/plugins/grafana-piechart-panel
$ grafana-cli plugins install grafana-piechart-panel $ systemctl restart grafana-server # 安装完成后重启生效
*************** 当你发现自己的才华撑不起野心时,就请安静下来学习吧!***************
分类:
监控
, Prometheus
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律