将文本分析容器部署到 Azure Kubernetes 服务Deploy a Text Analytics container to Azure Kubernetes Service

了解如何将 Azure 认知服务文本分析容器映像部署到 Azure Kubernetes 服务 (AKS)。Learn how to deploy the Azure Cognitive Services Text Analytics container image to Azure Kubernetes Service (AKS). 该过程展示了创建文本分析资源的方法、创建关联的情绪分析映像的方法,以及在浏览器中练习前两项的相关业务流程的方法。This procedure shows how to create a Text Analytics resource, how to create an associated sentiment analysis image, and how to exercise this orchestration of the two from a browser. 使用容器可以将你的关注点从管理基础结构转移到应用程序开发上。Using containers can shift your attention away from managing infrastructure to instead focusing on application development.

先决条件Prerequisites

此过程要求必须在本地安装和运行多个工具。This procedure requires several tools that must be installed and run locally. 需要满足以下条件:You need the following:

  • Azure 订阅。An Azure subscription. 如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a Trial before you begin.
  • 文本编辑器,例如 Visual Studio CodeA text editor, for example, Visual Studio Code.
  • 已安装 Azure CLIThe Azure CLI installed.
  • 已安装 Kubernetes CLIThe Kubernetes CLI installed.
  • 具有适当定价层的 Azure 资源。An Azure resource with the correct pricing tier. 并非所有定价层都适用于此容器:Not all pricing tiers work with this container:
    • 仅具有 F0 或标准定价层的 Azure 文本分析资源。Azure Text Analytics resource with F0 or standard pricing tiers only.
    • 具有 S0 定价层的 Azure 认知服务资源。Azure Cognitive Services resource with the S0 pricing tier.

创建认知服务文本分析资源Create a Cognitive Services Text Analytics resource

  1. 登录 Azure 门户Sign in to the Azure portal.

  2. 选择“创建资源”,然后转到“AI + 机器学习” > “文本分析”。Select Create a resource, and then go to AI + Machine Learning > Text Analytics. 或者,转到 创建文本分析Or, go to Create Text Analytics.

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

    设置Setting Value
    名称Name 输入名称(2-64 个字符)。Enter a name (2-64 characters).
    订阅Subscription 选择适当的订阅。Select the appropriate subscription.
    位置Location 选择一个邻近的位置。Select a nearby location.
    定价层Pricing tier 输入 S(标准定价层)。Enter S, the standard pricing tier.
    资源组Resource group 选择可用的资源组。Select an available resource group.
  4. 选择“创建”,然后等待创建资源。Select Create, and wait for the resource to be created. 浏览器将自动重定向到新创建的资源页。Your browser automatically redirects to the newly created resource page.

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

    门户中的“资源”选项卡Resource tab in portal 设置Setting “值”Value
    概述Overview 端点Endpoint 复制终结点。Copy the endpoint. 其外观类似于 https://my-resource.cognitiveservices.azure.cn/text/analytics/v3.0It appears similar to https://my-resource.cognitiveservices.azure.cn/text/analytics/v3.0.
    “键”Keys API 密钥API Key 复制两个密钥中的一个。Copy one of the two keys. 它是一个 32 个字符的字母数字字符串(不包含空格或短划线):<xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>。It's a 32-character alphanumeric string with no spaces or dashes: <xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>.

