Prometheus之修煉篇

Prometheus之修煉篇

官方文檔:https://prometheus.io

中文文檔:

  • 非官方:https://songjiayang.gitbooks.io/prometheus/content/

一、入門

A、配置

新建配置文件prometheus.yml:

scrape_configs:
  - job_name: 'test'
    # 拉取時間間隔
    scrape_interval: 30s
    # 拉取超時時間
    # scrape_timeout: 60s
    static_configs:
    - targets:
      - '127.0.0.1:9090' 

B、部署

  • 源碼包部署

    各自參考官網部署方式即可。

  • docker部署

    docker run -d \
               --name=prometheus \
               -p 9090:9090 \
               -v /root/monitor_home/prometheus.yml:/config/prometheus.yml \
               prom/prometheus --config.file=/config/prometheus.yml
    
  • docker部署(保存監控數據)

    docker run -d \
               --name=prometheus \
               -p 9090:9090 \
               -u $(id -u):$(id -g) \
               -v /root/monitor_home/prometheus.yml:/config/prometheus.yml \
               -v /root/monitor_home/data:/prometheus \
               prom/prometheus --config.file=/config/prometheus.yml
    
  • docker-compose部署

    version: "3"
    services: 
      # 監控
      prometheus:
        image: prom/prometheus
        ports:
          - "9090:9090"  
        restart: always 
        container_name: prometheus
        volumes:  
          - /root/monitor_home/prometheus.yml:/etc/prometheus/prometheus.yml
    
  • docker-compose部署(保存監控數據)

    version: "3"
    services: 
      # 監控
      prometheus:
        image: prom/prometheus
        ports:
          - "9090:9090"  
        restart: always 
        container_name: prometheus
        volumes:  
          - /root/prometheus.yml:/etc/prometheus/prometheus.yml
          - /root/monitor_home:/prometheus
        # 以root運行一個容器,不是一個好的解決方案      
        user: 'root'
    

二、使用

A、管理API

Prometheus提供了一組管理API,以簡化自動化和集成。

注意:{ip:port} 是普羅米修斯所在的IP和端口

1、健康檢查

GET {ip:port}/-/healthy

該端點始終返回200,應用於檢查Prometheus的運行狀況。

2、準備檢查

GET {ip:port}/-/ready

當Prometheus準備服務流量(即響應查詢)時,此端點返回200。

3、刷新

PUT  {ip:port}/-/reload
POST {ip:port}/-/reload

該端點觸發Prometheus配置和規則文件的重新加載。默認情況下它是禁用的,可以通過該--web.enable-lifecycle標誌啓用。

  • docker,如下拼接命令接口
docker run -d \
          --name=prometheus \
          -p 9090:9090 \
          -v /root/monitor_home/prometheus.yml:/prometheus-config/prometheus.yml \
          prom/prometheus --web.enable-lifecycle \
          --config.file=/prometheus-config/prometheus.yml
  • docker-compose
services: 
 prometheus:
   image: prom/prometheus
   ports:
        - "9090:9090"
   container_name: prometheus
   volumes:
        - /root/monitor_home/prometheus.yml:/prometheus-config/prometheus.yml
   command:
      [ "--config.file=/prometheus-config/prometheus.yml",
        "--web.enable-lifecycle"
      ]

觸發配置重新加載的另一種方法是將a發送SIGHUP給Prometheus進程。

4、放棄

PUT  {ip:port}/-/quit
POST {ip:port}/-/quit

該端點觸發Prometheus的正常關閉。默認情況下它是禁用的,可以通過該--web.enable-lifecycle標誌啓用。

觸發正常關閉的另一種方法是將a發送SIGTERM給Prometheus進程。

B、儲存

以下是經常使用到的儲存配置:

  • --storage.tsdb.path: Prometheus監控的數據存放點. 默認data/.
  • --storage.tsdb.retention.time: 數據保存最長時間. 默認 15d. 此配置會覆蓋掉storage.tsdb.retention(storage.tsdb.retention是過時配置) .
  • --storage.tsdb.retention.size: [EXPERIMENTAL] This determines the maximum number of bytes that storage blocks can use (note that this does not include the WAL size, which can be substantial). The oldest data will be removed first. Defaults to 0 or disabled. This flag is experimental and can be changed in future releases. Units supported: KB, MB, GB, PB. Ex: “512MB”
  • --storage.tsdb.wal-compression: 配置是否打開WAL的壓縮功能。你可以期望WAL的大小減少一半,而只增加很少的cpu負載。請注意,如果您啓用了這個標記,並隨後將Prometheus降級到2.11.0以下的版本,您將需要刪除WAL,因爲它將無法讀取。.

N、附件

global:
  # 默認情況下抓取目標的頻率.
  [ scrape_interval: <duration> | default = 1m ]

  # 抓取超時時間.
  [ scrape_timeout: <duration> | default = 10s ]

  # 評估規則的頻率.
  [ evaluation_interval: <duration> | default = 1m ]

  # 與外部系統通信時添加到任何時間序列或警報的標籤
  #(聯合,遠程存儲,Alertma# nager).
  external_labels:
    [ <labelname>: <labelvalue> ... ]

# 規則文件指定了一個globs列表. 
# 從所有匹配的文件中讀取規則和警報.
rule_files:
  [ - <filepath_glob> ... ]

# 抓取配置列表.
scrape_configs:
  [ - <scrape_config> ... ]

# 警報指定與Alertmanager相關的設置.
alerting:
  alert_relabel_configs:
    [ - <relabel_config> ... ]
  alertmanagers:
    [ - <alertmanager_config> ... ]

# 與遠程寫入功能相關的設置.
remote_write:
  [ - <remote_write> ... ]

# 與遠程讀取功能相關的設置.
remote_read:
  [ - <remote_read> ... ]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章