如何對 Amazon ECS 中卡在 DELETE_IN _PROGRESS 狀態的任務定義問題進行疑難排解?

2 分的閱讀內容
0

我刪除了 Amazon Elastic Container Service (Amazon ECS) 任務定義,但它卡在 DELETE_IN_PROGRESS 狀態。

解決方法

注意: 如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤訊息,則請參閱對 AWS CLI 進行錯誤疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本

當 Amazon ECS 資源取決於任務定義修訂版本時,將無法完成任務定義刪除請求。如果獨立任務或服務使用了您已刪除的任務定義,則該任務定義將卡在 DELETE_IN_PROGRESS 狀態。

若要解決此問題,請執行下列 list-clusters AWS CLI 命令來找出所有使用該任務定義的資源:

TaskDefARN=TaskDefinitionARN
REGION=Regioncode
for cluster in $(aws ecs list-clusters --region $REGION --output text --query 'clusterArns[]'); do echo "Checking cluster: $cluster"; for task in $(aws ecs list-tasks --region $REGION --cluster $cluster --output text --query 'taskArns[]'); do task_def=$(aws ecs describe-tasks --region $REGION --cluster $cluster --tasks $task --query 'tasks[].taskDefinitionArn' --output text); if [[ "$task_def" == "$TaskDefARN" ]]; then echo "Task using this definition: $task"; fi; done; for service in $(aws ecs list-services --region $REGION --cluster $cluster --output text --query 'serviceArns[]'); do service_def=$(aws ecs describe-services --region $REGION --cluster $cluster --services $service --query 'services[].taskDefinition' --output text); if [[ "$service_def" == "$TaskDefARN" ]]; then echo "Service using this definition: $service"; fi; done; done

注意:TaskDefinitionARN 替換為任務定義的 ARN,並將 Regioncode 替換為您的 AWS 區域。

輸出範例:

Checking cluster: arn:aws:ecs:us-east-1:123456789012:cluster/cluster-1
Checking cluster: arn:aws:ecs:us-east-1:123456789012:cluster/cluster-2
Checking cluster: arn:aws:ecs:us-east-1:123456789012:cluster/cluster-3
Task using this definition: arn:aws:ecs:us-east-1:123456789012:task/cluster-3/abcde
Service using this definition: arn:aws:ecs:us-east-1:123456789012:service/cluster-3/service-1

在上面的範例中,abcde 任務屬於 service-1。若要刪除 abcde 任務的任務定義,您必須先刪除 service-1

如果您的服務正在主動維護任務,那麼當您嘗試刪除該服務時,可能會收到以下錯誤訊息:

「呼叫 DeleteService 作業時發生的錯誤(InvalidParameterException): 當服務的規模超過 0 時,無法停止服務。」

若要解決此問題,請在刪除服務之前確保服務沒有正在執行的任務且任務數為 0。或者,在執行 delete-service 命令時新增 --force 選項。

使用 Amazon ECS 主控台來刪除服務。或者,執行以下 delete-service 命令:

aws ecs delete-service --cluster clustername --service servicename

注意:clustername 替換為您的叢集名稱,將 servicename 替換為您的服務名稱。

對於 Amazon ECS 任務,停止任務後最多可能需要 1 小時才能刪除任務定義。對於 Amazon ECS 服務,刪除部署或任務集後,最多可能需要 24 小時才能刪除任務定義。

只有在完整刪除後,您才可以建立與先前任務定義同名的新任務定義。如果您尚未刪除任務定義,那麼您建立的會是相同任務定義的新修訂版本,而不是新的任務定義。在先前的任務定義完全刪除前,您可以使用修訂版本來啟動新任務並進行必要的更改。

相關資訊

Amazon ECS 任務定義狀態

describe-services

describe-tasks

AWS 官方
AWS 官方已更新 3 個月前