一分鐘部署prometheus&grafana全方面監控SpringBoot專案

fengzeng發表於2024-05-28

0x01 建立目錄

找一個你喜歡的地方,建立專案根目錄

example:

[root@demo-78 ~]#  mkdir /data/prometheus

0x02 建立配置檔案

進入到專案根目錄:

[root@demo-78 ~]#  cd /data/prometheus

需要新建三個檔案,分別是docker-compose.ymlprometheus.ymlnode_down.yml,詳細配置如下

在以下配置中,除了docker-compose.yml中的埠對映配置以外,所有你能看到的關於hostportusernamepassowrd的配置,都修改成你自己的,順便注意一下docker-compose.yml配置中關於檔案掛載的路勁問題。

prometheus.yml配置如下:

global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets: ['10.0.5.78:9093']
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  - "node_down.yml"
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['10.0.5.78:9094']

  - job_name: 'redis'
    static_configs:
     - targets: ['10.0.5.78:9121']
       labels:
         instance: redis

  - job_name: 'node'
    scrape_interval: 8s
    static_configs:
     - targets: ['10.0.5.78:9100']
       labels:
         instance: node

  - job_name: 'cadvisor'
    static_configs:
     - targets: ['10.0.5.78:8088']
       labels:
         instance: cadvisori
         
  #基於檔案自動載入新監控任務
  - job_name: 'file_ds'
    file_sd_configs:
    - files: ['/etc/prometheus/reload/*.yml']
      refresh_interval: 5s

docker-compose.yml配置如下:

version: '3'

networks:
    monitor:
        driver: bridge

services:
    prometheus:
        image: prom/prometheus
        container_name: prometheus
        hostname: prometheus
        restart: always
        volumes:
            - /prometheus/reload:/etc/prometheus/reload
            - /prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
            - /prometheus/node_down.yml:/etc/prometheus/node_down.yml
        ports:
            - "9094:9090"
        networks:
            - monitor

    grafana:
        image: grafana/grafana
        container_name: grafana
        hostname: grafana
        restart: always
        ports:
            - "3000:3000"
        networks:
            - monitor
            
    redis-exporter:
        image: oliver006/redis_exporter
        container_name: redis_exporter
        hostname: redis_exporter
        restart: always
        ports:
            - "9121:9121"
        networks:
            - monitor
        command:
            - '--redis.addr=redis://10.0.5.79:6379'
            - '--redis.password=Pig_1234'

    node-exporter:
        image: quay.io/prometheus/node-exporter
        container_name: node-exporter
        hostname: node-exporter
        restart: always
        ports:
            - "9100:9100"
        networks:
            - monitor

    mysql-exporter:
        image: prom/mysqld-exporter
        container_name: mysql-exporter
        hostname: mysql-exporter
        restart: always
        ports:
            - "9104:9104"
        networks:
            - monitor
        command:
            - "--mysqld.address=10.0.5.79:3306"
            - "--mysqld.username=pigdigital:Pigdigital_1234"
          
    cadvisor:
        image: google/cadvisor:latest
        container_name: cadvisor
        hostname: cadvisor
        restart: always
        volumes:
            - /:/rootfs:ro
            - /var/run:/var/run:rw
            - /sys:/sys:ro
            - /var/lib/docker/:/var/lib/docker:ro
        ports:
            - "8088:8080"
        networks:
            - monitor

node_down.yml配置如下:

groups:
- name: node_down
  rules:
  - alert: InstanceDown
    expr: up == 0
    for: 1m
    labels:
      user: test
    annotations:
      summary: "Instance {{ $labels.instance }} down"
      description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 1 minutes."

0x03 啟動容器

# 啟動容器
[root@demo-78 prometheus]# docker-compose up -d

自行執行命令檢視是否啟動成功

0x04 FAQ

  1. 如何使用prometheus.yml配置的 "基於檔案自動載入新監控任務"

    專案啟動後,在根目錄會生產一個名叫reload的資料夾,只需要在當前資料夾中加入新增的監控配置就能自動註冊到prometheus服務中,配置書寫格式如下:

    [root@demo-78 ~]# cat /prometheus/reload/web.yml 
    - targets:
      - '10.0.5.79:9100'
      labels:
        job: 'pig-web'
        __metrics_path__: '/web/actuator/prometheus'
    

    或:

    [root@demo-piggpt-78 ~]# cat /prometheus/reload/mysql.yml 
    - targets: ['10.0.5.78:9104']
    

    這裡只是給個例項,有特別的需求請自行檢視資料,這兩種寫法唯一的區別在於:第一種寫法我需要自定義metrics_path,否則預設拉取指標的路徑(第二種寫法)就是:10.0.5.78:9104/metrics,對於很多官網的exporter,使用第二種方式即可

  2. 容器mysql-export啟動報錯:caller=mysqld_exporter.go:225 level=info msg="Error parsing host config" file=.my.cnf err="no configuration found"

    由於mysql-exporter更新過後,配置寫法有改變,截止2024年5月28日,最新的mysql-exporter:0.15.1按照當前配置部署能夠成功啟動,如遇無法啟動,請自行檢視官方文件

相關文章