我們都知道,Prometheus 是一個監控和可觀察性的一個標準解決方案。這裏呢,我也不準備去介紹 Prometheus 是什麼,我們直接切入到我們主題。我們怎麼使用 Cortex 實現Prometheus 的多租戶的管理的一個平臺的搭建,下一章,主要講我們怎麼使用 API 去實現對 Cortex 的管理控制。
Prometheus
安裝 Prometheus
首先我們下載好我們的 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/
然後我們看到的是這個:
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-2Bcdxvfy-1591760607464)(http://www.source.sun-iot.xyz/prometheus/1.png)]
按照圖中的紅色框框,我們可以看到的是這個:
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-FDvdKT1Y-1591760607466)(http://www.source.sun-iot.xyz/prometheus/2.png)]
我們看最後一個,最後一個是我配置的 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 .
這裏我給大家看一下這兩個數據源的配置後:
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-zKoewLvC-1591760607473)(http://www.source.sun-iot.xyz/prometheus/4.png)]
這裏特別注意: cortex 中的 URL : “http://192.168.1.111:9009/api/prom”
我們在配置好數據源後,就開始添加我們的 DashBoard .
這裏可以看到,我們的Prometheus , Cortex 添加的 DashBoard 都是有東西的,雖然我們的這個DashBoard 還是初始化的狀態。但這說明,我們的DashBoard 是正常的。現在,我們就要來配置我們的 Query .
配置 Query
首先配置我們的 Prometheus-Dashboard .
我們選擇 “Edit”
按照我的配置來:
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-8taqVRgd-1591760607479)(http://www.source.sun-iot.xyz/prometheus/8.png)]
這裏我選擇的 “metrics” 是 wmi_cpu_time_total , 這個大家可以隨意 , 大家可以直接填 “up” . 我這裏的是對我的 Windows 系統做的監控。然後,我們保存:
配置完Prometheus ,我們配置我們的 Cortex-Dashboard
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-VYleuUxg-1591760607481)(http://www.source.sun-iot.xyz/prometheus/10.png)]
這裏,我爲了方便查看 Cortex 和 Prometheus , 我把監控放在了一起。
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-1h0eIDJS-1591760607482)(http://www.source.sun-iot.xyz/prometheus/11.png)]
兩邊都有了數據,說明我們的配置成功。