使用 Azure 门户大规模部署 IoT Edge 模块Deploy IoT Edge modules at scale using the Azure portal

Applies to: yes icon IoT Edge 1.1 yes icon IoT Edge 1.2

在 Azure 门户中创建“IoT Edge 自动部署”,以便同时管理多个设备的正在进行的部署。Create an IoT Edge automatic deployment in the Azure portal to manage ongoing deployments for many devices at once. IoT Edge 的自动部署属于 IoT 中心的自动设备管理功能。Automatic deployments for IoT Edge are part of the automatic device management feature of IoT Hub. 部署是允许将多个模块部署到多个设备、跟踪模块的状态和运行状况以及在必要时做出更改的动态过程。Deployments are dynamic processes that enable you to deploy multiple modules to multiple devices, track the status and health of the modules, and make changes when necessary.

有关详细信息,请参阅了解单个设备或大规模的 IoT Edge 自动部署For more information, see Understand IoT Edge automatic deployments for single devices or at scale.

使用标记标识设备Identify devices using tags

创建部署之前,必须能够指定想要影响的设备。Before you can create a deployment, you have to be able to specify which devices you want to affect. Azure IoT Edge 标识使用设备孪生中的标记标识设备。Azure IoT Edge identifies devices using tags in the device twin. 每个设备都可以具有多个标记,你可以采用适合你的解决方案的任何方式定义这些标记。Each device can have multiple tags that you define in any way that makes sense for your solution.

例如,如果管理有智能楼宇的校园,可以将位置、房间类型和环境标记添加到设备:For example, if you manage a campus of smart buildings, you might add location, room type, and environment tags to a device:

    "building": "20",
    "floor": "2"
  "roomtype": "conference",
  "environment": "prod"

有关设备孪生和标记的详细信息,请参阅了解和使用 IoT 中心的设备孪生For more information about device twins and tags, see Understand and use device twins in IoT Hub.

创建部署Create a deployment

IoT Edge 提供两种不同类型的自动部署,可用于自定义你的方案。IoT Edge provides two different types of automatic deployments that you can use to customize your scenario. 可创建一个标准部署,其中包括系统运行时模块和任何其他模块和路由。You can create a standard deployment, which includes that system runtime modules and any additional modules and routes. 每个设备只能应用一个部署。Each device can only apply one deployment. 也可创建一个分层部署,使其仅包含自定义模块和路由,而不包含系统运行时。Or you can create a layered deployment, which only includes custom modules and routes, not the system runtime. 在标准部署的基础上,可以在设备上组合许多分层部署。Many layered deployments can be combined on a device, on top of a standard deployment. 有关这两种类型的自动部署如何协同工作的详细信息,请参阅了解单设备的 IoT Edge 自动部署或大规模部署For more information about how the two types of automatic deployments work together, see Understand IoT Edge automatic deployments for single devices or at scale.

创建部署和创建分层部署的步骤非常相似。The steps for creating a deployment and a layered deployment are very similar. 任何差异都会在以下步骤中显示出来。Any differences are called out in the following steps.

  1. Azure 门户中,转到 IoT 中心。In the Azure portal, go to your IoT Hub.
  2. 在左窗格的菜单上,选择“自动设备管理”下的“IoT Edge”。On the menu in the left pane, select IoT Edge under Automatic Device Management.
  3. 在上面的栏中,选择“创建部署”或“创建分层部署” 。On the upper bar, select Create Deployment or Create Layered Deployment.

创建部署需要五个步骤。There are five steps to create a deployment. 下列各节将引导完成每个步骤。The following sections walk through each one.


本文中的步骤反映了 IoT Edge 代理和中心的最新架构版本。The steps in this article reflect the latest schema version of 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 the module startup order and route prioritization features.

如果要部署到运行 1.0.9 或更早版本的设备,请在向导的“模块”步骤中编辑“运行时设置”,以使用架构版本 1.0 。If you are deploying to a device running version 1.0.9 or earlier, edit the Runtime Settings in the Modules step of the wizard to use schema version 1.0.

