cortex:一個支持多租戶、水平擴展的prometheus服務。前端
當時調研cortex實際上是由於看到了Weave Cloud這個商業產品中的監控模塊介紹,weave也叫weave works,官方地址是:https://cloud.weave.works,是一個專一於容器微服務的paas平臺。linux
WeaveCloud在監控模塊最大化利用了Prometheus,並在其基礎上添加了不少組件,實現了多租戶管理、高可用的監控集羣。其使用的核心監控組件就是cortex。nginx
本文主要分享的是cortex的運行機制,關於Weave Cloud的產品定位和功能能夠看下後續的文章:[商業方案-weave work]()git
Cortex是一個CNCF的沙盒項目,目前被幾個線上產品使用:Weave Cloud、GrafanaCloud和FreshTracks.iogithub
爲何不直接運行Prometheus,而用Cortex?shell
ps:來自cortex kubecon大會演講json
針對以上需求,Cortex提供的主要功能或特點以下:bootstrap
類似的競品:api
ps:來自weave work上試用監控模塊時的截圖緩存
在k8s集羣中部署所須要的yaml列表爲:
[https://github.com/weaveworks...](https://github.com/weaveworks...
)
部署的agent時的腳本內容是:
#!/bin/sh set -e # Create a temporary file for the bootstrap binary TMPFILE="$(mktemp -qt weave_bootstrap.XXXXXXXXXX)" || exit 1 finish(){ # Send only when this script errors out # Filter out the bootstrap errors if [ $? -ne 111 ] && [ $? -ne 0 ]; then curl -s >/dev/null 2>/dev/null -H "Accept: application/json" -H "Authorization: Bearer $token" -X POST -d \ '{"type": "onboarding_failed", "messages": {"browser": { "type": "onboarding_failed", "text": "Installation of Weave Cloud agents did not finish."}}}' \ https://cloud.weave.works/api/notification/external/events || true fi # Arrange for the bootstrap binary to be deleted rm -f "$TMPFILE" } # Call finish function on exit trap finish EXIT # Parse command-line arguments for arg in "$@"; do case $arg in --token=*) token=$(echo $arg | cut -d '=' -f 2) ;; esac done if [ -z "$token" ]; then echo "error: please specify the instance token with --token=<TOKEN>" exit 1 fi # Notify installation has started curl -s >/dev/null 2>/dev/null -H "Accept: application/json" -H "Authorization: Bearer $token" -X POST -d \ '{"type": "onboarding_started", "messages": {"browser": { "type": "onboarding_started", "text": "Installation of Weave Cloud agents has started"}}}' \ https://cloud.weave.works/api/notification/external/events || true # Get distribution unamestr=$(uname) if [ "$unamestr" = 'Darwin' ]; then dist='darwin' elif [ "$unamestr" = 'Linux' ]; then dist='linux' else echo "This OS is not supported" exit 1 fi # Download the bootstrap binary echo "Downloading the Weave Cloud installer... " curl -Ls "https://get.weave.works/bootstrap?dist=$dist" >> "$TMPFILE" # Make the bootstrap binary executable chmod +x "$TMPFILE" # Execute the bootstrap binary "$TMPFILE" "--scheme=https" "--wc.launcher=get.weave.works" "--wc.hostname=cloud.weave.works" "--report-errors" "$@"
Cortex與Prometheus的交互圖:
原理圖:
Cortex中各組件的做用:
Cortex由多個可水平擴展的微服務組成。每一個微服務使用最合適的技術進行水平縮放; 大多數是無狀態的,而有些(即Retrieval)是半有狀態的而且依賴於一致性哈希
Prometheus實例從各類目標中抓取樣本,而後將它們推送到Cortex(使用Prometheus的遠程寫入API),並對發送的Protocol Buffers序列化數據進行Snappy壓縮。
Cortex要求每一個HTTP請求都帶有一個header,用於指定請求的租戶ID。請求身份驗證和受權由外部反向代理處理。
傳入的樣本(來自Prometheus的寫入)由Distributor處理,而傳入的讀取(PromQL查詢)由查詢前端處理。
查詢緩存:
查詢時會緩存存查詢結果,並在後續查詢中複用它們。若是緩存的結果不完整,則查詢前端計算所需的子查詢並在下游查詢器上並行執行它們。
併發查詢:
查詢做業接受來自查詢器的gRPC流請求,爲了實現高可用性,建議您運行多個前端,且前端數量少於查詢器數量。在大多數狀況下,兩個應該足夠了。
本文爲容器監控實踐系列文章,完整內容見:container-monitor-book