将 IoT Edge 上的 Azure Blob 存储模块部署到设备Deploy the Azure Blob Storage on IoT Edge module to your device

有多种方法可以将模块部署到 IoT Edge 设备,并且所有这些方法都适用于 IoT Edge 上的 Azure Blob 存储模块。There are several ways to deploy modules to an IoT Edge device and all of them work for Azure Blob Storage on IoT Edge modules. 两种最简单的方法是使用 Azure 门户或 Visual Studio Code 模板。The two simplest methods are to use the Azure portal or Visual Studio Code templates.

先决条件Prerequisites

从 Azure 门户部署Deploy from the Azure portal

Azure 门户引导你创建部署清单并将部署推送到 IoT Edge 设备。The Azure portal guides you through creating a deployment manifest and pushing the deployment to an IoT Edge device.

选择设备Select your device

  1. 登录 Azure 门户,导航到 IoT 中心。Sign in to the Azure portal and navigate to your IoT hub.
  2. 从菜单中选择“IoT Edge” 。Select IoT Edge from the menu.
  3. 在设备列表中单击目标设备的 ID。Click on the ID of the target device from the list of devices.
  4. 选择“设置模块” 。Select Set Modules.

配置部署清单Configure a deployment manifest

部署清单是一个 JSON 文档,其中描述了要部署的模块、数据在模块间的流动方式以及模块孪生的所需属性。A deployment manifest is a JSON document that describes which modules to deploy, how data flows between the modules, and desired properties of the module twins. Azure 门户提供了一个引导你完成创建部署清单的向导。The Azure portal has a wizard that walks you through creating a deployment manifest. 该向导将三个步骤整理成多个选项卡:“模块”、“路由”和“查看 + 创建” 。It has three steps organized into tabs: Modules, Routes, and Review + Create.

