这个task展示如何配置Istio自动收集网格中的TCP服务的遥测。完成这个task,调用一个你的网格中的TCP服务将会生成一个新的metric。
本task中使用 Bookinfo 作为示例。
Before you begin
- 在你的集群安装Istio并部署一个应用。
- 这个task假设Bookinfo 被部署在
default
命名空间中。如果你使用不同的命名空间,你将需要更新例子中的配置和命令。 - 安装Prometheus 插件。Prometheus 将被用来验证task是否成功。
kubectl apply -f install/kubernetes/addons/prometheus.yaml
Prometheus 的更多细节。
Collecting new telemetry data
1.新建一个新的YAML文件来保存Istio自动生成和收集的信息metric和日志流的配置。
保存如下内容到 tcp_telemetry.yaml
:
# Configuration for a metric measuring bytes sent from a server
# to a client
apiVersion: "config.istio.io/v1alpha2"
kind: metric
metadata:
name: mongosentbytes
namespace: default
spec:
value: connection.sent.bytes | 0 # uses a TCP-specific attribute
dimensions:
source_service: source.service | "unknown"
source_version: source.labels["version"] | "unknown"
destination_version: destination.labels["version"] | "unknown"
monitoredResourceType: '"UNSPECIFIED"'
---
# Configuration for a metric measuring bytes sent from a client
# to a server
apiVersion: "config.istio.io/v1alpha2"
kind: metric
metadata:
name: mongoreceivedbytes
namespace: default
spec:
value: connection.received.bytes | 0 # uses a TCP-specific attribute
dimensions:
source_service: source.service | "unknown"
source_version: source.labels["version"] | "unknown"
destination_version: destination.labels["version"] | "unknown"
monitoredResourceType: '"UNSPECIFIED"'
---
# Configuration for a Prometheus handler
apiVersion: "config.istio.io/v1alpha2"
kind: prometheus
metadata:
name: mongohandler
namespace: default
spec:
metrics:
- name: mongo_sent_bytes # Prometheus metric name
instance_name: mongosentbytes.metric.default # Mixer instance name (fully-qualified)
kind: COUNTER
label_names:
- source_service
- source_version
- destination_version
- name: mongo_received_bytes # Prometheus metric name
instance_name: mongoreceivedbytes.metric.default # Mixer instance name (fully-qualified)
kind: COUNTER
label_names:
- source_service
- source_version
- destination_version
---
# Rule to send metric instances to a Prometheus handler
apiVersion: "config.istio.io/v1alpha2"
kind: rule
metadata:
name: mongoprom
namespace: default
spec:
match: context.protocol == "tcp"
&& destination.service == "mongodb.default.svc.cluster.local"
actions:
- handler: mongohandler.prometheus
instances:
- mongoreceivedbytes.metric
- mongosentbytes.metric
2.推送新配置
istioctl create -f tcp_telemetry.yaml
预期输出类似:
Created config metric/default/mongosentbytes at revision 3852843
Created config metric/default/mongoreceivedbytes at revision 3852844
Created config prometheus/default/mongohandler at revision 3852845
Created config rule/default/mongoprom at revision 3852846
3.使用MongoDB部署Bookinfo 。
- 安装
ratings
服务的v2
版
如果你使用开启自动注入sidecar 的集群,使用kubectl
简单部署服务:
kubectl apply -f samples/bookinfo/kube/bookinfo-ratings-v2.yaml
如果你手动注入sidecar,使用:
kubectl apply -f <(istioctl kube-inject -f samples/bookinfo/kube/bookinfo-ratings-v2.yaml)
预期结果:
deployment "ratings-v2" configured
- 安装
mongodb
服务:
如果你使用开启自动注入sidecar 的集群,使用kubectl
简单部署服务:
kubectl apply -f samples/bookinfo/kube/bookinfo-db.yaml
如果你手动注入sidecar,使用:
kubectl apply -f <(istioctl kube-inject -f samples/bookinfo/kube/bookinfo-db.yaml)
预期结果:
service "mongodb" configured
deployment "mongodb-v1" configured
- 为发送到
v2
版的ratings
服务的流量添加路由规则:
istioctl create -f samples/bookinfo/kube/route-rule-ratings-db.yaml
预期结果:
Created config route-rule//ratings-test-v2 at revision 7216403
Created config route-rule//reviews-test-ratings-v2 at revision 7216404
4.向示例应用发送流量
对于Bookinfo ,在你浏览器访问 http://$GATEWAY_URL/productpage
或者使用如下命令:
curl http://$GATEWAY_URL/productpage
5.核实生成和收集的新的metric值。
在k8s环境中,通过执行如下命令来为Prometheus 设置端口转发:
kubectl -n istio-system port-forward $(kubectl -n istio-system get pod -l app=prometheus -o jsonpath='{.items[0].metadata.name}') 9090:9090 &
通过Prometheus UI查看新的metric值。
提供的链接开启了 Prometheus UI,并执行对istio_mongo_received_bytes
metric值的查询。Console(控制台)选项卡中展示的表格内容类似:
istio_mongo_received_bytes{destination_version="v1",instance="istio-mixer.istio-system:42422",job="istio-mesh",source_service="ratings.default.svc.cluster.local",source_version="v2"} 2317
注意:Istio也收集对于MongoDB指定协议的统计。例如,从ratings
服务发送的 OP_QUERY信息的全部值都被这个metric收集起来了:envoy_mongo_mongo_collection_ratings_query_total
(click here to execute the query)。
Understanding TCP telemetry collection
在这个task中,你添加Istio配置来指示Mixer为网格中的所有到TCP服务的流量自动生成和报告一个新的metric。
和 Collecting Metrics and Logs 类似,新的配置包含 instances, a handler, and a rule 。请查看那个task的metric收集组件的完整描述。
TCP服务的Metrics标准集仅在可用于实例的有限属性集中有所不同。
TCP Attributes
通过Istio的几个指定TCP属性开启TCP策略和控制。这些属性通过服务端Envoy代理生成并在建立连接和关闭连接时转发给Mixer。另外,context属性提供区别 http
和tcp
协议能力的策略。
Cleanup
- 移除新的遥测配置
istioctl delete -f tcp_telemetry.yaml
- 移除任何
port-forward
进程:
killall kubectl
- 如果你不打算探索接下来地任何课题,参考 Bookinfo cleanup 指南来关闭应用。