はじめに
Cortexについて簡単に紹介して、実際にローカルで動かしてみます。
Cortexとは
Cortexとは、Prometheusのための可用性が高い長期間ストレージです。
Prometheusのローカルストレージは長期保存には向かないため、長期間保存したい場合は、remote write APIを使ってCortexなどに保存する必要があります。
Cortexは下記のような特徴があります。
- 水平スケール可能
- 高可用性
- Prometheusのマルチテナント対応
- 長期保存
アーキテクチャ
Cortexは複数のマイクロサービスから構成され、アーキテクチャは下記のようになっています。
Cortexのドキュメントより
各サービス
各サービスをざっくり紹介します。
Distributor
: Prometheusからのデータを処理Ingester
: データを一時的に保持し長期保存ストレージに書き込むQuerier
: PromQLを処理Compactor
: ストレージのブロックをコンパクト化Store gateway
: ブロックストレージにクエリを問い合わせるAlertmanager
(optional): アラートを通知(PrometheusのAlertmanagerをベースに構築)Configs API
(optional): RulerとAlertmanagerの設定を管理Overrides exporter
(optional): Prometheusメトリクスを公開Query frontend
(optional): QuerierのAPIのエンドポイントQuery scheduler
(optional): Query frontendから内部キューを移動Ruler
(optional): ルールやアラートを記録するPromQLを実行
Docker composeで試してみる
実際にDocker composeを使ってローカルで動かてみます。
作成するファイルは下記の通りです。
.
├── docker-compose.yml
├── prometheus.yml
└── single-process-config-blocks-local.yaml
docker-compose.yml
docker-compose.yml
は下記の通りです。Prometheus, Grafana, Node exporterそしてCortexのコンテナを起動するようにしています。
version: '3'
services:
prometheus:
image: prom/prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
ports:
- '9090:9090'
grafana:
image: grafana/grafana
ports:
- "3000:3000"
node-exporter:
image: quay.io/prometheus/node-exporter
ports:
- 9100:9100
volumes:
- ./proc:/host/proc
- ./sys:/host/sys
- ./rootfs:/rootfs
cortex:
image: cortexproject/cortex:master-c0e4545
command: -config.file=/etc/single-process-config-blocks-local.yaml
ports:
- "9009:9009"
volumes:
- "./single-process-config-blocks-local.yaml:/etc/single-process-config-blocks-local.yaml"
Prometheusの設定
Prometheusの設定ファイル(prometheus.yml
)を作成します。
下記の通りNode exporterからメトリクスを取得して、Cortexにremote writeで書き込むようにしています。
global:
scrape_interval: 15s
external_labels:
monitor: 'codelab-monitor'
scrape_configs:
- job_name: 'node'
scrape_interval: 5s
static_configs:
- targets: ['{node-exporter container name}:9100']
remote_write:
- url: http://{cortex container name}:9009/api/v1/push
Cortexの設定
Cortexの設定ファイルを作成します。
作成する内容は、公式が用意しているsingle-process-config-blocks-local.yaml
を使用します。
auth_enabled: false
server:
http_listen_port: 9009
grpc_server_max_recv_msg_size: 104857600
grpc_server_max_send_msg_size: 104857600
grpc_server_max_concurrent_streams: 1000
distributor:
shard_by_all_labels: true
pool:
health_check_ingesters: true
ingester_client:
grpc_client_config:
max_recv_msg_size: 104857600
max_send_msg_size: 104857600
grpc_compression: gzip
ingester:
lifecycler:
min_ready_duration: 0s
final_sleep: 0s
num_tokens: 512
ring:
kvstore:
store: inmemory
replication_factor: 1
blocks_storage:
tsdb:
dir: /tmp/cortex/tsdb
bucket_store:
sync_dir: /tmp/cortex/tsdb-sync
backend: filesystem # s3, gcs, azure or filesystem are valid options
filesystem:
dir: ./data/tsdb
compactor:
data_dir: /tmp/cortex/compactor
sharding_ring:
kvstore:
store: inmemory
frontend_worker:
match_max_concurrent: true
ruler:
enable_api: true
ruler_storage:
backend: local
local:
directory: /tmp/cortex/rules
コンテナの起動
Docker composeでコンテナを起動します。
docker compose up -d
動作確認
Prometheusのコンテナのログを確認すると、Cortexに書き込んでいることがわかります。
❯ docker container logs prometheus-grafana-cortex-prometheus-1
ts=2023-01-10T12:48:58.381Z caller=main.go:491 level=info msg="No time or size retention was set so using the default time retention" duration=15d
ts=2023-01-10T12:48:58.381Z caller=main.go:535 level=info msg="Starting Prometheus Server" mode=server version="(version=2.37.1, branch=HEAD, revision=1ce2197e7f9e95089bfb95cb61762b5a89a8c0da)"
ts=2023-01-10T12:48:58.381Z caller=main.go:540 level=info build_context="(go=go1.18.6, user=root@3caaaea7ba87, date=20220912-12:42:39)"
ts=2023-01-10T12:48:58.381Z caller=main.go:541 level=info host_details="(Linux 5.15.49-linuxkit #1 SMP Tue Sep 13 07:51:46 UTC 2022 x86_64 92d5e53c4e7f (none))"
ts=2023-01-10T12:48:58.381Z caller=main.go:542 level=info fd_limits="(soft=1048576, hard=1048576)"
ts=2023-01-10T12:48:58.381Z caller=main.go:543 level=info vm_limits="(soft=unlimited, hard=unlimited)"
ts=2023-01-10T12:48:58.385Z caller=web.go:553 level=info component=web msg="Start listening for connections" address=0.0.0.0:9090
ts=2023-01-10T12:48:58.392Z caller=main.go:972 level=info msg="Starting TSDB ..."
ts=2023-01-10T12:48:58.394Z caller=tls_config.go:195 level=info component=web msg="TLS is disabled." http2=false
ts=2023-01-10T12:48:58.429Z caller=head.go:493 level=info component=tsdb msg="Replaying on-disk memory mappable chunks if any"
ts=2023-01-10T12:48:58.430Z caller=head.go:536 level=info component=tsdb msg="On-disk memory mappable chunks replay completed" duration=5.956µs
ts=2023-01-10T12:48:58.430Z caller=head.go:542 level=info component=tsdb msg="Replaying WAL, this may take a while"
ts=2023-01-10T12:48:58.431Z caller=head.go:613 level=info component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
ts=2023-01-10T12:48:58.431Z caller=head.go:619 level=info component=tsdb msg="WAL replay completed" checkpoint_replay_duration=48.061µs wal_replay_duration=1.325947ms total_replay_duration=1.410066ms
ts=2023-01-10T12:48:58.435Z caller=main.go:993 level=info fs_type=EXT4_SUPER_MAGIC
ts=2023-01-10T12:48:58.435Z caller=main.go:996 level=info msg="TSDB started"
ts=2023-01-10T12:48:58.435Z caller=main.go:1177 level=info msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
ts=2023-01-10T12:48:58.448Z caller=dedupe.go:112 component=remote level=info remote_name=752f9f url=http://prometheus-grafana-cortex-cortex-1:9009/api/v1/push msg="Starting WAL watcher" queue=752f9f
ts=2023-01-10T12:48:58.449Z caller=dedupe.go:112 component=remote level=info remote_name=752f9f url=http://prometheus-grafana-cortex-cortex-1:9009/api/v1/push msg="Starting scraped metadata watcher"
ts=2023-01-10T12:48:58.449Z caller=dedupe.go:112 component=remote level=info remote_name=752f9f url=http://prometheus-grafana-cortex-cortex-1:9009/api/v1/push msg="Replaying WAL" queue=752f9f
ts=2023-01-10T12:48:58.456Z caller=main.go:1214 level=info msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=20.453165ms db_storage=2.472µs remote_storage=3.700617ms web_handler=1.128µs query_engine=2.292µs scrape=6.012476ms scrape_sd=55.72µs notify=2.065µs notify_sd=2.781µs rules=2.567µs tracing=10.343µs
ts=2023-01-10T12:48:58.457Z caller=main.go:957 level=info msg="Server is ready to receive web requests."
ts=2023-01-10T12:48:58.458Z caller=manager.go:941 level=info component="rule manager" msg="Starting rule manager..."
ts=2023-01-10T12:49:05.042Z caller=dedupe.go:112 component=remote level=info remote_name=752f9f url=http://prometheus-grafana-cortex-cortex-1:9009/api/v1/push msg="Done replaying WAL" duration=6.593108321s
Grafanaからhttp://{cortex container name}:9009/prometheus
を指定することで、Cortex用のDataa Sourceを作成することができます。