Istio 已内置分布式链路追踪能力,通过简单的配置就能将部署在服务网格的应用接入到应用性能监控 APM,关于 Istio 的更多详情,请参考 Istio 官方文档。Istio 基于 Envoy 实现链路数据采集与上报,请参考 Envoy 官方文档。
前提条件
Kubernetes 集群已经安装 Istio,且 Istio 版本在1.19及以上。
接入步骤
步骤1:获取接入点和 token
1. 登录 腾讯云可观测平台 控制台。
2. 在左侧菜单栏中选择应用性能监控 > 应用列表,单击接入应用。
3. 选择接入其他应用。
4. 选择您所要接入的地域以及业务系统。
5. 选择您所想要的上报方式,获取接入点和 Token。
说明:
内网上报:使用此上报方式,您的服务需运行在腾讯云 VPC。通过 VPC 直接连通,在避免外网通信的安全风险同时,可以节省上报流量开销。
外网上报:当您的服务部署在本地或非腾讯云 VPC 内,可以通过此方式上报数据。请注意外网通信存在安全风险,同时也会造成一定上报流量费用。
步骤2:安装 OpenTelemetry Collector
Istio 上报的链路数据需要通过 OpenTelemetry Collector 转发到应用性能监控 APM。如果 Kubernetes 集群尚未安装 OpenTelemetry Collector,请参考该步骤完成安装。如果 Kubernetes 集群已经安装 OpenTelemetry Collector,请参考该步骤调整接入点和 Token 信息。
1. 编写部署描述文件 otel.yaml,并将 ConfigMap 中的
opentelemetry-collector-config.exporters.otlp.endpoint 字段和 opentelemetry-collector-config.exporters.otlp.endpoint 字段设置为 步骤1 拿到的接入点和 Token。cat > otel.yaml <<EOFapiVersion: v1kind: ConfigMapmetadata:name: opentelemetry-collector-conflabels:app: opentelemetry-collectordata:opentelemetry-collector-config:receivers:otlp:protocols:grpc:http:processors:batch:exporters:otlp:endpoint: "<APM-Endpoint>" # Replace <APM-Endpoint> with the access point obtained in the previous stepheaders:authorization: "<APM-Token>" # Replace <APM-Token> with the token obtained in the previous steptls:insecure: truelogging:loglevel: debugextensions:health_check:port: 13133service:extensions:- health_checkpipelines:logs:receivers: [otlp]processors: [batch]exporters: [logging]traces:receivers:- otlpexporters:- logging- otlp---apiVersion: v1kind: Servicemetadata:name: opentelemetry-collectorlabels:app: opentelemetry-collectorspec:ports:- name: grpc-opencensusport: 55678protocol: TCPtargetPort: 55678- name: grpc-otlp # Default endpoint for OpenTelemetry receiver.port: 4319protocol: TCPtargetPort: 4319- name: http-otlp # HTTP endpoint for OpenTelemetry receiver.port: 4318protocol: TCPtargetPort: 4318selector:app: opentelemetry-collector---apiVersion: apps/v1kind: Deploymentmetadata:name: opentelemetry-collectorspec:selector:matchLabels:app: opentelemetry-collectorstrategy:rollingUpdate:maxSurge: 1maxUnavailable: 1type: RollingUpdatetemplate:metadata:labels:app: opentelemetry-collectorsidecar.istio.io/inject: "false" # do not injectspec:containers:- command:- "/otelcol"- "--config=/conf/opentelemetry-collector-config.yaml"env:- name: POD_NAMEvalueFrom:fieldRef:apiVersion: v1fieldPath: metadata.name- name: POD_NAMESPACEvalueFrom:fieldRef:apiVersion: v1fieldPath: metadata.namespaceimage: otel/opentelemetry-collector:0.54.0imagePullPolicy: IfNotPresentname: opentelemetry-collectorports:- containerPort: 4319protocol: TCP- containerPort: 4318protocol: TCP- name: grpc-opencensuscontainerPort: 55678protocol: TCPresources:limits:cpu: "2"memory: 4Girequests:cpu: 200mmemory: 400MiterminationMessagePath: /dev/termination-logterminationMessagePolicy: FilevolumeMounts:- name: opentelemetry-collector-config-volmountPath: /confdnsPolicy: ClusterFirstrestartPolicy: AlwaysschedulerName: default-schedulerterminationGracePeriodSeconds: 30volumes:- configMap:defaultMode: 420items:- key: opentelemetry-collector-configpath: opentelemetry-collector-config.yamlname: opentelemetry-collector-confname: opentelemetry-collector-config-volEOF
2. 创建
observability 命名空间。kubectl create namespace observability
3. 将 OpenTelemetry Collector 部署到
observability 命名空间。kubectl apply -f otel.yaml -n observability
步骤3:配置 Istio 链路追踪
1. 在集群中运行以下命令,允许 Istio 将链路数据通过 OpenTelemetry 协议发送到集群中部署的 OpenTelemetry Collector。
cat <<EOF | istioctl install -y -f -apiVersion: install.istio.io/v1alpha1kind: IstioOperatorspec:meshConfig:defaultConfig:tracingServiceName: CANONICAL_NAME_ONLYenableTracing: trueextensionProviders:- name: otel-tracingopentelemetry:port: 4319service: opentelemetry-collector.observability.svc.cluster.localresource_detectors:environment: {}EOF
说明:
Istio 提供了配置选项
tracingServiceName来控制应用名称的生成方式。在 APM 中,多个使用相同应用名称接入的应用进程,会表现为相同应用下的多个实例。tracingServiceName有三种选项:APP_LABEL_AND_NAMESPACE(默认值):使用 <app 标签>.<命名空间> 格式。
CANONICAL_NAME_ONLY:仅使用工作负载的规范名称,如 Deployment 的名称。
CANONICAL_NAME_AND_NAMESPACE:使用 <规范名称>.<命名空间> 格式。
2. 运行以下命令启用 Istio 链路追踪。
kubectl apply -f - <<EOFapiVersion: telemetry.istio.io/v1kind: Telemetrymetadata:name: otel-demonamespace: istio-systemspec:tracing:- providers:- name: otel-tracingrandomSamplingPercentage: 100EOF
说明:
通过调整 Telemetry 资源的配置,可以灵活指定开启链路追踪的工作负载的范围,主要包括如下几种策略:
整个服务网络:本文的示例使用该策略,代表整个服务网络中的业务工作负载都开启链路追踪。
指定命名空间。
指定工作负载。
接入验证
在应用有正常流量的情况下,应用性能监控 > 应用列表 中将展示接入的应用。单击应用名称/ID 进入应用详情页,再选择实例分析,即可看到接入的应用实例。由于可观测数据的处理存在一定延时,如果接入后在控制台没有查询到应用或实例,请等待30秒左右。