你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:将连接的注册表部署到 IoT Edge 设备

在本快速入门中使用 Azure CLI 将连接的注册表作为模块部署在 Azure IoT Edge 设备上。 IoT Edge 设备可访问云中的父 Azure 容器注册表。

有关将连接的注册表与 IoT Edge 结合使用的概述,请参阅将连接的注册表与 Azure IoT Edge 结合使用。 此场景对应于 IoT Edge 层次结构顶层的设备。

先决条件

将映像导入到云注册表

使用 az acr import 命令将以下容器映像导入到云注册表。 如果已导入这些映像,请跳过此步骤。

连接注册表映像

为了支持嵌套的 IoT Edge 方案,连接注册表运行时的容器映像必须在专用 Azure 容器注册表中可用。 使用 az acr import 命令将连接注册表映像导入专用注册表。

# Use the REGISTRY_NAME variable in the following Azure CLI commands to identify the registry
REGISTRY_NAME=<container-registry-name>

az acr import \
  --name $REGISTRY_NAME \
  --source mcr.microsoft.com/acr/connected-registry:0.8.0

IoT Edge 和 API 代理映像

若要支持嵌套 IoT Edge 上连接的注册表,需要为 IoT Edge 和 API 代理部署模块。 将这些映像导入专用注册表。

IoT Edge API 代理模块允许 IoT Edge 设备在同一端口(例如 443)上使用 HTTPS 协议公开多个服务。

az acr import \
  --name $REGISTRY_NAME \
  --source mcr.microsoft.com/azureiotedge-agent:1.2.4

az acr import \
  --name $REGISTRY_NAME \
  --source mcr.microsoft.com/azureiotedge-hub:1.2.4

az acr import \
  --name $REGISTRY_NAME \
  --source mcr.microsoft.com/azureiotedge-api-proxy:1.1.2

az acr import \
  --name $REGISTRY_NAME \
  --source mcr.microsoft.com/azureiotedge-diagnostics:1.2.4

Hello-world 映像

若要测试连接注册表,请导入 hello-world 映像。 此存储库将与连接注册表同步,并由连接注册表客户端拉取。

az acr import \
  --name $REGISTRY_NAME \
  --source mcr.microsoft.com/hello-world:1.1.2

检索连接注册表配置

在将连接的注册表部署到 IoT Edge 设备之前,需要从 Azure 中的连接的注册表资源中检索配置设置。

使用 az acr connected-registry get-settings 命令获取安装连接的注册表所需的设置信息。 以下示例将 HTTPS 指定为父协议。 如果父注册表为云注册表,则需要此协议。

az acr connected-registry get-settings \
  --registry $REGISTRY_NAME \
  --name $CONNECTED_REGISTRY_RW \
  --parent-protocol https

默认情况下,设置信息不包括同步令牌密码,部署连接的注册表时也需要该密码。 (可选)通过传递 --generate-password 1generate-password 2 参数来生成密码之一。 将生成的密码保存到安全位置。 不能再次检索该密码。

警告

重新生成密码将轮换同步令牌凭据。 如果使用旧密码配置了设备,则需要更新配置。

命令输出包括注册表连接字符串和相关设置。 下例输出显示了与父级注册表 contosoregistry 连接的名为 myconnectedregistry 的注册表的连接字符串 :

{
  "ACR_REGISTRY_CONNECTION_STRING": "ConnectedRegistryName=myconnectedregistry;SyncTokenName=myconnectedregistry-sync-token;SyncTokenPassword=xxxxxxxxxxxxxxxx;ParentGatewayEndpoint=contosoregistry.eastus.data.azurecr.io;ParentEndpointProtocol=https"
}

配置 IoT Edge 的部署清单

部署清单是一个 JSON 文档,用于描述要部署到 IoT Edge 设备的模块。 有关详细信息,请参阅了解如何使用、配置和重用 IoT Edge 模块

若要使用 Azure CLI 部署连接的注册表和 API 代理模块,请将以下部署清单在本地保存为 manifest.json 文件。 在下一部分通过运行命令将配置应用到设备时,会用到这个文件路径。

连接注册表模块设置

  • 使用前面部分中的令牌凭据和连接字符串更新 env 节点中的相关 JSON 值。

  • 以下环境变量在 env 节点中是可选的:

    变量 说明
    ACR_REGISTRY_LOGIN_SERVER 指定唯一的主机名或 FQDN。 如果使用,则连接注册表仅接受对此登录服务器值的请求。

    如果未提供值,则可以通过任何登录服务器值访问连接注册表。
    ACR_REGISTRY_CERTIFICATE_VOLUME 如果可以通过 HTTPS 访问连接注册表,则指向存储 HTTPS 证书的卷。

    如果未设置,则默认位置为 /var/acr/certs
    ACR_REGISTRY_DATA_VOLUME 覆盖连接注册表将存储映像的默认位置 /var/acr/data

    此位置必须与容器的卷绑定匹配。

    重要

    如果连接注册表侦听 80 和 443 以外的端口,则 ACR_REGISTRY_LOGIN_SERVER 值(若指定)必须包含该端口。 示例:192.168.0.100:8080

  • 如果未使用 API 代理模块,应设置连接注册表的 HostPort 绑定。 示例:

     "createOptions": "{\"HostConfig\":{\"Binds\":[\"/home/azureuser/connected-registry:/var/acr/data\"],\"PortBindings\":{\"8080/tcp\":[{\"HostPort\":\"8080\"}]}}}"
    

