如何取得已啟動 Amazon ECS Exec 中 Fargate 任務的 SSM 代理程式日誌?

2 分的閱讀內容
0

我想要為 AWS Fargate 任務取得 AWS Systems Manager Agent (SSM Agent) 日誌,這些任務已啟動 Amazon Elastic Container Service (Amazon ECS) Exec。但是,我不知道怎麼做。

簡短描述

開始使用 Amazon ECS Exec 之前,請參閱使用 ECS Exec 的先決條件

若要取得已啟動 ECS Exec 中 Fargate 任務的 SSM 代理程式日誌,請建立 Amazon Elastic File System (Amazon EFS) 檔案系統。然後,在 Fargate 容器上掛載 Amazon EFS 檔案系統。最後,在 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. Volumes (磁碟區) 區段中,選擇 Add volume (新增磁碟區)。
  7. Name (名稱) 中,輸入您的磁碟區名稱。
  8. Volume type (磁碟區類型) 中,輸入「EFS」。
  9. File system ID (檔案系統 ID) 中,輸入檔案系統的 ID。
  10. Containers definition (容器定義) 區段中,導覽至 STORAGE AND LOGGING (儲存和記錄) 區段,然後選取您為來源磁碟區建立的磁碟區。
  11. Container path (容器路徑),選擇 /var/log/amazon
  12. 使用您建立的任務定義來更新 Fargate 服務或任務。

在 Amazon EC2 執行個體上掛載 Amazon EFS 檔案系統,並取得 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 年前