operator は Prometheus 互換のメトリクスと Kubernetes のヘルスプローブを公開しているため、リコンサイルの動作を観測し、処理が停滞したコントローラーを検出し、障害時にアラートを設定できます。
このガイドでは、operator が公開する内容、スクレイプ方法、そして日常的に役立つクエリについて説明します。
エンドポイント
operator のプロセスは、manager ポッド内で 2 つの HTTP エンドポイントを公開します。
| エンドポイント | デフォルトポート | パス | 用途 |
|---|---|---|---|
| メトリクス | 8080 (Helm) / 0 で無効 (バイナリのデフォルト) |
/metrics |
Prometheus エクスポジション形式 |
| ヘルスプローブ | 8081 |
/healthz, /readyz |
Kubernetes の liveness と readiness |
operator のバイナリを直接実行する場合、メトリクスエンドポイントはデフォルトで無効です (--metrics-bind-address=0)。Helm チャートでは、metrics.enable: true と metrics.port: 8080 によって有効になります。
ヘルスプローブエンドポイントは常に有効です。デプロイメントテンプレートでは、/healthz と /readyz がポート 8081 のポッドの liveness probe および readiness probe に割り当てられます。
Operator バイナリのフラグ
関連する manager フラグ (cmd/main.go で定義) :
| フラグ | デフォルト | 説明 |
|---|---|---|
--metrics-bind-address |
0 (無効) |
メトリクスエンドポイントのバインドアドレスです。HTTPS の場合は :8443、HTTP の場合は :8080 に設定します。メトリクスサーバーを無効にする場合は 0 のままにします。 |
--metrics-secure |
true |
認証・認可付きで、HTTPS 経由でメトリクスを提供します。プレーン HTTP にするには false に設定します。 |
--metrics-cert-path |
empty | メトリクスサーバー用の TLS 証明書ファイル (tls.crt、tls.key) が格納されたディレクトリです。 |
--metrics-cert-name |
tls.crt |
--metrics-cert-path 内の証明書ファイル名です。 |
--metrics-cert-key |
tls.key |
--metrics-cert-path 内の秘密鍵ファイル名です。 |
--enable-http2 |
false |
メトリクス および webhook サーバーで HTTP/2 を有効にします。CVE-2023-44487 / CVE-2023-39325 の緩和のため、デフォルトでは無効になっています。 |
--leader-elect |
false (バイナリ) / true (Helm チャート) |
リーダー選出を有効にし、一度に 1 つのレプリカだけがリコンサイルするようにします。Helm チャートでは、このフラグはデフォルトで manager.args に設定されます。 |
--health-probe-bind-address |
:8081 |
/healthz と /readyz のバインドアドレスです。 |
Helm でメトリクスを有効にする
この chart は、メトリクス用の Service と、必要に応じて prometheus-operator 向けの ServiceMonitor をすでに作成します。
メトリクスエンドポイント 自体はデフォルトで有効です (metrics.enable: true、ポート 8080、metrics.secure: true により HTTPS 経由で提供) 。通常、変更が必要なのは prometheus.enable だけで、これを有効にすると chart が ServiceMonitor を作成します。
# values.yaml — minimal override
prometheus:
enable: truecert-manager を使用しない場合は、追加で certManager.enable: false を設定してください。その場合、ServiceMonitor は insecureSkipVerify: true でスクレイプを行い、ベアラートークン認証のみに依存します。
メトリクス関連のデフォルト設定一式は次のとおりです。
metrics:
enable: true
port: 8080
secure: true # HTTPS with authn/authz enforced on every scrape
certManager:
enable: true # Issues the metrics server certificate
prometheus:
enable: false # Set to true to render the ServiceMonitor
scraping_annotations: false # Alternative: prometheus.io/scrape pod annotations適用:
helm upgrade --install clickhouse-operator \
oci://ghcr.io/clickhouse/clickhouse-operator-helm \
-n clickhouse-operator-system --create-namespace \
-f values.yamlインストール後、チャートによって次が作成されます:
Service/<resource-prefix>-metrics-service— ポート8080を公開します (metrics.secure: trueの場合は HTTPS) 。ServiceMonitor/<resource-prefix>-controller-manager-metrics-monitor—prometheus.enable: trueの場合に作成されます。ClusterRole/<resource-prefix>-metrics-reader— 非リソース URL/metricsに対するget権限。
メトリクス エンドポイントの保護
metrics.secure: true の場合、メトリクス サーバーはすべてのスクレイプに対して、TLS および Kubernetes の認証/認可を必須にします。スクレーパーは次の条件を満たす必要があります。
- 有効な Kubernetes ベアラートークン を提示する。
- 非リソース URL
/metricsへのgetを許可するクラスター ロールにバインドされた ServiceAccount に属している。
このチャートには、そのようなクラスター ロールが含まれています。
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: clickhouse-operator-metrics-reader
rules:
- nonResourceURLs:
- /metrics
verbs:
- getこれを、スクレーパー (通常は Prometheus) が使用する ServiceAccount に紐付けます:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus-clickhouse-operator-metrics-reader
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: clickhouse-operator-metrics-reader
subjects:
- kind: ServiceAccount
name: <prometheus-sa>
namespace: <prometheus-namespace>ServiceMonitor リファレンス
prometheus.enable: true の場合、チャートは以下のような ServiceMonitor をレンダリングします:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: <release>-controller-manager-metrics-monitor
namespace: <operator-namespace>
labels:
control-plane: controller-manager
spec:
selector:
matchLabels:
control-plane: controller-manager
endpoints:
- path: /metrics
port: https # "http" when metrics.secure: false
scheme: https
bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
tlsConfig:
serverName: <release>-metrics-service.<operator-namespace>.svc
ca:
secret:
name: metrics-server-cert
key: ca.crt
cert:
secret:
name: metrics-server-cert
key: tls.crt
keySecret:
name: metrics-server-cert
key: tls.keyPrometheus インスタンスで cert-manager が実行されていない場合は、tlsConfig.insecureSkipVerify: true を設定し、ベアラートークン認証のみに依存してください — certManager.enable: false の場合、チャートではすでにこのように設定されています。
スタンドアロンの Prometheus の例
kube-prometheus-stack を使用しない場合は、リポジトリに examples/prometheus_secure_metrics_scraper.yaml という自己完結型のサンプルが用意されています。これにより、ServiceAccount、必要な RBAC、および operator の ServiceMonitor を選択する Prometheus CR が作成されます。
ヘルスプローブのエンドポイント
| Path | 使用先 | 戻り値 |
|---|---|---|
/healthz |
Kubernetes liveness probe | プローブサーバーがリッスンしている限り 200 OK。 |
/readyz |
Kubernetes readiness probe | プローブサーバーがリッスンしている限り 200 OK。 |
どちらのエンドポイントにも、同じ簡易的な Ping チェック (sigs.k8s.io/controller-runtime の healthz.Ping) が登録されています。したがって、プローブの失敗が意味するのは「マネージャープロセスが :8081 で HTTP を提供していない」ということであり、「コントローラーが不健全である」という意味ではありません。コントローラーレベルの問題を検出するには、代わりにreconciliation メトリクスを使用してください。
どちらのエンドポイントも、デフォルトではポート 8081 で提供されます。デプロイメントには次のように設定されています。
livenessProbe:
httpGet:
path: /healthz
port: 8081
initialDelaySeconds: 15
periodSeconds: 20
readinessProbe:
httpGet:
path: /readyz
port: 8081
initialDelaySeconds: 5
periodSeconds: 10プローブの失敗が繰り返し発生する場合、通常はプローブ用の server 自体が起動していないことを意味します。たとえば、起動時の早い段階で manager が終了した場合です。manager のログで、unable to start manager、RBAC の失敗、または cache did not sync のエラーを確認してください。
メトリクスカタログ
この operator はカスタムの Prometheus collector を登録しません。以下はすべて、基盤となる controller-runtime および client-go ライブラリによって公開されるものです。特に有用な series を用途別にまとめると、次のとおりです。
リコンサイルのアクティビティ
| メトリクス | 型 | ラベル |
|---|---|---|
controller_runtime_reconcile_total |
カウンター | controller, result (success / error / requeue / requeue_after) |
controller_runtime_reconcile_errors_total |
カウンター | controller |
controller_runtime_reconcile_time_seconds_bucket |
ヒストグラム | controller |
controller_runtime_active_workers |
Gauge | controller |
controller_runtime_max_concurrent_reconciles |
Gauge | controller |
controller ラベルは、For(...) に登録されたリソース型をもとに controller-runtime が導出します。現在の internal/controller/clickhouse および internal/controller/keeper のコードでは、これはそれぞれ clickhousecluster と keepercluster になります。operator をカスタマイズしている場合は、/metrics を一回限りスクレイプして確認してください。
ワークキュー
| メトリック | 型 | ラベル |
|---|---|---|
workqueue_depth |
Gauge | name, controller, priority |
workqueue_adds_total |
カウンター | name, controller |
workqueue_retries_total |
カウンター | name, controller |
workqueue_unfinished_work_seconds |
Gauge | name, controller |
workqueue_longest_running_processor_seconds |
Gauge | name, controller |
workqueue_queue_duration_seconds_bucket |
ヒストグラム | name, controller |
workqueue_work_duration_seconds_bucket |
ヒストグラム | name, controller |
name と controller のラベルには、同じ値 (コントローラー名) が設定されます。
API サーバーのトラフィック
| メトリクス | 型 | ラベル |
|---|---|---|
rest_client_requests_total |
カウンター | code, method, host |
リーダー選出
| メトリクス | 型 | ラベル |
|---|---|---|
leader_election_master_status |
Gauge | name (= d4ceba06.clickhouse.com) |
Helm チャートではデフォルトで --leader-elect が有効になっているため、このメトリクスは標準的な Helm インストールでは利用できます。フラグを付けずにバイナリを直接実行した場合、このメトリクスは出力されません。
ランタイム
標準の Go プロセスおよびランタイム collector — go_goroutines, go_memstats_*, process_cpu_seconds_total, process_resident_memory_bytes など。
役立つPromQLクエリ
ヘルス概要
# Reconciliation rate per controller
sum by (controller) (rate(controller_runtime_reconcile_total[5m]))
# Error rate per controller (alert if > 0 sustained)
sum by (controller) (rate(controller_runtime_reconcile_errors_total[5m]))
# p99 reconcile latency
histogram_quantile(
0.99,
sum by (le, controller) (rate(controller_runtime_reconcile_time_seconds_bucket[5m]))
)バックログの検知
# Pending items in the work queue — a sustained value > 0 indicates a backlog,
# but short spikes during large reconciles are normal.
avg_over_time(workqueue_depth[10m])
# Reconciles that have been running for a long time
workqueue_longest_running_processor_seconds > 60スロットリングとAPIへの負荷
# Throttled requests to the API server
sum by (code, host) (rate(rest_client_requests_total{code=~"4..|5.."}[5m]))リーダーのステータス (HA 構成)
# Should be exactly 1 across the replica set (Helm install enables --leader-elect by default)
sum(leader_election_master_status{name="d4ceba06.clickhouse.com"})推奨アラート
PrometheusRule のひな形 (しきい値はご利用の環境に合わせて調整してください) :
groups:
- name: clickhouse-operator
rules:
- alert: ClickHouseOperatorReconcileErrors
# > 0.1 errors/s sustained = > ~6 errors/min, filters transient conflicts.
expr: sum by (controller) (rate(controller_runtime_reconcile_errors_total[5m])) > 0.1
for: 15m
labels:
severity: warning
annotations:
summary: 'ClickHouse operator is failing to reconcile {{ $labels.controller }}'
- alert: ClickHouseOperatorWorkqueueBacklog
# avg_over_time avoids alerting on transient bursts during large reconciles.
expr: avg_over_time(workqueue_depth[10m]) > 5
for: 30m
labels:
severity: warning
annotations:
summary: 'Operator work queue backlog sustained for 30m'
- alert: ClickHouseOperatorReconcileSlow
expr: |
histogram_quantile(
0.99,
sum by (le, controller) (rate(controller_runtime_reconcile_time_seconds_bucket[10m]))
) > 30
for: 15m
labels:
severity: warning
annotations:
summary: 'p99 reconcile latency for {{ $labels.controller }} > 30s'
- alert: ClickHouseOperatorNoLeader
expr: absent(leader_election_master_status{name="d4ceba06.clickhouse.com"}) == 1
for: 5m
labels:
severity: critical
annotations:
summary: 'No leader for the ClickHouse operator (HA deployment)'最後のルールは、リーダー選出 が有効な場合にのみ意味があります。
セットアップの確認
clickhouse-operator-system にチャートがインストールされていることを前提に、エンドツーエンドで簡単に確認します。
NS=clickhouse-operator-system
# The metrics Service exists and selects the manager pod
kubectl -n $NS get svc -l control-plane=controller-manager
# The ServiceMonitor exists (only with prometheus.enable=true)
kubectl -n $NS get servicemonitor -l control-plane=controller-manager
# Manager pod is Ready (readiness probe answers)
kubectl -n $NS get pod -l control-plane=controller-manager
# Direct scrape from inside the cluster (with the metrics-reader binding)
kubectl -n $NS run curl-metrics --rm -it --restart=Never \
--image=curlimages/curl:8.10.1 -- sh -c '
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
curl -sk -H "Authorization: Bearer $TOKEN" \
https://<release>-metrics-service.'$NS'.svc:8080/metrics \
| head -20
'スクレイプで Prometheus エクスポジション形式 のメトリクスが返される場合、エンドポイントと RBAC は正しく設定されています。