zl程序教程

您现在的位置是:首页 >  云平台

当前栏目

Prometheus安装部署及简单监控

监控安装部署 简单 Prometheus
2023-09-14 09:15:47 时间

1,安装部署

1,环境准备

IP 地址角色
192.168.111.3Prometheus Server
192.168.111.4node_exporter
版本:

测试通过系统:CentOS Linux release 7.4.1708 (Core)
Prometheus:2.4.2.linux-amd64
Alertmanager:0.15.2.linux-amd64
node_exporter:0.16.0.linux-amd64
软件包下载地址:https://prometheus.io/download/

2,部署 Prometheus Server。

1,下载安装包。

$ cd /usr/local/src/
$ wget https://github.com/prometheus/prometheus/releases/download/v2.4.2/prometheus-2.4.2.linux-amd64.tar.gz
$ wget https://github.com/prometheus/alertmanager/releases/download/v0.15.2/alertmanager-0.15.2.linux-amd64.tar.gz
$ wget https://github.com/prometheus/node_exporter/releases/download/v0.16.0/node_exporter-0.16.0.linux-amd64.tar.gz

2,安装 Prometheus。

创建 prometheus 用户。

$ groupadd prometheus
$ useradd -g prometheus -m -d /var/lib/prometheus -s /sbin/nologin prometheus

解压安装包。

$ tar xf prometheus-2.4.2.linux-amd64.tar.gz -C /usr/local/
$ cd /usr/local/
$ mv prometheus-2.4.2.linux-amd64/ prometheus

创建启动脚本:

$ vim /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=/var/lib/prometheus --storage.tsdb.retention=15d --log.level=info
Restart=on-failure
[Install]
WantedBy=multi-user.target

3,安装 node_exporter。

在 Prometheus 节点和另一台节点上分别安装 node_exporter。

$ tar xf node_exporter-0.16.0.linux-amd64.tar.gz -C /usr/local/
$ cd /usr/local/
$ mv node_exporter-0.16.0.linux-amd64/ node_exporter
$ chown -R prometheus.prometheus node_exporter/

创建 node_exporter 启动脚本:

$ vim /usr/lib/systemd/system/node_exporter.service 

添加如下内容:

[Unit]
Description=node_export
Documentation=https://github.com/prometheus/node_exporter
After=network.target
 
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target

注意:node_exporter 的运行用户也是 prometheus 用户需要在每台节点上都创建该用户。

启动 node_exporter 服务:

$ systemctl enable node_exporter.service
$ systemctl start node_exporter.service
$ systemctl status node_exporter.service
$ ss -tnl | grep 9100

4,配置 Prometheus 添加监控目标

$ cd /usr/local/prometheus
$ vim prometheus.yml 
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'
 
    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.
 
    static_configs:
    - targets: ['localhost:9090','localhost:9100'] # 对本机node_exporter 监控
 
#新添加的对其它node节点抓取数据
  - job_name: '111.4'
    #重写了全局抓取间隔时间,由15秒重写成5秒。
    scrape_interval: 5s
    static_configs:
    - targets: ['192.168.111.4:9100']

启动 Prometheus 服务:

$ chown -R prometheus.prometheus prometheus/
$ systemctl enable prometheus.service
$ systemctl start prometheus.service
$ systemctl status prometheus.service

注意:要留意启动之前的目录权限更改,否则可能会在启动的时候报错Feb 11 16:08:41

localhost alertmanager: level=error ts=2019-02-11T08:08:41.419390133Z
caller=main.go:179 msg=“Unable to create data directory” err=“mkdir
data/: permission denied”。

访问 Prometheus WEB 查看我们定义的目标主机:http://192.168.111.3:9090/targets
在这里插入图片描述