为 Azure Sphere 设置 Azure IoT Edge

Azure IoT Edge在下游设备(如 Azure Sphere 和 Azure IoT 中心)之间提供筛选和数据处理层。 如果 Azure Sphere 设备生成大量需要后期处理的数据或数据,请考虑使用 Azure IoT Edge。

本主题介绍了使用 Azure Sphere 设置 Azure IoT Edge 设备的步骤。 Azure Sphere 支持 Azure IoT Edge版本 1.1 和 1.2;说明中指出了显著差异。 main差异如下:

  • 包名称从 iotedge 更改为 aziot-edge
  • 默认配置文件具有新的名称和位置。 在 Azure IoT Edge 1.1 中,配置文件为 /etc/iotedge/config.yaml。 在 Azure IoT Edge 1.2 中,配置文件为 /etc/aziot/config.toml
  • Azure IoT Edge 1.2 需要完全限定的域名 (FQDN) 通过网络上的 DNS 服务器访问。

完成本主题中的任务后,Azure Sphere 设备将配置为通过充当透明网关的 Azure IoT Edge 设备将数据发送到Azure IoT 中心。 可以按照 Azure IoT Edge 模块指南,在 Azure IoT Edge 设备上添加其他数据筛选和处理。

开始之前

本主题中的步骤假定:

  • Azure Sphere 设备通过 USB 连接到电脑。
  • 你有一个 Azure 订阅。
  • 已创建Azure IoT 中心实例并手动预配了设备。 请注意,在步骤 6) (过程中,需要对IoT 中心使用目录 CA 证书。
  • 你已创建一个资源组,与Azure IoT 中心和IoT Edge服务相关的所有资源都必须属于此资源组。

重要

虽然可以免费创建 Azure 订阅,但注册过程需要输入信用额度卡编号。 Azure 提供多个级别的订阅服务。 默认情况下,创建Azure IoT 中心实例时,会选择“标准”层(需要每月服务费)。 若要避免每月收费,请选择“免费”层。 免费层包括将设备与Azure IoT 中心实例(包括设备孪生)配合使用所需的服务。 如果没有 Azure 订阅,请在开始之前 创建一个免费帐户

概述

设置 Azure IoT Edge设备并将其配置为与 Azure Sphere 设备配合使用需要多步骤过程,并且应计划大约 8 小时才能完成这些步骤,尤其是在不熟悉 Azure IoT Edge的情况下。 如果这是你第一次使用 Azure IoT Edge,请按照每个部分中的快速入门说明设置和配置在 Linux 虚拟机上运行的IoT Edge设备。

每个 Azure Sphere 目录和 Azure IoT Edge 设备只能完成一次这些步骤;但是,必须在 Azure IoT 中心 中手动配置每个 Azure Sphere 设备,并且必须将 Azure IoT Edge 设备设置为 Azure Sphere 设备的父级。

设置步骤可以分为三个逻辑分组:

  • 创建IoT Edge设备并将其配置为透明网关

    • 步骤 1. 创建 Azure IoT Edge设备。
    • 步骤 2. 将 Azure IoT Edge网关设备配置为服务器。
    • 步骤 3. 打开 Azure IoT Edge网关设备端口进行通信。
    • 步骤 4. 在 config.toml (Azure IoT Edge版本 1.2) 或 config.yaml (Azure IoT Edge 1.1) 中更新网关主机名。
  • 在 Azure IoT 中心 中配置 Azure Sphere 设备

    • 步骤 5. 将 Azure IoT Edge 设备设置为 Azure Sphere 设备的父级。
  • 在 Azure Sphere 设备和IoT Edge设备之间建立受信任的连接

    • 步骤 6. 将 Azure Sphere 设备的目录 CA 证书提供给 Azure IoT Edge 设备。
    • 步骤 7. 向 Azure Sphere 设备提供 Azure IoT Edge设备的根 CA 证书。

步骤 1. 创建 Azure IoT Edge设备

必须设置 Azure IoT Edge设备,并将其注册到 Azure IoT 中心(如果尚未这样做)。

可以使用设备预配服务 (DPS) 预配 Azure IoT Edge 设备。 但是,不能使用 DPS 预配 Azure Sphere 设备。 不支持使用 Azure IoT 中心 DPS 在 Azure IoT Edge 设备下游自动预配设备。

可以按照IoT Edge快速入门中的步骤设置在 Linux VMWindows 设备上运行的 Azure IoT Edge 设备,并将其注册到 Azure IoT 中心。

使用快速入门中的步骤:

  • 将 Azure IoT Edge 设备注册到 Azure IoT 中心 实例。 请勿在此步骤中创建新的 IoT 中心,只需将 Azure IoT Edge 设备注册到已创建的 IoT 中心即可。
  • 在 Azure IoT Edge 设备上安装并启动 Azure IoT Edge 运行时。

注意

在IoT Edge快速入门中,在部署模块之前停止。

步骤 2. 将IoT Edge网关设备配置为服务器

按照将 Azure IoT Edge设备配置为充当 Azure IoT Edge 1.2) 或 Azure IoT Edge 1.1 (透明网关的说明进行操作,但在到达“在网关设备上打开端口”部分时停止。尽管这些说明指示将证书文件复制到IoT Edge设备,但在此过程的步骤 8 之前,请勿将文件复制到 VM。

在完成将设备网关配置为服务器的步骤中,你已具备:

  • 配置了 Azure IoT Edge设备证书。
  • 已部署 Azure IoT Edge 中心模块。
  • 配置了通过 Azure IoT Edge路由消息。

此外,在这些步骤中,你将创建以下证书:

  • 根 CA 证书: certs/azure-iot-test-only.root.ca.cert.pem
  • 用于将IoT Edge设备注册到IoT 中心) 的设备 CA 证书和私钥 (:
    • certs/iot-edge-device-identity-<cert-name>-full-chain.cert.pem
    • private/iot-edge-device-identity-<cert-name>.key.pem
  • IoT Edge CA 证书和私钥 (复制到IoT Edge设备并在其配置文件中引用) :
    • certs/iot-edge-device-ca-<cert-name>-full-chain.cert.pem
    • private/iot-edge-device-ca-<cert-name>.key.pem

如果遵循快速入门,请使用 Linux 说明来配置演示证书(如果生成证书的计算机是基于 Linux 的计算机)。 如果要生成证书的计算机是基于 Windows 的计算机,请使用 Windows 说明生成演示证书。 请参阅将 根证书复制到 Linux VM 或本地 Windows 计算机部分。

Azure IoT Edge根 CA 证书将在步骤 7 中修改,但在步骤 8 中需要原始证书。 保留原始证书的副本,以便可以重复使用它。

注意

更新 Azure (config.toml IoT Edge 1.2) 或 config.yaml (Azure IoT Edge 1.1) 文件的证书部分时, 请确保 certificates: config.toml (Azure IoT Edge 1.2) 或 config.yaml (Azure IoT Edge 1.1) 没有前面的空格,并且每个嵌套证书缩进两个空格。

更新文件后,验证 edgehub 模块是否在 Azure IoT Edge 设备上运行:

sudo iotedge list

有关详细信息,请参阅如何将IoT Edge设备用作网关 (Azure IoT Edge 1.2) Azure IoT Edge 1.1

如果使用测试证书,请在生成下游设备证书之前停止,如 (Azure IoT Edge 1.2) Azure IoT Edge 1.1 中所述。

步骤 3. 打开 Azure IoT Edge网关设备端口进行通信

网关设备必须能够接收来自其下游设备的消息。 若要使网关方案正常工作,必须至少为来自下游设备的入站流量打开 IoT 中心支持的协议之一。

Azure Sphere 使用协议 MQTT。 此协议使用端口 8883。

有关详细信息,请参阅在网关设备上打开端口 (Azure IoT Edge 1.2) Azure IoT Edge 1.1

在快速入门中设置 Edge 设备后,请按照以下步骤在 Windows VM 上打开端口 8883:

  1. 如有必要,请使用 Azure 帐户登录到Azure 门户
  2. 导航到在步骤 1:创建 Azure IoT Edge设备部分中创建的虚拟机。
  3. 在左侧 的“设置” 部分中,选择“ 网络”,然后选择 “添加入站端口规则”。
  4. “添加入站端口规则” 边栏选项卡中 的“目标端口范围”下,将值更改为 8883
  5. 在“ 协议”下,选择“ TCP”。
  6. “名称”下,将值更改为 MQTT
  7. 将所有其他设置保留为默认值,然后选择“ 添加”。

在快速入门中设置 Azure IoT Edge设备后,按照以下步骤在 Linux VM 上打开端口 8883:

  1. 运行以下命令,检查 MQTT 的端口是否已打开:

    sudo netstat -lptu

  2. 如有必要,请使用以下命令打开端口:

    sudo ufw allow 8883

这将定义入站安全规则,以允许 MQTT 协议与 Azure IoT Edge 网关通信。

步骤 4. 更新网关主机名

此步骤使用不同的过程,具体取决于所使用的 Azure IoT Edge版本:Azure IoT Edge 1.2Azure IoT Edge 1.1

Azure IoT Edge 1.2:更新 config.toml 中的网关主机名

Azure IoT Edge运行时支持少于 64 个字符的主机名。 物理计算机通常没有较长的主机名,但如果使用虚拟机作为 Azure IoT Edge设备(如快速入门中的示例),则必须手动配置主机名。

若要对无效主机名进行故障排除,请参阅 常见错误解决方法

在快速入门中配置 MQTT 端口后,请按照以下步骤配置 Azure IoT Edge设备主机名:

  1. 在 Azure 门户中导航到IoT Edge设备 (Linux VM) ,并从概述页复制 DNS 名称的值,找到IoT Edge网关的完全限定域名 (FQDN) 。

  2. 如有必要,请登录到 Azure IoT Edge设备。

  3. 在文本编辑器中打开 config.toml 文件。

    /etc/aziot/config.toml

  4. 将 FQDN 粘贴到 config.toml 的主机名部分。 确保名称全部为小写。

    hostname: "<iotedge_machinename>.<mydomain>"
    

    注意

    根据 Azure IoT Edge v1.2 及更高版本的设计,主机名必须是 FQDN 名称, (不再允许使用 IP 地址,如 v1.1) 中一样;因此,必须在同一网络上具有相应 A 记录的 DNS 服务器。

  5. iotedge重启守护程序。

    sudo systemctl restart iotedge

  6. 如果在状态 ) 看到错误 (带前缀的彩色文本 ) ,请检查守护程序日志以获取详细的错误信息。

    sudo journalctl -u iotedge --no-pager --no-full

  7. 若要避免出现警告,请将 Azure IoT Edge 设备上的模块的 DNS 配置设置为包含 /etc/docker/daemon.json 处的配置文件,例如:

    {
         "dns": ["<IP address of your DNS server>"]
    }
    

