你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

Azure Kubernetes 服务的自动检测(预览)

重要

有关 beta 版本、预览版或尚未正式发布的版本的 Azure 功能所适用的法律条款,请参阅 Microsoft Azure 预览版的补充使用条款

本指南逐步讲解如何为 Azure Kubernetes 服务 (AKS) 工作负载启用 Azure Monitor Application Insights,而无需修改源代码。

我们将介绍如何 安装 aks-preview Azure CLI 扩展注册 AzureMonitorAppMonitoringPreview 功能标志准备群集载入部署重启部署。 这些步骤会导致自动检测将 Azure Monitor OpenTelemetry Distro 注入到应用程序 Pod 中来生成遥测数据。 有关自动结构及其优点的详细信息,请参阅 什么是 Azure Monitor Application Insights 的自动结构?

先决条件

警告

  • 此功能与 Windows(任何体系结构)和 Linux Arm64 节点池不兼容。

安装 aks-preview Azure CLI 扩展

重要

AKS 预览功能是可选择启用的自助功能。 预览版按“现状”和“视供应情况”提供,它们不包括在服务级别协议和有限保证范围内。 AKS 预览功能是由客户支持尽最大努力部分覆盖。 因此,这些功能并不适合用于生产。 有关详细信息,请参阅以下支持文章:

安装 aks-preview 扩展:

az extension add --name aks-preview

更新到最新版本的扩展:

az extension update --name aks-preview

验证已安装的 Azure CLI 版本是否满足 先决条件 部分中的要求:

az version

如果版本不符合要求,请按照前面提到的步骤安装和更新 Azure CLI。

注册 AzureMonitorAppMonitoringPreview 功能标志

# Log into Azure CLI
az login

# Register the feature flag for Azure Monitor App Monitoring in preview
az feature register --namespace "Microsoft.ContainerService" --name "AzureMonitorAppMonitoringPreview"

# List the registration state of the Azure Monitor App Monitoring Preview feature
# It could take hours for the registration state to change from Registering to Registered
az feature list -o table --query "[?contains(name, 'Microsoft.ContainerService/AzureMonitorAppMonitoringPreview')].{Name:name,State:properties.state}"

# Once the feature shows as Registered in the prior step, re-register the Microsoft.ContainerService provider to apply the new feature settings
az provider register --namespace "Microsoft.ContainerService"

# Check the registration state of the Microsoft.ContainerService provider
az provider show --namespace "Microsoft.ContainerService" --query "registrationState"

准备群集

若要准备群集,请运行以下 Azure CLI 命令。

az aks update --resource-group={resource_group} --name={cluster_name} --enable-azure-monitor-app-monitoring 

小窍门

AKS 群集可以在创建群集期间为此功能做好准备。 若要了解详细信息,请参阅 在 AKS 群集创建期间准备群集

载入部署

可以通过两种方式载入部署:命名空间范围方法或按部署方法。 使用命名空间范围的方法在命名空间中载入所有部署。 对于跨多个部署进行选择性或可变配置的载入,请使用按部署方法。

命名空间范围的载入

为了在命名空间中整合所有部署,请在每个命名空间中创建一个名为 的检测自定义资源。default 更新 applicationInsightsConnectionString,以设置 Application Insights 资源的连接字符串。

小窍门

可以从 Application Insights 资源的概述页检索连接字符串。

apiVersion: monitor.azure.com/v1
kind: Instrumentation
metadata:
  name: default
  namespace: mynamespace1
spec:
  settings:
    autoInstrumentationPlatforms: []
  destination: # required
    applicationInsightsConnectionString: "InstrumentationKey=11111111-1111-1111-1111-111111111111;IngestionEndpoint=https://eastus2-3.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus2.livediagnostics.monitor.azure.com/"

至少需要以下配置:

  • spec.settings.autoInstrumentationPlatforms:基于 Pod 正在运行的语言的一个或多个值。
  • spec.destination.applicationInsightsConnectionString:Application Insights 资源的连接字符串。

小窍门

  • 如果需要按部署替代,请使用“注释”。 有关详细信息,请参阅 批注
  • 重启部署 ,使设置生效。

按部署载入

