我尝试使用Istio获取自定义指标
这篇文章是富士通云技术2022年降临日历的第8天的文章,介绍了提供云服务的Nifcloud等。
前天我尝试了@yusayoshi的playwright和Gitlab CI/CD来自动化E2E测试。
通过自动化繁琐的测试任务,不仅能节省工时,而且在测试失败时还能通过日志和附带视频提供详细信息,感觉非常完善和出色。
我想解释一下关于如何获取Istio的自定义指标的方法。
事实上,我刚配属到Hatoba团队,还在积极学习有关k8s的知识。
我想分享一些我学习的内容。
只要有k8s环境,我们会努力提供可以让Istio初学者动手实践的内容。
Istio是什么?
简述
Istio是一种用于构建和管理微服务服务网格的开源软件(OSS)。
最近的服务越来越微服务化,一个服务中存在着多个应用之间复杂的协作和运行。
管理这些应用之间的通信以及进行更新是一项非常耗费精力的工作。
Istio可以帮助管理这些像网一样交织的应用通信网络(服务网格)。
Istio 可以做的事情
根据官方网站介绍,以下功能被提及。
サービス間の通信の暗号化
ロードバランシング機能(HTTP, gRPC, WebSocket, TCP)
サービス間のトラフィック管理(ルーティング, リトライ, フェイルオーバー, フォールトインジェクション)
メトリクスやログ、トレースを用いたサービス内のトラフィックの自動監視
使用Istio的好处
作为Istio的一个主要特点之一,它可以在无需改动现有代码的情况下进行引入。
它作为每个应用程序的sidecar,注入了一个代理,通过代理实现应用程序之间的双向通信。
应用程序可以在不必意识到代理的情况下进行通信,而Istio负责通过每个代理实现通信的控制和监视。
关于监视
在运行和开发系统的过程中,监视是至关重要的。它是确保系统正常运行的手段。更重要的是,它能够了解系统的状态,知道什么是系统的正确状态,以及需要什么。监视的要素包括日志、指标和追踪三个部分。
-
- ログ
文字でシステムの状態や動作を記録する
そのため、データ量は巨大で解析にも時間が掛かる
メトリクス
数値でシステムの状態や動作を記録する
状態の時間変化を確認できる
トレース
アプリケーション間の通信とその経路を記録する
アプリケーションの連携を確認できる
本文只针对指标进行讨论。
获取自定义指标
安装Istio
本文介绍了我们在以下环境下进行验证。
我将参考Istio官方文档来进行Istio环境的搭建。
首先,我们要进行Istioctl的安装。
$ curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.15.2 TARGET_ARCH=x86_64 sh -
$ cp -p istio-1.15.2/bin/istioctl /usr/local/bin
$ istioctl version
no running Istio pods in "istio-system"
1.15.2
接下来,我们将使用配置文件来安装 Istio。
$ istioctl install --set profile=demo -y
✔ Istio core installed
✔ Istiod installed
✔ Egress gateways installed
✔ Ingress gateways installed
✔ Installation complete Making this installation the default for injection and validation.
我会确认部署是否成功。
$ kubectl get all -n istio-system
NAME READY STATUS RESTARTS AGE
pod/istio-egressgateway-789bbc8b8b-zpr5s 1/1 Running 0 17m
pod/istio-ingressgateway-5474bb49b8-6q8nj 1/1 Running 0 2m8s
pod/istiod-6dcd646fc9-fs48q 1/1 Running 0 17m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/istio-egressgateway ClusterIP 10.99.96.227 <none> 80/TCP,443/TCP 17m
service/istio-ingressgateway LoadBalancer 10.107.127.207 111.64.92.157 15021:30817/TCP,80:32633/TCP,443:30004/TCP 17m
service/istiod ClusterIP 10.108.217.159 <none> 15010/TCP,15012/TCP,443/TCP,15014/TCP 17m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/istio-egressgateway 1/1 1 1 17m
deployment.apps/istio-ingressgateway 1/1 1 1 17m
deployment.apps/istiod 1/1 1 1 17m
NAME DESIRED CURRENT READY AGE
replicaset.apps/istio-egressgateway-789bbc8b8b 1 1 1 17m
replicaset.apps/istio-ingressgateway-5474bb49b8 1 1 1 17m
replicaset.apps/istiod-6dcd646fc9 1 1 1 17m
将侧边车自动部署。
$ kubectl label namespace default istio-injection=enabled
namespace/default labeled
将httpbin作为示例服务部署。
$ kubectl apply -f istio-1.15.2/samples/httpbin/httpbin.yaml
serviceaccount/httpbin created
service/httpbin created
deployment.apps/httpbin created
$ kubectl apply -f istio-1.15.2/samples/httpbin/httpbin-gateway.yaml
gateway.networking.istio.io/httpbin-gateway created
virtualservice.networking.istio.io/httpbin created
我将确认部署是否成功。
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/httpbin-847f64cc8d-ft5cj 2/2 Running 0 4s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/httpbin ClusterIP 10.105.123.149 <none> 8000/TCP 3m3s
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 132m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/httpbin 1/1 1 1 3m3s
NAME DESIRED CURRENT READY AGE
replicaset.apps/httpbin-847f64cc8d 1 1 1 3m3s
我将尝试使用curl命令访问。
IP地址和端口因环境而异,请参考官方文档进行获取。
在我自己的环境中,我是通过以下方式获取的。
$ export INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
$ export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].port}')
$ curl http://$INGRESS_HOST:$INGRESS_PORT/get
{
"args": {},
"headers": {
"Accept": "*/*",
"Host": "xxx.xxx.xxx.xxx",
"User-Agent": "curl/7.68.0",
"X-B3-Parentspanid": "fe58d5ff194cc300",
"X-B3-Sampled": "1",
"X-B3-Spanid": "85a24cab8430ae49",
"X-B3-Traceid": "afcfbec3b49a33e1fe58d5ff194cc300",
"X-Envoy-Attempt-Count": "1",
"X-Envoy-External-Address": "xxx.xxx.xxx.xxx",
"X-Forwarded-Client-Cert": "By=spiffe://cluster.local/ns/default/sa/httpbin;Has......."
},
"origin": "xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx",
"url": "http://xxx.xxx.xxx.xxx/get"
}
如果能够收到来自httpbin的响应,那就表示成功。
获取指标
下一步我们将查看Istio默认公开的指标。
本次将使用Prometheus来进行指标检查。
可以按照以下步骤安装Prometheus。
$ kubectl apply -f istio-1.15.2/samples/addons/prometheus.yaml
erviceaccount/prometheus created
configmap/prometheus created
clusterrole.rbac.authorization.k8s.io/prometheus created
clusterrolebinding.rbac.authorization.k8s.io/prometheus created
service/prometheus created
deployment.apps/prometheus created
我将检查Prometheus的仪表盘。
$ kubectl -n istio-system port-forward service/prometheus 9090:9090
当你在浏览器中访问http://localhost:9090时,你可以看到仪表盘。
通过在表达式中输入istio_request_total并执行,您可以查看指标。