Azure IoT Edge 1.1:更新 config.yaml 中的网关主机名

Azure IoT Edge运行时支持少于 64 个字符的主机名。 物理计算机通常没有较长的主机名,但如果使用虚拟机作为 Azure IoT Edge设备(如快速入门中的示例),则必须手动配置主机名。

若要对无效主机名进行故障排除,请参阅 常见错误解决方法

在快速入门中配置 MQTT 端口后,请按照以下步骤配置 Azure IoT Edge设备主机名:

  1. 在Azure 门户中,导航到虚拟机。 复制“概述”部分中计算机) 的 DNS 名称 (FQDN 的值。

  2. 如有必要,请登录到 Azure IoT Edge设备。

  3. 在文本编辑器中打开 config.yaml 文件。

    /etc/iotedge/config.yaml

  4. 将 FQDN 粘贴到 config.yaml 的主机名部分。 确保名称全部为小写。

    `hostname: "<machinename>.<region>.cloudapp.azure.com"`
    

    注意

    如果 DNS 解析程序位于网络上,则可能必须使用主机名的裸机名称 (IP 地址或实际主机名) ,因为 Azure Sphere 不支持 netbios。

  5. iotedge重启守护程序。

    sudo systemctl restart iotedge

  6. 如果在状态 ) 看到错误 (带前缀的彩色文本 ) ,请检查守护程序日志以获取详细的错误信息。

    sudo journalctl -u iotedge --no-pager --no-full

  7. 若要避免出现警告,请将 Azure IoT Edge 设备上的模块的 DNS 配置设置为包含 /etc/docker/daemon.json 处的配置文件,例如:

    {
        "dns": ["<IP address of your DNS server>"]
    }
    

