快速入门:将第一个 IoT Edge 模块部署到虚拟 Linux 设备Quickstart: Deploy your first IoT Edge module to a virtual Linux 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 with all prerequisites installed 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 设备。Remotely deploy a module to an IoT Edge device.

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

本快速入门指导你创建配置为 IoT Edge 设备的 Linux 虚拟机。This quickstart walks you through creating a Linux virtual machine that's configured 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

先决条件Prerequisites

云资源: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 设备的 Linux 设备或虚拟机。A Linux device or virtual machine to act as your IoT Edge device. 应该使用 Microsoft 提供的 Azure IoT Edge on Ubuntu 虚拟机,该虚拟机在设备上预先安装了运行 IoT Edge 所需的所有项。You should use the Microsoft-provided Azure IoT Edge on Ubuntu virtual machine, which preinstalls everything you need to run IoT Edge on a device. 接受使用条款并使用以下命令创建此虚拟机:Accept the terms of use and create this virtual machine using the following commands:

    az vm image accept-terms --urn microsoft_iot_edge:iot_edge_vm_ubuntu:ubuntu_1604_edgeruntimeonly:latest
    az vm create --resource-group IoTEdgeResources --name EdgeVM --image microsoft_iot_edge:iot_edge_vm_ubuntu:ubuntu_1604_edgeruntimeonly:latest --admin-username azureuser --generate-ssh-keys
    

    可能需要几分钟才能创建并启动新的虚拟机。It may take a few minutes to create and start the new virtual machine.

    创建新的虚拟机时,请记下在 create 命令的输出中提供的 publicIpAddressWhen you create a new virtual machine, make a note of the publicIpAddress, which is provided as part of the create command output. 在本快速入门中,稍后将使用此公用 IP 地址连接到虚拟机。You will use this public IP address to connect to the virtual machine later in the quickstart.

  • 如果希望在自己的设备上运行 Azure IoT Edge 运行时,请遵循在 Linux 上安装 Azure IoT Edge 运行时中的说明。If you prefer to run the Azure IoT Edge runtime on your own device, follow instructions at Install the Azure IoT Edge runtime on Linux.

创建 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 Edge 设备创建设备标识,以便它可以与 IoT 中心通信。Create a device identity for your IoT Edge 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 --hub-name {hub_name} --device-id myEdgeDevice --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 设备Configure your IoT Edge device

在 IoT Edge 设备上启动 Azure IoT Edge 运行时。Start the Azure IoT Edge runtime on your IoT Edge 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 facilitates deployment and monitoring of modules on the IoT Edge device, including the IoT Edge hub. IoT Edge 中心 管理 IoT Edge 设备模块之间以及设备和 Azure IoT 中心之间的通信。The IoT Edge hub manages communications between modules on the IoT Edge device, and between the device and IoT Hub.

在运行时配置期间,你提供设备连接字符串。During the runtime configuration, you provide 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 设备上设置连接字符串Set the connection string on the IoT Edge device

如果使用的是先决条件中所述的 Azure IoT Edge on Ubuntu 虚拟机,则表示设备已安装 IoT Edge 运行时。If you're using the Azure IoT Edge on Ubuntu virtual machine as described in the prerequisites, then your device already has the IoT Edge runtime installed. 只需使用上一节中检索的设备连接字符串来配置设备即可。You just need to configure your device with the device connection string that you retrieved in the previous section. 可以在不连接虚拟机的情况下进行远程配置。You can do this remotely without having to connect to the virtual machine. 运行以下命令,将 {device_connection_string} 替换为自己的字符串 。Run the following command, replacing {device_connection_string} with your own string.

az vm run-command invoke -g IoTEdgeResources -n EdgeVM --command-id RunShellScript --script "/etc/iotedge/configedge.sh '{device_connection_string}'"

如果要在本地计算机或 ARM32 或 ARM64 设备上运行 IoT Edge,则需要在设备上安装 IoT Edge 运行时及其必备组件。If you're running IoT Edge on your local machine or an ARM32 or ARM64 device, you need to install the IoT Edge runtime and its prerequisites on your device. 按照在 Linux 上安装 Azure IoT Edge 运行时中的说明进行操作,然后返回到本快速入门。Follow the instructions in Install the Azure IoT Edge runtime on Linux, then return to this quickstart.

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

此快速入门中的其余命令都在 Azure IoT Edge 上完成,以便查看设备发生的情况。The rest of the commands in this quickstart take place on your IoT Edge device itself, so that you can see what's happening on the device. 如果使用的是虚拟机,请立即使用创建命令输出的公共 IP 地址连接到该虚拟机。If you're using a virtual machine, connect to that machine now using the public IP address that was output by the creation command. 也可在 Azure 门户中虚拟机的概览页上找到公共 IP 地址。You can also find the public IP address on your virtual machine's overview page in the Azure portal. 使用以下命令连接到虚拟机。Use the following command to connect to your virtual machine. 如果所用用户名与先决条件中建议的用户名不同,请替换 {azureuser} 。Replace {azureuser} if you used a different username than the one suggested in the prerequisites. {publicIpAddress} 替换为你的计算机的地址。Replace {publicIpAddress} with your machine's address.

ssh azureuser@{publicIpAddress}

验证是否已在 IoT Edge 设备上成功安装并配置运行时。Verify that the runtime was successfully installed and configured on your IoT Edge device.

Tip

需要提升的权限才能运行 iotedge 命令。You need elevated privileges to run iotedge commands. 安装 IoT Edge 运行时后从计算机中注销并第一次重新登录后,你的权限将自动更新。Once you sign out of your machine and sign back in the first time after installing the IoT Edge runtime, your permissions are automatically updated. 在此之前,请在命令前使用 sudoUntil then, use sudo in front of the commands.

  1. 查看 IoT Edge 安全守护程序是否正作为系统服务运行。Check to see that the IoT Edge security daemon is running as a system service.

    sudo systemctl status iotedge
    

    查看作为系统服务运行的 Edge 守护程序

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

    journalctl -u iotedge
    
  3. 查看在设备上运行的模块。View the modules running on your device.

    sudo iotedge list
    

    查看设备上的一个模块

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 will send 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 created a new 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 设备上打开命令提示符,或者从 Azure CLI 使用 SSH 连接。Open the command prompt on your IoT Edge device again, or use the SSH connection from Azure CLI. 确认从云中部署的模块正在 IoT Edge 设备上运行:Confirm that the module deployed from the cloud is running on your IoT Edge device:

sudo iotedge list

查看设备上的三个模块

查看从温度传感器模块发送的消息:View the messages being sent from the temperature sensor module:

sudo iotedge logs SimulatedTemperatureSensor -f

Tip

引用模块名称时,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.