これまで取り上げてきたように、Istioには大きく分けて次の3つの特徴があります。

  • Traffic Management:ルーティング制御、ゲートウェイ、サーキットブレーカー、カオスエンジニアリング
  • Observability:メトリクス、分散トレーシング、アクセスログ管理
  • Secure:鍵や証明書の管理、認証(Authentication)、認可(Authorization)
  • Istio

    Istioの3つの特徴/出典:公式サイト

今回は2つ目の特徴として「Observability」について紹介します。

特徴2:Observability

マイクロサービスを導入すると、サービスがメッシュ状につながり、全体像が見えづらくなります。障害発生時に各サービスのログをそれぞれ解析するのには限界があるでしょう。

Istioを用いると、以下のようなツールとシームレスに連携できます。

  • メトリクス:収集したメトリクスを「Prometheus」で抽出したり、「Grafana」を用いてビジュアライズしたりします
  • 分散トレーシング:「Zipkin」「Jaeger」「Lightstep」などと連動させてAPIコールスタックを確認、Kialiを用いてサービスグラフの確認
  • アクセスログ:個々のマイクロサービスのログを確認

以降では、それぞれの特徴がどのように機能するのか、またどのような設定をすれば良いのかを順に説明します。

  • Observability

    Observabilityの特徴2-1~2-3の全体像

特徴2-1:メトリクス

マイクロサービス単位でのリクエストレスポンス時間をベースに、さまざまな付帯情報を取得可能です。以下の3種類のメトリクスを取得可能なので、ご利用の環境に応じて必要な情報をピックアップすると良いでしょう。

分類 概要 取得できる情報の例
Proxy-level metrics プロキシ(サイドカー)のメトリクス サイドカーのインバウンド・アウトバウンドの情報
Service-level metrics サービスのメトリクス リクエスト数、リクエストの実行時間、リクエスト・レスポンスサイズ
Control plane metrics Control Planeのメトリクス EnvoyプロキシのInjectionの失敗や設定伝搬の失敗

ここで注意していただきたいのは、出力されるログの量です。

一般的なログ設計でもDebugログは出力が膨大になるため、本番環境では出力せず、Infoログまでに止めるのが一般的です。

同様に、Istioにおいても、Proxy-level metricsはService-level metricsで代替するなど、非機能観点での確認を行った上で、真に必要な情報にとどめておくと良いでしょう。

それでは、サービスのメトリクスを確認するため、PrometheusとGrafanaを導入してみましょう。前提として、本連載第27回の手順を全て実行済みとします。

Prometheusの導入

Prometheusをロールアウトします。

$kubectl rollout status deployment/prometheus -n istio-system
Waiting for deployment "prometheus" rollout to finish: 0 of 1 updated replicas are available...
deployment "prometheus" successfully rolled out

サンプルアプリにリクエストを発行します。

for n in `seq 1 9`; do curl -s -o /dev/null http://$GATEWAY_URL/productpage; done

以下のコマンドを実行し、ダッシュボードへアクセスします。 自動的にブラウザが開かれPrometheusのダッシュボードが表示されます。

$istioctl dashboard prometheus
http://localhost:9090

Prometheusのダッシュボードにグラフを表示してみます。「Table」タブに切り替え、Prometheusの検索窓に「istiorequestbytes_bucket」と入力して、「Execute」ボタンを押下してみましょう。

以下のようにPrometheusのダッシュボードのグラフが表示されます。

  • Prometheus

    Prometheusのダッシュボード

Grafanaの導入

Grafanaをロールアウトします。

$kubectl rollout status deployment/grafana -n istio-system
deployment "grafana" successfully rolled out

以下のコマンドを実行し、ダッシュボードへアクセスします。自動的にブラウザが開かれGrafanaのダッシュボードが表示されます。

$istioctl dashboard grafana &
http://localhost:3000

Prometheusのダッシュボードにグラフを表示してみます。「ダッシュボード」アイコンにマウスオーバーし、「Manage」を押下します。「Istio」フォルダを押下し、「Istio Mesh Dashboard」を押下しましょう。Grafanaダッシュボードが表示されます。

メトリクスの変化を見るため、ターミナルに戻り、サンプルアプリにリクエストを送ってみましょう。

for n in `seq 1 9`; do curl -s -o /dev/null http://$GATEWAY_URL/productpage; done

以下のようにGrafanaダッシュボードを見ると、「Global Request Volume」が「0 ops/s」から変化したり、「Success Rate」の列が黄緑色になったりすることが確認できます。

  • Grafana

    Grafanaのダッシュボード

