在 Ubuntu 虚拟机上运行 Azure IoT EdgeRun Azure IoT Edge on Ubuntu Virtual Machines

使用 Azure IoT Edge 运行时可将设备转变为 IoT Edge 设备。The Azure IoT Edge runtime is what turns a device into an IoT Edge device. 该运行时可以部署在像 Raspberry Pi 一样小的设备上,也可以部署在像工业服务器一样大的设备上。The runtime can be deployed on devices as small as a Raspberry Pi or as large as an industrial server. 使用 IoT Edge 运行时配置设备后,即可开始从云中部署业务逻辑。Once a device is configured with the IoT Edge runtime, you can start deploying business logic to it from the cloud.

若要了解有关 IoT Edge 运行时如何工作以及包含哪些组件的详细信息,请参阅了解 Azure IoT Edge 运行时及其体系结构To learn more about how the IoT Edge runtime works and what components are included, see Understand the Azure IoT Edge runtime and its architecture.

本文列出了部署一个使用预先提供的设备连接字符串安装并配置了 Azure IoT Edge 运行时的 Ubuntu 18.04 LTS 虚拟机的步骤。This article lists the steps to deploy an Ubuntu 18.04 LTS virtual machine with the Azure IoT Edge runtime installed and configured using a pre-supplied device connection string. 部署是使用 iotedge-vm-deploy 项目存储库中维护的基于 cloud-initAzure 资源管理器模板完成的。The deployment is accomplished using a cloud-init based Azure Resource Manager template maintained in the iotedge-vm-deploy project repository.

首次启动时,该 Ubuntu 18.04 LTS 虚拟机会通过 cloud-init 安装最新版本的 Azure IoT Edge 运行时On first boot, the Ubuntu 18.04 LTS virtual machine installs the latest version of the Azure IoT Edge runtime via cloud-init. 在运行时启动之前,该虚拟机还会设置提供的连接字符串,使你可以轻松配置和连接 IoT Edge 设备,而无需启动 SSH 或远程桌面会话。It also sets a supplied connection string before the runtime starts, allowing you to easily configure and connect the IoT Edge device without the need to start an SSH or remote desktop session.

使用“部署到 Azure”按钮进行部署Deploy using Deploy to Azure Button

