在 Azure 容器实例上部署和运行容器Deploy and run container on Azure Container Instance

使用以下步骤,可以轻松地通过 Azure 容器实例在云中缩放 Azure 认知服务应用程序。With the following steps, scale Azure Cognitive Services applications in the cloud easily with Azure Container Instances. 容器化可帮助你集中精力构建应用程序,而不是管理基础结构。Containerization helps you focus on building your applications instead of managing the infrastructure. 有关使用容器的详细信息,请参阅功能和优势For more information on using containers, see features and benefits.

先决条件Prerequisites

此方案适用于任何认知服务容器。The recipe works with any Cognitive Services container. 使用此方案之前,必须先创建认知服务资源。The Cognitive Service resource must be created before using the recipe. 支持容器的每个认知服务都有一篇“如何安装”文章,其中介绍了如何为容器安装并配置服务。Each Cognitive Service that supports containers has a "How to install" article for installing and configuring the service for a container. 某些服务需要一个文件或一组文件作为容器的输入。在使用此解决方案之前,请确保了解并已成功使用过容器。Some services require a file or set of files as input for the container, it is important that you understand and have used the container successfully before using this solution.

  • 你正在使用的 Azure 认知服务的 Azure 资源。An Azure resource for the Azure Cognitive Service you're using.

  • 认知服务终结点 URL - 查看容器的特定服务的“如何安装”文章,以了解终结点 URL 在 Azure 门户内的位置,以及 URL 的正确示例的样式。Cognitive Service endpoint URL - review your specific service's "How to install" for the container, to find where the endpoint URL is from within the Azure portal, and what a correct example of the URL looks like. 确切的格式可能因服务而异。The exact format can change from service to service.

  • 认知服务密钥 - 密钥位于 Azure 资源的“密钥”页面上。Cognitive Service key - the keys are on the Keys page for the Azure resource. 您只需要两个密钥中的一个即可。You only need one of the two keys. 密钥是包含 32 个字母数字字符的字符串。The key is a string of 32 alpha-numeric characters.

  • 本地主机(你的计算机)上的单个认知服务容器。A single Cognitive Services Container on your local host (your computer). 确保你可以:Make sure you can:

    • 使用 docker pull 命令拉取映像。Pull down the image with a docker pull command.
    • 使用 docker run 命令通过所有必需的配置设置成功运行本地容器。Run the local container successfully with all required configuration settings with a docker run command.
    • 调用容器的终结点,获取 HTTP 2xx 响应和 JSON 响应。Call the container's endpoint, getting a response of HTTP 2xx and a JSON response back.

尖括号 <> 中的所有变量都需要替换为你自己的值。All variables in angle brackets, <>, need to be replaced with your own values. 此替换包括尖括号。This replacement includes the angle brackets.

重要

LUIS 容器需要在运行时中拉取的 .gz 模型文件。The LUIS container requires a .gz model file that is pulled in at runtime. 容器必须能够通过容器实例中的卷装载来访问此模型文件。The container must be able to access this model file via a volume mount from the container instance. 若要上传模型文件,请执行以下步骤:To upload a model file, follow these steps:

  1. 创建 Azure 文件共享Create an Azure file share. 记下 Azure 存储帐户名称、密钥和文件共享名称,因为稍后需要用到它们。Take note of the Azure Storage account name, key, and file share name as you'll need them later.
  2. 从 LUIS 门户中导出你的 LUIS 模型(打包的应用)export your LUIS model (packaged app) from the LUIS portal.
  3. 在 Azure 门户中,导航到你的存储帐户资源的“概述”页,然后选择“文件共享”。In the Azure portal, navigate to the Overview page of your storage account resource, and select File shares.
  4. 选择你最近创建的文件共享名称,然后选择“上传”。Select the file share name that you recently created, then select Upload. 接着上传打包的应用。Then upload your packaged app.

