天天看點

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

Prometheus

安裝 Prometheus

Promethwus下載下傳位址

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

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

執行:

./prometheus           

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

配置 監控服務

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

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

這裡,我們重新啟動一下 Prometheus 服務,為了驗證一下,我們需要在浏覽器中輸入位址:

http://192.168.1.111:9090/

然後我們看到的是這個:

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

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

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

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

配置 遠端存儲

這裡我們要将資料寫入到 Cortex 中 ,這裡,我們是将 Cortex 作為 Prometheus 的一個遠端存儲點。是以我們需要在 Prometheus 配置中針對這個 遠端存儲進行配置。

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

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

remote_write:
- url: http://localhost:9009/api/prom/push           

啟動 Prometheus

Cortex

安裝 Cortex

這裡安裝 Cortex ,主要看個人的網速。

首先,我們需要先克隆 cortex 下來:

git clone https://github.com/cortexproject/cortex.git           

進入 cortex 的根目錄:

go build ./cmd/cortex           

這裡,隻要不出問題,那就沒問題。出了問題,就隻好跟着錯誤資訊進行排除了。

啟動 Cortex

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

Grafana

安裝 Grafana

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

Ubuntu & Debian

sudo apt-get install -y adduser libfontconfig1

wget https://dl.grafana.com/oss/release/grafana_6.6.2_amd64.deb

sudo dpkg -i grafana_6.6.2_amd64.deb           

Standalone Linux Binaries

wget https://dl.grafana.com/oss/release/grafana-6.6.2.linux-amd64.tar.gz

tar -zxvf grafana-6.6.2.linux-amd64.tar.gz           

Redhat & Centos

wget https://dl.grafana.com/oss/release/grafana-6.6.2-1.x86_64.rpm

sudo yum localinstall grafana-6.6.2-1.x86_64.rpm           

配置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" 是 wmi_cpu_time_total , 這個大家可以随意 , 大家可以直接填 "up" . 我這裡的是對我的 Windows 系統做的監控。然後,我們儲存:

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

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

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

這裡,我為了友善檢視 Cortex 和 Prometheus , 我把監控放在了一起。

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

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

繼續閱讀