0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

RBACを有効化したKong Gatewayのメトリクス取得方法

Last updated at Posted at 2024-05-27

以前Kong Prometheus Pluginで非Promethus対応のサービスのメトリクスを取得するという記事でKong Gateway利用時にGatewayを通過する通信のメトリクスの取得方法を確認したが、RBAC化した場合上手く動かなくて少しハマったので対応策をメモに残す。

問題の背景

Kong GatewayでPrometheus用のメトリクスを取得しようと思った場合、Admin APIの/metricsを叩くと公式のドキュメントには記載がある
ただ、RBACを有効化した状態だと認証が通らず以下のようなエラーとなる。

$ curl https://kong.${MYHOST}/api/metrics -k
{"message":"Invalid credentials. Token or User credentials required"}

認証を突破するにはヘッダに"kong-admin-token:<AdminのPassword>"を渡せば突破できる。

$ curl https://kong.${MYHOST}/api/metrics -k -H kong-admin-token:kong
- # TYPE kong_bandwidth_bytes counter
:(省略)

ただ、厄介なのがこのヘッダの形式は(おそらく)Prometheusに直接渡す事が出来ない。
少なくともPrometheus Operatorのkind: ScrapeConfigでは渡せる認証はBasic認証かAuthenticationヘッダの指定のみとなっている。
そのため、Admin APIが期待するヘッダ(kong-admin-token)をPrometheus内に設定することが出来ない。

対応策

認証なしで/metricsにアクセスする方法があるのでこれを使う。
Kong Gateway内にはStatus APIと呼ばれるヘルスチェック用のAPIが用意されている。
公式ドキュメントにはこの辺に記載がある。
これには/statusしか書いていないが、実は/metricsでメトリクスも取得することが出来る。
なのでこれを使って取得してみる。
なお、Kong GatewayのHelmChartの説明には明確にServiceIngressを提供しないと記載されているため、手動で作成する。

cat <<EOF > ./status-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: kong-status
  namespace: kong
spec:
  ports:
  - name: status
    port: 8100
    protocol: TCP
  selector:
    app.kubernetes.io/component: app
    app.kubernetes.io/name: kong
EOF
kubectl apply -f ./status-svc.yaml

次にPrometheusとKong Gatewayが異なるクラスタに存在するなどの場合はIngressも作成する。(ここでは省略する)
Prometheus Operatorを使っている場合、ScrapeConfigを作成してスクレイピングを行う。

cat <<EOF > ./scraping.yaml
apiVersion: monitoring.coreos.com/v1alpha1
kind: ScrapeConfig
metadata:
  labels:
    release: prometheus-stack
  name: scraping-kong
  namespace: kong
spec:
  metricsPath: /metrics
  scheme: HTTP
  staticConfigs:
  - labels:
      job: kong-gateway
    targets:
    - kong-status.kong.svc:8100
EOF
kubectl apply -f ./scraping.yaml

変更適用後、ScrapeInterval(デフォルトで30秒)待つと以下のようにRBACが有効な環境でも無事メトリクスを見に行くことが出来た。
20240527150525.png

なお、今回作成したServiceはHelmChartと別管理になるため、デプロイやアップグレード時の修正などライフサイクル管理を行う際は取りこぼさないよう気をつける必要がある。

※追記
公式の説明はここにある。
色々試行錯誤し、記事化した後に公式の説明があるのに気がついた。。。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?