添加模块Add modules

  1. 在页面的“IoT Edge 模块”部分,单击“添加”下拉列表并选择“IoT Edge 模块”,显示“添加 IoT Edge 模块”页面 。In the IoT Edge Modules section of the page, click the Add dropdown and select IoT Edge Module to display the Add IoT Edge Module page.

  2. 在“模块设置”选项卡上,提供模块的名称,然后指定容器映像 URI :On the Module Settings tab, provide a name for the module and then specify the container image URI:

    示例:Examples:

    • IoT Edge 模块名称azureblobstorageoniotedgeIoT Edge Module Name: azureblobstorageoniotedge
    • 映像 URImcr.microsoft.com/azure-blob-storage:latestImage URI: mcr.microsoft.com/azure-blob-storage:latest

    模块孪生设置

    如本程序所述,在“模块设置”、“容器创建选项”和“模块孪生设置”选项卡上指定值之前,请勿选择“添加” 。Don't select Add until you've specified values on the Module Settings, Container Create Options, and Module Twin Settings tabs as described in this procedure.

    重要

    调用模块时,Azure IoT Edge 区分大小写,存储 SDK 也默认为小写。Azure IoT Edge is case-sensitive when you make calls to modules, and the Storage SDK also defaults to lowercase. 虽然在 Azure 市场中此模块的名称为 AzureBlobStorageonIoTEdge,但将名称更改为小写有助于确保与 IoT Edge 上的 Azure Blob 存储模块建立的连接不会中断。Although the name of the module in the Azure Marketplace is AzureBlobStorageonIoTEdge, changing the name to lowercase helps to ensure that your connections to the Azure Blob Storage on IoT Edge module aren't interrupted.

  3. 打开“容器创建选项” 选项卡。Open the Container Create Options tab.

    模块孪生设置

    将以下 JSON 复制并粘贴到框中,以便为设备上的存储提供存储帐户信息和装载。Copy and paste the following JSON into the box, to provide storage account information and a mount for the storage on your device.

    {
      "Env":[
        "LOCAL_STORAGE_ACCOUNT_NAME=<your storage account name>",
        "LOCAL_STORAGE_ACCOUNT_KEY=<your storage account key>"
      ],
      "HostConfig":{
        "Binds":[
            "<storage mount>"
        ],
        "PortBindings":{
          "11002/tcp":[{"HostPort":"11002"}]
        }
      }
    }
    
  4. 使用以下信息更新已复制到容器创建选项中的 JSON:Update the JSON that you copied into Container Create Options with the following information:

    • 请将 <your storage account name> 替换为容易记住的名称。Replace <your storage account name> with a name that you can remember. 帐户名长度应为 3 到 24 个字符,并带有小写字母和数字。Account names should be 3 to 24 characters long, with lowercase letters and numbers. 不含空格。No spaces.

    • <your storage account key> 替换为 64 字节 base64 密钥。Replace <your storage account key> with a 64-byte base64 key. 你可以使用 GeneratePlus 等工具生成密钥。You can generate a key with tools like GeneratePlus. 你将使用这些凭据从其他模块访问 blob 存储。You'll use these credentials to access the blob storage from other modules.

    • 根据容器操作系统替换 <storage mount>Replace <storage mount> according to your container operating system. 提供 blob 模块将在其中存储其数据的 IoT Edge 设备上的名称或指向现有目录的绝对路径。Provide the name of a volume or the absolute path to an existing directory on your IoT Edge device where the blob module will store its data. 存储装载将你在设备上提供的位置映射到模块中设置的位置。The storage mount maps a location on your device that you provide to a set location in the module.

      重要

      请勿更改存储装载值的后半部分,该部分指向 IoT Edge 模块上 Blob 存储中的特定位置。Do not change the second half of the storage mount value, which points to a specific location in the Blob Storage on IoT Edge module. 对于 Linux 容器,存储装载必须始终以“:/blobroot” 结尾;对于 Windows 容器,必须以“:C:/BlobRoot” 结尾。The storage mount must always end with :/blobroot for Linux containers and :C:/BlobRoot for Windows containers.

  5. 在“模块孪生设置”选项卡上,复制以下 JSON 并将其粘贴到框中 。On the Module Twin Settings tab, copy the following JSON and paste it into the box.

    模块孪生设置

    使用适当的值配置每个属性,如占位符所示。Configure each property with an appropriate value, as indicated by the placeholders. 如果使用 IoT Edge 模拟器,请按照 deviceToCloudUploadPropertiesdeviceAutoDeleteProperties 中所述,将这些属性的值设置为相关环境变量。If you are using the IoT Edge simulator, set the values to the related environment variables for these properties as described by deviceToCloudUploadProperties and deviceAutoDeleteProperties.

    {
      "deviceAutoDeleteProperties": {
        "deleteOn": <true, false>,
        "deleteAfterMinutes": <timeToLiveInMinutes>,
        "retainWhileUploading": <true,false>
      },
      "deviceToCloudUploadProperties": {
        "uploadOn": <true, false>,
        "uploadOrder": "<NewestFirst, OldestFirst>",
        "cloudStorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=<your Azure Storage Account Name>;AccountKey=<your Azure Storage Account Key>; EndpointSuffix=<your end point suffix>",
        "storageContainersForUpload": {
          "<source container name1>": {
            "target": "<target container name1>"
          }
        },
        "deleteAfterUpload": <true,false>
      }
    }
    

    有关在部署模块后配置 deviceToCloudUploadProperties 和 deviceAutoDeleteProperties 的信息,请参阅编辑模块孪生For information on configuring deviceToCloudUploadProperties and deviceAutoDeleteProperties after your module has been deployed, see Edit the Module Twin. 若要详细了解所需属性,请参阅定义或更新所需属性For more information about desired properties, see Define or update desired properties.

  6. 选择“添加” 。Select Add.

  7. 在完成时选择“下一步: 路由”继续转到路由部分。Select Next: Routes to continue to the routes section.

指定路由Specify routes

保留默认路由并选择“下一步: 查看 + 创建”继续转到查看部分。Keep the default routes and select Next: Review + create to continue to the review section.

评审部署Review deployment

评审部分介绍了根据上述两部分中的选择所创建的 JSON 部署清单。The review section shows you the JSON deployment manifest that was created based on your selections in the previous two sections. 另外还有两个模块已声明但却未添加,它们是: $edgeAgent$edgeHubThere are also two modules declared that you didn't add: $edgeAgent and $edgeHub. 这两个模块构成 IoT Edge 运行时,且是每个部署中所需的默认设置。These two modules make up the IoT Edge runtime and are required defaults in every deployment.

查看部署信息,然后选择“创建” 。Review your deployment information, then select Create.

