使用 Visual Studio Code 大规模部署 IoT Edge 模块Deploy IoT Edge modules at scale using Visual Studio Code

可以使用 Visual Studio Code 接口创建 IoT Edge 自动部署,以便同时管理多个设备的持续部署。You can create an IoT Edge automatic deployment using Visual Studio Code 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. 还可以跟踪模块的状态和运行状况,并按需进行更改。You can also 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.

在本文中,你将设置 Visual Studio Code 和 IoT 扩展。In this article, you set up Visual Studio Code and the IoT extension. 然后,了解如何将模块部署到一组 IoT Edge 设备。You then learn how to deploy modules to a set of IoT Edge devices.

先决条件Prerequisites

登录以访问 IoT 中心Sign in to access your IoT hub

可以使用 Visual Studio Code 的 Azure IoT 扩展来执行与中心相关的操作。You can use the Azure IoT extensions for Visual Studio Code to do operations with your Hub. 为让操作顺利进行,需登录到 Azure 帐户并选择要使用的 IoT 中心。For these operations to work, you need to sign into your Azure account and select the IoT hub that you are working on.

  1. 在 Visual Studio Code 中打开“资源管理器”视图 。In Visual Studio Code, open the Explorer view.

  2. 在资源管理器底部,展开“Azure IoT 中心”部分 。At the bottom of the Explorer, expand the Azure IoT Hub section.

  3. 单击“Azure IoT 中心”部分标题中的“...” 。Click on the ... in the Azure IoT Hub section header. 如果没有看到省略号,请将鼠标悬停在标题处。If you don't see the ellipsis, hover over the header.

  4. 选择“选择 IoT 中心” 。Choose Select IoT Hub.

  5. 如果尚未登录 Azure 帐户,请按照提示登录。If you are not signed into your Azure account, follow the prompts to do so.

  6. 选择 Azure 订阅。Select your Azure subscription.

  7. 选择 IoT 中心。Select your IoT hub.

配置部署清单Configure a deployment manifest

部署清单是一个 JSON 文档,用于描述要部署的模块。A deployment manifest is a JSON document that describes which modules to deploy. 它还描述了模块之间的数据流动方式,以及模块孪生的所需属性。It also describes how data flows between the modules, and desired properties of the module twins. 有关详细信息,请参阅了解如何在 IoT Edge 中部署模块和建立路由For more information, see Learn how to deploy modules and establish routes in IoT Edge.

若要使用 Visual Studio Code 部署模块,请将部署清单本地保存为 .JSON 文件。To deploy modules using Visual Studio Code, save the deployment manifest locally as a .JSON file. 运行命令将配置应用到设备时,需要提供清单的位置。You will need to provide its location when you run the command to apply the configuration to your device.

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

{
  "content": {
    "modulesContent": {
      "$edgeAgent": {
        "properties.desired": {
          "schemaVersion": "1.0",
          "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.0",
          "routes": {
            "upstream": "FROM /messages/* INTO $upstream"
          },
          "storeAndForwardConfiguration": {
            "timeToLiveSecs": 7200
          }
        }
      },
      "SimulatedTemperatureSensor": {
        "properties.desired": {
          "SendData": true,
          "SendInterval": 5
        }
      }
    }
  }
}

如果需要确定当前可以配置哪些 IoT Edge 设备,请运行“IoT Edge: 获取设备信息”命令。If you need to determine which IoT Edge devices you can currently configure, run the IoT Edge: Get Device Info command.

使用目标条件标识设备Identify devices with target conditions

若要标识接收部署的 IoT Edge 设备,必须指定目标条件。To identify the IoT Edge devices that are to receive the deployment, you must specify a target condition. 当 deviceId、标记值或报告属性值与指定的条件匹配时,将满足目标条件。A target condition is met when specified criteria is matched by a deviceId, tag value, or a reported property value.

在设备孪生中配置标记。You configure tags in the device twin. 下面是具有标记的设备孪生示例:Here is an example of a device twin that has tags:

"tags":{
  "location":{
    "building": "20",
    "floor": "2"
  },
  "roomtype": "conference",
  "environment": "prod"
}

如果部署的目标条件包含与某个标记值匹配的表达式(例如 tag.location.building = '20'),则此设备将接收部署。This device will receive a deployment if the target condition for the deployment contains an expression that matches one of the tag's values, such as tag.location.building = '20'.

若要以特定的设备为目标,而不管其标记或其他值为何,只需指定目标条件的 deviceIdIf you want to target a specific device regardless of its tags or other values, just specify the deviceId for the target condition.

下面提供了更多示例:Here are some more examples:

  • deviceId ='linuxprod1'deviceId ='linuxprod1'
  • deviceId = 'linuxprod1' OR deviceId = 'linuxprod2' OR deviceId = 'linuxprod3'deviceId = 'linuxprod1' OR deviceId = 'linuxprod2' OR deviceId = 'linuxprod3'
  • tags.environment ='prod'tags.environment ='prod'
  • tags.environment = 'prod' AND tags.location = 'chinaeast2'tags.environment = 'prod' AND tags.location = 'chinaeast2'
  • tags.environment = 'prod' OR tags.location = 'chinaeast2'tags.environment = 'prod' OR tags.location = 'chinaeast2'
  • tags.operator = 'John' AND tags.environment = 'prod' AND NOT deviceId = 'linuxprod1'tags.operator = 'John' AND tags.environment = 'prod' AND NOT deviceId = 'linuxprod1'

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

编辑设备孪生Edit the device twin

可以在 Visual Studio Code 中编辑设备孪生,以配置标记。You can edit the device twin in Visual Studio Code to configure the tags. 在“视图”菜单中,选择“命令面板”并运行“IoT Edge: 编辑设备孪生”命令。From the View menu, select Command Palette and run the IoT Edge: Edit Device Twin command. 选择 IoT Edge 设备,随即会显示设备孪生。Select your IoT Edge device and the device twin appears.

此示例中未定义任何标记。In this example, no tags have been defined. 将当前的空节 "tags": {} 替换为自己的标记定义。Replace the current empty section "tags": {} with your own tags definition.

{
    "deviceId": "myEdgeDevice",
    "etag": "AAAAAAAAAAE=",
    "deviceEtag": "NTgwMDg5MDAz",
    "status": "enabled",
    "statusUpdateTime": "0001-01-01T00:00:00Z",
    "connectionState": "Disconnected",
    "lastActivityTime": "0001-01-01T00:00:00Z",
    "cloudToDeviceMessageCount": 0,
    "authenticationType": "sas",
    "x509Thumbprint": {
        "primaryThumbprint": null,
        "secondaryThumbprint": null
    },
    "version": 2,
    "properties": {
        "desired": {
            "$metadata": {
                "$lastUpdated": "2019-12-29T00:58:49.9315265Z"
            },
            "$version": 1
        },
        "reported": {
            "$metadata": {
                "$lastUpdated": "2019-12-29T00:58:49.9315265Z"
            },
            "$version": 1
        }
    },
    "capabilities": {
        "iotEdge": true
    },
    "deviceScope": "ms-azure-iot-edge://myEdgeDevice-637131779299315265",
    "tags": {}
}

保存本地文件后,运行“IoT Edge: 更新设备孪生”命令。After you save the local file, run the IoT Edge: Update Device Twin command.

大规模创建部署Create deployment at scale

配置部署清单并在设备孪生中配置标记后,便可以开始部署了。After you have configured the deployment manifest and configured tags in the device twin, you're ready to deploy.

  1. 在“视图”菜单中,选择“命令面板”并选择“Azure IoT Edge: 大规模创建部署”命令。From the View menu, select Command Palette and select the Azure IoT Edge: Create Deployment at Scale command.

  2. 导航至要使用的部署清单 JSON 文件,然后单击“选择 Edge 部署清单” 。Navigate to the deployment manifest JSON file that you want to use, and click Select Edge Deployment Manifest.

  3. 按提示提供值,从“部署 ID”开始。 Provide values as prompted, starting with the deployment id.

    指定部署 ID

    指定以下参数的值:Specify values for these parameters:

参数Parameter 说明Description
部署 IDDeployment id 将在 IoT 中心创建的部署的名称。The name of the deployment that will be created in the IoT hub. 为部署提供唯一名称(最多包含 128 个小写字母)。Give your deployment a unique name that is up to 128 lowercase letters. 避免空格和以下无效字符:& ^ [ ] { } \ | " < > /Avoid spaces and the following invalid characters: & ^ [ ] { } \ | " < > /.
目标条件Target condition 输入目标条件,确定用作此部署的目标的设备。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' and properties.reported.devicemodel='4000x'。  For example, tags.environment='test' and properties.reported.devicemodel='4000x'
优先级Priority 正整数。A positive integer. 如果同一设备上确定的部署目标至少有两个,则会应用优先级数值最高的部署。If two or more deployments are targeted at the same device, the deployment with the highest numerical value for Priority will apply.

指定优先级后,终端应会显示类似于以下描述内容的输出:After specifying the priority, the terminal should display output similar to the following depiction:

[Edge] Start deployment with deployment id [{specified-value}] and target condition [{specified-value}]
[Edge] Deployment with deployment id [{specified-value}] succeeded.

监视和修改部署Monitoring and modifying deployments

使用 Azure CLIAzure 门户来监视、修改和删除部署。Use the Azure CLI or the Azure portal to monitor, modify, and delete deployments. 这两个工具都会提供有关部署的指标。Both provide metrics on your deployments.

后续步骤Next steps

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