Istio可扩展的策略和遥测

Istio策略和遥测的原理

Istio通过专门的服务端组件,提供一种扩展机制来手机服务运行的遥测数据和服务间的访问,执行一定的策略。

应用场景

采集数据、存储数据、检索数据

通过Sidecar Inbound和Outbound都想Mixer上报数据。Mixer提供对应APM的Adapter。解耦Sidecar和APM的通讯,由Mixer来集中对接。

工作原理

每个插件都是一个Adapter,Mixer通过它们与不同的基础设施后端连接。提供日志、监控、配额、ACL检查等功能。

因为每个基础设施后端都有不同的接口和操作,所以需要自定义代码进行处理,这就是Mixer的Adapter机制。

Mixer通过Protobuf格式定义Adapter的配置。

属性

Envoy上报的数据istio中称为属性 attribute。属性是一小块数据,描述服务请求或者服务运行环境的信息。

属性列表: https://istio.io/docs/reference/config/policy-and-telemetry/attribute-vocabulary/

属性表达式

属性表达式: https://istio.io/docs/reference/config/policy-and-telemetry/expression-language/

Mixer的匹配模型

istio主要通过handler、instance、rule这三个资源对象来描述Adapter的配置。

业务处理 Handler

Handler来描述Adapters及其配置。Mixer每个Adapter都需要一些配置才能运行。不同额Adapter有不同的配置,比如服务后端信息之类的。

如果Adapter看做是一个模板的定义的话,Handler就是这个模板的实现。

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
### Sample-1: No connection specified (for compiled in adapters)
### Note: if connection information is not specified, the adapter configuration is directly inside
### `spec` block. This is going to be DEPRECATED in favor of Sample-2
apiVersion: "config.istio.io/v1alpha2"
kind: handler
metadata:
name: requestcount
namespace: istio-system
spec:
compiledAdapter: prometheus
params:
metrics:
- name: request_count
instance_name: requestcount.metric.istio-system
kind: COUNTER
label_names:
- source_service
- source_version
- destination_service
- destination_version
---
### Sample-2: With connection information (for out-of-process adapters)
### Note: Unlike sample-1, the adapter configuration is parallel to `connection` and is nested inside `param` block.
apiVersion: "config.istio.io/v1alpha2"
kind: handler
metadata:
name: requestcount
namespace: istio-system
spec:
compiledAdapter: prometheus
params:
param:
metrics:
- name: request_count
instance_name: requestcount.metric.istio-system
kind: COUNTER
label_names:
- source_service
- source_version
- destination_service
- destination_version
connection:
address: localhost:8090
---

Handler: https://istio.io/docs/reference/config/policy-and-telemetry/istio.policy.v1beta1/#Handler

数据定义 Instance

定义了Adapter要处理的数据对象。通过模板为Adapter提供对元数据的定义。Mixer通过Instance来吧子代理的属性拆分并分发给不同的适配器。

比如将response.code映射成response_code

关联规则 Rule

Rule配置一组规则告诉Mixer有哪个Instance在什么时候发送给哪个Handler处理,一般包括匹配的表达式和执行动作(action)。表达式控制在什么时候调用Adapter和Instance

配置示例

1
2
3
4
5
6
7
8
9
10
11
apiVersion: "config.istio.io/v1alpha2"
kind: rule
metadata:
name: stdio
namespace: istio-system
spec:
match: context.protocol == "http" || context.protocol == "grpc"
actions:
- handler: stdio
instance:
- logentry

详细: https://istio.io/docs/reference/config/policy-and-telemetry/istio.policy.v1beta1/#Rule

Istio常用遥测适配器配置示例

日志收集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Configuration for logentry instances
apiVersion: config.istio.io/v1alpha2
kind: instance
metadata:
name: newlog
namespace: istio-system
spec:
compiledTemplate: logentry
params:
severity: '"info"'
timestamp: request.time
variables:
source: source.labels["app"] | source.workload.name | "unknown"
user: source.user | "unknown"
destination: destination.labels["app"] | destination.workload.name | "unknown"
responseCode: response.code | 0
responseSize: response.size | 0
latency: response.duration | "0ms"
monitored_resource_type: '"UNSPECIFIED"'
---
# Configuration for a Fluentd handler
apiVersion: config.istio.io/v1alpha2
kind: handler
metadata:
name: handler
namespace: istio-system
spec:
compiledAdapter: fluentd
params:
address: "fluentd-es.logging:24224"
---
# Rule to send logentry instances to the Fluentd handler
apiVersion: config.istio.io/v1alpha2
kind: rule
metadata:
name: newlogtofluentd
namespace: istio-system
spec:
match: "true" # match for all requests
actions:
- handler: handler
instances:
- newlog
---

Prometheus适配器

当前应用最广的开源系统监控和报警平台。

Prometheus主要工作就是抓取数据存储,并提供PromQL语法进行查询。或者对家Grafana、kiali等dashboard进行显示,还可以根据规则生成警告。

Prometheus会主动pull来向目标发送http请求采集数据并存储数据。

Adapter的功能

主要通过Istio收集到服务生成的Metric信息,转为Prometheus格式。相当于Prometheus的Exporter的Istio实现。

  1. Envoy通过Report接口上报数据给Mixer
  2. Mixer根据配置将请求分发给Prometheus Adapter
  3. Prometheus Adapter通过http接口发布Metric数据
  4. Prometheus拉取、保存Metric数据,并提供接口供检索数据
  5. 相关工具通过PromQL语法向Prometheus检索数据并展示

参考资料

关注公众号 尹安灿