API 代理模块设置

  • API 代理将侦听配置为 NGINX_DEFAULT_PORT 的端口 8000。 有关 API 代理设置的更多信息,请参阅 IoT Edge GitHub 存储库
{
    "modulesContent": {
        "$edgeAgent": {
            "properties.desired": {
                "modules": {
                    "connected-registry": {
                        "settings": {
                            "image": "<REPLACE_WITH_CLOUD_REGISTRY_NAME>.azurecr.io/acr/connected-registry:0.8.0",
                            "createOptions": "{\"HostConfig\":{\"Binds\":[\"/home/azureuser/connected-registry:/var/acr/data\"]}}"
                        },
                        "type": "docker",
                        "env": {
                            "ACR_REGISTRY_CONNECTION_STRING": {
                                "value": "ConnectedRegistryName=<REPLACE_WITH_CONNECTED_REGISTRY_NAME>;SyncTokenName=<REPLACE_WITH_SYNC_TOKEN_NAME>;SyncTokenPassword=REPLACE_WITH_SYNC_TOKEN_PASSWORD;ParentGatewayEndpoint=<REPLACE_WITH_CLOUD_REGISTRY_NAME>.<REPLACE_WITH_CLOUD_REGISTRY_REGION>.data.azurecr.io;ParentEndpointProtocol=https"
                            }
                        },
                        "status": "running",
                        "restartPolicy": "always",
                        "version": "1.0"
                    },
                    "IoTEdgeAPIProxy": {
                        "settings": {
                            "image": "<REPLACE_WITH_CLOUD_REGISTRY_NAME>.azurecr.io/azureiotedge-api-proxy:1.1.2",
                            "createOptions": "{\"HostConfig\":{\"PortBindings\":{\"8000/tcp\":[{\"HostPort\":\"8000\"}]}}}"
                        },
                        "type": "docker",
                        "env": {
                            "NGINX_DEFAULT_PORT": {
                                "value": "8000"
                            },
                            "CONNECTED_ACR_ROUTE_ADDRESS": {
                                "value": "connected-registry:8080"
                            },
                            "BLOB_UPLOAD_ROUTE_ADDRESS": {
                                "value": "AzureBlobStorageonIoTEdge:11002"
                            }
                        },
                        "status": "running",
                        "restartPolicy": "always",
                        "version": "1.0"
                    }
                },
                "runtime": {
                    "settings": {
                        "minDockerVersion": "v1.25",
                        "registryCredentials": {
                            "cloudregistry": {
                                "address": "<REPLACE_WITH_CLOUD_REGISTRY_NAME>.azurecr.io",
                                "password": "<REPLACE_WITH_SYNC_TOKEN_PASSWORD>",
                                "username": "<REPLACE_WITH_SYNC_TOKEN_NAME>"
                            }
                        }
                    },
                    "type": "docker"
                },
                "schemaVersion": "1.1",
                "systemModules": {
                    "edgeAgent": {
                        "settings": {
                            "image": "<REPLACE_WITH_CLOUD_REGISTRY_NAME>.azurecr.io/azureiotedge-agent:1.2.4",
                            "createOptions": ""
                        },
                        "type": "docker",
                        "env": {
                            "SendRuntimeQualityTelemetry": {
                                "value": "false"
                            }
                        }
                    },
                    "edgeHub": {
                        "settings": {
                            "image": "<REPLACE_WITH_CLOUD_REGISTRY_NAME>.azurecr.io/azureiotedge-hub:1.2.4",
                            "createOptions": "{\"HostConfig\":{\"PortBindings\":{\"443/tcp\":[{\"HostPort\":\"443\"}],\"5671/tcp\":[{\"HostPort\":\"5671\"}],\"8883/tcp\":[{\"HostPort\":\"8883\"}]}}}"
                        },
                        "type": "docker",
                        "status": "running",
                        "restartPolicy": "always"
                    }
                }
            }
        },
        "$edgeHub": {
            "properties.desired": {
                "routes": {
                    "route": "FROM /messages/* INTO $upstream"
                },
                "schemaVersion": "1.1",
                "storeAndForwardConfiguration": {
                    "timeToLiveSecs": 7200
                }
            }
        }
    }
}

在 IoT Edge 上部署连接的注册表和 API 代理模块

通过运行以下命令,使用在上一节中创建的部署清单在 IoT Edge 设备上部署连接的注册表和 API 代理模块。 提供 IoT Edge 顶层设备的 ID 及其所指示的 IoT 中心的名称。

# Set the IOT_EDGE_TOP_LAYER_DEVICE_ID and IOT_HUB_NAME environment variables for use in the following Azure CLI command
IOT_EDGE_TOP_LAYER_DEVICE_ID=<device-id>
IOT_HUB_NAME=<hub-name>

az iot edge set-modules \
  --device-id $IOT_EDGE_TOP_LAYER_DEVICE_ID \
  --hub-name $IOT_HUB_NAME \
  --content manifest.json

有关详细信息,请参阅使用 Azure CLI 部署 Azure IoT Edge 模块

要检查连接的注册表的状态,请使用以下 az acr connected-registry show 命令。 连接的注册表名为 $CONNECTED_REGISTRY_RW 的值。

az acr connected-registry show \
  --registry $REGISTRY_NAME \
  --name $CONNECTED_REGISTRY_RW \
  --output table

成功部署后,连接的注册表的状态显示为 Online

后续步骤

本快速入门介绍了如何将连接的注册表部署到 IoT Edge 设备。 继续学习下一个指南,了解如何从新部署的连接注册表中拉取映像或在嵌套的 IoT Edge 设备上部署连接的注册表。