prometheus安裝和使用記錄

来源:https://www.cnblogs.com/huizit1/archive/2023/06/19/17492003.html
-Advertisement-
Play Games

Getting started | Prometheus Configuration | Prometheus Download | Prometheus Download Grafana | Grafana Labs # prometheusmkdir -m=777 -p /data/{downl ...


Getting started | Prometheus

Configuration | Prometheus Download | Prometheus Download Grafana | Grafana Labs  
# prometheus
mkdir
-m=777 -p /data/{download,app_logs,app/prometheus} cd /data/download wget https://github.com/prometheus/prometheus/releases/download/v2.45.0-rc.0/prometheus-2.45.0-rc.0.linux-amd64.tar.gz tar xvfz prometheus-*.tar.gz
ln -s /data/download/prometheus-2.45.0-rc.0.linux-amd64/prometheus /usr/bin/prometheus
cp
/data/download/prometheus-2.45.0-rc.0.linux-amd64/prometheus.yml /data/app/prometheus/prometheus.yml
prometheus --config.file=/data/app/prometheus/prometheus.yml --web.listen-address=:9090 --web.enable-lifecycle --storage.tsdb.path=/data/app/prometheus/data >>/data/app_logs/prometheus.log 2>&1 &

# node_exporter 在需要監控的伺服器里安裝
mkdir -m=777 -p /data/{download,app_logs,app/prometheus}
cd /data/download
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.0/node_exporter-1.6.0.linux-amd64.tar.gz
tar xvfz node_exporter*
ln -s /data/download/node_exporter-1.6.0.linux-amd64/node_exporter /usr/bin/node_exporter
# 啟動node_exporter,伺服器暴露的埠是8080,同時伺服器里有其他服務占用了8080埠,可以使用nginx將node_exporter獲取指標的api暴露出去
# location /metrics {
# proxy_pass http://127.0.0.1:9000/metrics;
# }
node_exporter --web.listen-address 127.0.0.1:9000 >>/data/app_logs/node_exporter.log 2>&1 &
# 添加node_exporter之後,需要更新prometheus.xml添加targets,然後運行:curl -X PUT http://server_address:port/-/reload重新載入配置文件

#
alert_manager可以和prometheus安裝到同一臺伺服器
cd /data/download
wget https://github.com/prometheus/alertmanager/releases/download/v0.25.0/alertmanager-0.25.0.linux-amd64.tar.gz
tar xvfz alertmanager*
ln -s /data/download/alertmanager-0.25.0.linux-amd64/alertmanager /usr/bin/alertmanager
cp /data/download/alertmanager-0.25.0.linux-amd64/alertmanager.yml /data/app/prometheus/alertmanager.yml
alertmanager --config.file=/data/app/prometheus/alertmanager.yml --web.listen-address 127.0.0.1:9001 >>/data/app_logs/node_exporter.log 2>&1 &
# 將alert_manager的地址添加到prometheus.yml里的alertmanagers的targets里,然後運行:curl -X PUT http://server_address:port/-/reload重新載入配置文件

測試報警郵件功能:設置如果安裝exporter的伺服器記憶體占用率超過50%或者tcp timewait超過10的時候就發郵件(在實際工作中需要設置一個合適的條件):

prometheus.yml里添加rule_files的路徑:

# my global config
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:
          - 127.0.0.1:9001

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"
  - "/data/app/prometheus/alert.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'.
    scrape_interval: 5s

    static_configs:
            - targets: ["node1_ip:8080"]
            - targets: ["node2_ip:8080"]
              labels:
                groups: 'container'

alert.rules.yml里添加具體的rule,node_socket_TCP_tw這些具體的指標通過http://node_exporter_ip:port/metrics可以獲取到

groups:
- name: tcp-alert-group
  rules:
  - alert: TcpTimeWait
    expr: node_sockstat_TCP_tw > 10
    for: 10m
    labels:
      severity: warning
    annotations:
      summary: tcp time wait more than 10
      description: please check node_sockstat_TCP_tw metric
  - alert: MemoryUse
    expr: (node_memory_MemTotal_bytes-node_memory_MemFree_bytes-node_memory_Buffers_bytes-node_memory_Cached_bytes)/node_memory_MemTotal_bytes > 0.5
    for: 10m
    labels:
      severity: warning
    annotations:
      summary: memory use more than 50% for 10 min
      description: please check memory use

alertmanager.yml里配置告警郵件的信息:

global:
  resolve_timeout: 5m
  smtp_smarthost: your_smpt_host:port
  smtp_from: alertmanager@your_email_domain
  smtp_require_tls: false
route:
  group_by: ['alertname']
  group_wait: 30s
  group_interval: 5m
  repeat_interval: 10m
  receiver: 'email'
receivers:
  - name: 'email'
    email_configs:
    - to: 'receiver_email'
      send_resolved: true

