CentOS7搭建Prometheus 监控Linux主机

2025-05-27 0 25

简介

prometheus可以拆分成多个节点进行指标收集。

安装环境:CentOS7

CentOS7搭建Prometheus 监控Linux主机

安装prometheus

wget-chttps://github.com/prometheus/prometheus/releases/download/v2.23.0/prometheus-2.23.0.linux-amd64.tar.gz

tarzxvfprometheus-2.23.0.linux-amd64.tar.gz-C/opt/

cd/opt/

ln-sprometheus-2.23.0.linux-amd64prometheus

cat>/etc/systemd/system/prometheus.service<<EOF

[Unit]

Description=prometheus

After=network.target

[Service]

Type=simple

WorkingDirectory=/opt/prometheus

ExecStart=/opt/prometheus/prometheus–config.file="/opt/prometheus/prometheus.yml"

LimitNOFILE=65536

PrivateTmp=true

RestartSec=2

StartLimitInterval=0

Restart=always

[Install]

WantedBy=multi-user.target

EOF

systemctldaemon-reload

systemctlenableprometheus

systemctlstartprometheus

CentOS7搭建Prometheus 监控Linux主机

配置Prometheus

这里配置的是监听/opt/prometheus/servers/目录下的json文件

cat>/opt/prometheus/prometheus.yml<<EOF

#myglobalconfig

global:

scrape_interval:15s#Setthescrapeintervaltoevery15seconds.Defaultisevery1minute.

evaluation_interval:15s#Evaluaterulesevery15seconds.Thedefaultisevery1minute.

#scrape_timeoutissettotheglobaldefault(10s).

#Alertmanagerconfiguration

alerting:

alertmanagers:

-static_configs:

-targets:

#-alertmanager:9093

#Loadrulesonceandperiodicallyevaluatethemaccordingtotheglobal'evaluation_interval'.

rule_files:

#-"first_rules.yml"

#-"second_rules.yml"

#Ascrapeconfigurationcontainingexactlyoneendpointtoscrape:

#Hereit'sPrometheusitself.

scrape_configs:

#Thejobnameisaddedasalabel`job=<job_name>`toanytimeseriesscrapedfromthisconfig.

-job_name:'prometheus'

#metrics_pathdefaultsto'/metrics'

#schemedefaultsto'http'.

static_configs:

-targets:['localhost:9090']

-job_name:'servers'

file_sd_configs:

-refresh_interval:61s

files:

-/opt/prometheus/servers/*.json

EOF

systemctlrestartprometheus

json格式

CentOS7搭建Prometheus 监控Linux主机

每个json文件需要是一个数组对象,如果不需要自定义标签,可以直接写到targets里面去也可以,可以有多个文件

[

{

"targets":[

"192.168.1.164:9100"

],

"labels":{

"instance":"192.168.1.164",

"job":"node_exporter"

}

},

{

"targets":[

"192.168.1.167:9100"

],

"labels":{

"instance":"192.168.1.167",

"job":"node_exporter"

}

}

]

安装node_exporter

安装到/opt/node_exporter路径下,保持默认的端口

https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz

tarzxvfnode_exporter-1.0.1.linux-amd64.tar.gz-C/opt/

cd/opt/

ln-snode_exporter-1.0.1.linux-amd64node_exporter

cat>/etc/systemd/system/node_exporter.service<<EOF

[Unit]

Description=node_exporter

After=network.target

[Service]

Type=simple

WorkingDirectory=/opt/node_exporter

ExecStart=/opt/node_exporter/node_exporter

LimitNOFILE=65536

PrivateTmp=true

RestartSec=2

StartLimitInterval=0

Restart=always

[Install]

WantedBy=multi-user.target

EOF

systemctldaemon-reload

systemctlenablenode_exporter

systemctlstartnode_exporter

图形展示

直接安装grafana进行展示

yum-yinstallhttps://dl.grafana.com/oss/release/grafana-7.3.6-1.x86_64.rpm

systemctlenablegrafana-server

systemctlstartgrafana-server

启动之后,grafana默认监听的是3000端口,直接使用浏览器进行访问就可以了,默认用户名密码是admin/admin,第一次登陆之后会提示修改。

CentOS7搭建Prometheus 监控Linux主机

配置数据源:鼠标左边的菜单 Configuration -> Data Source -> Add data source -> 选择prometheus -> url那栏填入prometheus的地址就可以了 -> 最后 Save & test 就可以了。

grafana.com/grafana/dashboards 官网已经有人做好的模板,我们直接import进来就可以了。

导入面板:鼠标左边的菜单 Dashboards -> Import -> 填入id -> Load -> 选择数据源就可以了。

我经常用的是:1860 、8919 这两个来查看node_exporter监控

CentOS7搭建Prometheus 监控Linux主机

总结

安装这些服务都是使用systemd进行管理的,操作起来比较方便的。

这里没有设置告警,可以根据自己的需要设置对应的告警规则,使用alertmanager进行告警。

原文地址:https://www.toutiao.com/i6914917162675192324/

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 CentOS7搭建Prometheus 监控Linux主机 https://www.kuaiidc.com/56689.html

相关文章

发表评论
暂无评论