Prometheus 普罗米修斯

Prometheus 普罗米修斯

  • Prometheus由go语言开发,是一套开源的监控、报警、时间序列数据库的组合。适合监控docker容器。因为Kubernetes (K8S) 的流行带动了prometheus的发展
时间序列数据
  • TimeSeries Data,按照时间顺序记录系统、设备状态变化的数据
  • 时间序列数据特点
    • 性能好,关系型数据库对于大规模数据的处理性能糟糕。NoSQL可以比较好的处理大规模数据,但依然比不上时间序列数据库
    • 存储成本低,高效的压缩算法、节省存储空间、有效降低 IO
      • Prometheus有着非常高效的时间序列数据存储方法,每个采样数据仅占用3.5byte左右空间,上百万条时间序列,30秒间隔,保留60天,大概花了200多G
Prometheus特征
  • 多维度数据模型
  • 灵活的查询语言
  • 不依赖分布式存储,单个服务器节点是自主的
  • 以HTTP方式,通过pull模型拉取时间序列数据
  • 可以通过中间网关支持push模型
  • 通过服务发现或者静态配置,来发现目标服务对象
  • 支持多种多样的图标和界面展示,需要结合Grafana展示
时间同步
# mount /dev/sro /mnt
# yum install ntpdate -y
# ntpdate cn.ntp.org.cn

查看系统时间
# date
查看硬件时间
# clock -r
把系统时间同步给硬件时间
# hwclock --systohc
设置Linux网络环境
  • 编辑网络配置文件

    PS:有的克隆不需要修改
    # vim /etc/sysconfig/network-scripts/ifcfg-ens33
    
    修改:
    BOOTPROTO=dhcp 改为 BOOTPROTO=static
    设置固定ip:
    IPADDR=192.168.22.23
    NETMASK=255.255.255.0
    GATEWAY=192.168.22.200
    DNS1=8.8.8.8	进行外网通信
    DNS2=114.114.114.114
    
    因为是克隆,所有修改UUID后三位,随意即可
    
    重启网卡
    # service network restart
    
安装Prometheus
  • 下载

    • https://prometheus.io/download/
    • prometheus-2.34.0.linux-amd64.tar.gz
  • 安装

    • 解压到指定目录

      # tar xf prometheus-2.34.0.linux-amd64.tar.gz -C /usr/local/
      
    • 默认配置文件启动Prometheus

      # /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
      & 连接符代表后台运行,不占用终端窗口
      
    • 查看端口占用情况

      	prometheus默认端口9090
      # lsof -i:9090
      	lsof没有安装的话,可以使用下面组合指令查看端口占用情况
      # ss -naltp | grep 9090
      
    • 访问prometheus界面

      http://服务器ip:9090
      
  • 系统默认初始监控自己的主机信息

    • 进入界面后 Status 栏目下 Targets
    • 获取监控数据是从 http://服务器ip:9090/metrics 接口返回的
prometheus默认监控图像
  • 首页搜索 process_cpu_seconds_total,点击 Execute 查看当前Linux CPU运行情况

  • net_conntrack_dialer_conn_attempted_total,查看网络连接状态

监控远程Linux主机
  • 被监控端安装 node_exporter组件

    • node_exporter 组件用于收集 prometheus 需要的数据
    • 主要收集 Linux 系统信息
  • 官网下载

    • https://prometheus.io/download/
    • node_exporter-1.3.1.linux-amd64.tar.gz
  • 解压安装

    # tar xf node_exporter-1.3.1.linux-amd64.tar.gz -C /usr/local/
    
  • 启动 node_exporter

    # nohup /usr/local/node_exporter/node_exporter &
    nohup可以让 node_exporter 组件永久运行
    node_exporter 默认端口为 9100
    
    nohup: ignoring input and appending output to ‘nohup.out’,未启动或异常会把日志追加到 nohup.out
    
    查看是否启动成功
    # ss -naltp | grep 9100
    
  • 查看被监控端收集的监控信息

    • http://被监控端ip:9100/metrics
  • 在 prometheus服务器配置文件添加被监控机器的配置段

    # vim prometheus.yml
    文件最后添加
    - job_name: 'slave'		# 取一个job名称来代表被监控的机器
      static_configs:
      - targets: ['192.168.32.129:9100']	# 改为被监控的机器ip
    
    修改完配置后重启服务
    kill掉prometheus
    # pkill prometheus
    重启
    # /usr/local/prometheus/prometheus --config.file="/usr/local/prometheus/prometheus.yml" &
    
  • 在 web 管理页面 Status —> Target 即可看到多了一台监控目标

Grafana 可视化图形工具
  • Grafana 是一个开源的度量分析、可视化工具,可以通过将采集的数据分析、查询,然后进行可视化的展示并实现报警
  • 官网
    • https://grafana.com/
使用 Grafana 连接 prometheus
  • 下载

    • https://grafana.com/grafana/download
    • wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.4.4-1.x86_64.rpm
    • https://www.cnblogs.com/biaopei/p/12017150.html 解决wget慢
  • rpm 安装

    # sudo yum install grafana-enterprise-8.4.4-1.x86_64.rpm -y
    
  • 启动

    # systemctl start grafana-server	
    # systemctl enable grafana-server	开机启动
    
    查看是否启动,默认端口为 3000
    # ss -naltp | grep 3000
    
  • 打开 web 界面

    • 192.168.32.128:3000
  • 登录

    • 默认用户名密码均为 admin
  • 添加 prometheus 数据源

    • 点击首页 add data source
    • 设置数据源、数据源名称、类型、IP地址等
  • 单击右侧齿轮图标,选择 Data sources,查看是否设置成功

  • 给添加好的数据源做图形展示

    • 点击右侧加号图标,选择 Dashboard
    • Add a new panel
      • Data source 选择 Prometheus
    • 右上保存图标可以保存设置,ctrl + s 也可以
Grafana + onealert 报警
  • prometheus 报警需要使用 alertmanager 组件,而且报警规则需要手动编写,所以通常使用 Grafana + onealert报警

  • 报警前要把机器做时间同步

  • 搜索 onealert,进入睿象云,注册账号

  • 登录,点击左侧智能告警平台

  • 新界面点击上方集成,点击左侧Grafana图标

  • 先在 onealert 里添加 grafana 应用

  • 设置应用名称和警告时间,点击保存,拿到AppKey

  • 接下来根据配置步骤配置即可

    • 在Grafana主界面左侧,点击铃铛图标,选择Notification channels
    • 点击 Add channel设置,url可以在睿象云 AppKey界面下滑配置步骤中复制
    • Type可以选择webhook
      • Notification settings中选择Include image和Send reminders
    • 点击Save
  • 找到dashboard中的图标,点击Edit,点击下方Alert,设置报警阈值,在Notifications中设置报警内容

  • 最后在睿象云中配置通知策略,此时基本报警信息完成

  • 耗费CPU命令测试

    # cat /dev/urandom | md5sum
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值