使用“部署到 Azure”按钮可以简化 GitHub 中维护的 Azure 资源管理器模板的部署。The Deploy to Azure Button allows for streamlined deployment of Azure Resource Manager templates maintained on GitHub. 本部分将演示 iotedge-vm-deploy 项目存储库中包含的“部署到 Azure”按钮的用法。This section will demonstrate usage of the Deploy to Azure Button contained in the iotedge-vm-deploy project repository.

  1. 我们将使用 iotedge-vm-deploy Azure 资源管理器模板部署支持 Azure IoT Edge 的 Linux VM。We will deploy an Azure IoT Edge enabled Linux VM using the iotedge-vm-deploy Azure Resource Manager template. 若要开始,请单击下面的按钮:To begin, click the button below:

    iotedge-vm-deploy 的“部署到 Azure”按钮Deploy to Azure Button for iotedge-vm-deploy

  2. 在新启动的窗口中,填写可用的窗体字段:On the newly launched window, fill in the available form fields:

    显示 iotedge-vm-deploy 模板的屏幕截图Screenshot showing the iotedge-vm-deploy template

    订阅:要将虚拟机部署到的活动 Azure 订阅。Subscription: The active Azure subscription to deploy the virtual machine into.

    资源组:用于包含该虚拟机及其相关资源的现有或新建的资源组。Resource group: An existing or newly created Resource Group to contain the virtual machine and it's associated resources.

    DNS 标签前缀:必需的值,用作虚拟机主机名的前缀。DNS Label Prefix: A required value of your choosing that is used to prefix the hostname of the virtual machine.

    管理员用户名:一个用户名,将为其提供对部署的 root 特权。Admin Username: A username, which will be provided root privileges on deployment.

    设备连接字符串:在所需 IoT 中心内创建的设备的设备连接字符串Device Connection String: A Device Connection string for a device that was created within your intended IoT Hub.

    VM 大小:要部署的虚拟机的大小VM Size: The size of the virtual machine to be deployed

    Ubuntu OS 版本:要在基础虚拟机上安装的 Ubuntu OS 版本。Ubuntu OS Version: The version of the Ubuntu OS to be installed on the base virtual machine.

    位置:要将虚拟机部署到的地理区域,此值默认为所选资源组的位置。Location: The geographic region to deploy the virtual machine into, this value defaults to the location of the selected Resource Group.

    身份验证类型:根据偏好选择“sshPublicKey”或“password”。 Authentication Type: Choose sshPublicKey or password depending on your preference.

    管理员密码或密钥:SSH 公钥的值或密码的值,具体取决于所选的身份验证类型。Admin Password or Key: The value of the SSH Public Key or the value of the password depending on the choice of Authentication Type.

    填写所有字段后,选中页面底部的复选框以接受条款,然后选择“购买”开始部署。 When all fields have been filled in, select the checkbox at the bottom of the page to accept the terms and select Purchase to begin the deployment.

  3. 验证部署是否已成功完成。Verify that the deployment has completed successfully. 虚拟机资源应已部署到所选的资源组中。A virtual machine resource should have been deployed into the selected resource group. 请记下计算机名称,此名称应采用 vm-0000000000000 格式。Take note of the machine name, this should be in the format vm-0000000000000. 另外,请记下关联的“DNS 名称”,其格式应为 <dnsLabelPrefix>.<location>.cloudapp.azure.com。 Also, take note of the associated DNS Name, which should be in the format <dnsLabelPrefix>.<location>.cloudapp.azure.com.

    可以从 Azure 门户中新部署的虚拟机的“概述”部分获取“DNS 名称”。 The DNS Name can be obtained from the Overview section of the newly deployed virtual machine within the Azure portal.

    显示 IoT Edge VM 的 DNS 名称的屏幕截图Screenshot showing the dns name of the iotedge vm

  4. 设置后若要通过 SSH 连接到此 VM,请在命令中使用关联的“DNS 名称”:ssh <adminUsername>@<DNS_Name>If you want to SSH into this VM after setup, use the associated DNS Name with the command: ssh <adminUsername>@<DNS_Name>

