はじめに
ローカルでPrometheusとIstioを動かして、PrometheusからIstioのメトリクスを確認してみます。
Istioのメトリクス
まずは、簡単にIstioの標準的なメトリクスについて紹介します。
標準的なメトリクスには、HTTP, HTTP/2, gRPCのトラフィックから生成されるメトリクスとTCPのトラフィックから生成されるメトリクスがあります。
HTTP, HTTP/2, gRPC
メトリクス | タイプ | 詳細 |
---|---|---|
istio_requests_total | COUNTER | リクエスト数 |
istio_request_duration_milliseconds | DISTRIBUTION | リクエストの処理時間 |
istio_request_bytes | DISTRIBUTION | リクエストのサイズ |
istio_response_bytes | DISTRIBUTION | レスポンスのサイズ |
istio_request_messages_total | COUNTER | gRPCのリクエストメッセージ数 |
istio_response_messages_total | COUNTER | gRPCのレスポンスメッセージ数 |
DISTRIBUTIONタイプのメトリクスは、Prometheusから確認する場合、_bucket
、 _count
、 _sum
がついています。
TCP
メトリクス | タイプ | 詳細 |
---|---|---|
istio_tcp_sent_bytes_total | COUNTER | 送信バイト数 |
istio_tcp_received_bytes_total | COUNTER | 受信バイト数 |
istio_tcp_connections_opened_total | COUNTER | OpenなTCPコネクション |
istio_tcp_connections_closed_total | COUNTER | CloseなTCPコネクション |
ローカルで試す
実際にローカルでPrometheusとIstioを動かして、メトリクスを確認していきます。
用意するファイルは下記の通りです。
.
├── prometheus
│ ├── helmfile.yaml
│ └── prometheus.yaml
├── istio.yaml
├── deployment.yaml
└── service.yaml
HelmでPrometheusのデプロイ
まずは、Helmとhelmfileを使ってPrometheusをデプロイします。
prometheus/helmfile.yaml
は下記の通りです。monitoring Namespaceにデプロイされるようにしています。
releases:
- name: prometheus
namespace: monitoring
chart: prometheus-community/prometheus
values:
- ./prometheus.yaml
利用しているチャートはこちらです。
prometheus/prometheus.yaml
は下記の通りです。Prometheus以外のものはデプロイしないようにしています。
alertmanager:
enabled: false
kube-state-metrics:
enabled: false
prometheus-pushgateway:
enabled: false
prometheus-node-exporter:
enabled: false
server:
service:
servicePort: 9090
helmfileでデプロイします。
helmfile apply
Istioのインストール
Istioのインストールをします。
istioctl install --set profile=demo -y
default Namespaceでサイドカーの有効化をします。
kubectl label namespace default istio-injection=enabled
デモアプリのデプロイ
次にデモアプリ用のDeployment、Service、Virtual Service、Gatewayをデプロイします。
deployment.yaml
は下記の通りです。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
service.yaml
は下記の通りです。
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
ports:
- protocol: TCP
name: http
port: 8080
targetPort: 80
selector:
app: nginx
istio.yaml
は下記の通りです。
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: nginx-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: nginx
spec:
hosts:
- "*"
gateways:
- nginx-gateway
http:
- route:
- destination:
host: nginx-svc
port:
number: 8080
それぞれデプロイします。
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f istio.yaml
メトリクス確認の準備
Prometheus
まずはPrometheusにアクセスできるようにポートフォワードします。
kubectl -n monitoring port-forward svc/prometheus-server 9090:9090
これで、localhost:9090
でPrometheusにアクセスできるようになりました。
デモアプリ
次にIstioのingressgateway経由でデモアプリにアクセスできるようにします。
まずはingressgatewayのポート番号を環境変数として設定します。
export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')
ボート番号を確認してみます。
❯ echo $INGRESS_PORT
32333
設定した環境変数を使って、ポートフォワードします。
kubectl -n istio-system port-forward svc/istio-ingressgateway $INGRESS_PORT:80
これで、localhost:32333
でデモアプリにアクセスできるようになりました。
メトリクスの確認
デモアプリにリクエストをいくつか送って、Prometheusからメトリクスを確認してみます。
まずは、いくつかデモアプリにリクエストを送ってみます。
curl localhost:32333
一気に送る場合は、下記を使うこともできます。
for i in $(seq 1 100); do curl -s -o /dev/null "localhost:32333"; done
Prometheusからistio_requests_total
を確認してみます。
条件を絞って、確認することもできます。
確認したPromQLは下記になります。
rate(istio_requests_total{reporter="destination",destination_service_name="nginx-svc"}[5m])
reporter
は、destination
だとサーバ側のistio-proxyから、source
だとクライアント側のistio-proxyもしくはgatewayからメトリクスを取得します。