LoginSignup
30
25

More than 5 years have passed since last update.

Amazon ECS デバッグ方法

Posted at

ログ等のデバッグ情報 種類

ECSにおいて以下の種類のログ等を活用したデバッグ方法を説明します。

  • アプリケーションログ
  • Amazon ECS サービス ログ
  • Amazon ECS タスク ログ
  • Amazon ECS コンテナエージェントログ
  • Amazon ECS ecs-init ログ
  • タスク認証情報の監査ログ用の IAM ロール
  • ECS Logs Collectorの使用
  • エージェントのイントロスペクション診断
  • API failures エラーメッセージ

アプリケーションログ

ECS上のDockerで起動しているアプリケーションのログ出力の設定をします。

サイドメニューの[タスク定義]から新しいリビジョンのタスクを定義する際に[コンテナの定義]をします。このときに[ストレージとログ]項目中のログを以下のように設定します。

  • ログ設定
    • ログドライバー
      • awslogs
    • ログオプション
      • awslogs-group
        • sample-ecs-loggroup
      • awslogs-region
        • us-east-1
      • awslogs-stream-prefix
        • sample-ecs

以上で例えば、Node.jsを使用するときはconsole.log()などで出力される内容を
CloudWatchのサイドメニューの[ログ]から先程指定したawslogs-groupの項目にログ内容が出力されます。

Amazon ECS サービス ログ

クラスター > <対象のクラスター>で[サービス]タブ中のサービス一覧から対象のサービスを選択します。
遷移先のページの[イベント]タブ中のページで各イベントのイベント時間、メッセージなどが出力されるので参考にする。

Amazon ECS タスク ログ

クラスター > <対象のクラスター>で[タスク]タブの[必要なタスクのステータス]から[Stopped]を選択します。

すると、タスクが停止した場合などの原因などが表示される。

Amazon ECS コンテナエージェントログ、Amazon ECS ecs-init ログ、タスク認証情報の監査ログ用のIAMロール

EC2インスタンスにSSHでログインします。

$ ssh -i ~/.ssh/id_rsa ec2-user@XX.XX.XX.XX
$ sudo yum install vim
$ vim /etc/sysconfig/docker

以下のようにOPTIONSの設定に-D"--default-ulimit nofile=1024:4096"の先頭に追加することでログ出力を詳細に出力します。

Before

# The max number of open files for the daemon itself, and all
# running containers.  The default value of 1048576 mirrors the value
# used by the systemd service unit.
DAEMON_MAXFILES=1048576

# Additional startup options for the Docker daemon, for example:
# OPTIONS="--ip-forward=true --iptables=true"
# By default we limit the number of open files per container
OPTIONS="--default-ulimit nofile=1024:4096"

# How many seconds the sysvinit script waits for the pidfile to appear
# when starting the daemon.
DAEMON_PIDFILE_TIMEOUT=10

After

# The max number of open files for the daemon itself, and all
# running containers.  The default value of 1048576 mirrors the value
# used by the systemd service unit.
DAEMON_MAXFILES=1048576

# Additional startup options for the Docker daemon, for example:
# OPTIONS="--ip-forward=true --iptables=true"
# By default we limit the number of open files per container
OPTIONS="-D --default-ulimit nofile=1024:4096"

# How many seconds the sysvinit script waits for the pidfile to appear
# when starting the daemon.
DAEMON_PIDFILE_TIMEOUT=10
$ sudo service docker restart

ECSのログは/var/log/ecs/以下に吐き出される。

ログの種類

  • Amazon ECS コンテナエージェントログ /var/log/ecs/ecs-agent.log.timestamp.YYYY-MM-DD-HH
  • Amazon ECS ecs-init ログ /var/log/ecs/ecs-init.log.timestamp.YYYY-MM-DD-HH
  • タスク認証情報の監査ログ用の IAM ロール /var/log/ecs/audit.log.YYYY-MM-DD-HH

/var/log/ecs/ecs-agent.log.2017-03-26-17

