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

在 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:

"tags":{
  "location":{
    "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.

步骤 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

最多可以向一个部署添加 30 个模块。You can add up to 30 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.

可以添加三种类型的模块:You can add three types of modules:

  • IoT Edge 模块IoT Edge Module
  • 市场模块Marketplace Module
  • Azure 流分析模块Azure Stream Analytics Module

添加 IoT Edge 模块Add an IoT Edge module

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

  1. 在页面的“容器注册表凭据”部分中,为包含此部署模块映像的任何专用容器注册表提供名称和凭据。In the Container Registry Credentials section of the page, provide the names and credentials for any private container registries that contain the module images for this deployment. 如果找不到 Docker 映像的容器注册表凭据,IoT Edge 代理会报告错误 500。The IoT Edge Agent will report error 500 if it can't find the container registry credential for a Docker image.
  2. 在页面的“IoT Edge 模块”部分,单击“添加” 。In the IoT Edge Modules section of the page, click Add.
  3. 从下拉菜单中选择“IoT Edge 模块”。Select IoT Edge Module from the drop-down menu.
  4. 为模块指定“IoT Edge 模块名称”。Give your module a IoT Edge Module Name.
  5. 对于“映像 URI”字段,输入模块的容器映像。For the Image URI field, enter the container image for your module.
  6. 使用下拉菜单选择“重启策略”。Use the drop-down menu to select a Restart policy. 从以下选项中选择:Choose from the following options:
    • 始终 - 如果模块因任何原因关闭,它始终会重启。always - The module always restarts if it shuts down for any reason.
    • 从不 - 如果模块因任何原因关闭,该模块将不再重启。never - The module never restarts if it shuts down for any reason.
    • 运行故障时 - 如果模块发生故障,它将重启,但如果完全关闭,则不重启。on-failure - The module restarts if it crashes, but not if it shuts down cleanly.
    • 运行不正常时 - 如果模块发生故障或者返回不正常状态,它将重启。on-unhealthy - The module restarts if it crashes or returns an unhealthy status. 这取决于每个执行运行状况监控功能的模块。It's up to each module to implement the health status function.
  7. 使用下拉菜单选择模块的所需状态。Use the drop-down menu to select the Desired Status for the module. 从以下选项中选择:Choose from the following options:
    • 正在运行 -“正在运行”是默认选项。running - Running is the default option. 该模块在部署之后将立即开始运行。The module will start running immediately after being deployed.
    • 已停止 - 部署之后,模块将保持空闲状态,直到你或另一个模块要求启动。stopped - After being deployed, the module will remain idle until called upon to start by you or another module.
  8. 指定应传递到容器的任意容器创建选项。Specify any Container Create Options that should be passed to the container. 有关详细信息,请参阅 docker createFor more information, see docker create.
  9. 如果要向模块孪生添加标记或其他属性,请选择“设置模块孪生的所需属性”。Select Module Twin Settings if you want to add tags or other properties to the module twin.
  10. 输入此模块的“环境变量”。Enter Environment Variables for this module. 环境变量为模块提供配置信息。Environment variables provide configuration information to a module.
  11. 选择“添加”,将模块添加到部署中。Select Add to add your module to the deployment.

从市场添加模块Add a module from the Marketplace

若要从 Azure 市场添加模块,请执行以下步骤:To add a module from the Azure Marketplace, follow these steps:

  1. 在页面的“IoT Edge 模块”部分,单击“添加” 。In the IoT Edge Modules section of the page, click Add.
  2. 从下拉菜单中选择“市场模块”。Select Marketplace Module from the drop-down menu.
  3. 从“IoT Edge 模块市场”页面选择一个模块。Choose a module from the IoT Edge Module Marketplace page. 系统会自动为你的订阅、资源组和设备配置所选的模块。The module you select is automatically configured for your subscription, resource group, and device. 然后,所选模块将显示在 IoT Edge 模块列表中。It then appears in your list of IoT Edge modules. 某些模块可能需要其他配置。Some modules may require additional configuration. 有关详细信息,请参阅从 Azure 市场部署模块For more information, see Deploy modules from Azure Marketplace.

添加流分析模块Add a Stream Analytics module

要添加 Azure 流分析中的模块,请执行以下步骤:To add a module from Azure Stream Analytics, follow these steps:

  1. 在页面的“IoT Edge 模块”部分,单击“添加” 。In the IoT Edge Modules section of the page, click Add.
  2. 从下拉菜单中选择“Azure 流分析模块”。Select Azure Stream Analytics module from the drop-down menu.
  3. 在右窗格中,选择你的订阅On the right pane, choose your Subscription.
  4. 选择你的 IoT Edge 作业。Choose your IoT Edge job.
  5. 选择“保存”,将模块添加到部署。Select Save to add your module to the deployment.

配置模块设置Configure module settings

将模块添加到部署后,选择模块名称,打开“更新 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, and module twin. 如果你从市场添加了模块,则该模块可能已经填充了其中的一些参数。If you added a module from the marketplace, it may already have some of these parameters filled in.

如果要创建分层部署,则可能需要配置针对相同设备的其他部署中的模块。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

路由定义模块在部署中如何相互通信。Routes define how modules communicate with each other within a deployment. 默认情况下,向导会提供名为“上游”的路由,并将其定义为“从 /messages/* 到 $upstream”,这意味着任何模块输出的任何消息都将发送到 IoT 中心 。By default the wizard gives you a route called upstream and defined as FROM /messages/* INTO $upstream, which means that any messages output by any modules are sent to your IoT hub.

在路由中添加或更新声明路由中的信息,再选择“下一步”继续转到评审部分。Add or update the routes with information from Declare routes, then select Next to continue to the review section.

在完成时选择“下一步: 指标”。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.