首頁>技術>

Prometheus的本地儲存設計可以減少其自身運維和管理的複雜度,同時能夠滿足大部分使用者監控規模的需求。但是本地儲存也意味著Prometheus無法持久化資料,無法儲存大量歷史資料,同時也無法靈活擴充套件和遷移。

為了保持Prometheus的簡單性,Prometheus並沒有嘗試在自身中解決以上問題,而是透過定義兩個標準介面(remote_write/remote_read),讓使用者可以基於這兩個介面對接將資料儲存到任意第三方的儲存服務中,這種方式在Promthues中稱為Remote Storage。

Remote Write

使用者可以在Prometheus配置檔案中指定Remote Write(遠端寫)的URL地址,一旦設定了該配置項,Prometheus將採集到的樣本資料透過HTTP的形式傳送給介面卡(Adaptor)。而使用者則可以在介面卡中對接外部任意的服務。外部服務可以是真正的儲存系統,公有云的儲存服務,也可以是訊息佇列等任意形式。

remote write

Remote Read

如下圖所示,Promthues的Remote Read(遠端讀)也通過了一個介面卡實現。在遠端讀的流程當中,當用戶發起查詢請求後,Promthues將向remote_read中配置的URL發起查詢請求(matchers,ranges),Adaptor根據請求條件從第三方儲存服務中獲取響應的資料。同時將資料轉換為Promthues的原始樣本資料返回給Prometheus Server。

當獲取到樣本資料後,Promthues在本地使用PromQL對樣本資料進行二次處理。

remote write

注意:啟用遠端讀設定後,只在資料查詢時有效,對於規則檔案的處理,以及Metadata API的處理都只基於Prometheus本地儲存完成。

目前Prometheus社群也提供了部分對於第三方資料庫的Remote Storage支援:

儲存服務                 支援模式AppOptics              write Chronix                   write Cortex                     read/write CrateDB                  read/write Gnocchi                  write Graphite                 write InfluxDB                 read/write OpenTSDB             write PostgreSQL/TimescaleDB  read/write SignalFx                  write

這裡將介紹如何使用Influxdb作為Prometheus的Remote Storage,從而確保當Prometheus發生宕機或者重啟之後能夠從Influxdb中恢復和獲取歷史資料。

1.準備influxdb container
1.安裝docker安裝docker tools# yum install yum-utils device-mapper-persistent-data lvm2 -y# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo# yum list docker-ce --showduplicates | sort -r安裝docker-ce# yum install docker-ce-19.03.9-3.el7 -y修改docker映象加速器# mkdir /etc/docker# vi  /etc/docker/daemon.json{  "exec-opts": ["native.cgroupdriver=systemd"],  "registry-mirrors": ["https://3ca84f1l.mirror.aliyuncs.com"]         }備註:阿里雲docker映象加速器:開啟網址:https://cr.console.aliyun.com/#/accelerator      註冊、登入、設定密碼,左下角,然後在頁面上可以看到加速器地址,類似於:https://123abc.mirror.aliyuncs.com# systemctl  daemon-reload# systemctl  enable docker  # systemctl  start  docker # docker info  2.安裝docker-compose# sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose# chmod +x /usr/local/bin/docker-compose# docker-compose -vdocker-compose version 1.27.4, build 40524192 最新發行的版本地址:https://github.com/docker/compose/releases3.建立docker-compose.ymlversion: '2'services:  influxdb:    image: influxdb:1.3.5    command: -config /etc/influxdb/influxdb.conf    ports:      - "8086:8086"    environment:      - INFLUXDB_DB=prometheus      - INFLUXDB_ADMIN_ENABLED=true      - INFLUXDB_ADMIN_USER=admin      - INFLUXDB_ADMIN_PASSWORD=admin      - INFLUXDB_USER=prom      - INFLUXDB_USER_PASSWORD=prom4.啟動influxdb服務# docker-compose up -d# #  docker psCONTAINER ID   IMAGE            COMMAND                  CREATED         STATUS         PORTS                    NAMESac81ef4f221a   influxdb:1.3.5   "/entrypoint.sh -con…"   7 seconds ago   Up 5 seconds   0.0.0.0:8086->8086/tcp   tmp_influxdb_1到這裡influxdb容器已經執行成功,對映埠為8086
2.準備go語言環境和編譯原始碼remote_storage_adapter
1. 下載golang安裝包# wget https://golang.org/doc/install?download=go1.15.7.linux-amd64.tar.gz 網路原因可能下不下來,用下面的這個地址進行下載wget https://dl.google.com/go/go1.15.7.linux-amd64.tar.gz解壓# tar -C /usr/local -xzf go1.15.7.linux-amd64.tar.gz設定環境變數 ~/.bash_profile ,新增go目錄vi ~/.bash_profilePATH=$PATH:$HOME/bin:/usr/local/go/binexport PATHsource ~/.bash_profile臨時新增:# export PATH=$PATH:/usr/local/go/bin2.安裝git# yum install git -y3.獲取remote_storage_adapter原始碼,並編譯獲取並啟動Prometheus提供的Remote Storage Adapter:# git clone https://github.com/prometheus/prometheus.git# cd prometheus/documentation/examples/remote_storage/remote_storage_adapter# go build會生成一個執行檔案 remote_storage_adapter,這個就是連線influxdb,並暴露埠的程式# chmod +x /usr/local/go/bin/remote_storage_adapter# cp remote_storage_adapter /usr/local/go/bin/4. 用remote_storage_adapter連線influxdb# ./remote_storage_adapter --influxdb-url=http://localhost:8086/ --influxdb.database=prometheus --influxdb.retention-policy=autogen參考地址 https://github.com/prometheus/prometheus/tree/master/documentation/examples/remote_storage/remote_storage_adapter5.啟動remote_storage_adapter並且設定Influxdb相關的認證資訊nohup /usr/local/go/bin/remote_storage_adapter --influxdb-url=http://localhost:8086/ --influxdb.username=prom --influxdb.database=prometheus --influxdb.retention-policy=autogen &到這裡remote_storage_adapter可以連線influxdb,並暴露埠9201, Prometheus就暴露的API,進行資料的寫入和讀取。相當於influxdb + adapter 變成了一個exporter。
3.修改prometheus配置檔案/etc/prometheus/prometheus.yml
/etc/prometheus/prometheus.yml新增如下配置remote_write:  - url: "http://localhost:9201/write"remote_read:  - url: "http://localhost:9201/read"