步骤 5. 将 Azure IoT Edge 设备设置为 Azure Sphere 设备的父级

按照以下步骤将 Azure IoT Edge 设备设置为 Azure Sphere 设备的父级:

  1. 导航到上面手动预配的 Azure Sphere 设备。
  2. 选择“ 设备 ID”。
  3. 选择“ 无父设备”下的齿轮图标。
  4. 选择要设置为父级的 Azure IoT Edge设备。
  5. 选择 “确定”,然后选择“ 保存”。

Azure IoT Edge设备现在成为 Azure Sphere 设备的父级。

步骤 6. 将 Azure Sphere 设备的目录 CA 证书提供给IoT Edge设备

若要验证 Azure Sphere 设备证书,Azure IoT Edge设备必须具有自己的目录 CA 副本。

  1. 下载目录 CA 证书(如果尚未下载)。 注意:作为配置Azure IoT 中心的一部分,你应该已经完成了此操作。

    • 在命令提示符下,使用 Azure 登录名登录:

      az login
      
    • 下载 Azure Sphere 目录的 CA 证书。 此命令将证书下载到当前工作目录中名为 CAcertificate.cer 的文件。 确保将文件下载到具有写入权限的目录,否则下载操作将失败。 输出文件必须具有 .cer 扩展名。

      az sphere ca-certificate download --resource-group MyResourceGroup --catalog MyCatalog --output-file CACertificate.cer
      
  2. 将目录 CA 证书转换为 PEM 格式。 在 Windows 计算机上转换格式的示例说明如下:

    • 在计算机上找到证书的路径,然后双击证书将其打开。
    • 打开“ 详细信息 ”选项卡,然后选择“ 复制到文件”。
    • “证书导出 ”向导中,选择“ 下一步”。
    • 选择 Base-64 编码 X.509 (CER) 格式,然后选择“ 下一步”。
    • 输入要导出的证书的文件名,然后选择“ 下一步”。
    • 选择“ 完成” 以完成向导。
    • 将下载的证书文件重命名为 .pem 文件扩展名。
  3. 将目录证书追加到 Azure IoT Edge根证书。 请记住,你在步骤 2 中创建了 Azure IoT Edge证书。

    • 如有必要,请登录到 Azure IoT Edge设备。
    • 找到IoT Edge设备的根 CA 证书的路径,并在文本编辑器中将其打开。 如果遵循快速入门,然后使用 Azure IoT Edge git 存储库中提供的脚本来创建演示证书,则根 CA 证书名为 azure-iot-test-only.root.ca.cert.pem
    • 在文本编辑器中打开 PEM 格式的 Azure Sphere 目录 CA 证书 () 。 复制内容并将其粘贴到IoT Edge根 CA 证书的末尾。
    • 保存对 Azure IoT Edge根 CA 证书所做的更改,然后关闭该文件。
    • 重启 Azure IoT Edge 设备。 对于 Linux Azure IoT Edge设备,请运行:sudo systemctl restart iotedge
    • 验证模块是否在 Azure IoT Edge 设备上运行。 对于 Linux Azure IoT Edge设备,请运行:sudo iotedge list

