将异常检测器容器部署到 Azure 容器实例Deploy an Anomaly Detector container to Azure Container Instances

了解如何将认知服务异常检测器容器部署到 Azure 容器实例Learn how to deploy the Cognitive Services Anomaly Detector container to Azure Container Instances. 此过程演示如何创建异常探测器资源。This procedure demonstrates the creation of an Anomaly Detector resource. 然后讨论如何拉取关联的容器映像。Then we discuss pulling the associated container image. 最后,我们重点介绍了从浏览器中执行这两个业务流程的能力。Finally, we highlight the ability to exercise the orchestration of the two from a browser. 使用容器可以将开发人员的注意力从管理基础结构转移到应用程序开发上。Using containers can shift the developers' attention away from managing infrastructure to instead focusing on application development.

先决条件Prerequisites

  • 使用 Azure 订阅。Use an Azure subscription. 如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.
  • 安装 Azure CLI (az)Install the Azure CLI (az).
  • Docker 引擎并验证 Docker CLI 是否可在控制台窗口中工作。Docker engine and validate that the Docker CLI works in a console window.

创建异常检测器资源Create an Anomaly Detector resource

  1. 登录到 Azure 门户Sign into the Azure portal.

  2. 选择创建异常检测器 资源。Select Create Anomaly Detector resource.

  3. 输入所有必需的设置:Enter all required settings:

    设置Setting ValueValue
    名称Name 所需名称(2-64 个字符)Desired name (2-64 characters)
    订阅Subscription 选择相应的订阅Select appropriate subscription
    位置Location 选择附近任何可用的位置Select any nearby and available location
    定价层Pricing Tier F0 - 每秒 10 次调用,每月 20,000 个事务。F0 - 10 Calls per second, 20K Transactions per month.
    或者:Or:
    S0 - 每秒 80 次调用S0 - 80 Calls per second
    资源组Resource Group 选择可用的资源组Select an available resource group
  4. 单击“创建”并等待创建资源。Click Create and wait for the resource to be created. 创建资源后,导航到“资源”页After it is created, navigate to the resource page

  5. 收集配置的 endpoint 和 API 密钥:Collect configured endpoint and an API key:

    门户中的“密钥和终结点”选项卡Keys and Endpoint tab in the portal 设置Setting ValueValue
    概述Overview 终结点Endpoint 复制终结点。Copy the endpoint. 它看起来类似于 https://<your-resource-name>.cognitiveservices.azure.cn/It looks similar to https://<your-resource-name>.cognitiveservices.azure.cn/
    “键”Keys API 密钥API Key 复制两个密钥中的 1 个。Copy 1 of the two keys. 它是一个由 32 个字母数字组成的字符串(不包含空格或短划线),即 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxIt is a 32 alphanumeric-character string with no spaces or dashes, xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.

在 Azure CLI 中创建 Azure 容器实例资源Create an Azure Container Instance resource from the Azure CLI

下面的 YAML 定义Azure 容器实例资源。The YAML below defines the Azure Container Instance resource. 将内容复制并粘贴到名为 my-aci.yaml 的新文件中,并将注释的值替换为自己的值。Copy and paste the contents into a new file, named my-aci.yaml and replace the commented values with your own. 请参阅模板格式以获取有效的 YAML。Refer to the template format for valid YAML. 请参阅容器存储库和映像,获取可用的映像名称及其相应的存储库。Refer to the container repositories and images for the available image names and their corresponding repository. 有关容器实例的 YAML 引用的详细信息,请参阅 YAML 参考:Azure 容器实例For more information of the YAML reference for Container instances, see YAML reference: Azure Container Instances.

