Amazon ECS Exec がアクティブ化されている Fargate タスクの SSM エージェントログを取得する方法を教えてください。

所要時間2分
0

Amazon Elastic Container Service (Amazon ECS) Exec がアクティブ化されている AWS Fargate タスクの AWS Systems Manager Agent (SSM Agent) ログを取得したいと考えています。しかし、どうすればいいのかわかりません。

簡単な説明

Amazon ECS Exec を使い始める前に、「ECS Exec を使用するための前提条件」を参照してください。

ECS Exec が有効化されている Fargate タスクの SSM エージェントログを取得するには、Amazon Elastic File System (Amazon EFS) を作成します。次に、Amazon EFS ファイルシステムを Fargate コンテナにマウントします。最後に、同じファイルシステムを Amazon Elastic Compute Cloud (Amazon EC2) インスタンスにマウントして SSM エージェントログを取得します。

重要: Amazon EFS ファイルシステム、Amazon ECS クラスター、および Fargate タスクはすべて同じ Amazon Virtual Private Cloud (Amazon VPC) 内にある必要があります。

**注:**次の解決策は、ECS Exec がアクティブになっている Fargate タスクのみを対象としています。この解決手順は、デバッグのみに使用してください。スタンドアロンタスクとして起動するか、ログが上書きされないように Amazon ECS サービスの desiredCount タスク数を「1」にしておきます。コンテナからの stderr/stdout 以外のログを確認する必要があるシナリオでは、次の解決策を使用することもできます。

解決方法

Amazon EFS ファイルシステムを作成し、タスクまたはサービス内の Fargate コンテナにマウントします。

  1. Amazon EFS ファイルシステム を作成します
  2. Amazon EFS ID とセキュリティグループ ID に注意します。
  3. Fargate タスクに関連付けられているセキュリティグループのポート 2049 でのインバウンド接続を許可するように、ファイルシステムのセキュリティグループルールを編集します。
  4. Amazon ECS セキュリティグループを更新して、ポート 2049 でファイルシステムのセキュリティグループへのアウトバウンド接続を許可します。
  5. Amazon ECS コンソールを開き、Amazon ECS タスク定義に移動します。
  6. [ボリューム] セクションで、[ボリュームの追加] を選択します。
  7. [名前] にはポリシーの名前を入力します。
  8. [ボリュームタイプ] に「EFS」と入力します。
  9. [ファイルシステム ID] に、ファイルシステムの ID を入力します。
  10. [コンテナ定義] セクションで、[ストレージとログ] セクションに移動し、ソースボリューム用に作成したボリュームを選択します。
  11. [コンテナパス] で、/var/log/amazon を選択します。
  12. 作成したタスク定義を使用して Fargate サービスまたはタスクを更新します。

Amazon EFS ファイルシステムを Amazon EC2 インスタンスにマウントし、SSM エージェントログを取得します。

1.    ファイルシステムを EC2 インスタンスにマウントします

2.    以下のコマンドを実行してログデータを取得します。

sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-01b0bxxxxxxxx.efs.ap-southeast-1.amazonaws.com:/ /efs

出力例:

# df -h
Filesystem                                          Size  Used Avail Use% Mounted on
fs-01b0bxxxxxxxx.efs.us-west-2.amazonaws.com:/      8.0E     0  8.0E   0% /efs

以下は、Fargate コンテナの /var/log/amazon/ssm/amazon-ssm-agent.log パスに保存されているログの例です。

[root@ip-172-31-32-32 efs]# cd ssm/
[root@ip-172-31-32-32 ssm]# ls
amazon-ssm-agent.log  audits
[root@ip-172-31-32-32 ssm]# cat amazon-ssm-agent.log | tail -n 10
2022-10-20 11:50:34 INFO [ssm-agent-worker] [MessageService] [MessageHandler] ended idempotency deletion thread
2022-10-20 11:50:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread started
2022-10-20 11:50:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread done
2022-10-20 11:55:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread started
2022-10-20 11:55:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread done
2022-10-20 12:00:34 INFO [ssm-agent-worker] [MessageService] [MessageHandler] started idempotency deletion thread
2022-10-20 12:00:34 WARN [ssm-agent-worker] [MessageService] [MessageHandler] [Idempotency] encountered error open /var/lib/amazon/ssm/170b15cacf5846ed836bcd7903cbee48-2531612879/idempotency: no such file or directory while listing replies in /var/lib/amazon/ssm/170b15cacf5846ed836bcd7903cbee48-2531612879/idempotency
2022-10-20 12:00:34 INFO [ssm-agent-worker] [MessageService] [MessageHandler] ended idempotency deletion thread
2022-10-20 12:00:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread started
2022-10-20 12:00:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread done
[root@ip-172-31-32-32 ssm]#

AWS公式
AWS公式更新しました 1年前
コメントはありません

関連するコンテンツ