创建 Azure Kubernetes 服务群集资源Create an Azure Kubernetes Service cluster resource

  1. 转到 Azure Kubernetes 服务,然后选择“创建”。Go to Azure Kubernetes Service, and select Create.

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

    设置Setting ValueValue
    订阅Subscription 选择相应的订阅。Select an appropriate subscription.
    资源组Resource group 选择可用的资源组。Select an available resource group.
    Kubernetes 群集名称Kubernetes cluster name 输入名称(小写)。Enter a name (lowercase).
    区域Region 选择附近的位置。Select a nearby location.
    Kubernetes 版本Kubernetes version 标记为“(默认值)”的任何值。Whatever value is marked as (default).
    DNS 名称前缀DNS name prefix 自动创建,但可以重写。Created automatically, but you can override.
    节点大小Node size 标准 DS2 v2:Standard DS2 v2:
    2 vCPUs, 7 GB2 vCPUs, 7 GB
    节点计数Node count 将滑块留在默认值的位置。Leave the slider at the default value.
  3. 在“缩放”选项卡上,将“虚拟节点”和“VM 规模集”设置为其默认值。On the Scale tab, leave Virtual nodes and VM scale sets set to their default values.

  4. 在“身份验证”选项卡上,将“服务主体”和“启用 RBAC”设为其默认值。 On the Authentication tab, leave Service principal and Enable RBAC set to their default values.

  5. 在“网络”选项卡上,输入以下选择:On the Networking tab, enter the following selections:

    设置Setting ValueValue
    网络配置Networking configuration 基本Basic
  6. 在“监视”选项卡上,确保将“启用容器监视”设置为“是”,并让“Log Analytics 工作区”保留默认值 。On the Monitoring tab, make sure that Enable container monitoring is set to Yes, and leave Log Analytics workspace as the default value.

  7. 在“标记”选项卡上,暂时让名称/值对保留空白。On the Tags tab, leave the name/value pairs blank for now.

  8. 选择“查看并创建”。Select Review and Create.

  9. 通过验证后,选择“创建”。After validation passes, select Create.

备注

如果验证失败,可能是由于“服务主体”错误。If validation fails, it might be because of a "Service principal" error. 返回到“身份验证”选项卡,然后回到“查看 + 创建”,应该会在其中运行验证,然后验证会通过。 Go back to the Authentication tab and then go back to Review + create, where validation should run and then pass.

将关键短语提取容器部署到 AKS 群集Deploy the Key Phrase Extraction container to an AKS cluster

  1. 打开 Azure CLI,登录到 Azure。Open the Azure CLI, and sign in to Azure.

    az cloud set -n AzureChinaCloud
    az login
    
  2. 登录到 AKS 群集。Sign in to the AKS cluster. your-cluster-nameyour-resource-group 替换为相应的值。Replace your-cluster-name and your-resource-group with the appropriate values.

    az aks get-credentials -n your-cluster-name -g -your-resource-group
    

    此命令在运行后会报告类似以下内容的消息:After this command runs, it reports a message similar to the following:

    Merged "your-cluster-name" as current context in /home/username/.kube/config
    

    警告

    如果在 Azure 帐户上有多个可用订阅,而 az aks get-credentials 命令返回错误,则表明你使用了错误的订阅,这是一个常见问题。If you have multiple subscriptions available to you on your Azure account and the az aks get-credentials command returns with an error, a common problem is that you're using the wrong subscription. 将 Azure CLI 会话的上下文设置为使用你在创建这些资源时使用的订阅,然后重试。Set the context of your Azure CLI session to use the same subscription that you created the resources with and try again.

     az account set -s subscription-id
    
  3. 打开所选的文本编辑器。Open the text editor of choice. 此示例使用 Visual Studio Code。This example uses Visual Studio Code.

    code .
    
  4. 在文本编辑器中创建名为“keyphrase.yaml”的新文件,然后将以下 YAML 粘贴到其中。Within the text editor, create a new file named keyphrase.yaml , and paste the following YAML into it. 请确保将 billing/valueapikey/value 替换为自己的信息。Be sure to replace billing/value and apikey/value with your own information.

    apiVersion: apps/v1beta1
    kind: Deployment
    metadata:
      name: keyphrase
    spec:
      template:
        metadata:
          labels:
            app: keyphrase-app
        spec:
          containers:
          - name: keyphrase
            image: mcr.microsoft.com/azure-cognitive-services/keyphrase
            ports:
            - containerPort: 5000
            resources:
              requests:
                memory: 2Gi
                cpu: 1
              limits:
                memory: 4Gi
                cpu: 1
            env:
            - name: EULA
              value: "accept"
            - name: billing
              value: # {ENDPOINT_URI}
            - name: apikey
              value: # {API_KEY}
    
    --- 
    apiVersion: v1
    kind: Service
    metadata:
      name: keyphrase
    spec:
      type: LoadBalancer
      ports:
      - port: 5000
      selector:
        app: keyphrase-app
    
  5. 保存该文件并关闭文本编辑器。Save the file, and close the text editor.

  6. 运行 Kubernetes apply 命令,将 keyphrase.yaml 文件作为其目标:Run the Kubernetes apply command with the keyphrase.yaml file as its target:

    kubectl apply -f keyphrase.yaml
    

    当命令成功应用部署配置以后,会出现类似于以下输出的消息:After the command successfully applies the deployment configuration, a message appears similar to the following output:

    deployment.apps "keyphrase" created
    service "keyphrase" created
    
  7. 验证 Pod 是否已部署:Verify that the pod was deployed:

    kubectl get pods
    

    Pod 的运行状态输出:The output for the running status of the pod:

    NAME                         READY     STATUS    RESTARTS   AGE
    keyphrase-5c9ccdf575-mf6k5   1/1       Running   0          1m
    
  8. 验证服务是否可用,然后获取 IP 地址。Verify that the service is available, and get the IP address.

    kubectl get services
    

    Pod 中 keyphrase 服务的运行状态输出:The output for the running status of the keyphrase service in the pod:

    NAME         TYPE           CLUSTER-IP    EXTERNAL-IP      PORT(S)          AGE
    kubernetes   ClusterIP      10.0.0.1      <none>           443/TCP          2m
    keyphrase    LoadBalancer   10.0.100.64   168.61.156.180   5000:31234/TCP   2m
    

