使用 Cortex 實現 Prometheus 的多租戶管理

我們都知道,Prometheus 是一個監控和可觀察性的一個標準解決方案。這裡呢,我也不準備去介紹 Prometheus 是什麼,我們直接切入到我們主題。我們怎麼使用 Cortex 實現Prometheus 的多租戶的管理的一個平臺的搭建,下一章,主要講我們怎麼使用 API 去實現對 Cortex 的管理控制。

Prometheus


安裝 Prometheus

https://prometheus.io/download/

首先我們下載好我們的 Prometheus 之後,老夫下載的是: prometheus-2.13.0.linux-amd64.tar.gz . 解壓到指定的目錄:

<code>tar -zxvf prometheus-2.13.0.linux-amd64.tar.gz -C /opt/module/<code>

執行:

<code>./prometheus/<code>

這樣,一個簡單的 Prometheus 服務就啟動了。

配置 監控服務

這裡,當我們需要監控某一個服務時,我們需要在配置文件中進行一下配置。這裡我們監控一下:

<code>scrape_configs:  # The job name is added as a label `job=` to any timeseries scraped from this config.  - job_name: 'prometheus'    static_configs:    - targets: ['192.168.1.111:9090']      labels:        instance: prometheus/<code>

這裡,我們重新啟動一下 Prometheus 服務,為了驗證一下,我們需要在瀏覽器中輸入地址: http://192.168.1.111:9090/

然後我們看到的是這個:

使用 Cortex 實現 Prometheus 的多租戶管理

按照圖中的紅色框框,我們可以看到的是這個:

使用 Cortex 實現 Prometheus 的多租戶管理

我們看最後一個,最後一個是我配置的 Prometheus 的配置。 他的 state 是 up , 表示當前的監控是 OK 的。上面的三個暫時不管,我有 exporter 沒有啟動。看到 up 之後,就表示我們的這個服務是正常的。


配置 遠程存儲

這裡我們要將數據寫入到 Cortex 中 ,這裡,我們是將 Cortex 作為 Prometheus 的一個遠程存儲點。所以我們需要在 Prometheus 配置中針對這個 遠程存儲進行配置。

<code>remote_write:  - url: http://192.168.50.178:8088/api/prom/push    basic_auth:      username: "sunyang_2"      password: "sunyang"/<code>

這裡,我們的這個 basic_auth 會在請求的 Header 裡面添加 Authorization 。 這裡的url 是我們的 cortex 服務的地址,可以是你自己的的代理地址,也可以是 cortex 原地址。下面是 cortex 的地址。

<code>remote_write:- url: http://localhost:9009/api/prom/push/<code>


啟動 Prometheus


Cortex


安裝 Cortex

這裡安裝 Cortex ,主要看個人的網速。首先,我們需要先克隆 cortex 下來:

<code>git clone https://github.com/cortexproject/cortex.git/<code>

進入 cortex 的根目錄:

<code>go build ./cmd/cortex/<code>

這裡,只要不出問題,那就沒問題。出了問題,就只好跟著錯誤信息進行排除了。

啟動 Cortex


<code>./cortex -config.file=./docs/configuration/single-process-config.yaml/<code>


Grafana


安裝 Grafana

老夫這個安裝的是 Windows 版本的。 Linux 版本中,我們的安裝也很簡單:

Ubuntu & Debian

<code>sudo apt-get install -y adduser libfontconfig1wget https://dl.grafana.com/oss/release/grafana_6.6.2_amd64.debsudo dpkg -i grafana_6.6.2_amd64.deb/<code>

Standalone Linux Binaries

<code>wget https://dl.grafana.com/oss/release/grafana-6.6.2.linux-amd64.tar.gztar -zxvf grafana-6.6.2.linux-amd64.tar.gz/<code>

Redhat & Centos

<code>wget https://dl.grafana.com/oss/release/grafana-6.6.2-1.x86_64.rpmsudo yum localinstall grafana-6.6.2-1.x86_64.rpm/<code> 


配置DataSource

我們在 瀏覽器中 輸入 "http://ip:3000" , 默認的賬號密碼是 : admin / admin

我們進去之後,首先創建一個 數據源,這裡,我們需要創建的數據源是 Prometheus ,但是我們的真實地址在 Cortex 。現在他們的這個流程是這樣的:

使用 Cortex 實現 Prometheus 的多租戶管理

我們這裡添加兩個數據源,一個是 cortex , 一個是 prometheus .

這裡我給大家看一下這兩個數據源的配置後:

使用 Cortex 實現 Prometheus 的多租戶管理

這裡特別注意: cortex 中的 URL : "http://192.168.1.111:9009/api/prom"

我們在配置好數據源後,就開始添加我們的 DashBoard .

使用 Cortex 實現 Prometheus 的多租戶管理

使用 Cortex 實現 Prometheus 的多租戶管理

這裡可以看到,我們的Prometheus , Cortex 添加的 DashBoard 都是有東西的,雖然我們的這個DashBoard 還是初始化的狀態。但這說明,我們的DashBoard 是正常的。現在,我們就要來配置我們的 Query .

配置 Query

首先配置我們的 Prometheus-Dashboard .

我們選擇 "Edit"

使用 Cortex 實現 Prometheus 的多租戶管理

按照我的配置來:

使用 Cortex 實現 Prometheus 的多租戶管理

這裡我選擇的 "metrics" 是 wmicputime_total , 這個大家可以隨意 , 大家可以直接填 "up" . 我這裡的是對我的 Windows 系統做的監控。然後,我們保存:

使用 Cortex 實現 Prometheus 的多租戶管理

配置完Prometheus ,我們配置我們的 Cortex-Dashboard

使用 Cortex 實現 Prometheus 的多租戶管理

這裡,我為了方便查看 Cortex 和 Prometheus , 我把監控放在了一起。

使用 Cortex 實現 Prometheus 的多租戶管理

兩邊都有了數據,說明我們的配置成功。


分享到:


相關文章: