快速入门:将第一个 IoT Edge 模块部署到虚拟 Windows 设备Quickstart: Deploy your first IoT Edge module to a virtual Windows device

在本快速入门中通过将容器化代码部署到虚拟 IoT Edge 设备来测试 Azure IoT Edge。Test out Azure IoT Edge in this quickstart by deploying containerized code to a virtual IoT Edge device. IoT Edge 允许你远程管理设备上的代码,这样你就可以将更多工作负荷发送到 Edge。IoT Edge allows you to remotely manage code on your devices so that you can send more of your workloads to the edge. 对于本快速入门,我们建议使用 Azure 虚拟机作为 IoT Edge 设备,这样可以快速创建测试计算机,安装必备组件,并在完成后将其删除。For this quickstart, we recommend using an Azure virtual machine for your IoT Edge device, which allows you to quickly create a test machine, install the prerequisites, and then delete it when you're finished.

此快速入门介绍如何:In this quickstart you learn how to:

  1. 创建 IoT 中心。Create an IoT Hub.
  2. 将 IoT Edge 设备注册到 IoT 中心。Register an IoT Edge device to your IoT hub.
  3. 在虚拟设备上安装并启动 IoT Edge 运行时。Install and start the IoT Edge runtime on your virtual device.
  4. 以远程方式将模块部署到 IoT Edge 设备并将遥测数据发送到 IoT 中心。Remotely deploy a module to an IoT Edge device and send telemetry to IoT Hub.

关系图 - 设备和云架构的快速入门

本快速入门将指导你创建 Windows 虚拟机并将其配置为 IoT Edge 设备。This quickstart walks you through creating a Windows virtual machine and configuring it to be IoT Edge device. 然后即可将模块从 Azure 门户部署到设备。Then you can deploy a module from the Azure portal to your device. 在本快速入门中部署的模块为模拟传感器,可以生成温度、湿度和压强数据。The module that you deploy in this quickstart is a simulated sensor that generates temperature, humidity, and pressure data. 其他 Azure IoT Edge 教程均以本教程中通过部署模块(这些模块通过分析模拟数据来获得业务见解)执行的操作为基础。The other Azure IoT Edge tutorials build upon the work you do here by deploying modules that analyze the simulated data for business insights.

如果没有可用的 Azure 订阅,可以在开始前创建一个试用帐户If you don't have an active Azure subscription, create a trial account before you begin.

请使用 Azure CLI 完成本快速入门中的多个步骤。Azure IoT 有一个可以启用其他功能的扩展。You use the Azure CLI to complete many of the steps in this quickstart, and Azure IoT has an extension to enable additional functionality.

将 Azure IoT 扩展 。Add the Azure IoT extension.

az extension add --name azure-cli-iot-ext


云资源:Cloud resources:

  • 一个资源组,用于管理在本快速入门中使用的所有资源。A resource group to manage all the resources you use in this quickstart.

    az group create --name IoTEdgeResources --location chinaeast

IoT Edge 设备:IoT Edge device:

  • 充当 IoT Edge 设备的 Windows 虚拟机。A Windows virtual machine to act as your IoT Edge device. 可使用以下命令创建此虚拟机,并将 {password} 替换为安全密码 :You can create this virtual machine using the following command, replacing {password} with a secure password:

    az vm create --resource-group IoTEdgeResources --name EdgeVM --image MicrosoftWindowsDesktop:Windows-10:rs5-pro:latest --admin-username azureuser --admin-password {password} --size Standard_DS1_v2

    可能需要几分钟才能创建并启动新的虚拟机。It may take a few minutes to create and start the new virtual machine. 然后,在连接到虚拟机时下载 RDP 文件进行使用:You can then download an RDP file for use when connecting to your virtual machine:

    1. 导航到 Azure 门户中新的 Windows 虚拟机。Navigate to your new Windows virtual machine in the Azure portal.
    2. 选择“连接” 。Select Connect.
    3. 在“RDP”选项卡上,选择“下载 RDP 文件” 。On the RDP tab, select Download RDP File.

    使用远程桌面连接打开此文件,以通过用 az vm create 指定的管理员姓名和密码连接到 Windows 虚拟机。Open this file with Remote Desktop Connection to connect to your Windows virtual machine using the administrator name and password you specified with the az vm create command.


为简单起见,本快速入门使用 Windows 桌面虚拟机。This quickstart uses a Windows desktop virtual machine for simplicity. 要了解哪些 Windows 操作系统针对生产环境公开发布,请参阅 Azure IoT Edge 支持的系统For information about which Windows operating systems are generally available for production scenarios, see Azure IoT Edge supported systems.

如果你已准备好为 IoT Edge 配置自己的 Windows 设备,包括运行 IoT Core 的设备,请按照在 Windows 上安装 Azure IoT Edge 运行时中的步骤进行操作。If you're ready to configure your own Windows device for IoT Edge, including devices running IoT Core, follow the steps in Install the Azure IoT Edge runtime on Windows.

创建 IoT 中心Create an IoT hub

使用 Azure CLI 创建 IoT 中心,启动快速入门。Start the quickstart by creating an IoT hub with Azure CLI.

关系图 - 在云中创建 IoT 中心

免费级的 IoT 中心适用于此快速入门。The free level of IoT Hub works for this quickstart. 如果曾经用过 IoT 中心并且已创建免费的中心,则可使用该 IoT 中心。If you've used IoT Hub in the past and already have a free hub created, you can use that IoT hub. 每个订阅仅能有一个免费 IoT 中心。Each subscription can only have one free IoT hub.

以下代码在资源组“IoTEdgeResources”中创建免费的“F1”中心 。The following code creates a free F1 hub in the resource group IoTEdgeResources. {hub_name} 替换为 IoT 中心的唯一名称。Replace {hub_name} with a unique name for your IoT hub.

az iot hub create --resource-group IoTEdgeResources --name {hub_name} --sku F1 

如果由于订阅中已经有一个免费的中心而出现错误,请将 SKU 更改为 S1If you get an error because there's already one free hub in your subscription, change the SKU to S1. 如果出现一条错误,指示 IoT 中心名称不可用,则表明他人已使用具有该名称的中心。If you get an error that the IoT Hub name isn't available, it means that someone else already has a hub with that name. 请尝试一个新名称。Try a new name.

注册 IoT Edge 设备Register an IoT Edge device

使用新创建的 IoT 中心注册 IoT Edge 设备。Register an IoT Edge device with your newly created IoT Hub. 关系图 - 使用 IoT 中心标识注册设备Diagram - Register a device with an IoT Hub identity

为模拟设备创建设备标识,以便它可以与 IoT 中心通信。Create a device identity for your simulated device so that it can communicate with your IoT hub. 设备标识存在于云中,而将物理设备关联到设备标识时,则使用唯一的设备连接字符串。The device identity lives in the cloud, and you use a unique device connection string to associate a physical device to a device identity.

由于 IoT Edge 设备的行为和托管方式与典型 IoT 设备不同,请使用 --edge-enabled 标志声明此标识,使之用于 IoT Edge 设备。Since IoT Edge devices behave and can be managed differently than typical IoT devices, declare this identity to be for an IoT Edge device with the --edge-enabled flag.

  1. 输入以下命令,以便在中心创建名为 myEdgeDevice 的设备。Enter the following command to create a device named myEdgeDevice in your hub.

    az iot hub device-identity create --device-id myEdgeDevice --hub-name {hub_name} --edge-enabled

    如果出现有关 iothubowner 策略密钥的错误,请确保运行最新版的 azure-cli-iot-ext 扩展。If you get an error about iothubowner policy keys, make sure that you are running the latest version of the azure-cli-iot-ext extension.

  2. 检索设备的连接字符串,该字符串将物理设备与其在 IoT 中心的标识链接在一起。Retrieve the connection string for your device, which links your physical device with its identity in IoT Hub.

    az iot hub device-identity show-connection-string --device-id myEdgeDevice --hub-name {hub_name}
  3. 复制 JSON 输出中 connectionString 键的值并保存。Copy the value of the connectionString key from the JSON output and save it. 该值为设备连接字符串。This value is the device connection string. 在下一部分中配置 IoT Edge 运行时时将用到此连接字符串。You'll use this connection string to configure the IoT Edge runtime in the next section.

    从 CLI 输出中检索连接字符串

安装和启动 IoT Edge 运行时Install and start the IoT Edge runtime

在 IoT Edge 设备上安装 Azure IoT Edge 运行时,并使用设备连接字符串对其进行配置。Install the Azure IoT Edge runtime on your IoT Edge device and configure it with a device connection string. 关系图 - 在设备上启动运行时Diagram - Start the runtime on device

IoT Edge 运行时部署在所有 IoT Edge 设备上。The IoT Edge runtime is deployed on all IoT Edge devices. 它有三个组件。It has three components. 每次 IoT Edge 设备启动并通过启动 IoT Edge 代理启动设备时,IoT Edge 安全守护程序就会启动。The IoT Edge security daemon starts each time an IoT Edge device boots and bootstraps the device by starting the IoT Edge agent. IoT Edge 代理管理 IoT Edge 设备上模块(包括 IoT Edge 中心)的部署和监视。The IoT Edge agent manages deployment and monitoring of modules on the IoT Edge device, including the IoT Edge hub. IoT Edge 中心处理 IoT Edge 设备模块之间以及设备和 IoT 中心之间的通信。The IoT Edge hub handles communications between modules on the IoT Edge device, and between the device and IoT Hub.

安装脚本还包含一个名为 Moby 的容器引擎,用于管理 IoT Edge 设备上的容器映像。The installation script also includes a container engine called Moby that manages the container images on your IoT Edge device.

在运行时安装期间,系统会要求你提供设备连接字符串。During the runtime installation, you're asked for a device connection string. 请使用从 Azure CLI 检索的字符串。Use the string that you retrieved from the Azure CLI. 此字符串将物理设备与 Azure 中的 IoT Edge 设备标识关联在一起。This string associates your physical device with the IoT Edge device identity in Azure.

连接到 IoT Edge 设备Connect to your IoT Edge device

本部分中的步骤均在 IoT Edge 设备上执行,因此需要立即通过远程桌面连接到此虚拟机。The steps in this section all take place on your IoT Edge device, so you want to connect to that virtual machine now via remote desktop.

安装并配置 IoT Edge 服务Install and configure the IoT Edge service

使用 PowerShell 下载并安装 IoT Edge 运行时。Use PowerShell to download and install the IoT Edge runtime. 使用从 IoT 中心检索的设备连接字符串来配置设备。Use the device connection string that you retrieved from IoT Hub to configure your device.

  1. 如果还没有,请按照注册新的 Azure IoT Edge 设备中的步骤注册设备并检索设备连接字符串。If you haven't already, follow the steps in Register a new Azure IoT Edge device to register your device and retrieve the device connection string.

  2. 以管理员身份运行 PowerShell。Run PowerShell as an administrator.


    使用 PowerShell 的 AMD64 会话安装 IoT Edge,不要使用 PowerShell (x86)。Use an AMD64 session of PowerShell to install IoT Edge, not PowerShell (x86). 如果不确定您使用的是什么会话类型,请运行以下命令:If you're not sure which session type you're using, run the following command:

    (Get-Process -Id $PID).StartInfo.EnvironmentVariables["PROCESSOR_ARCHITECTURE"]
  3. Deploy-IoTEdge 命令执行以下检查:Windows 计算机使用受支持版本、启用容器功能、下载 moby 运行时并下载 IoT Edge 运行时 。The Deploy-IoTEdge command checks that your Windows machine is on a supported version, turns on the containers feature, downloads the moby runtime, and then downloads the IoT Edge runtime.

    . {Invoke-WebRequest -useb aka.ms/iotedge-win} | Invoke-Expression; `
    Deploy-IoTEdge -ContainerOs Windows
  4. 计算机可能会自动重新启动。Your machine may restart automatically. 如果 Deploy-IoTEdge 命令提示你重启,请重启。If you are prompted by the Deploy-IoTEdge command to reboot, do so now.

  5. 再次以管理员身份运行 PowerShell。Run PowerShell as an administrator again.

  6. Initialize-IoTEdge 命令在计算机上配置 IoT Edge 运行时 。The Initialize-IoTEdge command configures the IoT Edge runtime on your machine. 该命令默认为使用 Windows 容器手动预配。The command defaults to manual provisioning with Windows containers.

    . {Invoke-WebRequest -useb aka.ms/iotedge-win} | Invoke-Expression; `
    Initialize-IoTEdge -ContainerOs Windows
  7. 当系统提示输入 DeviceConnectionString 时,请提供在上一部分复制的字符串。When prompted for a DeviceConnectionString, provide the string that you copied in the previous section. 请勿对连接字符串使用引号。Don't include quotes around the connection string.

查看 IoT Edge 运行时状态View the IoT Edge runtime status

验证是否已成功安装并配置运行时。Verify that the runtime was successfully installed and configured.

  1. 检查 IoT Edge 服务的状态。Check the status of the IoT Edge service.

    Get-Service iotedge
  2. 若需排查服务问题,请检索服务日志。If you need to troubleshoot the service, retrieve the service logs.

    . {Invoke-WebRequest -useb aka.ms/iotedge-win} | Invoke-Expression; Get-IoTEdgeLog
  3. 查看在 IoT Edge 设备上运行的所有模块。View all the modules running on your IoT Edge device. 由于此服务是第一次运行,因此只会看到 edgeAgent 模块在运行。Since the service just started for the first time, you should only see the edgeAgent module running. edgeAgent 模块会默认运行,用于安装并启动部署到设备的任何其他模块。The edgeAgent module runs by default, and helps to install and start any additional modules that you deploy to your device.

    iotedge list


可能需要几分钟才能完成安装并启动 IoT Edge 代理模块。It may take a few minutes for the installation to complete and the IoT Edge agent module to start.

IoT Edge 设备现在已配置好。Your IoT Edge device is now configured. 它可以运行云部署型模块了。It's ready to run cloud-deployed modules.

部署模块Deploy a module

从云端管理 Azure IoT Edge 设备,部署将遥测数据发送到 IoT 中心的模块。Manage your Azure IoT Edge device from the cloud to deploy a module that sends telemetry data to IoT Hub. 关系图 - 将模块从云部署到设备Diagram - deploy module from cloud to device

Azure IoT Edge 的主要功能之一是能够从云中将代码部署到 IoT Edge 设备。One of the key capabilities of Azure IoT Edge is being able to deploy code to your IoT Edge devices from the cloud. IoT Edge 模块是以容器形式实现的可执行程序包。IoT Edge modules are executable packages implemented as containers. 在本部分中,你将从 Azure 市场的“IoT Edge 模块”部分部署一个预建的模块。In this section, you deploy a pre-built module from the IoT Edge Modules section of the Azure Marketplace.

在本部分中部署的模块模拟一个传感器并发送生成的数据。The module that you deploy in this section simulates a sensor and sends generated data. 开始使用 IoT Edge 时,此模块是非常有用的代码段,因为你可以使用模拟的数据进行开发和测试。This module is a useful piece of code when you're getting started with IoT Edge because you can use the simulated data for development and testing. 如果要确切了解此模块的功能,则可以查看模拟的温度传感器源代码If you want to see exactly what this module does, you can view the simulated temperature sensor source code.

若要从 Azure 市场部署你的第一个模块,请使用以下步骤:To deploy your first module from the Azure Marketplace, use the following steps:

  1. Azure 门户中,在搜索中输入“模拟的温度传感器”并打开市场结果。In the Azure portal, enter Simulated Temperature Sensor into the search and open the Marketplace result.

    Azure 门户搜索中的模拟的温度传感器

  2. 选择要接收该模块的 IoT Edge 设备。Choose an IoT Edge device to receive this module. 在“IoT Edge 模块的目标设备”页面上,提供以下信息:On the Target Devices for IoT Edge Module page, provide the following information:

    1. 订阅:选择包含要使用的 IoT 中心的订阅。Subscription: select the subscription that contains the IoT hub you're using.

    2. IoT 中心:选择要使用的 IoT 中心的名称。IoT Hub: select the name of the IoT hub you're using.

    3. IoT Edge 设备名:如果在此快速入门早期已使用推荐的设备名,则输入 myEdgeDevice。IoT Edge Device Name: if you used the suggested device name earlier in this quickstart, enter myEdgeDevice. 或者,选择“查找设备”以从 IoT 中心的 IoT Edge 设备列表中进行选择。Or, select Find Device to choose from a list of IoT Edge devices in your IoT hub.

    4. 选择“创建” 。Select Create.

  3. 现在,你已从 Azure 市场选择了 IoT Edge 模块,并且选择了要接收该模块的 IoT Edge 设备,接下来,你将转到一个具有三个步骤的向导,该向导可帮助你确切定义如何部署该模块。Now that you've chosen an IoT Edge module from the Azure Marketplace, and chosen an IoT Edge device to receive the module, you're taken to a three-step wizard that helps you define exactly how to deploy the module. 在向导的“添加模块”步骤中,请注意“SimulatedTemperatureSensor”模块会自动填充。In the Add Modules step of the wizard, notice that the SimulatedTemperatureSensor module is autopopulated. 在教程中,将使用此页向部署添加其他模块。In the tutorials, you use this page to add additional modules to your deployment. 对于本快速入门,只需要部署这一个模块。For this quickstart, only deploy this one module. 选择“下一步”继续执行向导的下一步。Select Next to continue to the next step of the wizard.

  4. 在向导的“指定路由”步骤中,定义消息在模块之间传递以及传递到 IoT 中心的方式。In the Specify Routes step of the wizard, you define how messages are passed between modules and to IoT Hub. 对于本快速入门,需要来自所有模块的所有消息都转到 IoT 中心 ($upstream)。For the quickstart, you want all messages from all modules to go to IoT Hub ($upstream). 如果未自动填充,请添加以下代码,然后选择“下一步”:If it's not autopopulated, add the following code then select Next:

     "routes": {
         "route": "FROM /messages/* INTO $upstream"
  5. 在向导的“查看部署”步骤中,可以预览定义部署到 IoT Edge 设备的所有模块的 JSON 文件。In the Review Deployment step of the wizard, you can preview the JSON file that defines all the modules that get deployed to your IoT Edge device. 请注意,包括 SimulatedTemperatureSensor 模块以及名为 edgeAgentedgeHub 的两个其他系统模块。Notice that the SimulatedTemperatureSensor module is included, and two additional system modules called edgeAgent and edgeHub. 查看完成后,选择“提交”。Select Submit when you're done reviewing.

    在将新部署提交到 IoT Edge 设备时,不会向设备推送任何内容。When you submit a new deployment to an IoT Edge device, nothing is pushed to your device. 相反,设备会定期对 IoT 中心进行查询以获取任何新指令。Instead, the device queries IoT Hub regularly for any new instructions. 如果设备找到了更新的部署清单,则它会使用关于新部署的信息来从云中拉取模块映像,然后开始在本地运行模块。If the device finds an updated deployment manifest, it uses the information about the new deployment to pull the module images from the cloud then starts running the modules locally. 此过程可能需要几分钟时间。This process may take a few minutes.

  6. 提交模块部署详细信息后,向导将引导你返回到 IoT 中心的 IoT Edge 页。After you submit the module deployment details, the wizard returns you to the IoT Edge page of your IoT hub. 从 IoT Edge 设备列表中选择设备,以查看其详细信息。Select your device from the list of IoT Edge devices to see its details.

  7. 在设备详细信息页上,向下滚动至“模块”部分。On the device details page, scroll down to the Modules section. 应有三个模块列出:$edgeAgent、$edgeHub 和 SimulatedTemperatureSensor。Three modules should be listed: $edgeAgent, $edgeHub, and SimulatedTemperatureSensor. 如果列出了部署中指定的一个或多个模块,但设备未报告这些模块,则意味着 IoT Edge 设备仍在启动它们。If one or more of the modules are listed as specified in deployment but not reported by device, your IoT Edge device is still starting them. 请稍等片刻,然后选择页面顶部的“刷新”。Wait a few moments and select Refresh at the top of the page.

    查看已部署模块列表中的 SimulatedTemperatureSensor

查看生成的数据View generated data

在本快速入门中,注册了 IoT Edge 设备,并在该设备上安装了 IoT Edge 运行时。In this quickstart, you registered an IoT Edge device and installed the IoT Edge runtime on it. 然后,使用了 Azure 门户部署 IoT Edge 模块,使其在不更改设备本身的情况下在设备上运行。Then, you used the Azure portal to deploy an IoT Edge module to run on the device without having to make changes to the device itself.

在这种情况下,推送的模块会创建可用于测试的样本数据。In this case, the module that you pushed creates sample data that you can use for testing. 模拟温度传感器模块会生成可用于以后测试的环境数据。The simulated temperature sensor module generates environment data that you can use for testing later. 模拟传感器正在监视一台计算机和该计算机周围的环境。The simulated sensor is monitoring both a machine and the environment around the machine. 例如,该传感器可能位于服务器机房中、工厂地板上或风力涡轮机上。For example, this sensor might be in a server room, on a factory floor, or on a wind turbine. 该消息包括环境温度和湿度、机器温度和压力以及时间戳。The message includes ambient temperature and humidity, machine temperature and pressure, and a timestamp. IoT Edge 教程使用此模块创建的数据作为测试数据进行分析。The IoT Edge tutorials use the data created by this module as test data for analytics.

确认从云中部署的模块正在 IoT Edge 设备上运行。Confirm that the module deployed from the cloud is running on your IoT Edge device.

iotedge list


查看从温度传感器模块发送到云的消息。View the messages being sent from the temperature sensor module to the cloud.

iotedge logs SimulatedTemperatureSensor -f


引用模块名称时,IoT Edge 命令区分大小写。IoT Edge commands are case-sensitive when referring to module names.


也可以使用 Visual Studio Code 的 Azure IoT Toolkit 扩展(以前称为 Azure IoT 工具包扩展)查看到达 IoT 中心的消息。You can also watch the messages arrive at your IoT hub by using the Azure IoT Hub Toolkit extension for Visual Studio Code (formerly Azure IoT Toolkit extension).

清理资源Clean up resources

若要继续学习 IoT Edge 教程,可以使用在本快速入门中注册并设置的设备。If you want to continue on to the IoT Edge tutorials, you can use the device that you registered and set up in this quickstart. 否则,可删除已创建的 Azure 资源,避免产生费用。Otherwise, you can delete the Azure resources that you created to avoid charges.

如果是在新资源组中创建的虚拟机和 IoT 中心,则可删除该组以及所有关联的资源。If you created your virtual machine and IoT hub in a new resource group, you can delete that group and all the associated resources. 仔细检查资源组的内容,确保没有要保留的内容。Double check the contents of the resource group to make sure that there's nothing you want to keep. 如果不希望删除整个组,则可改为删除单个资源。If you don't want to delete the whole group, you can delete individual resources instead.

删除 IoTEdgeResources 组。Remove the IoTEdgeResources group.

az group delete --name IoTEdgeResources 

后续步骤Next steps

在本快速入门中,你创建了一个 IoT Edge 设备并使用 Azure IoT Edge 云接口将代码部署到该设备上。In this quickstart, you created an IoT Edge device and used the Azure IoT Edge cloud interface to deploy code onto the device. 现在,你有了一个可以生成其环境的原始数据的测试设备。Now, you have a test device generating raw data about its environment.

下一步是设置本地开发环境,使你可以开始创建运行业务逻辑的 IoT Edge 模块。The next step is to set up your local development environment so that you can start creating IoT Edge modules that run your business logic.