一 背景说明:

领导想弄套监控系统,我就采用了prometheus+grafana+consulmanager+Alertmanager 主要监控运行的各个端口号和域名的连通性,以及各个服务器的基本指标。主要用到的是node_exporter和blackbox-exporter。接下来主要记录安装过程和配置过程。操作系统使用的是centos7.9

二 prometheus安装

2.1 下载安装

https://prometheus.io/download/#prometheus #官网下载地址
目前使用的版本是 prometheus-2.28.0.linux-amd64.tar.gz
下载下来,解压即可。

2.2 启动方式

2.2.1 直接启动

/usr/local/prometheus/prometheus --config.file=/usr/local/prometheus/prometheus.yml --web.listen-address=:9090

2.2.2 添加到系统服务进行启动

cat  /etc/systemd/system/prometheus.service
 
[Unit]
Description=Prometheus Monitoring System
Documentation=Prometheus Monitoring System
[Service]
ExecStart=/usr/local/prometheus/prometheus --web.enable-lifecycle --config.file=/usr/local/prometheus/prometheus.yml --web.listen-address=:9090
[Install]
WantedBy=multi-user.target


#启动
systemctl daemon-reload
systemctl start prometheus.service
systemctl enable prometheus.service
也可以热加载,支持热加载必须添加 --web.enable-lifecycle 才可以。
curl -X POST http://127.0.0.1:9090/-/reload

2.3 prometheus配置文件设置

2.3.1 直接写入到prometheus.yml配置文件里

类似下面的格式

  - job_name: 'agent'
    static_configs:
    - targets: ['123.56.9.x:9100']

2.3.2 采用file_sd_configs 这种文件发现的方式

- job_name: '数据处理主-123.56.9.x'
    file_sd_configs:
      - files:
        - /usr/local/prometheus/targets/nodes/mongodb01.json
        refresh_interval: 1m

mongodb01.json的内容如下:(格式要注意,否则无法启动)

[
  {
  "targets":["123.56.9.x:9100"] 
  }
]


2.3.3 检查配置文件

 /usr/local/prometheus/promtool check config /usr/local/prometheus/prometheus.yml

三 node_exporter下载安装

3.1 下载和安装

下载地址 https://prometheus.io/download/#prometheus 首先下载好安装包,然后通过下面的脚本进行安装启动。

wget http://172.19.149.191/downold/node_exporter-1.6.1.linux-amd64.tar.gz
PACKAGE="node_exporter-1.6.1.linux-amd64.tar.gz"
mv $PACKAGE /usr/local/
cd /usr/local
tar xf $PACKAGE && mv node_exporter-1.6.1.linux-amd64  node_exporter
echo  "node_exporter部署完成"
cat > /etc/systemd/system/node_exporter.service <<EOF
[Unit]
Description=Prometheus node_exporter
[Service]
User=nobody
ExecStart=/usr/local/node_exporter/node_exporter --log.level=error
ExecStop=/usr/bin/killall node_exporter
MemoryLimit=300M 
CPUQuota=100
[Install]
WantedBy=default.target
EOF
echo "配置自启动job"
systemctl daemon-reload
systemctl enable node_exporter.service
systemctl start node_exporter.service
echo `netstat -ntlp |grep 9100`
echo "验证通过"


当然node_exporter这个的启动端口也可以自定义的

/usr/local/node_exporter/node_exporter --web.listen-address=:9600

3.2 配置prometheus配置文件

虽然node_exporter安装好了,还需要配置prometheus的配置文件,去拉取node_exporter的数据才可以

 - job_name: 'agent'
    static_configs:
    - targets: ['123.56.9.8:9100']  #node_exporter的端口号

四 blackbox-exporter安装

image.png 我部署在了prometheus和grafana这台服务端上,当部署在被监控端的时候,监控tcp的时候一直在模版上显示离线状态

4.1 下载安装

wget https://github.com/prometheus/blackbox_exporter/releases/download/v0.24.0/blackbox_exporter-0.24.0.linux-amd64.tar.gz

tar -zxvf blackbox_exporter-0.24.0.linux-amd64.tar.gz
 mv blackbox_exporter-0.24.0.linux-amd64  /usr/local/blackbox_exporter

4.2 添加为系统服务

cat  /usr/lib/systemd/system/blackbox_exporter.service
[Unit]
Description=blackbox_exporter
After=network.target
 
[Service]
User=root
Type=simple
ExecStart=/usr/local/blackbox_exporter/blackbox_exporter  --config.file=/usr/local/blackbox_exporter/blackbox.yml
Restart=on-failure
 
[Install]
WantedBy=multi-user.target

4.3 启动blackbox

systemctl enable blackbox_exporter.service
systemctl start blackbox_exporter.service
systemctl status blackbox_exporter.service
默认端口 9115

blackbox的配置文件默认不需要配置,所有实现均在prometheus.yml里配置

4.4 配置prometheus.yml

 ##监控tcp端口
 - job_name: '数据处理主-123.56.9.x-port'
    metrics_path: /probe
    params:
      module: [tcp_connect]
    static_configs:
      - targets: ['123.56.9.x:27018','123.56.9.x:22'] 
      - labels:
          instance: 'port_status'
          group: 'tcp'
    relabel_configs:
        - source_labels: [__address__]
          target_label: __param_target
        - source_labels: [__param_target]
          target_label: instance
        - target_label: __address__
          replacement: 127.0.0.1:9115


#监控http
  - job_name: 'blackbox_http_2xx'
 
    metrics_path: /probe
 
    params:
 
      module: [http_2xx]
 
    static_configs:
 
      - targets:
 
        - https://www.baidu.com
 
        - https://www.qq.com
 
    relabel_configs:
 
      - source_labels: [__address__]
 
        target_label: __param_target
 
      - source_labels: [__param_target]
 
        target_label: instance
 
      - target_label: __address__
 
        #blackbox exporter 所在节点
 
        replacement: 127.0.0.1:9115 


配置完之后,重启prometheus即可。

五 安装grafana

上面的监控都配置完了,接下来接入grafana,通过网页和指定模版去查看监控到的数据

5.1 安装并启动

yum install grafana
systemctl start grafana-server
 
systemctl enable grafana-server

或者去官网下载安装

sudo yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-10.0.2-1.x86_64.rpm
端口号默认为 3000

5.2 登录

使用默认的admin用户,admin密码就可以登陆了
第一次登录会让你更改密码

5.3 添加prometheus数据源

image.png

image.png

5.4 导入模版

新版本的grafana导入模版的地方,发生了变化, image.png

image.png

六 总结

这里我们不通过直接更改prometheus配置文件去监控各个指标,接下来会介绍如何通过consulmanager去监控所有的指标,会更加的方便。