步骤 1:名称和标签Step 1: Name and label

  1. 为部署提供唯一名称(最多包含 128 个小写字母)。Give your deployment a unique name that is up to 128 lowercase letters. 避免空格和以下无效字符:& ^ [ ] { } \ | " < > /Avoid spaces and the following invalid characters: & ^ [ ] { } \ | " < > /.
  2. 可以将标签作为键/值对添加,以帮助跟踪部署。You can add labels as key-value pairs to help track your deployments. 例如,HostPlatformLinux,或 Version3.0.1For example, HostPlatform and Linux, or Version and 3.0.1.
  3. 在完成时选择“下一步:模块”,进入到步骤 2。Select Next: Modules to move to step two.

步骤 2:模块Step 2: Modules

最多可以向一个部署添加 50 个模块。You can add up to 50 modules to a deployment. 如果创建不含模块的部署,就会从目标设备中删除任何当前模块。If you create a deployment with no modules, it removes any current modules from the target devices.

在部署中,你可以管理 IoT Edge 代理和 IoT Edge 中心模块的设置。In deployments, you can manage the settings for the IoT Edge agent and IoT Edge hub modules. 选择“运行时设置”可配置两个运行时模块。Select Runtime Settings to configure the two runtime modules. 在分层部署中,不包括运行时模块,因此无法对其进行配置。In layered deployment, the runtime modules are not included so cannot be configured.

若要将自定义代码添加为模块,或手动添加 Azure 服务模块,请执行以下步骤:To add custom code as a module, or to manually add an Azure service module, follow these steps:

  1. 在此页的“容器注册表设置”部分,提供用于访问包含模块映像的任何专用容器注册表的凭据。In the Container Registry Settings section of the page, provide the credentials to access any private container registries that contain your module images.

  2. 在此页的“IoT Edge 模块”部分,选择“添加” 。In the IoT Edge Modules section of the page, select Add.

  3. 从下拉菜单中选择以下三种模块中的一种:Choose one of the three types of modules from the drop-down menu:

    • IoT Edge 模块 - 提供模块名称和容器映像 URI。IoT Edge Module - You provide the module name and container image URI. 例如,示例 SimulatedTemperatureSensor 模块的映像 URI 为 mcr.microsoft.com/azureiotedge-simulated-temperature-sensor:1.0For example, the image URI for the sample SimulatedTemperatureSensor module is mcr.microsoft.com/azureiotedge-simulated-temperature-sensor:1.0. 如果模块映像存储在专用容器注册表中,则在此页面上添加凭据来访问该映像。If the module image is stored in a private container registry, add the credentials on this page to access the image.
    • 市场模块 - Azure 市场中托管的模块。Marketplace Module - Modules hosted in the Azure Marketplace. 某些市场模块需要其他配置,因此请查看 Azure 市场 IoT Edge 模块列表中的模块详细信息。Some marketplace modules require additional configuration, so review the module details in the Azure Marketplace IoT Edge Modules list.
    • Azure 流分析模块 - 通过 Azure 流分析工作负载生成的模块。Azure Stream Analytics Module - Modules generated from an Azure Stream Analytics workload.
  4. 如果需要,请重复步骤 2 和 3,向部署中添加其他模块。If needed, repeat steps 2 and 3 to add additional modules to your deployment.

将模块添加到部署后,选择模块名称,打开“更新 IoT Edge 模块”页面。After you add a module to a deployment, you can select its name to open the Update IoT Edge Module page. 可在此页面编辑模块设置、环境变量、创建选项、启动顺序和模块孪生。On this page, you can edit the module settings, environment variables, create options, startup order, and module twin. 如果你从市场添加了模块,则该模块可能已经填充了其中的一些参数。If you added a module from the marketplace, it may already have some of these parameters filled in. 有关可用模块设置的详细信息,请参阅模块配置和管理For more information about the available module settings, see Module configuration and management.

如果要创建分层部署,则可能需要配置针对相同设备的其他部署中的模块。If you're creating a layered deployment, you may be configuring a module that exists in other deployments targeting the same devices. 若要更新模块孪生但不覆盖其他版本,请打开“模块孪生设置”选项卡。创建新的“模块孪生属性”,使其具有模块孪生所需属性中某个子节的唯一名称,例如 properties.desired.settingsTo update the module twin without overwriting other versions, open the Module Twin Settings tab. Create a new Module Twin Property with a unique name for a subsection within the module twin's desired properties, for example properties.desired.settings. 如果只在 properties.desired 字段中定义属性,它将覆盖在任何较低优先级部署中定义的模块的所需属性。If you define properties within just the properties.desired field, it will overwrite the desired properties for the module defined in any lower priority deployments.