yml文件一旦更新,需要重新載入配置:curl -X PUT http://server_address:port/-/reload

在Prometheus的界面可以看到添加的alert:

 當alert的條件滿足後,alertmanager就會發郵件

 

grafana的安裝和啟動:

# grafana可以和prometheus里安裝到同一臺伺服器
yum install -y https://dl.grafana.com/enterprise/release/grafana-enterprise-10.0.0-1.x86_64.rpm
# grafana預設啟動的埠號是3000,如果伺服器沒有暴露3000埠的話,需要修改grafana的配置文件
sed -i 's/3000/8080/g' /usr/share/grafana/conf/defaults.ini
grafana server >> /data/app_logs/grafana.log 2>&1 &
# grafana數據保存地址:/var/lib/grafana.db

grafana啟動之後就可以在瀏覽器上打開對應的地址,初次登錄用戶名和密碼:admin/admin

Data sources里添加prometheus,grafana和prometheus啟動在同一臺伺服器里的話,地址就可以用localhost

 添加dashboard,在Explore里可以查詢指標並且添加到dashboard

cpu使用率:avg(1-irate(node_cpu_seconds_total{mode="idle"}[1m])) by(instance)

記憶體使用率:(node_memory_MemTotal_bytes-node_memory_MemFree_bytes-node_memory_Buffers_bytes-node_memory_Cached_bytes)/node_memory_MemTotal_bytes

tcp連接數:node_sockstat_TCP_alloc

 dashboard:

 

 

註意點:

1.prometheus啟動的時候添加--web.enable-lifecycle才允許通過調用/-/reload介面重新載入配置文件
2.prometheus啟動的時候指定一個固定的數據存放位置--storage.tsdb.path=/data/app/prometheus/data,如果數據存放位置不一致,啟動後查不到歷史數據,歷史數據做備份的話,prometheus啟動的伺服器還可以變更
3.grafana的數據保存地址:/var/lib/grafana.db,定期做備份,伺服器發生系統錯誤無法使用的時候,在新的伺服器里同步/var/lib/grafana.db文件之後,啟動grafana之前的配置不會丟失

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 以WebMvcAutoConfiguration自動配置的原理為例,SpringBoot內部對大量的第三方庫或Spring內部庫進行了預設配置,這些配置是否生效,取決於我們是否引入了對應庫所需的依賴,如果有那麼預設配置就會生效。如果引入springboot-starter-web那麼對應的web配置 ...
  • `NumPy`(Numerical Python)是一個`Python`庫,主要用於高效地處理多維數組和矩陣計算。它是科學計算領域中使用最廣泛的一個庫。 在`NumPy`中,**數組**是最核心的概念,用於存儲和操作數據。 `NumPy`數組是一種多維數組對象,可以存儲相同類型的元素,它支持高效的數 ...
  • 一、簡介 官網: https://spring.io/projects/spring-framework#overview 官方下載工具: https://repo.spring.io/release/org/springframework/spring/ github下載: https://git ...
  • # 頁面預覽 ## 預約掛號 - 根據預約周期,展示可預約日期,根據有號、無號、約滿等狀態展示不同顏色,以示區分 - 可預約最後一個日期為即將放號日期 - 選擇一個日期展示當天可預約列表 ![image-20230227202834422](https://s2.loli.net/2023/06/1 ...
  • # 一、緒論 ## 1.1 基本概念 1. 加速比:表示加速效果。單個處理器運行花費時間 / P個處理器運行花費時間;$S=\frac{T(1)}{T(p)}$ 2. 效率:$E = \frac{S}{p} = \frac{T(1)}{T(p)\times p}$ 3. 開銷:$C=T(p)\tim ...
  • 1、簡介 SiftingAppender 可根據給定的運行時屬性將日誌分離或篩選。例如,SiftingAppender 可以根據用戶會話將日誌事件分開,以便不同用戶生成的日誌進入不同的日誌文件,每個用戶一個日誌文件。SiftingAppender 有兩個屬性: timeout SiftingAppe ...
  • 這一次咱們來探究一下怎麼用純代碼寫 WPF 模板。模板有個共同基類 FrameworkTemplate,數據模板、控制項模板等是從此類派生的,因此,該類已定義了一些通用成員。 用代碼構建模板,重要的成員是 VisualTree 屬性,它的類型是 FrameworkElementFactory。可見,模 ...
  • ## 前言 使用 ABP vNext(下文簡稱 ABP)時,通常都是從 cli 開始新建模板,從一個空項目開始。對已經存續的項目來說,現有的數據,特別是用戶等核心數據需要進行遷移。 老的項目,隨著規模越來越大,每次修改都需要更改非常多地方,最重要的是,共用資料庫使得維護起來需要小心翼翼。為了後續維護 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...