验证部署Verify your deployment

创建部署后,返回到 IoT 中心的“IoT Edge”页。 After you create the deployment, you return to the IoT Edge page of your IoT hub.

  1. 选择用作部署目标的 IoT Edge 设备,以打开其详细信息。Select the IoT Edge device that you targeted with the deployment to open its details.
  2. 在设备详细信息,检查 Blob 存储模块是否列出为“在部署中指定”和“由设备报告”。 In the device details, verify that the blob storage module is listed as both Specified in deployment and Reported by device.

可能需要等待一段时间,该模块才会在设备上启动并向 IoT 中心发回报告。It may take a few moments for the module to be started on the device and then reported back to IoT Hub. 刷新页面以查看更新的状态。Refresh the page to see an updated status.

从 Visual Studio Code 部署Deploy from Visual Studio Code

Azure IoT Edge 在 Visual Studio Code 中提供模板,以帮助你开发边缘解决方案。Azure IoT Edge provides templates in Visual Studio Code to help you develop edge solutions. 使用以下步骤创建带有 Blob 存储模块的新 IoT Edge 解决方案,并配置部署清单。Use the following steps to create a new IoT Edge solution with a blob storage module and to configure the deployment manifest.

  1. 选择“查看” > “命令面板” 。Select View > Command Palette.

  2. 在“命令面板”中,输入并运行 Azure IoT Edge:New IoT Edge solution 命令。In the command palette, enter and run the command Azure IoT Edge: New IoT Edge solution.

    运行新的 IoT Edge 解决方案

    按命令面板中的提示创建解决方案。Follow the prompts in the command palette to create your solution.

    字段Field Value
    选择文件夹Select folder 在适用于 Visual Studio Code 的开发计算机上选择用于创建解决方案文件的位置。Choose the location on your development machine for Visual Studio Code to create the solution files.
    提供解决方案名称Provide a solution name 输入解决方案的描述性名称,或者接受默认的 EdgeSolutionEnter a descriptive name for your solution or accept the default EdgeSolution.
    选择模块模板Select module template 选择“现有模块(输入完整映像 URL)”。 Choose Existing Module (Enter full image URL).
    提供模块名称Provide a module name 输入模块的全小写名称,例如 azureblobstorageoniotedgeEnter an all-lowercase name for your module, like azureblobstorageoniotedge.

    必须对 IoT Edge 上的 Azure Blob 存储模块使用小写名称。It's important to use a lowercase name for the Azure Blob Storage on IoT Edge module. 引用模块时,IoT Edge 区分大小写,存储 SDK 默认为小写。IoT Edge is case-sensitive when referring to modules, and the Storage SDK defaults to lowercase.
    提供模块的 Docker 映像Provide Docker image for the module 提供映像 URI:mcr.microsoft.com/azure-blob-storage:latestProvide the image URI: mcr.microsoft.com/azure-blob-storage:latest

    Visual Studio Code 采用你提供的信息,创建一个 IoT Edge 解决方案,然后在新窗口中加载它。Visual Studio Code takes the information you provided, creates an IoT Edge solution, and then loads it in a new window. 解决方案模板创建包含 blob 存储模块映像的部署清单模板,但需要配置模块的创建选项。The solution template creates a deployment manifest template that includes your blob storage module image, but you need to configure the module's create options.

  3. 在新的解决方案工作区中打开 deployment.template.json ,然后找到“模块” 部分。Open deployment.template.json in your new solution workspace and find the modules section. 进行以下配置更改:Make the following configuration changes:

    1. 删除 SimulatedTemperatureSensor 模块,因为此部署不需要该模块。Delete the SimulatedTemperatureSensor module, as it's not necessary for this deployment.

    2. 将以下代码复制并粘贴到 createOptions 字段中:Copy and paste the following code into the createOptions field:

      "Env":[
       "LOCAL_STORAGE_ACCOUNT_NAME=<your storage account name>",
       "LOCAL_STORAGE_ACCOUNT_KEY=<your storage account key>"
      ],
      "HostConfig":{
        "Binds": ["<storage mount>"],
        "PortBindings":{
          "11002/tcp": [{"HostPort":"11002"}]
        }
      }
      

      更新模块 createOptions - Visual Studio Code

  4. 请将 <your storage account name> 替换为容易记住的名称。Replace <your storage account name> with a name that you can remember. 帐户名长度应为 3 到 24 个字符,并带有小写字母和数字。Account names should be 3 to 24 characters long, with lowercase letters and numbers. 不含空格。No spaces.

  5. <your storage account key> 替换为 64 字节 base64 密钥。Replace <your storage account key> with a 64-byte base64 key. 你可以使用 GeneratePlus 等工具生成密钥。You can generate a key with tools like GeneratePlus. 你将使用这些凭据从其他模块访问 blob 存储。You'll use these credentials to access the blob storage from other modules.

  6. 根据容器操作系统替换 <storage mount>Replace <storage mount> according to your container operating system. 提供的名称或 IoT Edge 设备上希望 blob 模块在其中存储其数据的目录绝对路径。Provide the name of a volume or the absolute path to a directory on your IoT Edge device where you want the blob module to store its data. 存储装载将你在设备上提供的位置映射到模块中设置的位置。The storage mount maps a location on your device that you provide to a set location in the module.

    重要

    请勿更改存储装载值的后半部分,该部分指向 IoT Edge 模块上 Blob 存储中的特定位置。Do not change the second half of the storage mount value, which points to a specific location in the Blob Storage on IoT Edge module. 对于 Linux 容器,存储装载必须始终以“:/blobroot” 结尾;对于 Windows 容器,必须以“:C:/BlobRoot” 结尾。The storage mount must always end with :/blobroot for Linux containers and :C:/BlobRoot for Windows containers.

  7. 通过将以下 JSON 添加到 deployment.template.json 文件,为模块配置 deviceToCloudUploadPropertiesdeviceAutoDeletePropertiesConfigure deviceToCloudUploadProperties and deviceAutoDeleteProperties for your module by adding the following JSON to the deployment.template.json file. 为每个属性配置适当的值并保存文件。Configure each property with an appropriate value and save the file. 如果使用 IoT Edge 模拟器,请将这些属性的值设置为相关环境变量,可以在 deviceToCloudUploadPropertiesdeviceAutoDeleteProperties 的说明部分中找到这些值If you are using the IoT Edge simulator, set the values to the related environment variables for these properties, which you can find in the explanation section of deviceToCloudUploadProperties and deviceAutoDeleteProperties

    "<your azureblobstorageoniotedge module name>":{
      "properties.desired": {
        "deviceAutoDeleteProperties": {
          "deleteOn": <true, false>,
          "deleteAfterMinutes": <timeToLiveInMinutes>,
          "retainWhileUploading": <true, false>
        },
        "deviceToCloudUploadProperties": {
          "uploadOn": <true, false>,
          "uploadOrder": "<NewestFirst, OldestFirst>",
          "cloudStorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=<your Azure Storage Account Name>;AccountKey=<your Azure Storage Account Key>;EndpointSuffix=<your end point suffix>",
          "storageContainersForUpload": {
            "<source container name1>": {
              "target": "<target container name1>"
            }
          },
          "deleteAfterUpload": <true, false>
        }
      }
    }
    

    设置 azureblobstorageoniotedge 的所需属性 - Visual Studio Code

    有关在部署模块后配置 deviceToCloudUploadProperties 和 deviceAutoDeleteProperties 的信息,请参阅编辑模块孪生For information on configuring deviceToCloudUploadProperties and deviceAutoDeleteProperties after your module has been deployed, see Edit the Module Twin. 若要详细了解容器创建选项、重启策略和所需状态,请参阅 EdgeAgent 所需属性For more information about container create options, restart policy, and desired status, see EdgeAgent desired properties.

  8. 保存 deployment.template.json 文件。Save the deployment.template.json file.

  9. 右键单击“deployment.template.json” 并选择“生成 IoT Edge 部署清单” 。Right-click deployment.template.json and select Generate IoT Edge deployment manifest.

  10. Visual Studio Code 会获取你在 deployment.template.json 中提供的信息,并使用它来创建新的部署清单文件。Visual Studio Code takes the information that you provided in deployment.template.json and uses it to create a new deployment manifest file. 在解决方案工作区中的新 config 文件夹中创建部署清单。The deployment manifest is created in a new config folder in your solution workspace. 获得该文件后,可以按照从 Visual Studio Code 中部署 Azure IoT Edge 模块使用 Azure CLI 2.0 部署 Azure IoT Edge 模块中的步骤进行操作。Once you have that file, you can follow the steps in Deploy Azure IoT Edge modules from Visual Studio Code or Deploy Azure IoT Edge modules with Azure CLI 2.0.

