4.4 KiB
| title | description | weight | keywords | owner | test | ||
|---|---|---|---|---|---|---|---|
| 使用 Telemetry API 配置访问日志 | 此任务向您演示如何使用 Telemetry API 配置 Envoy 代理来发送访问日志。 | 10 |
|
istio/wg-policies-and-telemetry-maintainers | yes |
Telemetry API 如今在 Istio 中作为核心 API 已经有一段时间了。
之前用户必须在 Istio 的 MeshConfig 中配置 Telemetry。
{{< boilerplate before-you-begin-egress >}}
{{< boilerplate start-httpbin-service >}}
安装
在本例中,我们将发送日志到 Grafana Loki,确保它已被安装。
{{< text syntax=bash snip_id=install_loki >}} $ istioctl install -f @samples/open-telemetry/loki/iop.yaml@ --skip-confirmation $ kubectl apply -f @samples/addons/loki.yaml@ -n istio-system $ kubectl apply -f @samples/open-telemetry/loki/otel.yaml@ -n istio-system {{< /text >}}
Telemetry API 入门
-
启用访问日志记录
{{< text bash >}} $ cat <<EOF | kubectl apply -n istio-system -f - apiVersion: telemetry.istio.io/v1alpha1 kind: Telemetry metadata: name: mesh-logging-default spec: accessLogging:
- providers:
- name: otel EOF {{< /text >}}
这个示例使用内置的
envoy访问日志提供程序,我们除了默认设置外没有进行任何其他配置。 - providers:
-
禁用特定工作负载的访问日志
您可以使用以下配置禁用
sleep服务的访问日志:{{< text bash >}} $ cat <<EOF | kubectl apply -n default -f - apiVersion: telemetry.istio.io/v1alpha1 kind: Telemetry metadata: name: disable-sleep-logging namespace: default spec: selector: matchLabels: app: sleep accessLogging:
- providers:
- name: otel disabled: true EOF {{< /text >}}
- providers:
-
通过工作负载模式过滤访问日志
您可以使用以下配置禁用
httpbin服务的入站访问日志:{{< text bash >}} $ cat <<EOF | kubectl apply -n default -f - apiVersion: telemetry.istio.io/v1alpha1 kind: Telemetry metadata: name: disable-httpbin-logging spec: selector: matchLabels: app: httpbin accessLogging:
- providers:
- name: otel match: mode: SERVER disabled: true EOF {{< /text >}}
- providers:
-
通过 CEL 表达式过滤访问日志
只有响应码大于等于 500 时,以下配置才显示访问日志:
{{< text bash >}} $ cat <<EOF | kubectl apply -n default -f - apiVersion: telemetry.istio.io/v1alpha1 kind: Telemetry metadata: name: filter-sleep-logging spec: selector: matchLabels: app: sleep accessLogging:
- providers:
- name: otel filter: expression: response.code >= 500 EOF {{< /text >}}
- providers:
-
通过 CEL 表达式设置默认的过滤访问日志
只有响应码大于等于 400 或请求转到 BlackHoleCluster 或 PassthroughCluster 时, 以下配置才显示访问日志(注意
xds.cluster_name仅可用于 Istio 1.16.2 及更高版本):{{< text bash >}} $ cat <<EOF | kubectl apply -f - apiVersion: telemetry.istio.io/v1alpha1 kind: Telemetry metadata: name: default-exception-logging namespace: istio-system spec: accessLogging:
- providers:
- name: otel filter: expression: "response.code >= 400 || xds.cluster_name == 'BlackHoleCluster' || xds.cluster_name == 'PassthroughCluster' "
EOF {{< /text >}}
有关更多信息,请参阅使用赋值表达式。
- providers:
使用 OpenTelemetry 提供程序
Istio 支持使用 OpenTelemetry 协议发送访问日志, 如此处所述。
清理
-
移除所有 Telemetry API:
{{< text bash >}} $ kubectl delete telemetry --all -A {{< /text >}}
-
移除
loki:{{< text bash >}} $ kubectl delete -f @samples/addons/loki.yaml@ -n istio-system $ kubectl delete -f @samples/open-telemetry/loki/otel.yaml@ -n istio-system {{< /text >}}
-
从集群中卸载 Istio:
{{< text bash >}} $ istioctl uninstall --purge --skip-confirmation {{< /text >}}