练习 - Azure 容器实例故障排除

已完成

为帮助你了解对容器实例进行故障排除的基本方法,请执行一些基本操作,例如:

  • 拉取容器日志
  • 查看容器事件
  • 附加到容器实例

从以前部署的容器实例中获取日志

运行以下 az container logs 命令,查看在上一个练习中创建的猫和狗投票应用容器的输出:

az container logs \
  --resource-group learn-deploy-aci-rg \
  --name aci-demo

将获得类似如下的输出:

Checking for script in /app/prestart.sh
Running script /app/prestart.sh
Running inside /app/prestart.sh, you could add migrations to this file, e.g.:

#! /usr/bin/env bash

# Let the DB start
sleep 10;
# Run migrations
alembic upgrade head
…

获取容器事件

az container attach 命令在容器启动过程中提供诊断信息。 容器启动后,它还将标准输出和标准错误流写入本地终端。

运行 az container attach 以附加到容器:

az container attach \
  --resource-group learn-deploy-aci-rg \
  --name aci-demo

将获得类似如下的输出:

Container 'aci-demo' is in state 'Running'...
(count: 1) (last timestamp: 2021-09-21 23:48:14+00:00) pulling image "mcr.microsoft.com/azuredocs/azure-vote-front"
(count: 1) (last timestamp: 2021-09-21 23:49:09+00:00) Successfully pulled image "mcr.microsoft.com/azuredocs/azure-vote-front"
(count: 1) (last timestamp: 2021-09-21 23:49:12+00:00) Created container
(count: 1) (last timestamp: 2021-09-21 23:49:13+00:00) Started container

Start streaming logs:
Checking for script in /app/prestart.sh
Running script /app/prestart.sh
…

提示

要与附加的容器断开,请输入 Ctrl+C

在容器中执行命令

在诊断和排查问题时,可能需要直接在正在运行的容器上运行命令。

  1. 若要查看容器中的命令,请运行以下 az container exec 命令以在容器中启动交互式会话:

    az container exec \
      --resource-group learn-deploy-aci-rg \
      --name aci-demo \
      --exec-command /bin/sh
    

    此时便可在容器内进行有效操作。

  2. 运行 ls 命令以显示工作目录的内容。

    # ls
    __pycache__  config_file.cfg  main.py  prestart.sh  static  templates  uwsgi.ini
    
  3. 如果你愿意,可进一步探索系统。 完成后,请运行 exit 命令以停止交互式会话。

监视容器上的 CPU 和内存使用情况

了解如何监视容器上的 CPU 和内存使用情况。

  1. 运行以下 az container show 命令以获取 Azure 容器实例的 ID,并将 ID 存储在 Bash 变量中:

    CONTAINER_ID=$(az container show \
      --resource-group learn-deploy-aci-rg \
      --name aci-demo \
      --query id \
      --output tsv)
    
  2. 运行 az monitor metrics list 命令以检索 CPU 使用率信息:

    az monitor metrics list \
      --resource $CONTAINER_ID \
      --metrics CPUUsage \
      --output table
    

    请记下 --metrics 参数。 此处将指定“CPUUsage”以检索 CPU 使用率。

    将看到类似于以下输出的文本:

    Timestamp            Name          Average
    -------------------  ------------  -----------
    2021-09-21 23:39:00  CPU Usage
    2021-09-21 23:40:00  CPU Usage
    2021-09-21 23:41:00  CPU Usage
    2021-09-21 23:42:00  CPU Usage
    2021-09-21 23:43:00  CPU Usage      0.375
    2021-09-21 23:44:00  CPU Usage      0.875
    2021-09-21 23:45:00  CPU Usage      1
    2021-09-21 23:46:00  CPU Usage      3.625
    2021-09-21 23:47:00  CPU Usage      1.5
    2021-09-21 23:48:00  CPU Usage      2.75
    2021-09-21 23:49:00  CPU Usage      1.625
    2021-09-21 23:50:00  CPU Usage      0.625
    2021-09-21 23:51:00  CPU Usage      0.5
    2021-09-21 23:52:00  CPU Usage      0.5
    2021-09-21 23:53:00  CPU Usage      0.5
    
  3. 运行 az monitor metrics list 命令以检索内存使用率信息:

    az monitor metrics list \
      --resource $CONTAINER_ID \
      --metrics MemoryUsage \
      --output table
    

    在此将为 --metrics 参数指定“MemoryUsage”以检索内存使用率信息。

    将看到类似于以下输出的文本:

    Timestamp            Name          Average
    -------------------  ------------  -----------
    2021-09-21 23:43:00  Memory Usage
    2021-09-21 23:44:00  Memory Usage  0.0
    2021-09-21 23:45:00  Memory Usage  15917056.0
    2021-09-21 23:46:00  Memory Usage  16744448.0
    2021-09-21 23:47:00  Memory Usage  16842752.0
    2021-09-21 23:48:00  Memory Usage  17190912.0
    2021-09-21 23:49:00  Memory Usage  17506304.0
    2021-09-21 23:50:00  Memory Usage  17702912.0
    2021-09-21 23:51:00  Memory Usage  17965056.0
    2021-09-21 23:52:00  Memory Usage  18509824.0
    2021-09-21 23:53:00  Memory Usage  18649088.0
    2021-09-21 23:54:00  Memory Usage  18845696.0
    2021-09-21 23:55:00  Memory Usage  19181568.0
    

在 Azure 门户中,Azure 容器实例 CPU 和内存使用情况信息如下所示:

Screenshot that shows the Azure portal view of Azure Container Instances CPU and memory usage information.

清理资源

在本模块中,你使用自己的 Azure 订阅创建了资源。 这些资源应清理,这样就不必再为其支付费用。

  1. 在 Azure 主页上,选择“所有资源”。

  2. 找到 learn-deploy-aci-rg 资源组或者你使用的任何资源组名称,然后将其选中。

  3. 在资源组的“概述”选项卡中,选择“删除资源组”。

  4. 将打开“新建”对话框。 再次输入资源组的名称,然后选择“删除”。 这会删除我们在本模块中创建的所有资源。