2017-03-26T17:00:56Z [INFO] Starting Agent: Amazon ECS Agent - v1.14.1 (467c3d7)
2017-03-26T17:00:56Z [INFO] Loading configuration
2017-03-26T17:00:56Z [INFO] Checkpointing is enabled. Attempting to load state
2017-03-26T17:00:56Z [INFO] Loading state! module="statemanager"
2017-03-26T17:00:56Z [INFO] Restored cluster 'sample-cluster'
2017-03-26T17:00:56Z [INFO] Event stream ContainerChange start listening...
2017-03-26T17:00:56Z [INFO] Detected Docker versions [1.17 1.18 1.19 1.20 1.21 1.22 1.23]
2017-03-26T17:00:56Z [INFO] Restored from checkpoint file. I am running as 'arn:aws:ecs:us-east-1:xxxxxxxxxxxxxxxxxx:container-instance/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' in cluster 'sample-cluster'
2017-03-26T17:00:56Z [INFO] Registered! module="api client"
2017-03-26T17:00:56Z [INFO] Adding image name- xxxxxxxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/sample-repository to Image state- sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2017-03-26T17:00:56Z [INFO] Updating container reference sample-container in Image State - sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2017-03-26T17:00:56Z [INFO] Saving state! module="statemanager"

/var/log/ecs/ecs-init.log.2017-03-26-17

2017-03-26T17:00:55Z [INFO] pre-start
2017-03-26T17:00:55Z [INFO] start
2017-03-26T17:00:55Z [INFO] Container name: /ecs-sample-task-1-sample-container-xxxxxxxxxxxxxxxxxxxxxxx
2017-03-26T17:00:55Z [INFO] Container name: /ecs-agent
2017-03-26T17:00:55Z [INFO] Removing existing agent container ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2017-03-26T17:00:55Z [INFO] Starting Amazon EC2 Container Service Agent

(参照) エージェントの再起動方法

$ sudo stop ecs
$ sudo start ecs

ECS Logs Collectorの使用

ECS Logs Collectorを使用することで、オペレーティングシステムログやDocker、ECS コンテナエージェントログを一括して集約することができます。
https://github.com/awslabs/ecs-logs-collector

$ curl -O https://raw.githubusercontent.com/awslabs/ecs-logs-collector/master/ecs-logs-collector.sh
$ sudo bash ./ecs-logs-collector.sh

ログ情報はcollectディレクトリ以下に集められます。例えば、以下のような構成されます。

collect
└── system
    ├── docker
    │   ├── container-310f75292484.txt
    │   ├── container-537de59ea686.txt
    │   ├── docker-images.txt
    │   ├── docker-info.txt
    │   ├── docker-ps.txt
    │   └── docker-version.txt
    ├── docker_log
    │   └── docker
    ├── ecs-agent
    │   ├── agent-running-info.txt
    │   ├── ecs-agent.log.2017-03-26-17
    │   ├── ecs.config
    │   └── ecs_agent_data.txt
    ├── iptables.txt
    ├── lvs.txt
    ├── mounts.txt
    ├── netstat.txt
    ├── pkglist.txt
    ├── ps.txt
    ├── pvs.txt
    ├── selinux.txt
    ├── services.txt
    ├── top.txt
    ├── var_log
    │   ├── dmesg
    │   └── messages
    └── vgs.txt

エージェントのイントロスペクション診断

ECSエージェントのイントロスペクション APIで診断情報を取得し、Docker IDの特定などを行います。

$ curl http://localhost:51678/v1/tasks | python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3731    0  3731    0     0   429k      0 --:--:-- --:--:-- --:--:--  455k
{
    "Tasks": [
        {
            "Arn": "arn:aws:ecs:us-east-1:xxxxxxxxxxxxx:task/xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            "Containers": [
                {
                    "DockerId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                    "DockerName": "ecs-sample-task-1-sample-container-xxxxxxxxxxxxxxxxxxxxx",
                    "Name": "sample-container"
                }
            ],
            "DesiredStatus": "STOPPED",
            "Family": "sample-task",
            "KnownStatus": "STOPPED",
            "Version": "1"
       }
    ]
}

先程調べたDocker IDを使用して、

ログ

$ docker logs <Docker ID> | head

コンテナ検査

$ docker inspect <Docker ID>

API failures エラーメッセージ

マネジメントコンソール、およびCLIからコマンドを叩いたときに出力されるエラー内容を参照しましょう。
各表示項目におけるエラー詳細は以下のURLを参照。
http://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/troubleshooting.html#api_failures_messages

参考

30
25
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
25