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

入门:在 Azure IoT 操作预览版中使用脚本配置可观测性

重要

Azure IoT 操作预览版(由 Azure Arc 启用)当前处于预览状态。 不应在生产环境中使用此预览版软件。

在正式发布版本推出时,你需要部署新的 Azure IoT 操作安装;无法升级预览版安装。

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

可观测性提供对 Azure IoT 操作配置的每一层的可见性。 它让你深入了解问题的实际行为,从而提高站点可靠性工程的有效性。 Azure IoT 操作通过托管在 Azure 中的自定义策展 Grafana 仪表板提供可观测性。 这些仪表板由适用于 Prometheus 的 Azure Monitor 托管服务和容器见解提供支持。 本文介绍如何配置可观测性所需的服务。

先决条件

配置订阅

运行以下代码,将提供程序注册到群集所在的订阅。

注意

对于每个订阅,只需运行此步骤一次。 若要注册资源提供程序,你需要有执行 /register/action 操作的权限,该权限包含在订阅“参与者”和“所有者”角色中。 有关详细信息,请参阅 Azure 资源提供程序和类型

az account set -s <subscription-id>
az provider register -n "Microsoft.Insights"
az provider register -n "Microsoft.AlertsManagement"

安装可观测性组件

本部分中的步骤用于安装共享监视资源,并将已启用 Arc 的群集配置为向这些资源发出可观测信号。 共享监视资源包括 Azure 托管 Grafana、Azure Monitor 工作区、Azure 托管 Prometheus、Azure Log Analytics 和容器见解。

  1. 在控制台中,转到要用于克隆 Azure IoT 操作存储库的本地文件夹:

    注意

    存储库包含 Azure IoT 操作的部署定义,以及包括本文中使用的示例仪表板的示例。

  2. 使用以下命令将存储库克隆到本地计算机:

    git clone https://github.com/Azure/azure-iot-operations.git
    
  3. 浏览到存储库本地副本中的以下路径:

    azure-iot-operations\tools\setup-3p-obs-infra

  4. 若要部署可观测性组件,请运行以下命令。 使用要监视的已启用 Arc 的群集的订阅 ID 和资源组。

    注意

    若要发现可以设置的其他可选参数,请参阅 bicep 文件。 可选参数可以指定群集资源的备用位置等内容。

    az deployment group create \
          --subscription <subscription-id> \
          --resource-group <cluster-resource-group> \
          --template-file observability-full.bicep \
          --parameters grafanaAdminId=$(az ad user show --id $(az account show --query user.name --output tsv) --query=id --output tsv) \
                        clusterName=<cluster-name> \
                        sharedResourceGroup=<shared-resource-group> \
                        sharedResourceLocation=<shared-resource-location> \
          --query=properties.outputs
    

    上一个命令向运行新创建的 Grafana 实例的用户授予对该实例的管理员访问权限。 如果该访问权限不是所需的,请改为运行以下命令。 要让每个人都可以访问 Grafana 实例,需要手动设置权限。

    az deployment group create \
        --subscription <subscription-id> \
        --resource-group <cluster-resource-group> \
        --template-file observability-full.bicep \
        --parameters clusterName=<cluster-name> \
                      sharedResourceGroup=<shared-resource-group> \
                      sharedResourceLocation=<shared-resource-location> \
        --query=properties.outputs
    

    若要手动设置权限,请为所有应具有访问权限的用户添加针对 Grafana 实例的角色分配。 根据所需的访问级别,分配 Grafana 角色之一(Grafana 管理员、Grafana 编辑者、Grafana 查看者)。

如果部署成功,则命令输出末尾会打印一些信息。 这些信息包括 Grafana URL 和已创建的 Log Analytics 和 Azure Monitor 资源的资源 ID。 通过 Grafana URL 可以转到在将仪表板部署到 Grafana 中配置的 Grafana 实例。 通过这两个资源 ID,你可以按照将已启用 Arc 的群集添加到现有可观测性基础结构中的步骤来配置其他已启用 Arc 的群集。

配置 Prometheus 指标收集

  1. 将以下配置复制并粘贴到名为 ama-metrics-prometheus-config.yaml 的新文件中,并保存该文件:

    apiVersion: v1
    data:
      prometheus-config: |2-
            scrape_configs:
            - job_name: e4k
              scrape_interval: 1m
              static_configs:
              - targets:
                - aio-mq-diagnostics-service.azure-iot-operations.svc.cluster.local:9600
            - job_name: nats
              scrape_interval: 1m
              static_configs:
              - targets:
                - aio-dp-msg-store-0.aio-dp-msg-store-headless.azure-iot-operations.svc.cluster.local:7777
            - job_name: otel
              scrape_interval: 1m
              static_configs:
              - targets:
                - aio-otel-collector.azure-iot-operations.svc.cluster.local:8889
            - job_name: aio-annotated-pod-metrics
              kubernetes_sd_configs:
              - role: pod
              relabel_configs:
              - action: drop
                regex: true
                source_labels:
                - __meta_kubernetes_pod_container_init
              - action: keep
                regex: true
                source_labels:
                - __meta_kubernetes_pod_annotation_prometheus_io_scrape
              - action: replace
                regex: ([^:]+)(?::\\d+)?;(\\d+)
                replacement: $1:$2
                source_labels:
                - __address__
                - __meta_kubernetes_pod_annotation_prometheus_io_port
                target_label: __address__
              - action: replace
                source_labels:
                - __meta_kubernetes_namespace
                target_label: kubernetes_namespace
              - action: keep
                regex: 'azure-iot-operations'
                source_labels:
                - kubernetes_namespace
              scrape_interval: 1m
    kind: ConfigMap
    metadata:
      name: ama-metrics-prometheus-config
      namespace: kube-system
    
  2. 若要应用创建的配置文件,请运行以下命令:

    kubectl apply -f ama-metrics-prometheus-config.yaml

将仪表板部署到 Grafana

Azure IoT 操作提供了一系列仪表板,旨在提供了解 Azure IoT 操作部署的运行状况和性能所需的许多可视化效果。

完成以下步骤以安装 Azure IoT 操作策展 Grafana 仪表板。

  1. 登录到 Grafana 控制台,然后在 Grafana 应用程序的右上角,选择 + 图标

  2. 选择“导入仪表板”,按照提示浏览到存储库本地克隆副本中的 samples\grafana-dashboards 路径,然后选择 JSON 仪表板文件

  3. 当应用程序提示时,选择托管 Prometheus 数据源

  4. 选择导入