使用 Windows 上的 IoT Edge 运行 Linux 容器Use IoT Edge on Windows to run Linux containers

使用 Windows 计算机测试适用于 Linux 设备的 IoT Edge 模块。Test IoT Edge modules for Linux devices using a Windows machine.

在生产方案中,Windows 设备应该只运行 Windows 容器。In a production scenario, Windows devices should only run Windows containers. 但是,一种常见的开发方案是使用 Windows 计算机生成适用于 Linux 设备的 IoT Edge 模块。However, a common development scenario is to use a Windows computer to build IoT Edge modules for Linux devices. 使用适用于 Windows 的 IoT Edge 运行时可以运行 Linux 容器以实现测试和开发目的。The IoT Edge runtime for Windows allows you to run Linux containers for testing and development purposes.

本文列出了在 Windows x64 (AMD/Intel) 系统上使用 Linux 容器安装 Azure IoT Edge 运行时的步骤。This article lists the steps to install the Azure IoT Edge runtime using Linux containers on your Windows x64 (AMD/Intel) system. 若要详细了解 IoT Edge 运行时安装程序,包括有关所有安装参数的详细信息,请参阅 在 Windows 上安装 Azure IoT Edge 运行时To learn more about the IoT Edge runtime installer, including details about all the installation parameters, see Install the Azure IoT Edge runtime on Windows.

有关最新 IoT Edge 版本包含的功能的信息,请参阅 Azure IoT Edge 版本For information about what's included in the latest version of IoT Edge, see Azure IoT Edge releases.

必备条件Prerequisites

参考本部分检查你的 Windows 设备是否支持 IoT Edge,并在安装之前为容器引擎准备好该设备。Use this section to review whether your Windows device can support IoT Edge, and to prepare it for a container engine before installation.

支持的 Windows 版本Supported Windows versions

带有 Linux 容器的 Azure IoT Edge 可以在满足 Docker Desktop 要求的任何 Windows 版本上运行Azure IoT Edge with Linux containers can run on any version of Windows that meets the requirements for Docker Desktop

若要在虚拟机上安装 IoT Edge,请启用嵌套虚拟化并分配至少 2-GB 内存。If you want to install IoT Edge on a virtual machine, enable nested virtualization and allocate at least 2-GB memory. 如何启用嵌套虚拟化取决于所用的虚拟机监控程序。How you enable nested virtualization is different depending on the hypervisor your use. 就 Hyper-V 来说,第 2 代虚拟机已默认启用嵌套虚拟化。For Hyper-V, generation 2 virtual machines have nested virtualization enabled by default. 如果使用 VMWare,则可通过切换开关在虚拟机上启用此功能。For VMWare, there's a toggle to enable the feature on your virtual machine.

准备容器引擎Prepare the container engine

Azure IoT Edge 依赖于 OCI 兼容的容器引擎。Azure IoT Edge relies on a OCI-compatible container engine. 在 Windows 计算机上运行 Windows 与 Linux 容器的最大配置差别在于,IoT Edge 安装包含 Windows 容器运行时,但在安装 IoT Edge 之前,需要提供自己的 Linux 容器运行时。The biggest configuration difference between running Windows and Linux containers on a Windows machine is that the IoT Edge installation includes a Windows container runtime, but you need to provide your own runtime for Linux containers before installing IoT Edge.

若要设置一台 Windows 计算机用于开发和测试适用于 Linux 设备的容器,可以使用 Docker Desktop 作为容器引擎。To set up a Windows machine to develop and test containers for Linux devices, you can use Docker Desktop as your container engine. 在安装 IoT Edge 之前,需要安装 Docker 并将其配置为使用 Linux 容器You need to install Docker and configure it to use Linux containers before installing IoT Edge.

如果 IoT Edge 设备是 Windows 计算机,请检查它是否符合 Hyper-V 的系统要求If your IoT Edge device is a Windows computer, check that it meets the system requirements for Hyper-V.

在新设备上安装 IoT EdgeInstall IoT Edge on a new device

备注

Azure IoT Edge 软件程序包受制于程序包中的许可条款(位于 LICENSE 目录中)。Azure IoT Edge software packages are subject to the license terms located in the packages (in the LICENSE directory). 使用程序包之前请阅读这些许可条款。Please read the license terms prior to using the package. 安装和使用程序包即表示接受这些条款。Your installation and use of the package constitutes your acceptance of these terms. 如果不同意许可条款,则不要使用程序包。If you do not agree with the license terms, do not use the package.

