[Hyperf] 在 Hyperf 框架中使用 prometheus + grafana 部署基本的監控

marun發表於2020-03-05

參考: hyperf利用prometheus接入服務監控,使用grafana實現資料的實時監控顯示
hyperf文件

本文章記錄本人的第一次部署所踩的坑,未深入瞭解prometheus 和grafana 如有不當的地方請指正,謝謝!

version: '2'
networks:
  monitor:
    driver: bridge

services:
  prometheus:
    image: prom/prometheus
    container_name: prometheus
    hostname: prometheus
    restart: always
    volumes:
        # 將你的prometheus.yml檔案放在當前檔案同級下,或自定義
      - ./prometheus.yml:/etc/prometheus/prometheus.yml
      #- /home/prometheus/node_down.yml:/etc/prometheus/node_down.yml
    ports:
      - "9090:9090"
    networks:
      monitor:
        ipv4_address: 172.18.0.3

  grafana:
    image: grafana/grafana
    container_name: grafana
    hostname: grafana
    restart: always
    volumes:
    # 建立 etc目錄,data目錄儲存grafana的資料
    - ./etc:/etc/grafana
    - ./data:/var/lib/grafana
    ports:
      - "3000:3000"
    networks:
      monitor:
        ipv4_address: 172.18.0.4

  node-exporter:
    image: prom/node-exporter
    container_name: node-exporter
    hostname: node-exporter
    restart: always
    ports:
      - "9100:9100"
    networks:
      monitor:
        ipv4_address: 172.18.0.2

注意:為了避免每次docker-compose 啟動之後 ip會發生變化,我這裡配置了固定IP,請根據個人實際情況配置,或參閱docker相關文件

使用命令docker-compose up啟動容器

因為對 prometheus的不瞭解,我直接使用hyperf預設配置

  • 引入元件 composer require hyperf/metric

  • 釋出預設配置檔案 php bin/hyperf.php vendor:publish hyperf/metric

  • config/autoload/dependencies.php中新增對應的Redis儲存

    return [
      \Prometheus\Storage\Adapter::class => \Hyperf\Metric\Adapter\Prometheus\RedisStorageFactory::class,
    ];

    在上面的第一篇文章中,老哥說使用swoole_table更高效,我還不知道如何使用,有興趣的老哥可以自己研究一下。

  • 增加中介軟體
    config/autoload/middlewares.php檔案中增加對應的中介軟體

    return [
      'http' => [
          \Hyperf\Metric\Middleware\MetricMiddleware::class,
      ],
    ];
  • 新增 metrics路由

    Router::get('/metrics', function(){
      $registry = Hyperf\Utils\ApplicationContext::getContainer()->get(Prometheus\CollectorRegistry::class);
      $renderer = new Prometheus\RenderTextFormat();
      return $renderer->render($registry->getMetricFamilySamples());
    });

    這樣對專案的配置就完成了

prometheus.yml檔案中增加對應的配置

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: ['localhost:9090']

  - job_name: 'node'

     # 注意這裡的IP需要填寫 node-exporter 容器的ip  
    static_configs:
    - targets: ['172.18.0.2:9100']


  - job_name: 'skeleton'
    # 這裡填寫的是宿主機的ip
    static_configs:
    - targets: ['10.0.75.1:9502']

配置完成之後,再次 dokcer-compose up

訪問 http://localhost:9090 檢視 prometheus

[Hyperf]  在Hyperf框架中使用prometheus + grafana 部署基本的監控

如圖所示,nodeskeleton 都已啟動

上面都配置完了,開始配置 Grafana
開啟 http://localhost:3000 預設密碼是: admin/admin

  • 新建datasource
    左側邊欄 add datasources 選擇Prometheus

[Hyperf]  在 Hyperf 框架中使用 prometheus + grafana 部署基本的監控

  • 配置 datasource

[Hyperf]  在 Hyperf 框架中使用 prometheus + grafana 部署基本的監控

填寫容器的IP:埠

  • 匯入hyperf官方的JSON檔案

[Hyperf]  在 Hyperf 框架中使用 prometheus + grafana 部署基本的監控

匯入之後需要將預設的 app_name改成你自己的
如:admin-api 就需要填寫admin_api 改成下劃線形式

[Hyperf]  在 Hyperf 框架中使用 prometheus + grafana 部署基本的監控

  • 檢視監控
    在Home中你就可以看到了

[Hyperf]  在 Hyperf 框架中使用 prometheus + grafana 部署基本的監控

點進去檢視

[Hyperf]  在 Hyperf 框架中使用 prometheus + grafana 部署基本的監控

到此結束,小白第一次配置監控,還有很多東西沒弄清楚

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章