使用按部署载入来确保使用特定语言检测部署,或将遥测定向到单独的 Application Insights 资源。

  1. 为每个场景创建唯一的检测自定义资源。 请避免使用名称default,该名称用于命名空间范围内的初始化。

    创建检测自定义资源以在每个命名空间中配置 Application Insights。 更新 applicationInsightsConnectionString,以设置 Application Insights 资源的连接字符串。

    小窍门

    可以从 Application Insights 资源的概述页检索连接字符串。

    apiVersion: monitor.azure.com/v1
    kind: Instrumentation
    metadata:
      name: cr1
      namespace: mynamespace1
    spec:
      settings:
        autoInstrumentationPlatforms: []
      destination: # required
        applicationInsightsConnectionString: "InstrumentationKey=11111111-1111-1111-1111-111111111111;IngestionEndpoint=https://eastus2-3.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus2.livediagnostics.monitor.azure.com/"
    

    至少需要以下配置:

    • spec.destination.applicationInsightsConnectionString:Application Insights 资源的连接字符串。
  2. 使用 批注将每个部署与相应的自定义资源相关联。 批注将替代自定义资源中设置的语言。

    重要

    为了避免将它们误添加到部署的注释,请在部署的 spec.template.metadata.annotations 级别添加注释。

    例子:

    • Java:instrumentation.opentelemetry.io/inject-java: "cr1"
    • Node.js:instrumentation.opentelemetry.io/inject-nodejs: "cr1"

    批注放置应如下所示。

    apiVersion: apps/v1
    kind: Deployment
    ...
    spec:
      template:
        metadata:
          annotations:
            instrumentation.opentelemetry.io/inject-nodejs: "cr1"
    

小窍门

重启部署 ,使设置生效。

混合模式加入

大多数部署使用默认配置时使用混合模式,一些部署必须使用不同的配置。

  1. 实现 命名空间范围的载入 以定义默认配置。
  2. 创建按部署加入配置,这会替代特定资源的默认配置。

重启部署

在创建所有自定义资源并选择性地批注部署后运行以下命令。

kubectl rollout restart deployment <deployment-name> -n mynamespace1

此命令会导致自动检测生效,从而启用 Application Insights。 可以通过生成流量并导航到资源来验证 Application Insights 是否已启用。 你的应用在 Application Insights 体验中被表示为云端角色。 可以使用除实时指标和 Application Insights Code Analysis 功能之外的所有 Application Insights 体验。 在此处详细了解可用的 Application Insights 体验。

移除 AKS 的自动检测

确保没有任何已检测的部署。 要取消检测已检测的部署,请删除关联的检测自定义资源,并在部署上运行 kubectl rollout restart。 接下来运行以下命令。

az aks update --resource-group={resource_group} --name={cluster_name} --disable-azure-monitor-app-monitoring 

注释

如果已检测的部署在禁用该功能后仍然保留,则在重新部署到其原始未检测状态或将其删除之前,将会继续对其进行检测。

批注

禁用自动检测

以下注释禁用所指示语言的自动检测。

  • Java:instrumentation.opentelemetry.io/inject-java

  • Node.js:instrumentation.opentelemetry.io/inject-nodejs

    instrumentation.opentelemetry.io/inject-java: "false"
    

禁用后重新启用自动检测。

instrumentation.opentelemetry.io/inject-java: "true"

批注放置应如下所示。

apiVersion: apps/v1
kind: Deployment
...
spec:
  template:
    metadata:
      annotations:
        instrumentation.opentelemetry.io/inject-java: "false"

在 Application Insights 中启用日志

可以选择在 Application Insights 中收集日志,作为对容器见解日志的补充或替代。

在 Application Insights 中启用日志可提供相关日志,使用户能够轻松查看分布式跟踪及其相关日志。 此外,某些微服务不会将日志写入控制台,因此 Container Insights 无法收集日志,并且仅 Application Insights 检测捕获这些日志。

相反,Application Insights 可能无法检测所有微服务。 例如,使用 NGINX 或不支持语言的自动检测。 对于此类微服务,用户可能更喜欢仅依赖容器见解日志。

如果有多个可观测性团队(例如使用 Container Insights 的基础结构工程师)和使用 Application Insights 的开发人员,还可以选择为日志启用这两个源。

查看应用程序的代码中的控制台日志记录配置,以确定是要启用 Application Insights 日志、容器见解日志还是同时启用这两者。 如果禁用容器洞察日志,请参阅 容器洞察设置

重要

为避免不必要的重复和增加成本,请启用 Application Insights 中的日志,以允许该功能从标准日志记录框架收集应用程序日志并将其发送到 Application Insights。

使用以下注释在 Application Insights 中启用日志

  • monitor.azure.com/enable-application-logs

重要

为了避免将它们误添加到部署的注释,请在部署的 spec.template.metadata.annotations 级别添加注释。

monitor.azure.com/enable-application-logs: "true"

在创建 AKS 群集期间准备群集

AKS 群集可以在创建群集期间为此功能做好准备。 如果想要在创建期间准备群集,请运行以下 Azure CLI 命令。 应用程序监视不会因群集已准备好就自动启用。 必须部署应用程序并将应用程序加入此功能。

az aks create --resource-group={resource_group} --name={cluster_name} --enable-azure-monitor-app-monitoring --generate-ssh-keys

后续步骤