ここまでで、PrometheusとGrafanaのダッシュボードを少し触っていただきました。

実際の開発・運用の現場では、今回表示したリクエストの情報だけでは不十分です。各Podやノードのリソース情報など、必要なメトリクスを選定し、ダッシュボードに追加しておきましょう。

特徴2-2:分散トレーシング

IstioはZipkin、Jaeger、Lightstep、Datadogなどさまざまな分散トレーシングの機能を持ったツールと連携できます。

例えばZipkinを使うと、リクエスト単位でのトレーシング情報を可視化することができます。

  • Zipkin

    Zipkinでのトレーシング情報の表示/出典:公式サイト

また、「Kiali」を用いてトレースグラフを確認することもできます(Kialiの詳細については、本連載第27回を参照してください)。

  • Kiali

    Kialiでのトレースグラフの表示

何かトラブルが発生した場合に、何をどの順で見れば良いかわからないという方もいらっしゃると思います。いきなり個別のマイクロサービスのログを見に行くよりは、トレーシング情報やトレースグラフを見て、全体像を掴むと良いでしょう。

特徴2-3:アクセスログ

個々のマイクロサービスがコンテナ上で動作し、Kubernetesにより自動的に必要数起動(・停止)されます。何かあってから状況を確認しようにも「ログはおろか、そもそも該当のコンテナは既に停止し跡形も無くなっていた」ということも珍しくありません。

Istioを用いることで、個々のマイクロサービスのアクセスログを統合的に管理可能です。管理は、Envoyのアクセスログの機能を用いて実行されます。また、Envoyコンテナの標準出力はkubectl logsコマンドで確認することができます。

それでは、実際にイメージを掴んでいただくため、アクセスログを有効化し、実際に取得してみましょう。ここでは、公式サイトの手順を参考に説明します。

アプリケーションのインストール

2つのアプリケーションをインストールします。

sleep: リクエスト送信元
httpbin: リクエスト受信
$kubectl apply -f samples/sleep/sleep.yaml
serviceaccount/sleep created
service/sleep created
deployment.apps/sleep created
$export SOURCE_POD=$(kubectl get pod -l app=sleep -o jsonpath={.items..metadata.name})
$kubectl apply -f samples/httpbin/httpbin.yaml
serviceaccount/httpbin created
service/httpbin created
deployment.apps/httpbin created
アクセスログの有効化
istioctl install --set profile=demo --set meshConfig.accessLogFile="/dev/stdout" --set meshConfig.accessLogEncoding="JSON"
This will install the Istio 1.11.4 demo profile with ["Istio core" "Istiod" "Ingress gateways" "Egress gateways"] components into the cluster. Proceed? (y/N) y
■ Istio core installed                                                            
■ Istiod installed                                                                
■ Egress gateways installed                                                       
■ Ingress gateways installed                                                      
■ Installation complete                                                           
Thank you for installing Istio 1.11.  Please take a few minutes to tell us about your install/upgrade experience!  https://forms.gle/kWULBRjUv7hHci7T6

※ 「■」は「✔(チェックマーク)」。

アクセスログの確認

sleepアプリケーションからhttpbinアプリケーションにリクエストを発行します。