重新啟動Prometheus能夠獲取資料後,登入到influxdb容器,並驗證資料寫入。如下所示,當資料能夠正常寫入Influxdb後可以看到Prometheus相關的指標。

# systemctl restart prometheus
4.登陸influxdb檢視資料表
# docker exec -it ac81ef4f221a influxConnected to http://localhost:8086 version 1.3.5InfluxDB shell version: 1.3.5> authusername: promUnable to process input: liner: function not supported in this terminal> promERR: error parsing query: found prom, expected SELECT, DELETE, SHOW, CREATE, DROP, GRANT, REVOKE, ALTER, SET, KILL at line 1, char 1Warning: It is possible this error is due to not setting a database.Please set a database with the command "use <database>".> use prometheusUsing database prometheus> SHOW MEASUREMENTSname: measurementsname----ALERTSALERTS_FOR_STATEgo_gc_duration_secondsgo_gc_duration_seconds_countgo_gc_duration_seconds_sumgo_goroutinesgo_infogo_memstats_alloc_bytesgo_memstats_alloc_bytes_totalgo_memstats_buck_hash_sys_bytesgo_memstats_frees_totalgo_memstats_gc_cpu_fractiongo_memstats_gc_sys_bytes檢視有很多go_ 開頭的表,說明Prometheus資料已經寫入
5.測試遠端儲存

當資料寫入成功後,停止Prometheus服務。同時刪除Prometheus的data目錄,模擬Promthues資料丟失的情況後重啟Prometheus。開啟Prometheus UI如果配置正常,Prometheus可以正常查詢到本地儲存已刪除的歷史資料記錄。

/var/lib/prometheus/ 為Prometheus資料目錄# ps -ef|grep promeprometh+ 19364     1 35 04:25 ?        00:04:55 /usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/ --web.console.templates=/etc/prometheus/consoles --web.console.libraries=/etc/prometheus/console_libraries# cd /var/lib/prometheus/# ls -l total 20drwxr-xr-x 3 prometheus prometheus    64 Jan 22 05:00 01EWM7DA4T0D5K94MQ84YM897Kdrwxr-xr-x 3 prometheus prometheus    64 Jan 23 07:00 01EWQ0NRKQR24J05P8N1BF5K3Hdrwxr-xr-x 3 prometheus prometheus    64 Jan 23 12:16 01EWQJS3VEKN6B16DB1DYBNR5Qdrwxr-xr-x 3 prometheus prometheus    64 Jan 24 02:51 01EWS4VB6MC2CSHEJYP83CTEC8drwxr-xr-x 3 prometheus prometheus    64 Jan 24 02:51 01EWS4VBV2DK4W77KW1T2Y7AFGdrwxr-xr-x 2 prometheus prometheus    45 Jan 24 04:25 chunks_head-rw-r--r-- 1 prometheus prometheus     0 Jan 21 09:55 lock-rw-r--r-- 1 prometheus prometheus 20001 Jan 24 04:40 queries.activedrwxr-xr-x 3 prometheus prometheus    77 Jan 24 04:25 wal# systemctl stop prometheus# rm -fr /var/lib/prometheus/01EW*# systemctl start prometheus

開啟Prometheus UI http://192.168.0.107:9090/ , 仍然可以看到如下資料,這裡資料就是從influxdb 中讀取的。說明,遠端儲存Prometheus資料完成。

prometheus資料還在

14
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • LeetCode 989. 陣列形式的整數加法