从 Azure CLI 部署Deploy from Azure CLI

  1. 使用以下命令来确保已安装 Azure CLI IoT 扩展:Ensure that you have installed the Azure CLI iot extension with:

    az extension add --name azure-iot
    
  2. 接下来,如果在桌面上使用 Azure CLI,请先登录:Next, if you're using Azure CLI on your desktop, start by logging in:

    az login
    
  3. 如果你有多个订阅,请选择要使用的订阅:If you have multiple subscriptions, select the subscription you'd like to use:

    1. 列出订阅:List your subscriptions:

      az account list --output table
      
    2. 复制要使用的订阅的 SubscriptionID 字段。Copy the SubscriptionID field for the subscription you'd like to use.

    3. 使用复制的 ID 设置工作订阅:Set your working subscription with the ID that you copied:

      az account set -s <SubscriptionId>
      
  4. 创建新资源组(或者在后续步骤中指定现有的资源组):Create a new resource group (or specify an existing one in the next steps):

    az group create --name IoTEdgeResources --location chinaeast2
    
  5. 创建新虚拟机:Create a new virtual machine:

    若要为 authenticationType 使用 password,请参阅以下示例: To use an authenticationType of password, see the example below:

    az group deployment create \
    --name edgeVm \
    --resource-group IoTEdgeResources \
    --template-uri "https://aka.ms/iotedge-vm-deploy" \
    --parameters dnsLabelPrefix='my-edge-vm1' \
    --parameters adminUsername='<REPLACE_WITH_USERNAME>' \
    --parameters deviceConnectionString=$(az iot hub device-identity show-connection-string --device-id <REPLACE_WITH_DEVICE-NAME> --hub-name <REPLACE-WITH-HUB-NAME> -o tsv) \
    --parameters authenticationType='password' \
    --parameters adminPasswordOrKey="<REPLACE_WITH_SECRET_PASSWORD>"
    

    若要使用 SSH 密钥进行身份验证,可为 authenticationType 指定 sshPublicKey,然后在 adminPasswordOrKey 参数中提供 SSH 密钥的值。 To authenticate with an SSH key, you may do so by specifying an authenticationType of sshPublicKey, then provide the value of the SSH key in the adminPasswordOrKey parameter. 下面显示了一个示例。An example is shown below.

     #Generate the SSH Key
     ssh-keygen -m PEM -t rsa -b 4096 -q -f ~/.ssh/iotedge-vm-key -N ""  
    
     #Create a VM using the iotedge-vm-deploy script
     az group deployment create \
     --name edgeVm \
     --resource-group IoTEdgeResources \
     --template-uri "https://aka.ms/iotedge-vm-deploy" \
     --parameters dnsLabelPrefix='my-edge-vm1' \
     --parameters adminUsername='<REPLACE_WITH_USERNAME>' \
     --parameters deviceConnectionString=$(az iot hub device-identity show-connection-string --device-id <REPLACE_WITH_DEVICE-NAME> --hub-name <REPLACE-WITH-HUB-NAME> -o tsv) \
     --parameters authenticationType='sshPublicKey' \
     --parameters adminPasswordOrKey="$(< ~/.ssh/iotedge-vm-key.pub)"
    
    
  6. 验证部署是否已成功完成。Verify that the deployment has completed successfully. 虚拟机资源应已部署到所选的资源组中。A virtual machine resource should have been deployed into the selected resource group. 请记下计算机名称,此名称应采用 vm-0000000000000 格式。Take note of the machine name, this should be in the format vm-0000000000000. 另外,请记下关联的“DNS 名称”,其格式应为 <dnsLabelPrefix>.<location>.cloudapp.azure.com。 Also, take note of the associated DNS Name, which should be in the format <dnsLabelPrefix>.<location>.cloudapp.azure.com.

    可以从上一步骤的 JSON 格式输出中获取“DNS 名称”,它作为“公共 SSH”条目的一部分显示在 outputs 节中。 The DNS Name can be obtained from the JSON-formatted output of the previous step, within the outputs section as part of the public SSH entry. 使用此条目的值可以通过 SSH 连接到新部署的计算机。The value of this entry can be used to SSH into to the newly deployed machine.

    "outputs": {
      "public SSH": {
        "type": "String",
        "value": "ssh <adminUsername>@<DNS_Name>"
      }
    }
    

    也可以从 Azure 门户中新部署的虚拟机的“概述”部分获取“DNS 名称”。 The DNS Name can also be obtained from the Overview section of the newly deployed virtual machine within the Azure portal.

    显示 IoT Edge VM 的 DNS 名称的屏幕截图Screenshot showing the dns name of the iotedge vm

  7. 设置后若要通过 SSH 连接到此 VM,请在命令中使用关联的“DNS 名称”:ssh <adminUsername>@<DNS_Name>If you want to SSH into this VM after setup, use the associated DNS Name with the command: ssh <adminUsername>@<DNS_Name>

后续步骤Next steps

预配了安装运行时的 IoT Edge 设备后,现在可以部署 IoT Edge 模块Now that you have an IoT Edge device provisioned with the runtime installed, you can deploy IoT Edge modules.

如果无法正确安装 IoT Edge 运行时,请参阅故障排除页。If you are having problems with the IoT Edge runtime installing properly, check out the troubleshooting page.

若要将现有安装更新到最新版本的 IoT Edge,请参阅更新 IoT Edge 安全守护程序和运行时To update an existing installation to the newest version of IoT Edge, see Update the IoT Edge security daemon and runtime.

若要通过 SSH 或其他入站连接打开端口以访问 VM,请参阅有关打开 Linux VM 的端口和终结点的 Azure 虚拟机文档If you'd like to open up ports to access the VM through SSH or other inbound connections, refer to the Azure Virtual Machines documentation on opening up ports and endpoints to a Linux VM