验证关键短语提取容器实例Verify the Key Phrase Extraction container instance

  1. 选择“概述”选项卡,并复制 IP 地址。Select the Overview tab, and copy the IP address.

  2. 打开新的浏览器选项卡,并输入 IP 地址。Open a new browser tab, and enter the IP address. 例如,输入 http://<IP-address>:5000 (http://55.55.55.55:5000)。For example, enter http://<IP-address>:5000 (http://55.55.55.55:5000). 此时将显示容器的主页,让你知道该容器正在运行。The container's home page is displayed, which lets you know the container is running.

    查看容器主页,以验证它是否处于运行状态

  3. 选择“服务 API 说明”链接,以转到该容器的 Swagger 页。Select the Service API Description link to go to the container's Swagger page.

  4. 选择任意一个 POST API,然后选择“试用”。此时将显示参数,其中包括以下示例输入:Choose any of the POST APIs, and select Try it out . The parameters are displayed, which includes this example input:

    {
      "documents": [
        {
          "id": "1",
          "text": "Hello world"
        },
        {
          "id": "2",
          "text": "Bonjour tout le monde"
        },
        {
          "id": "3",
          "text": "La carretera estaba atascada. Había mucho tráfico el día de ayer."
        },
        {
          "id": "4",
          "text": ":) :( :D"
        }
      ]
    }
    
  5. 将该输入替换为以下 JSON 内容:Replace the input with the following JSON content:

    {
      "documents": [
        {
          "language": "en",
          "id": "7",
          "text": "I was fortunate to attend the KubeCon Conference in Barcelona, it is one of the best conferences I have ever attended. Great people, great sessions and I thoroughly enjoyed it!"
        }
      ]
    }
    
  6. 将“showStats”设置为 trueSet showStats to true.

  7. 选择“执行”以确定文本的情绪。Select Execute to determine the sentiment of the text.

    在容器中打包的模型会生成范围为从 0 到 1 的分数,其中,0 表示消极,而 1 表示积极。The model that's packaged in the container generates a score that ranges from 0 to 1, where 0 is negative and 1 is positive.

    返回的 JSON 响应包括更新的文本输入的情绪:The JSON response that's returned includes sentiment for the updated text input:

    {
      "documents": [
        {
          "id": "7",
          "keyPhrases": [
            "Great people",
            "great sessions",
            "KubeCon Conference",
            "Barcelona",
            "best conferences"
          ],
          "statistics": {
            "charactersCount": 176,
            "transactionsCount": 1
          }
        }
      ],
      "errors": [],
      "statistics": {
        "documentsCount": 1,
        "validDocumentsCount": 1,
        "erroneousDocumentsCount": 0,
        "transactionsCount": 1
      }
    }
    

我们现在可以将响应有效负载的 JSON 数据的文档 id 关联到原始请求有效负载文档 idWe can now correlate the document id of the response payload's JSON data to the original request payload document id. 生成的文档有一个 keyPhrases 数组,其中包含已从相应输入文档中提取的关键短语的列表。The resulting document has a keyPhrases array, which contains the list of key phrases that have been extracted from the corresponding input document. 此外,每个生成的文档都有各种统计信息(例如 characterCounttransactionCount)。Additionally, there are various statistics such as characterCount and transactionCount for each resulting document.