我想取得 AWS Fargate 任務的 AWS Systems Manager Agent (SSM Agent) 日誌,該日誌已啟動 Amazon Elastic Container Service (Amazon ECS) Exec。
簡短說明
先決條件:
完成使用 ECS Exec 的要求。
若要針對已啟動 Amazon ECS Exec 的 Fargate 任務取得 SSM Agent 日誌,請建立 Amazon Elastic File System (Amazon EFS) 檔案系統。然後,在 Fargate 容器上掛載 Amazon EFS 檔案系統。最後,在 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體上掛載相同的檔案系統,以取得 SSM Agent 日誌。
**重要事項:**在掛載 EFS 檔案系統之前,您必須啟動 Amazon EC2 執行個體。您的 Amazon EFS 檔案系統、Amazon ECS 叢集,以及 Fargate 任務必須位於相同的 Amazon Virtual Private Cloud (Amazon VPC) 中。
解決方法
**注意:**下列解決方案僅適用於已啟動 ECS Exec 的 Fargate 任務。僅使用解決方法步驟來除錯。若要確保您不會覆寫日誌,請以獨立任務啟動任務。或者,在 Amazon ECS 服務中,將任務的 desiredCount 保持為 "1"。您也可以使用以下解決方法來處理必須從容器檢查非 stderr/stdout 日誌的情況。
建立您的 Amazon EFS 檔案系統,並將其掛載在 Fargate 容器上
若要建立 Amazon EFS 檔案系統,並將其掛載到任務或服務中的 Fargate 容器上,請完成下列步驟:
- 建立您的 Amazon EFS 檔案系統。
- 請注意 Amazon EFS ID 和安全群組 ID。
- 編輯檔案系統安全群組規則,以允許連接埠 2049 上從與 Fargate 任務相關聯的安全群組所傳入的連線。
- 更新您的 Amazon ECS 安全群組,以允許連接埠 2049 上傳出至檔案系統安全群組的連線。
- 開啟 Amazon ECS 主控台。
- 在導覽窗格中,選擇任務定義,然後選擇您的任務定義。
- 選擇建立新版本。
- 在儲存下,對於磁碟區,選擇新增磁碟區。然後,輸入下列資訊
對於磁碟區名稱,輸入您的磁碟區名稱。
對於磁碟區類型,輸入選擇 EFS。
對於檔案系統 ID 中,輸入檔案系統的 ID。
- 對於容器掛載點,選擇新增掛載點。然後,輸入下列資訊:
對於容器,選擇您的容器。
對於來源磁碟區,選擇來源磁碟區。
對於容器路徑,輸入 /var/log/amazon。
- 選擇建立。
注意:在將 Amazon EFS 檔案系統掛載至 Amazon EC2 執行個體之前,任務必須處於執行中狀態。
在 Amazon EC2 執行個體上掛載 Amazon EFS 檔案系統,並取得 SSM Agent 日誌
**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤,請參閱疑難排解 AWS CLI 錯誤。此外,請確定您使用的是最新的 AWS CLI 版本。
請完成下列步驟:
-
在 EC2 執行個體上掛載檔案系統。
-
若要取得日誌資料,請執行下列 AWS CLI 命令:
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]#