Prometheus的安装部署

1、先简单介绍一下Prometheus能做什么

它是开源的指标监控工具,专门收集各类数字监控数据、存历史趋势、做查询、触发告警,云原生环境最常用。

  1. 采集监控指标 定时主动去目标(exporter、业务程序)拉取数据:服务器 CPU / 内存磁盘、Nginx 连接数、MySQL 连接、接口 QPS、报错数量等。
  2. 保存时序历史 记录指标随时间变化的值,随时回看几分钟、几小时前的状态,看趋势。
  3. 灵活计算查询(PromQL) 对指标做计算:比如 CPU 使用率、请求增长率、磁盘剩余占比。
  4. 异常告警 写规则设置阈值,一旦异常(服务连不上、CPU 长期爆满、Nginx 宕机),通过 Alertmanager 推告警(钉钉 / 企业微信等)。

2、准备环境进行安装

2.1 准备三套linux环境 环境初始化脚本

bash 复制代码
#!/bin/bash
set -x
systemctl stop firewalld
systemctl disable firewalld
setenforce 0

sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
sed -i 's/^SELINUX=permissive/SELINUX=disabled/' /etc/selinux/config
cat /etc/selinux/config | grep SELINUX=


# 设置主机名 server agent1 grafana 安装自己安装稍微改下
hostnamectl set-hostname --static agent1

cat >> /etc/hosts << EOF
192.168.30.160 server
192.168.30.161 agent1
192.168.30.162 grafana
EOF

yum install vim wget rsync net-tools -y

2.2 安装Prometheus

下载地址: https://prometheus.io/download/

bash 复制代码
[root@server ~]# tar -xf prometheus-2.51.2.linux-amd64.tar.gz -C /usr/local/
[root@server ~]# mv /usr/local/prometheus-2.51.2.linux-amd64/ /usr/local/prometheus

2.3 配置文件介绍

bash 复制代码
# my global config
global:
  scrape_interval: 15s       # 全局默认采集间隔,所有job默认每15秒去拉取一次 /metrics 指标
  evaluation_interval: 15s   # 告警规则评估间隔,每15秒执行一次告警表达式判断
  # scrape_timeout 全局默认是10s,拉取指标超过10秒就判定超时

# Alertmanager 告警通知配置,当前行被注释,暂时不对接告警通知服务,就算产生告警也不会推送钉钉/企业微信等
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# 加载告警规则文件,目前两条注释,没有加载任何告警规则,不会生成告警
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# 采集任务配置列表,每个job代表一个采集任务,job_name会自动作为标签job=xxx附加到所有指标上
scrape_configs:
  # 采集prometheus自身指标,监控prometheus本身状态,地址为本机所在192.168.30.160:9090
  - job_name: "prometheus"
    static_configs:
      - targets: ["192.168.30.160:9090"]
  # agent1:采集192.168.30.161机器node_exporter,端口9100,获取服务器CPU、内存、磁盘、网络等主机指标
  - job_name: 'agent1'
    static_configs:
      - targets: ['192.168.30.161:9100']
  # agent1_mariadb:采集192.168.30.161的mysqld_exporter,端口9104,获取MariaDB/MySQL数据库指标,你之前connection refused就是这个目标连通异常
  - job_name: 'agent1_mariadb'
    static_configs:
      - targets: ['192.168.30.161:9104']

2.4 运行起来

bash 复制代码
nohup /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &

可以用jobs命令查看 是否后台运行起来了

kill %服务编号,删除后台运行的任务
kill %1

查看端口是否存在
netstat -ntlup |grep :9090

3、页面配置监控

3.1 登录页面

通过浏览器访问 http://服务器IP:9090 就可以访问到prometheus的主界面

查看本机的监控

3.2 监控Linux主机

简单说下逻辑

prometheus监控中心,他来统筹数据,所以要做被监控的主机上安装并运行相应的exporter 由exporter来监控数据 并传给prometheus

3.2.1 安装node_exporter

node_exporter 安装在要被监控的主机上并

可以在官网上下载 Download | Prometheus

bash 复制代码
[root@agent1 ~]# tar -xf node_exporter-1.7.0.linux-amd64.tar.gz -C /usr/local/
[root@agent1 ~]# mv /usr/local/node_exporter-1.7.0.linux-amd64/ /usr/local/node_exporter
[root@agent1 ~]# ls /usr/local/node_exporter/
LICENSE  node_exporter  NOTICE

后台运行 运行后端口是9100

bash 复制代码
 nohup /usr/local/node_exporter/node_exporter &

3.1.2 修改Prometheus配置文件

其它都不变,只添加了最后3行配置,注意YAML格式要求。

bash 复制代码
PowerShell
[root@server ~]# egrep -n : /usr/local/prometheus/prometheus.yml | awk -F'#' '{print $1}'
2:global:
3:  scrape_interval:   15s
4:  evaluation_interval: 15s
8:alerting:
9:  alertmanagers:
10:  - static_configs:
11:   - targets:
12:
15:rule_files:
19:
21:scrape_configs:
23:  - job_name: 'prometheus'
24:   static_configs:
25:   - targets: ['192.168.88.101:9090']
26:  - job_name: 'agent1'                                最后加上这三行,取一个job名称来代表被监控的机器
27:   static_configs:                                                
28:   - targets: ['192.168.88.102:9100']                 这里改成被监控机器的IP,后面端口接9100

然后重启 Prometheus

bash 复制代码
[root@server ~]# pkill prometheus
[root@server ~]# netstat -ntlup |grep 9090                        确认端口没有进程占用
[root@server ~]# nohup /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
[root@server ~]# netstat -ntlup |grep 9090                        确认端口被占用,说明重启成功
tcp6   0   0 :::9090    :::*     LISTEN    32651/prometheus

回到Web管理界面 --》点Status --》点Targets --》可以看到多了一台监控目标

后续下次再写

相关推荐
飞奔的瓜牛16 小时前
Loki + Promtail + Prometheus + Grafana 实现日志关键字监控及告警
grafana·prometheus·日志监控告警
JasonChen@6662 天前
Prometheus 黑盒监控入门:Blackbox Exporter 配置全解析(20)
prometheus·blackbox
JasonChen@6664 天前
Prometheus 存储实战:本地 TSDB 配置与 VictoriaMetrics 远端存储对接(18)
grafana·prometheus·victoria
JasonChen@6665 天前
Grafana 权限管理实战:用户授权与 Team 团队管理(17)
grafana·prometheus
JasonChen@6666 天前
pushgateway组件实现自定义监控数据采集(16)
grafana·prometheus·pushgateway
W_son8 天前
14.Grafana 监控看板实战:新建、调优、变量配置全流程
grafana·prometheus
真上帝的左手9 天前
2. 编程语言-Java-Spring Boot-集成SkyWalking + Prometheus 性能工程闭环实战
java·spring boot·性能优化·架构·系统架构·prometheus·skywalking
BullSmall10 天前
Prometheus监控栈安全加固全攻略
安全·grafana·prometheus
JasonChen@66612 天前
prometheus静态监控static_config案例四(10)
grafana·prometheus