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))

浏览器访问:

2021-02-05_115338


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。第一次登陆修改更改密码!

2021-02-05_134551
2021-02-05_134600
2021-02-05_134609
2021-02-05_134732

四、安装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已采集到数据

2021-02-05_142618

六、验证

使用Grafana添加图形,便于展示!(监控Linux主机推荐使用9276模板)!

2021-02-05_142757
2021-02-05_142840
2021-02-05_142848
2021-02-05_142910

大部分指标都可以获取到,但是极少指标是获取不到的,需要调整模板对应的PromQL语句,目前正在学习阶段,关于Prometheus相关文章持续更新!

监控Linux主机强烈推荐使用1860模板!该模板获取主机的指标更加的详细!
2021-02-05_152107

七、Grafana插件扩展之饼图安装

有些时候导入的插件里面有饼图类型,但是Grafana饼图插件默认并没有安装,所以需要自行安装!(强烈推荐在线安装,避免版本的问题)!

官网:https://grafana.com/grafana/plugins/grafana-piechart-panel

$ grafana-cli plugins install grafana-piechart-panel
$ systemctl restart grafana-server         # 安装完成后重启生效
posted @ 2021-02-05 14:36  吕振江  阅读(936)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end