步骤 7. 将 Azure IoT Edge 设备的根 CA 证书提供给 Azure Sphere 设备

若要验证 Azure IoT Edge设备证书,Azure Sphere 设备 (下游设备) 必须具有在步骤 2 中配置的 Azure IoT Edge设备根 CA 证书的自己的副本。

有关详细信息,请参阅将下游设备连接到 Azure IoT Edge网关

  1. 找到原始 Azure IoT Edge设备根证书。

    • 请记住,原始 Azure IoT Edge设备根证书是在步骤 2 中创建的证书。
    • 如果遵循快速入门,然后使用 IoT Edge git 存储库中提供的脚本来创建演示证书,则根 CA 证书称为 azure-iot-test-only.root.ca.cert.pem
  2. 通过将 Azure IoT Edge根证书添加到应用程序包,将其复制到 Azure Sphere 设备。

步骤 8. 将 IoT 根证书复制到远程计算机

按照以下步骤将 Azure IoT Edge 根证书复制到远程 Linux VM 或本地 Windows 计算机:

  • 可以安装 WinSCP 工具来 传输文件。 该工具提供 GUI,因此比命令行方法更易于使用。

  • 若要使用命令行,请使用 SCP (安全复制) 命令行工具。

  • 若要将文件从本地 Windows 计算机传输到远程 Linux VM,请在 Windows PowerShell 中运行以下命令:

    powershell -Command scp -r <path-to-file-name> <userName@remote-ip>:<path-to-destination-directory>
    

    示例语法:

    scp C:\Documents\cert.pem AzureUser@edgevmname-west.westus22.cloudapp.azure.com:/home/azureUser/test/

  • 若要将文件从远程 Linux VM 传输到本地 Windows 计算机,请在 Windows PowerShell 中运行以下命令:

    powershell -Command scp -r <userName@remote-ip>:<path-to-file-name> <path-to-destination-dir>
    

    示例语法:

    scp azureUser@edgevmname-west.westus22.cloudapp.azure.com:/home/azureUser/test/cert.pem C:\Documents\

故障 排除

如果在环境中运行 Azure IoT Edge时遇到问题,请使用以下文章进行故障排除和诊断:

后续步骤

  • 完成本主题中的步骤后,可以按照使用 Azure IoT Edge 进行连接的说明运行 Azure IoT 示例