Prometheus-07 Docker-compose安装配置prometheus以及初步分析CPU/内存利用率分析

本文介绍了如何使用docker-compose配置并安装Prometheus,详细讲解了配置文件设置,包括解决容器时间同步问题。同时,展示了如何监控CPU和内存利用率,通过PromQL查询语句分析了系统的资源使用情况。

docker-compose配置

关于docker-compose的配置安装可以参考我的这篇笔记: Docker-compose安装配置讲解

创建docker-compose.yml配置文件

version: '3'

services:
  prometheus:
    image: prom/prometheus:v2.30.3
    container_name: prometheus
    deploy:
      resources:
        limits:
          cpus: '0.8'
          memory: 12G
    ports:
      - 9090:9090
    privileged: true
    volumes:
      - /data/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
      - /data/prometheus/data:/prometheus
      - /app/ca/server/server.crt:/etc/prometheus/server.crt
      - /app/ca/server/server.key:/etc/prometheus/server.key
      - /app/ca/root/ca.crt:/etc/prometheus/ca.crt
      - /data/prometheus/rules:/etc/prometheus/rules
      - /data/prometheus/web-config.yml:/etc/prometheus/web-config.yml
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
    depends_on:
      - node-exporter
    environment:
      - TZ=Asia/Shanghai
    networks:
      - monitoring
  node-exporter:
    image: prom/node-exporter:v1.2.2
    container_name: node-exporter
    privileged: true
    ports:
      - 9100:9100
    volumes:
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/rootfs:ro
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
    command:
      - '--path.procfs=/host/proc'
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.ignored-mount-points'
      - '^/(sys|proc|dev|host|etc)($$|/)'
    networks:
      - monitoring
    environment:
     - TZ=Asia/Shanghai
  alertmanager:
    volumes:
      - /data/prometheus/alertmanager.yml:/etc/alertmanager/alertmanager.yml
    command:
      - '--config.file=/etc/alertmanager/alertmanager.yml'
    ports:
      - 9093:9093
    image: prom/alertmanager
    container_name: alertmanager
    networks:
      - monitoring
    volumes:
      - /etc/localtime:/etc/localtime
      -  /etc/timezone:/etc/timezone
    environment:
      - TZ=Asia/Shanghai

  grafana:
    image: grafana/grafana
    hostname: grafana
    container_name: grafana
    ports:
      - 3000:3000
    volumes:
      - /etc/localtime:/etc/localtime:ro
      -  /etc/timezone:/etc/timezone:ro
    environment:
      - TZ=Asia/Shanghai

networks:
  monitoring:
    external: true

上面给出了docker-compose配置,直接运行命令

docker-compose -f docke-compose.yml up -d

首先会拉取上面的镜像,如果镜像不存在就会下载到本地,然后使用镜像创建容器,以及挂载对应的目录,启动prometheus以及对应的组件

需要注意的时,需要将本地时间可能与容器时间存在8小时的时间差,所以需要将本地时间也配置到容器内

可以参考我的这篇博客: Prometheus-08 安装配置prometheus时的ntp时间同步问题

上面通过配置:

    volumes:
      - /etc/localtime:/etc/localtime:ro
      -  /etc/timezone:/etc/timezone:ro

或者配置

   environment:
      - TZ=Asia/Shanghai

同步容器时间

prometheus配置文件

prometheus.yml

global:
  scrape_interval:  15s
  scrape_timeout: 10s
  evaluation_interval: 15s

#rule
rule_files:
  - /etc/prometheus/rules/*.rules

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['192.168.235.128:9090']
        labels:
          instance: prometheus


  - job_name: 'node_export'
    static_configs:
      - targets: ['192.168.235.128:9100']
        labels:
          instance: node_export


#alarm
alerting:
  alertmanagers:
    - static_configs:
        - targets: ['alertmanager:9093']
    - scheme: http
    - timeout: 10s

alertmanager.yml配置文件

global:
  resolve_timeout: 5m
  smtp_smarthost: 'smtp.sina.com:25'
  smtp_from: '发送@sina.com' # 发送告警的邮箱
  smtp_auth_username: '*@sina.com'  #发送告警的邮箱
  smtp_auth_password: '' #邮箱授权密码
  smtp_require_tls: false

route:
  group_by: ['alertname']
  group_wait: 10s # 告警等待时间。告警产生后等待10s,如果有同组告警一起发出
  group_interval: 5m  # 两组告警的间隔时间
  repeat_interval: 5m #重复告警的间隔时间,减少相同右键的发送频率 此处为测试设置为5m
  receiver: 'email-notifications'   # 默认接收者  routes: # 指定那些组可以接收消息

receivers:
- name: 'email-notifications'
  email_configs:
  - to: '接收邮箱@sina.com'
    from: '用户邮箱@sina.com'
    smarthost: 'smtp.sina.com:25'
    auth_username: '用户邮箱@sina.com'
    auth_password: '密码'
    require_tls: false


创建的rule,统计5m内请求状态500的变化率求和

groups:
  - name: example-group
    rules:
      - alert: HighErrorRate
        expr: sum(rate(http_requests_total{status="500"}[5m])) by (job) > 10
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: High error rate detected
          description: Error rate is above the threshold

监控数据展示

这里我们以最基本cpu/内存利用率分析,首先进入到prometheus的Ui页面;
当使用 Prometheus 进行查询时,可以使用 PromQL(Prometheus Query Language)来编写查询语句。以下是一个示例 PromQL 查询语句,用于统计 CPU 使用率:
在这里插入图片描述

100 - (avg by (instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)

解释:

  • node_cpu_seconds_total 是一个内置的 Prometheus 指标,用于表示 CPU 每个模式(如 idleusersystem 等)的总时间(以秒为单位)。
  • irate 函数用于计算指标的瞬时速率,它计算一段时间内的值变化率。
  • avg by (instance) 用于按照 instance 标签对数据进行分组,并计算每个实例的平均值。
  • 100 - (...) * 100 用于将 CPU 空闲时间转换为使用率。

对内存使用率分析
在这里插入图片描述

(node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100

解释:

  • node_memory_MemTotal_bytes 是一个内置的 Prometheus 指标,表示总内存的字节数。
  • node_memory_MemAvailable_bytes 是一个内置的 Prometheus 指标,表示可用内存的字节数。

该查询语句计算了可用内存占总内存的比例,然后乘以 100,得到内存使用率的百分比。

curl接口分析

[root@maste prometheus]# curl localhost:9100/metrics | grep -i node_cpu | head
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 66917    0 66917    0     0  1374k      0 --:--:-- --:--:-- --:--:-- 1390k
# HELP node_cpu_guest_seconds_total Seconds the CPUs spent in guests (VMs) for each mode.
# TYPE node_cpu_guest_seconds_total counter
node_cpu_guest_seconds_total{cpu="0",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="0",mode="user"} 0
node_cpu_guest_seconds_total{cpu="1",mode="nice"} 0
node_cpu_guest_seconds_total{cpu="1",mode="user"} 0
# HELP node_cpu_seconds_total Seconds the CPUs spent in each mode.
# TYPE node_cpu_seconds_total counter
node_cpu_seconds_total{cpu="0",mode="idle"} 2235.44
node_cpu_seconds_total{cpu="0",mode="iowait"} 0.58
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员路同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值