通过多次执行之前的curl命令,可以观察到在prometheus图表上请求数量在增加。

在Istio中,默认公开了除了istio_request_total之外的以下各种指标。
添加标签
现在,让我们转移到关于定制指标的主题上。本文将介绍如何向指标添加标签。
我参考了以下官方文档。
例如,在`istio_request_total`指标中,已经存在着`app`标签和`response_code`标签等。
在Istio中,除了这些标签之外,还存在可以添加的值。
可添加的变量列表已经发布在以下网站:
我們將在這裡添加destination.port和request.method作為標籤。
我們將在IstioOperator中添加標籤設定。
我們將創建一個名為metrics.yaml的文件,內容如下。
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
profile: demo
meshConfig:
defaultConfig:
extraStatTags:
- destination_port
- request_method
values:
telemetry:
v2:
prometheus:
configOverride:
inboundSidecar:
metrics:
- name: requests_total
dimensions:
destination_port: string(destination.port)
request_method: request.method
应用设置。
$ istioctl install -f metrics.yaml -y
接下来,我们需要为httpbin的pod添加一个sidecar。我们将复制httpbin.yaml并添加配置。
$ cp istio-1.15.2/samples/httpbin/httpbin.yaml httpbin_metrics.yaml
将sidecar.istio.io/extraStatTags的设置添加到httpbin_metrics.yaml的annotations中。
...省略...
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpbin
annotations:
sidecar.istio.io/extraStatTags: destination_port, request_method
...省略...
应用更改设置,并重新启动 pod。
$ kubectl apply -f httpbin_metrics.yaml
$ kubectl delete pods --all
再次执行Prometheus的启动命令并确认后,我们可以看到已添加了标签。

总结
这次我向大家介绍了如何获取Istio的自定义指标。
使用Istio可以很容易地获取指标,所以我希望大家也尝试一下!
这篇文章是富士通云技术 Advent Calendar 2022的第八天的文章。
明天是@nori3636的”在WSL上安装minikube环境”。很巧合的是,这是连续的与k8s有关的内容。
我自己还没有尝试过minikube,所以很期待想知道要做什么。
文献引用