apiVersion: 2018-10-01
location: # < Valid location >
name: # < Container Group name >
properties:
  imageRegistryCredentials: # This is only required if you are pulling a non-public image that requires authentication to access. For example Text Analytics for health.
  - server: containerpreview.azurecr.io
    username: # < The username for the preview container registry >
    password: # < The password for the preview container registry >
  containers:
  - name: # < Container name >
    properties:
      image: # < Repository/Image name >
      environmentVariables: # These env vars are required
        - name: eula
          value: accept
        - name: billing
          value: # < Service specific Endpoint URL >
        - name: apikey
          value: # < Service specific API key >
      resources:
        requests:
          cpu: 4 # Always refer to recommended minimal resources
          memoryInGb: 8 # Always refer to recommended minimal resources
      ports:
        - port: 5000
  osType: Linux
  volumes: # This node, is only required for container instances that pull their model in at runtime, such as LUIS.
  - name: aci-file-share
    azureFile:
      shareName: # < File share name >
      storageAccountName: # < Storage account name>
      storageAccountKey: # < Storage account key >
  restartPolicy: OnFailure
  ipAddress:
    type: Public
    ports:
    - protocol: tcp
      port: 5000
tags: null
type: Microsoft.ContainerInstance/containerGroups

备注

并非所有位置都具有相同的 CPU 和内存可用性。Not all locations have the same CPU and Memory availability. 请参阅位置和资源表,以获取每个位置和 OS 的容器的可用资源列表。Refer to the location and resources table for the listing of available resources for containers per location and OS.

我们将依赖于我们为 az container create 命令创建的 YAML 文件。We'll rely on the YAML file we created for the az container create command. 在 Azure CLI 中执行 az container create 命令,将 <resource-group> 替换为你自己的值。From the Azure CLI, execute the az container create command replacing the <resource-group> with your own. 另外,若要在 YAML 部署中保护值,请参考安全值Additionally, for securing values within a YAML deployment refer to secure values.

az container create -g <resource-group> -f my-aci.yaml

如果命令有效,则命令的输出为 Running...。过一段时间后,输出更改为表示新建 ACI 资源的 JSON 字符串。The output of the command is Running... if valid, after sometime the output changes to a JSON string representing the newly created ACI resource. 容器映像很可能会在一段时间内不可用,但现在已部署资源。The container image is more than likely not be available for a while, but the resource is now deployed.

提示

请密切注意公共预览版 Azure 认知服务产品的位置,因为需要根据位置来相应地调整 YAML。Pay close attention to the locations of public preview Azure Cognitive Service offerings, as the YAML will needed to be adjusted accordingly to match the location.

验证容器是否正在运行Validate that a container is running

有几种方法可用于验证容器是否正在运行。There are several ways to validate that the container is running. 找到相关容器的外部 IP 地址和公开端口,并打开你常用的 Web 浏览器。Locate the External IP address and exposed port of the container in question, and open your favorite web browser. 使用以下各种请求 URL 验证容器是否正在运行。Use the various request URLs below to validate the container is running. 下面列出的示例请求 URL 是 http://localhost:5000,但是你的特定容器可能会有所不同。The example request URLs listed below are http://localhost:5000, but your specific container may vary. 请记住,你要依赖于容器的外部 IP 地址和公开端口。Keep in mind that you're to rely on your container's External IP address and exposed port.

请求 URLRequest URL 目的Purpose
http://localhost:5000/ 容器提供主页。The container provides a home page.
http://localhost:5000/status 使用 HTTP GET 进行请求,以便在不会导致终结点查询的情况下验证容器是否正在运行。Requested with an HTTP GET, to validate that the container is running without causing an endpoint query. 此请求可用于 Kubernetes 运行情况和就绪情况探测This request can be used for Kubernetes liveness and readiness probes.
http://localhost:5000/swagger 容器针对终结点及试用功能提供了一整套文档。The container provides a full set of documentation for the endpoints and a Try it out feature. 使用此功能可以将设置输入到基于 Web 的 HTML 表单并进行查询,而无需编写任何代码。With this feature, you can enter your settings into a web-based HTML form and make the query without having to write any code. 查询返回后,将提供示例 CURL 命令,用于演示所需的 HTTP 标头和正文格式。After the query returns, an example CURL command is provided to demonstrate the HTTP headers and body format that's required.

容器的主页

后续步骤Next steps