某个 PowerShell 脚本将下载并安装 Azure IoT Edge 安全守护程序。A PowerShell script downloads and installs the Azure IoT Edge security daemon. 然后,安全守护程序将启动两个运行时模块中的第一个,即 IoT Edge 代理,以便能够远程部署其他模块。The security daemon then starts the first of two runtime modules, the IoT Edge agent, which enables remote deployments of other modules.

首次在设备上安装 IoT Edge 运行时时,需要使用 IoT 中心内的标识预配该设备。When you install the IoT Edge runtime for the first time on a device, you need to provision the device with an identity from an IoT hub. 可以使用 IoT 中心提供的设备连接字符串手动预配单个 IoT Edge 设备。A single IoT Edge device can be provisioned manually using a device connections string provided by your IoT hub. 或者,可以使用设备预配服务自动预配设备,需要设置多个设备时,这种做法非常有用。Or, you can use the Device Provisioning Service to automatically provision devices, which is helpful when you have many devices to set up.

可以在在 Windows 上安装 Azure IoT Edge 运行时一文中详细了解不同的安装选项和参数。You can read more about the different installation options and parameters in the article Install the Azure IoT Edge runtime on Windows. 为 Linux 容器安装并配置 Docker Desktop 后,主要安装差别是使用 -ContainerOs 参数声明 Linux。Once you have Docker Desktop installed and configured for Linux containers, the main installation difference is declaring Linux with the -ContainerOs parameter. 例如:For example:

  1. 注册新的 IoT Edge 设备并检索设备连接字符串(如果尚未这样做)。If you haven't already, register a new IoT Edge device and retrieve the device connection string. 复制连接字符串,以便稍后在本部分中使用。Copy the connection string to use later in this section. 可以使用以下工具完成此步骤:You can complete this step using the following tools:

  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 运行时(不是用于 Linux 容器)和 IoT Edge 运行时。The Deploy-IoTEdge command checks that your Windows machine is on a supported version, turns on the containers feature, and then downloads the moby runtime (which is not used for Linux containers) and the IoT Edge runtime. 该命令默认使用 Windows 容器,因此会将 Linux 声明为所需的容器操作系统。The command defaults to Windows containers, so declare Linux as the desired container operating system.

    . {Invoke-WebRequest -useb aka.ms/iotedge-win} | Invoke-Expression; `
    Deploy-IoTEdge -ContainerOs Linux
    
  4. 此时,IoT Core 设备可能会自动重启。At this point, IoT Core devices may restart automatically. 其他 Windows 10 或 Windows Server 设备可能会提示你重启。Other Windows 10 or Windows Server devices may prompt you to restart. 如果是这样,请立即重启设备。If so, restart your device now. 设备准备就绪后,再次以管理员身份运行 PowerShell。Once your device is ready, run PowerShell as an administrator again.

  5. Initialize-IoTEdge 命令在计算机上配置 IoT Edge 运行时 。The Initialize-IoTEdge command configures the IoT Edge runtime on your machine. 该命令默认为使用设备连接字符串进行手动预配。The command defaults to manual provisioning with a device connection string. 再次将 Linux 声明为所需的容器操作系统。Declare Linux as the desired container operating system again.

    . {Invoke-WebRequest -useb aka.ms/iotedge-win} | Invoke-Expression; `
    Initialize-IoTEdge -ContainerOs Linux
    
  6. 出现提示时,请提供在步骤 1 中检索到的设备连接字符串。When prompted, provide the device connection string that you retrieved in step 1. 该设备连接字符串会将物理设备与 IoT 中心内的设备 ID 相关联。The device connection string associates the physical device with a device ID in IoT Hub.

    该设备连接字符串采用以下格式(不包括引号):HostName={IoT hub name}.azure-devices.cn;DeviceId={device name};SharedAccessKey={key}The device connection string takes the following format, and should not include quotation marks: HostName={IoT hub name}.azure-devices.cn;DeviceId={device name};SharedAccessKey={key}

验证是否成功安装Verify successful installation

检查 IoT Edge 服务的状态:Check the status of the IoT Edge service:

Get-Service iotedge

检查过去 5 分钟的服务日志:Examine service logs from the last 5 minutes:

. {Invoke-WebRequest -useb aka.ms/iotedge-win} | Invoke-Expression; Get-IoTEdgeLog

运行自动检查以查找最常见的配置和网络错误:Run an automated check for the most common configuration and networking errors:

iotedge check

列出正在运行的模块。List running modules. 在全新安装之后,应该看到运行的唯一模块是 edgeAgentAfter a new installation, the only module you should see running is edgeAgent. 首次部署 IoT Edge 模块后,其他系统模块 edgeHub 也会在设备上启动。After you deploy IoT Edge modules for the first time, the other system module, edgeHub, will start on the device too.

iotedge list

后续步骤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 installing IoT Edge 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.