Docker,Docker-Compose部署Prometheus,Grafana,Alertmanager, Node_exporter

简易版docker-compose安装prometheus配套监控服务

其他学习笔记
docker:  https://blog.csdn.net/Fall_enleaves/article/details/130325496?spm=1001.2014.3001.5502
docker-compose: https://blog.csdn.net/Fall_enleaves/article/details/130971055?spm=1001.2014.3001.5502
  • 在A服务器中
  • 克隆仓库 https://gitee.com/sunnyfe/docker-prometheus.git
  • docker-compose up -d 后台启动
version: '3.3'

volumes:
  prometheus_data: {}
  grafana_data: {}

networks:
  monitoring:
    driver: bridge

services:
  prometheus:
    image: prom/prometheus:v2.45.0
    container_name: prometheus
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./prometheus/:/etc/prometheus/
      - prometheus_data:/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yml'
      - '--storage.tsdb.path=/prometheus'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'
      #热加载配置
      - '--web.enable-lifecycle'
      #api配置
      #- '--web.enable-admin-api'
      #历史数据最大保留时间,默认15天
      - '--storage.tsdb.retention.time=30d'  
    networks:
      - monitoring
    links:
      - alertmanager
      - cadvisor
      - node_exporter
    expose:
      - '9090'
    ports:
      - 9090:9090
    depends_on:
      - cadvisor

  alertmanager:
    image: prom/alertmanager:v0.25.0
    container_name: alertmanager
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./alertmanager/:/etc/alertmanager/
    command:
      - '--config.file=/etc/alertmanager/config.yml'
      - '--storage.path=/alertmanager'
    networks:
      - monitoring
    expose:
      - '9093'
    ports:
      - 9093:9093

  cadvisor:
    image: google/cadvisor:latest
    container_name: cadvisor
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /:/rootfs:ro
      - /var/run:/var/run:rw
      - /sys:/sys:ro
      - /var/lib/docker/:/var/lib/docker:ro
    networks:
      - monitoring
    expose:
      - '8080'

  node_exporter:
    image: prom/node-exporter:v1.6.0
    container_name: node-exporter
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/rootfs:ro
    command: 
      - '--path.procfs=/host/proc' 
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc|rootfs/var/lib/docker)($$|/)'
    networks:
      - monitoring
    ports:
      - '9100:9100'

  grafana:
    image: grafana/grafana:10.0.1
    container_name: grafana
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - grafana_data:/var/lib/grafana
      - ./grafana/provisioning/:/etc/grafana/provisioning/
    env_file:
      - ./grafana/config.monitoring
    networks:
      - monitoring
    links:
      - prometheus
    ports:
      - 3000:3000
    depends_on:
      - prometheus
  • 开放配置文件中相应端口

用prometheus监控另一台服务器(B服务器)

  1. 在B服务器中安装node_exporter
  2. 用docker-compose方式, 或者docker方式启动node_expoter, 这里用docker-compose
version: '3.3'

services:
  node_exporter:
    image: prom/node-exporter:v1.6.0
    container_name: node-exporter
    restart: always
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /proc:/host/proc:ro
      - /sys:/host/sys:ro
      - /:/rootfs:ro
    command: 
      - '--path.procfs=/host/proc' 
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc|rootfs/var/lib/docker)($$|/)'
    ports:
      - '9100:9100'
  1. 开放9100端口
  2. 在A服务器的prometheus配置监控B服务器 , 在node-exporter添加一个节点
  3. 具体配置如下
# 全局配置
global:
	# 将搜刮间隔设置为每15秒一次。默认是每1分钟一次。
  scrape_interval:     15s
  # 每15秒评估一次规则。默认是每1分钟一次。
  evaluation_interval: 15s

# AlertManager 配置
alerting:
  alertmanagers:
  - static_configs:
    - targets: ['alertmanager:9093']

# 报警(触发器)配置
rule_files:
  - "alert.yml"

# 配置
scrape_configs:
  - job_name: 'prometheus'
    # 覆盖全局默认值,每15秒从该作业中刮取一次目标
    scrape_interval: 15s
    static_configs:
    - targets: ['localhost:9090']
  - job_name: 'alertmanager'
    scrape_interval: 15s
    static_configs:
    - targets: ['alertmanager:9093']
  - job_name: 'cadvisor'
    scrape_interval: 15s
    static_configs:
    - targets: ['cadvisor:8080']
      labels:
        instance: Prometheus服务器 

  - job_name: 'node-exporter'
    scrape_interval: 15s
    static_configs:
    - targets: ['node_exporter:9100']
      labels:
        instance: Prometheus服务器
    - targets: ['172.16.201.170:9100']
      labels:
        instance: docker2服务器
  1. 重启prometheus, 或以请求方式使配置生效(如果开启了prometheus热加载)
curl -X POST http://localhost:9090/-/reload
  1. 在grafana的dashbord中就能查看到新配的服务器
    Docker,Docker-Compose部署Prometheus,Grafana,Alertmanager, Node_exporter_第1张图片

以上完成简易版prometheus配套监控服务

一般监控应用操作流程

  1. 监控什么应用, 就安装相应的expoer
  2. 修改prometheus配置文件 添加job
  3. 修改告警配置
  4. 重载prometheus配置文件
  5. 配置grafana

官网expoter 列表

https://prometheus.io/docs/instrumenting/exporters/

你可能感兴趣的:(docker,prometheus,grafana)