部署多个模块实例Deploy multiple module instances

若要在 IoT Edge 模块上部署 Azure Blob 存储的多个实例,需要提供不同的存储路径,并更改模块绑定到的 HostPort 值。If you want to deploy multiple instances of the Azure Blob Storage on IoT Edge module, you need to provide a different storage path and change the HostPort value that the module binds to. Blob 存储模块始终在容器中公开端口 11002,但你可以声明它在主机上绑定到的端口。The blob storage modules always expose port 11002 in the container, but you can declare which port it's bound to on the host.

编辑“容器创建选项” (在 Azure 门户中)或 createOptions 字段(在 Visual Studio Code 的 deployment.template.json 文件中),更改 HostPort 值:Edit Container Create Options (in the Azure portal) or the createOptions field (in the deployment.template.json file in Visual Studio Code) to change the HostPort value:

"PortBindings":{
  "11002/tcp": [{"HostPort":"<port number>"}]
}

连接到其他 blob 存储模块时,请将终结点更改为指向更新的主机端口。When you connect to additional blob storage modules, change the endpoint to point to the updated host port.

配置代理支持Configure proxy support

如果组织使用代理服务器,则需要为 edgeAgent 和 edgeHub 运行时模块配置代理支持。If your organization is using a proxy server, you will need to configure proxy support for the edgeAgent and edgeHub runtime modules. 此过程涉及两项任务:This process involves two tasks:

  • 在设备上配置运行时守护程序和 IoT Edge 代理。Configure the runtime daemons and the IoT Edge agent on the device.
  • 为部署清单 JSON 文件中的模块设置 HTTPS_PROXY 环境变量。Set the HTTPS_PROXY environment variable for modules in the deployment manifest JSON file.

