使用 Azure CLI 部署 Azure IoT Edge 模块Deploy Azure IoT Edge modules with Azure CLI

使用业务逻辑创建 IoT Edge 模块后,需要将其部署到设备后才能在边缘操作。Once you create IoT Edge modules with your business logic, you want to deploy them to your devices to operate at the edge. 如果多个模块共同协作来收集和处理数据,可同时部署它们并声明用于连接它们的路由规则。If you have multiple modules that work together to collect and process data, you can deploy them all at once and declare the routing rules that connect them.

Azure CLI 是一个开源跨平台命令行工具,用于管理 IoT Edge 等 Azure 资源。Azure CLI is an open-source cross platform command-line tool for managing Azure resources such as IoT Edge. 使用 Azure CLI 2.0 可以管理 Azure IoT 中心资源、设备预配服务实例和现成的链接中心。It enables you to manage Azure IoT Hub resources, device provisioning service instances, and linked-hubs out of the box. 新的 IoT 扩展丰富了 Azure CLI 的功能,例如设备管理和完整的 IoT Edge 功能。The new IoT extension enriches Azure CLI with features such as device management and full IoT Edge capability.

本文介绍了如何创建 JSON 部署清单,然后使用此文件将部署推送至 IoT Edge 设备。This article shows how to create a JSON deployment manifest, then use that file to push the deployment to an IoT Edge device. 要了解如何创建基于设备的共享标记而面向多台设备的部署,请参阅大规模地部署和监视 IoT Edge 模块For information about creating a deployment that targets multiple devices based on their shared tags, see Deploy and monitor IoT Edge modules at scale

先决条件Prerequisites

配置部署清单Configure a deployment manifest

部署清单是一个 JSON 文档,其中描述了要部署的模块、数据在模块间的流动方式以及模块孪生的所需属性。A deployment manifest is a JSON document that describes which modules to deploy, how data flows between the modules, and desired properties of the module twins. 若要详细了解部署清单的工作原理及创建方式,请参阅了解如何使用、配置和重用 IoT Edge 模块For more information about how deployment manifests work and how to create them, see Understand how IoT Edge modules can be used, configured, and reused.

若要使用 Azure CLI 来部署模块,请将部署清单在本地另存为 .json 文件。To deploy modules using the Azure CLI, save the deployment manifest locally as a .json file. 在下一部分通过运行命令将配置应用到设备时,会用到这个文件路径。You will use the file path in the next section when you run the command to apply the configuration to your device.

下面是一个基本的部署清单示例,其中有一个模块:Here's a basic deployment manifest with one module as an example:

备注

此示例部署清单使用 IoT Edge 代理和中心的架构版本 1.1。This sample deployment manifest uses schema version 1.1 for the IoT Edge agent and hub. 架构版本 1.1 随 IoT Edge 版本 1.0.10 一起发布,支持模块启动顺序和路由优先级等功能。Schema version 1.1 was released along with IoT Edge version 1.0.10, and enables features like module startup order and route prioritization.

{
  "content": {
    "modulesContent": {
      "$edgeAgent": {
        "properties.desired": {
          "schemaVersion": "1.1",
          "runtime": {
            "type": "docker",
            "settings": {
              "minDockerVersion": "v1.25",
              "loggingOptions": "",
              "registryCredentials": {}
            }
          },
          "systemModules": {
            "edgeAgent": {
              "type": "docker",
              "settings": {
                "image": "mcr.microsoft.com/azureiotedge-agent:1.0",
                "createOptions": "{}"
              }
            },
            "edgeHub": {
              "type": "docker",
              "status": "running",
              "restartPolicy": "always",
              "settings": {
                "image": "mcr.microsoft.com/azureiotedge-hub:1.0",
                "createOptions": "{\"HostConfig\":{\"PortBindings\":{\"5671/tcp\":[{\"HostPort\":\"5671\"}],\"8883/tcp\":[{\"HostPort\":\"8883\"}],\"443/tcp\":[{\"HostPort\":\"443\"}]}}}"
              }
            }
          },
          "modules": {
            "SimulatedTemperatureSensor": {
              "version": "1.0",
              "type": "docker",
              "status": "running",
              "restartPolicy": "always",
              "settings": {
                "image": "mcr.microsoft.com/azureiotedge-simulated-temperature-sensor:1.0",
                "createOptions": "{}"
              }
            }
          }
        }
      },
      "$edgeHub": {
        "properties.desired": {
          "schemaVersion": "1.1",
          "routes": {
            "upstream": "FROM /messages/* INTO $upstream"
          },
          "storeAndForwardConfiguration": {
            "timeToLiveSecs": 7200
          }
        }
      },
      "SimulatedTemperatureSensor": {
        "properties.desired": {
          "SendData": true,
          "SendInterval": 5
        }
      }
    }
  }
}

部署到设备Deploy to your device

应用使用模块信息配置的部署清单即可将模块部署至设备。You deploy modules to your device by applying the deployment manifest that you configured with the module information.

将目录更改到保存有部署清单的文件夹。Change directories into the folder where your deployment manifest is saved. 如果使用了 VS Code IoT Edge 模板之一,请使用解决方案目录的 config 文件夹中的 deployment.json 文件,而不是使用 deployment.template.json 文件。If you used one of the VS Code IoT Edge templates, use the deployment.json file in the config folder of your solution directory and not the deployment.template.json file.

使用以下命令将配置应用于 IoT Edge 设备:Use the following command to apply the configuration to an IoT Edge device:

az iot edge set-modules --device-id [device id] --hub-name [hub name] --content [file path]

device ID 参数区分大小写。The device ID parameter is case-sensitive. content 参数指向你保存的部署清单文件。The content parameter points to the deployment manifest file that you saved.

az iot edge set-modules output

查看设备上的模块View modules on your device

将模块部署到设备后,可以使用以下命令查看所有模块:Once you've deployed modules to your device, you can view all of them with the following command:

查看 IoT Edge 设备上的模块:View the modules on your IoT Edge device:

az iot hub module-identity list --device-id [device id] --hub-name [hub name]

device ID 参数区分大小写。The device ID parameter is case-sensitive.

az iot hub module-identity list output

后续步骤Next steps

了解如何大规模地部署和监视 IoT Edge 模块Learn how to Deploy and monitor IoT Edge modules at scale