$kubectl exec "$SOURCE_POD" -c sleep -- curl -sS -v httpbin:8000/status/418
*   Trying 10.102.16.53:8000...
* Connected to httpbin (10.102.16.53) port 8000 (#0)
> GET /status/418 HTTP/1.1
> Host: httpbin:8000
> User-Agent: curl/7.80.0-DEV
> Accept: */*
> 

    -=[ teapot ]=-

       _...._
     .'  _ _ `.
    | ."` ^ `". _,
    \_;`"---"`|//
      |       ;/
      \_     _/
        `"""`
* Mark bundle as not supporting multiuse
< HTTP/1.1 418 Unknown
< server: envoy
< date: Wed, 15 Dec 2021 14:52:00 GMT
< x-more-info: http://tools.ietf.org/html/rfc2324
< access-control-allow-origin: *
< access-control-allow-credentials: true
< content-length: 135
< x-envoy-upstream-service-time: 152
< 
{ [135 bytes data]
* Connection #0 to host httpbin left intact

sleepアプリケーションのアクセスログを確認してみましょう。

$kubectl logs -l app=sleep -c istio-proxy
2021-12-15T14:49:23.821812Z info xdsproxy connected to upstream XDS server: istiod.istio-system.svc:15012
2021-12-15T14:49:24.070162Z info ads ADS: new connection for node:sleep-557747455f-9mtt9.default-1
2021-12-15T14:49:24.070162Z info ads ADS: new connection for node:sleep-557747455f-9mtt9.default-2
2021-12-15T14:49:24.073916Z info cache returned workload certificate from cache ttl=23h59m52.9261525s
2021-12-15T14:49:24.075925Z info cache returned workload trust anchor from cache ttl=23h59m52.9242079s
2021-12-15T14:49:24.086021Z info ads SDS: PUSH request for node:sleep-557747455f-9mtt9.default resources:1 size:4.0kB resource:default
2021-12-15T14:49:24.096267Z info ads SDS: PUSH request for node:sleep-557747455f-9mtt9.default resources:1 size:1.1kB resource:ROOTCA
2021-12-15T14:49:25.894822Z info Initialization took 14.4667748s
2021-12-15T14:49:25.894886Z info Envoy proxy is ready
{"response_code":418,"upstream_cluster":"outbound|8000||httpbin.default.svc.cluster.local","response_flags":"-","request_id":"2c7f11b1-5706-91fb-ad75-9b8d9a655f04","requested_server_name":null,"method":"GET","bytes_sent":135,"upstream_transport_failure_reason":null,"downstream_remote_address":"172.17.0.12:56314","downstream_local_address":"10.102.16.53:8000","connection_termination_details":null,"start_time":"2021-12-15T14:52:00.869Z","response_code_details":"via_upstream","user_agent":"curl/7.80.0-DEV","route_name":"default","x_forwarded_for":null,"bytes_received":0,"upstream_host":"172.17.0.13:80","protocol":"HTTP/1.1","duration":184,"path":"/status/418","upstream_local_address":"172.17.0.12:58766","upstream_service_time":"152","authority":"httpbin:8000"}

httpbinアプリケーションのアクセスログを確認してみましょう。

$kubectl logs -l app=httpbin -c istio-proxy
2021-12-15T14:50:30.714950Z info xdsproxy connected to upstream XDS server: istiod.istio-system.svc:15012
2021-12-15T14:50:30.775851Z info ads ADS: new connection for node:httpbin-74fb669cc6-7r98v.default-1
2021-12-15T14:50:30.776012Z info cache returned workload trust anchor from cache ttl=23h59m59.2240081s
2021-12-15T14:50:30.776391Z info ads SDS: PUSH request for node:httpbin-74fb669cc6-7r98v.default resources:1 size:1.1kB resource:ROOTCA
2021-12-15T14:50:30.778819Z info ads ADS: new connection for node:httpbin-74fb669cc6-7r98v.default-2
2021-12-15T14:50:30.779006Z info cache returned workload certificate from cache ttl=23h59m59.2210176s
2021-12-15T14:50:30.780242Z info ads SDS: PUSH request for node:httpbin-74fb669cc6-7r98v.default resources:1 size:4.0kB resource:default
2021-12-15T14:50:32.464214Z info Initialization took 2.6269208s
2021-12-15T14:50:32.464298Z info Envoy proxy is ready
{"bytes_sent":135,"x_forwarded_for":null,"upstream_transport_failure_reason":null,"route_name":"default","response_code":418,"upstream_cluster":"inbound|80||","authority":"httpbin:8000","user_agent":"curl/7.80.0-DEV","protocol":"HTTP/1.1","upstream_service_time":"6","response_code_details":"via_upstream","downstream_local_address":"172.17.0.13:80","method":"GET","start_time":"2021-12-15T14:52:00.974Z","requested_server_name":"outbound_.8000_._.httpbin.default.svc.cluster.local","bytes_received":0,"upstream_local_address":"127.0.0.6:55999","response_flags":"-","request_id":"2c7f11b1-5706-91fb-ad75-9b8d9a655f04","connection_termination_details":null,"path":"/status/418","downstream_remote_address":"172.17.0.12:58766","duration":9,"upstream_host":"172.17.0.13:80"}

以上により、アクセスログを有効化し、ログの内容を確認することができました。

トラブルシューティングの手順としては、特徴2-1(メトリクス)、特徴2-2(分散トレーシング)で全体像を把握し、問題箇所をある程度特定した上で、個別のアクセスログを見にいくと良いでしょう。

* * *

今回はIstioの3つの特徴のうち、Observabilityについて解説しました。

Istioでは、このObservabilityによって、アプリケーションロジックに手を入れることなく、メトリクス、分散トレーシング、アクセスログ管理を設定することができます。

ぜひ一度、お手元の環境で試してみてください。