如需深入了解分层部署中的模块孪生配置信息,请参阅分层部署For more information about module twin configuration in layered deployments, see Layered deployment.

配置部署的所有模块后,选择“下一步: 路由”,移动到步骤 3。Once you have all the modules for a deployment configured, select Next: Routes to move to step three.

步骤 3:路由Step 3: Routes

在“路由”选项卡中,定义消息在模块和 IoT 中心之间传递的方式。On the Routes tab, you define how messages are passed between modules and the IoT Hub. 使用名称/值对构造消息。Messages are constructed using name/value pairs.

例如,名为 route 且值为“FROM /messages/* INTO $upstream”的路由将接受任何模块输出的任何消息并将其发送到 IoT 中心 。For example, a route with a name route and a value FROM /messages/* INTO $upstream would take any messages output by any modules and send them to your IoT hub.

“优先级”和“生存时间”是可选参数,你可在路由定义中包含这些参数 。The Priority and Time to live parameters are optional parameters that you can include in a route definition. 通过优先级参数,你可选择哪些路由的消息应该首先处理,或哪些路由应该最后处理。The priority parameter allows you to choose which routes should have their messages processed first, or which routes should be processed last. 优先级通过设置数字 0-9 来确定,其中 0 为最高优先级。Priority is determined by setting a number 0-9, where 0 is top priority. 通过生存时间参数,你可声明该路由中的消息在被处理或从队列中删除之前应保留多长时间。The time to live parameter allows you to declare how long messages in that route should be held until they're either processed or removed from the queue.

有关如何创建路由的详细信息,请参阅声明路由For more information about how to create routes, see Declare routes.

在完成时选择“下一步: 指标”。Select Next: Metrics.

步骤 4:指标Step 4: Metrics

指标提供应用配置内容后设备可能报告的各种状态的摘要计数。Metrics provide summary counts of the various states that a device may report back as a result of applying configuration content.

  1. 在“指标名称”中输入名称。Enter a name for Metric Name.

  2. 在“指标条件”中输入查询。Enter a query for Metric Criteria. 该查询基于 IoT Edge 中心模块孪生报告的属性The query is based on IoT Edge hub module twin reported properties. 指标表示查询返回的行数。The metric represents the number of rows returned by the query.

    例如:For example:

    SELECT deviceId FROM devices
      WHERE properties.reported.lastDesiredStatus.code = 200

在完成时选择“下一步: 目标设备”。Select Next: Target Devices.

步骤 5:目标设备Step 5: Target devices

使用设备中的标记属性将应接收此部署的特定设备定为目标。Use the tags property from your devices to target the specific devices that should receive this deployment.

因为多个部署可能将同一个设备定为目标,所以应为每个部署提供优先级编号。Since multiple deployments may target the same device, you should give each deployment a priority number. 若有冲突,优先级最高(值越大表示优先级越高)的部署胜出。If there's ever a conflict, the deployment with the highest priority (larger values indicate higher priority) wins. 如果两个部署的优先级编号相同,则将最新创建的部署定为目标。If two deployments have the same priority number, the one that was created most recently wins.

如果多个部署将同一设备定为目标,则只应用优先级较高的部署。If multiple deployments target the same device, then only the one with the higher priority is applied. 如果多个分层部署将同一设备定为目标,则应用全部部署。If multiple layered deployments target the same device then they are all applied. 但如果任何属性都是重复的,例如有两个同名的路由,则来自高优先级分层部署的路由将覆盖其余路由。However, if any properties are duplicated, like if there are two routes with the same name, then the one from the higher priority layered deployment overwrites the rest.

任何将设备定为目标的分层部署都必须具有比基本部署更高的优先级才能被应用。Any layered deployment targeting a device must have a higher priority than the base deployment in order to be applied.

  1. 为部署优先级输入一个正整数。Enter a positive integer for the deployment Priority.
  2. 输入“目标条件”确定将作为此部署的目标的设备。Enter a Target condition to determine which devices will be targeted with this deployment.  该条件基于设备孪生标记或设备孪生报告的属性,应与表达式格式相匹配。The condition is based on device twin tags or device twin reported properties and should match the expression format.  例如 tags.environment='test'properties.reported.devicemodel='4000x'For example, tags.environment='test' or properties.reported.devicemodel='4000x'.

在完成时选择“下一步: 查看 + 创建”,转到最后一步。Select Next: Review + Create to move on to the final step.

步骤 6:查看并创建Step 6: Review and create

查看部署信息,然后选择“创建”。Review your deployment information, then select Create.

若要监视部署,请参阅监视 IoT Edge 部署To monitor your deployment, see Monitor IoT Edge deployments.

修改部署Modify a deployment

修改部署时,更改会立即复制到所有目标设备。When you modify a deployment, the changes immediately replicate to all targeted devices. 可以修改现有部署的以下设置和功能:You can modify the following settings and features for an existing deployment:

  • 目标条件Target conditions
  • 自定义指标Custom metrics
  • 标签Labels
  • TagsTags
  • 所需属性Desired properties

修改目标条件、自定义指标和标签Modify target conditions, custom metrics, and labels

  1. 从 IoT 中心的左窗格菜单中选择“IoT Edge”。In your IoT hub, select IoT Edge from the left pane menu.

  2. 选择“IoT Edge 部署”选项卡,然后选择要配置的部署。Select the IoT Edge deployments tab and then select the deployment you want to configure.

  3. 选择“目标条件”选项卡。更改“目标条件”,将预定设备作为目标。Select the Target Condition tab. Change the Target Condition to target the intended devices. 还可以调整“优先级”。You can also adjust the Priority. 选择“保存” 。Select Save.

    如果更新目标条件,将发生以下更新:If you update the target condition, the following updates occur:

    • 如果设备虽然不满足旧的目标条件,但满足新的目标条件,且此部署是此设备的最高优先级,则此部署将应用到设备。If a device didn't meet the old target condition, but meets the new target condition and this deployment is the highest priority for that device, then this deployment is applied to the device.
    • 如果当前运行此部署的设备不再满足目标条件,则它将卸载此部署并采用下一个最高优先级部署。If a device currently running this deployment no longer meets the target condition, it uninstalls this deployment and takes on the next highest priority deployment.
    • 如果当前运行此部署的设备不再满足目标条件且不满足任何其他部署的目标条件,则此设备上不会发生任何更改。If a device currently running this deployment no longer meets the target condition and doesn't meet the target condition of any other deployments, then no change occurs on the device. 设备在当前状态下继续运行当前模块,但不再作为此部署的一部分被托管。The device continues running its current modules in their current state, but is not managed as part of this deployment anymore. 一旦它满足任何其他部署的目标条件,将卸载此部署并采用新的部署。Once it meets the target condition of any other deployment, it uninstalls this deployment and takes on the new one.
  4. 选择“指标”选项卡,然后单击“编辑指标”按钮。Select the Metrics tab and click the Edit Metrics button. 以示例语法作为指南,添加或修改自定义指标。Add or modify custom metrics, using the example syntax as a guide. 选择“保存” 。Select Save.


  5. 选择“标签”选项卡并进行所需的更改,然后选择“保存”。Select the Labels tab and make any desired changes and select Save.

删除部署Delete a deployment

删除部署时,任何已部署的设备都将执行其下一个具有最高优先级的部署。When you delete a deployment, any deployed devices take on their next highest priority deployment. 如果设备不满足任何其他部署的目标条件,则删除该部署时不会删除模块。If your devices don't meet the target condition of any other deployment, then the modules are not removed when the deployment is deleted.

  1. 登录 Azure 门户并导航到 IoT 中心。Sign in to the Azure portal and navigate to your IoT Hub.

  2. 选择“IoT Edge”。Select IoT Edge.

  3. 选择“IoT Edge 部署”选项卡。Select the IoT Edge Deployments tab.

    查看 IoT Edge 部署

  4. 使用复选框选择想要删除的部署。Use the checkbox to select the deployment that you want to delete.

  5. 选择“删除” 。Select Delete.

  6. 提示将发出以下通知:此操作将删除此部署并还原为所有设备之前的状态。A prompt will inform you that this action will delete this deployment and revert to the previous state for all devices.  将应用优先级较低的部署。A deployment with a lower priority will apply.  如果没有将其他任何部署定为目标,则不会删除任何模块。If no other deployment is targeted, no modules will be removed. 若要从设备中删除所有模块,请创建零模块部署,并将它部署到相同设备。If you want to remove all modules from your device, create a deployment with zero modules and deploy it to the same devices.  选择“是”继续。Select Yes to continue.

后续步骤Next steps

详细了解将模块部署到 IoT Edge 设备Learn more about Deploying modules to IoT Edge devices.