一.概述
cortex:一個支持多租戶、水平擴展的prometheus服務。
當時調研cortex其實是因為看到了Weave Cloud這個商業(yè)產(chǎn)品中的監(jiān)控模塊介紹,weave也叫weave works,官方地址是:https://cloud.weave.works,是一個專注于容器微服務的paas平臺。
WeaveCloud在監(jiān)控模塊最大化利用了Prometheus,并在其基礎上添加了很多組件,實現(xiàn)了多租戶管理、高可用的監(jiān)控集群。其使用的核心監(jiān)控組件就是cortex。
本文主要分享的是cortex的運行機制,關于Weave Cloud的產(chǎn)品定位和功能可以看下后續(xù)的文章:商業(yè)方案-weave work
Cortex是一個CNCF的沙盒項目,目前被幾個線上產(chǎn)品使用:Weave Cloud、GrafanaCloud和FreshTracks.io
為什么不直接運行Prometheus,而用Cortex?
ps:來自cortex kubecon大會演講
- 作為服務,cortex提供了鑒權和訪問控制
- 數(shù)據(jù)永久保留,狀態(tài)能夠被管理
- 提供持久化、高可用、伸縮性
- 提供更好的查詢效率,尤其是長查詢
二.主要功能
針對以上需求,Cortex提供的主要功能或特色如下:
- 支持多租戶:Prometheus本身沒有的租戶概念。這意味著,它無法對特定于租戶的數(shù)據(jù)訪問和資源使用配額,提供任何形式的細粒度控制。Cortex可以從多個獨立的prometheus實例中獲取數(shù)據(jù),并按照租戶管理。
- 長期存儲:基于遠程寫入機制,支持四種開箱即用的長期存儲系統(tǒng):AWS DynamoDB、AWS S3、Apache Cassandra和Google Cloud Bigtable。
- 全局視圖:提供所有prometheus server 整合后的時間序列數(shù)據(jù)的單一,一致的“全局”視圖。
- 高可用:提供服務實例的水平擴展、聯(lián)邦集群等
- 最大化利用了Prometheus
相似的競品:
- Prometheus + InfluxDB:使用InfluxData
- Prometheus + Thanos:長期存儲、全局視圖
- Timbala:多副本、全局視圖,作者是Matt Bostock
- M3DB:自動擴縮容,來自uber
產(chǎn)品形態(tài)
ps:來自weave work上試用監(jiān)控模塊時的截圖
- 1.安裝監(jiān)控的agent:

- 2.概覽視圖

- 3.資源監(jiān)控面板

- 4.監(jiān)控詳情頁面

- 5.添加監(jiān)控

- 6.配置報警


在k8s集群中部署所需要的yaml列表為:
https://github.com/weaveworks/cortex/tree/master/k8s
部署的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" "$@"
三.實現(xiàn)原理
Cortex與Prometheus的交互圖:

原理圖:

Cortex中各組件的作用:
Retrieval:采集組件,運行在用戶k8s集群上,從用戶應用中拉取監(jiān)控指標,并把這些數(shù)據(jù)推送給云平臺的服務
Frontend: 負載均衡/路由轉發(fā)/權限認證,接受Retrieval發(fā)送來的請求,這里用的nginx
Distributor:分發(fā)器,把用戶推送來的監(jiān)控指標,按照用戶id、指標名稱、標簽做一致性hash,然后并行交給后面的多個ingester處理(grpc交互)。是監(jiān)控數(shù)據(jù)寫入的第一站
Ingester:處理器,將監(jiān)控數(shù)據(jù)保存到promtheus中,高度定制了MemorySeriesStorage模塊,分塊存儲、寫入內存并索引(使用AWS的DynamoDB產(chǎn)品),最后寫入磁盤
讀寫分離:ingest和query分開為兩個服務
Cortex由多個可水平擴展的微服務組成。每個微服務使用最合適的技術進行水平縮放; 大多數(shù)是無狀態(tài)的,而有些(即Retrieval)是半有狀態(tài)的并且依賴于一致性哈希
Prometheus實例從各種目標中抓取樣本,然后將它們推送到Cortex(使用Prometheus的遠程寫入API),并對發(fā)送的Protocol Buffers序列化數(shù)據(jù)進行Snappy壓縮。
Cortex要求每個HTTP請求都帶有一個header,用于指定請求的租戶ID。請求身份驗證和授權由外部反向代理處理。
傳入的樣本(來自Prometheus的寫入)由Distributor處理,而傳入的讀?。≒romQL查詢)由查詢前端處理。
查詢緩存:
查詢時會緩存存查詢結果,并在后續(xù)查詢中復用它們。如果緩存的結果不完整,則查詢前端計算所需的子查詢并在下游查詢器上并行執(zhí)行它們。
并發(fā)查詢:
查詢作業(yè)接受來自查詢器的gRPC流請求,為了實現(xiàn)高可用性,建議您運行多個前端,且前端數(shù)量少于查詢器數(shù)量。在大多數(shù)情況下,兩個應該足夠了。

技術方案實現(xiàn)討論細節(jié):https://goo.gl/prdUYV
kubecon 大會講稿:https://kccna18.sched.com/event/GrXL/cortex-infinitely-scalable-prometheus-bryan-boreham-weaveworks
本文為容器監(jiān)控實踐系列文章,完整內容見:container-monitor-book