将 IoT Edge 设备配置为通过代理服务器进行通信中介绍了此过程。This process is described in Configure an IoT Edge device to communicate through a proxy server.

此外,blob 存储模块还需要清单部署文件中的 HTTPS_PROXY 设置。In addition, a blob storage module also requires the HTTPS_PROXY setting in the manifest deployment file. 可以直接编辑部署清单文件,也可以使用 Azure 门户。You can directly edit the deployment manifest file, or use the Azure portal.

  1. 在 Azure 门户中导航到 IoT 中心,然后从左窗格菜单中选择“IoT Edge”。 Navigate to your Iot hub in the Azure portal and select Iot Edge from the left pane menu.

  2. 选择要配置模块的设备。Select the device with the module to configure.

  3. 选择“设置模块” 。Select Set Modules.

  4. 在此页的“IoT Edge 模块”部分,选择 blob 存储模块 。In the IoT Edge Modules section of the page, select the blob storage module.

  5. 在“更新 IoT Edge 模块”页上,选择“环境变量”选项卡。 On the Update IoT Edge Module page, select the Environment Variables tab.

  6. 添加 HTTPS_PROXY 作为“名称”, 添加代理 URL 作为“值”。 Add HTTPS_PROXY for the Name and your proxy URL for the Value.

    设置 HTTPS_PROXY 环境变量

  7. 单击“更新” ,然后单击“查看 + 创建” 。Click Update, then Review + Create.

  8. 请注意,代理会添加到部署清单中的模块,此时请选择“创建” 。Note that the proxy is added to the module in deployment manifest and select Create.

  9. 通过从设备详细信息页中选择模块来验证设置,然后在“IoT Edge 模块详细信息” 页的下半部分选择“环境变量” 选项卡。Verify the setting by selecting the module from the device details page, and on the lower part of the IoT Edge Modules Details page select the Environment Variables tab.

    设置 HTTPS_PROXY 环境变量

后续步骤Next steps

详细了解 IoT Edge 上的 Azure Blob 存储Learn more about Azure Blob Storage on IoT Edge.

若要详细了解部署清单的工作原理及创建方式,请参阅了解如何使用、配置和重用 IoT Edge 模块For more information about how deployment manifests work and how to create them, see Understand how IoT Edge modules can be used, configured, and reused.