通过 Azure 门户创建 Azure 容器实例资源Create an Azure Container Instance resource using the Azure portal

  1. 转到容器实例的创建页。Go to the Create page for Container Instances.

  2. 在“基本信息”选项卡中输入以下详细信息:On the Basics tab, enter the following details:

    设置Setting Value
    订阅Subscription 选择订阅。Select your subscription.
    资源组Resource group 选择可用的资源组,或者创建一个新的,例如 cognitive-servicesSelect the available resource group or create a new one such as cognitive-services.
    容器名称Container name 输入一个名称,例如 cognitive-container-instanceEnter a name such as cognitive-container-instance. 此名称必须小写。The name must be in lower caps.
    位置Location 选择要部署的区域。Select a region for deployment.
    映像类型Image type 如果容器映像存储在不需凭据的容器注册表中,请选择 PublicIf your container image is stored in a container registry that doesn’t require credentials, choose Public. 如果访问容器映像时需要凭据,请选择 PrivateIf accessing your container image requires credentials, choose Private. 请参阅容器存储库和映像,详细了解容器映像是 Public 还是 Private(“公共预览版”)。Refer to container repositories and images for details on whether or not the container image is Public or Private ("Public Preview").
    映像名称Image name 输入认知服务容器位置。Enter the Cognitive Services container location. 位置是用作 docker pull 命令参数的项。The location is what's used as an argument to the docker pull command. 请参阅容器存储库和映像,获取可用的映像名称及其相应的存储库。Refer to the container repositories and images for the available image names and their corresponding repository.

    映像名称必须完全限定,并指定三个部分。The image name must be fully qualified specifying three parts. 首先是容器注册表,接着是存储库,最后是映像名称:<container-registry>/<repository>/<image-name>First, the container registry, then the repository, finally the image name: <container-registry>/<repository>/<image-name>.

    这是一个示例:mcr.microsoft.com/azure-cognitive-services/keyphrase 表示 Azure 认知服务存储库下 Microsoft 容器注册表中的“关键短语提取”映像。Here is an example, mcr.microsoft.com/azure-cognitive-services/keyphrase would represent the Key Phrase Extraction image in the Microsoft Container Registry under the Azure Cognitive Services repository. 另一示例:containerpreview.azurecr.io/microsoft/cognitive-services-speech-to-text 表示“容器预览版”容器注册表的 Microsoft 存储库中的“语音转文本”映像。Another example is, containerpreview.azurecr.io/microsoft/cognitive-services-speech-to-text which would represent the Speech to Text image in the Microsoft repository of the Container Preview container registry.
    OS 类型OS type Linux
    大小Size 对于特定的认知服务容器,请将大小更改为建议的设置:Change size to the suggested recommendations for your specific Cognitive Service container:
    2 个CPU 核心2 CPU cores
    4 GB4 GB
  3. 在“网络”选项卡上,输入以下详细信息:On the Networking tab, enter the following details:

    设置Setting “值”Value
    端口Ports 将 TCP 端口设置为 5000Set the TCP port to 5000. 在端口 5000 上公开此容器。Exposes the container on port 5000.
  4. 在“高级”选项卡上,输入所需的 环境变量 作为 Azure 容器实例资源的容器计费设置:On the Advanced tab, enter the required Environment Variables for the container billing settings of the Azure Container Instance resource:

    Key Value
    ApiKey 从资源的“键和终结点”页复制。Copied from the Keys and endpoint page of the resource. 它是一个由 32 个字母数字组成的字符串(不包含空格或短划线),即 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxIt is a 32 alphanumeric-character string with no spaces or dashes, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
    Billing 从资源的“键和终结点”页复制的终结点 URL。Your endpoint URL copied from the Keys and endpoint page of the resource.
    Eula accept
  5. 单击“查看并创建”Click Review and Create

  6. 验证通过后,单击“创建”,完成创建过程After validation passes, click Create to finish the creation process

  7. 成功部署资源以后,即可使用它When the resource is successfully deployed, it's ready

使用容器实例Use the Container Instance

  1. 选择“概述”并复制 IP 地址。Select the Overview and copy the IP address. 它将是数字 IP 地址,例如 55.55.55.55It will be a numeric IP address such as 55.55.55.55.

  2. 打开新的浏览器选项卡,并使用 IP 地址(例如 http://<IP-address>:5000 (http://55.55.55.55:5000)。Open a new browser tab and use the IP address, for example, http://<IP-address>:5000 (http://55.55.55.55:5000). 此时会显示容器的主页,告知你该容器处于运行状态。You will see the container's home page, letting you know the container is running.

    容器的主页

  3. 选择“服务 API 说明”以查看容器的 Swagger 页面。Select Service API Description to view the swagger page for the container.

  4. 选择任意一个 POST API,然后选择“试用”。此时会显示参数,其中包括输入。Select any of the POST APIs and select Try it out. The parameters are displayed including the input. 填写这些参数。Fill in the parameters.

  5. 选择“执行”以将请求发送到你的容器实例。Select Execute to send the request to your Container Instance.

    你已成功地在 Azure 容器实例中创建并使用了认知服务容器。You have successfully created and used Cognitive Services containers in Azure Container Instance.