跳至內容

我該如何設定和監控 Amazon ECS 部署斷路器?

4 分的閱讀內容
0

我希望在我的 Amazon Elastic Container Service (Amazon ECS) 部署失敗時,自動復原並接收通知。

簡短描述

若要使用 Amazon ECS 部署斷路器自動復原和監控部署,請完成以下步驟:

  1. 設定部署斷路器。
  2. 設定 Amazon EventBridge 來監控部署斷路器。
  3. 測試部署失敗情境。

解決方法

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

設定部署斷路器

請完成下列步驟:

  1. 建立一個 JSON 檔案,其任務定義類似以下範例:
    {
        "family": "my-task",
        "containerDefinitions": [
            {
                "name": "sample-container",
                "image": "nginx:alpine",
                "essential": true
            }
        ],
        "executionRoleArn": "arn:aws:iam::123456789876:role/ecsTaskExecutionRole",
        "networkMode": "awsvpc",
        "requiresCompatibilities": [
            "FARGATE"
        ],
        "cpu": "256",
        "memory": "512"
    }
    **注意:**將 123456789876 替換為您的 AWS 帳戶 ID。如果您沒有 ecsTaskExecutionRole,請建立一個任務執行角色
  2. 若要註冊任務定義,請執行以下 register-task-definition 命令:
    aws ecs register-task-definition \
        --cli-input-json file://taskdef-success.json
    **注意:**將 taskdef-success.json 替換為您的任務定義 JSON 檔案。
  3. 若要建立已啟用部署斷路器和復原功能的 Amazon ECS 服務,請執行以下 create-service 命令:
    aws ecs create-service \
         --cluster default \
         --service-name my-sample-service \
         --deployment-controller type=ECS \
         --desired-count 1 \
         --deployment-configuration "deploymentCircuitBreaker={enable=true,rollback=true}" \
         --task-definition my-task:1 \
         --launch-type FARGATE \
         --network-configuration "awsvpcConfiguration={subnets=[subnet-12345],securityGroups=[sg-12345],assignPublicIp=ENABLED}"
    **注意:**將 subnet-12345 替換為您的子網路,並將 sg-12345 替換為您的安全群組。您必須將 deployment-controller 設定為 type=ECS,因為部署斷路器只適用於滾動更新部署。
    如果您沒有預設叢集,請執行以下 create-cluster 命令來建立叢集:
    aws ecs create-cluster \
        --cluster-name example-cluster
    **注意:**將 example-cluster 替換為您的叢集名稱。
  4. 執行以下 describe-services 命令,確認 Amazon ECS 服務處於穩定狀態:
    aws ecs describe-services \
        --cluster default \
        --services my-sample-service | jq '.services[0].events[] | {message}'
    您將收到類似以下範例的輸出結果:
    {
      "message": "(service my-sample-service) has reached a steady state."
    }
    {
      "message": "(service my-sample-service) (deployment ecs-svc/1234567890123456789) deployment completed."
    }
    {
      "message": "(service my-sample-service) has started 1 tasks: (task 2918eb15dd0f4d42affc2a3a07818abf)."
    }

設定 EventBridge 來監控部署斷路器

請完成下列步驟:

  1. 執行以下 create-topic 命令來建立 Amazon Simple Notification Service (Amazon SNS) 主題,作為 EventBridge 規則的目標:

    aws sns create-topic \
        --name my-topic

    **注意:**將 my-topic 替換為您的 SNS 主題名稱。

  2. 若要更新主題屬性,以允許呼叫所需的 API,請執行以下 set-topic-attributes 命令:

    aws sns set-topic-attributes \
        --topic-arn arn:aws:sns:eu-west-1:123456789876:my-topic \
        --attribute-name Policy \
        --attribute-value '{
      "Version": "2008-10-17",
      "Id": "my_topic_policy",
      "Statement": [
        {
          "Sid": "my_topic_default",
          "Effect": "Allow",
          "Principal": {
            "AWS": "*"
          },
          "Action": [
            "SNS:GetTopicAttributes",
            "SNS:SetTopicAttributes",
            "SNS:AddPermission",
            "SNS:RemovePermission",
            "SNS:DeleteTopic",
            "SNS:Subscribe",
            "SNS:ListSubscriptionsByTopic",
            "SNS:Publish"
          ],
          "Resource": "arn:aws:sns:eu-west-1:123456789876:my-topic",
          "Condition": {
            "StringEquals": {
              "AWS:SourceOwner": "123456789876"
            }
          }
        },
        {
          "Sid": "my_topic_for_sns_Publish",
          "Effect": "Allow",
          "Principal": {
            "Service": "events.amazonaws.com"
          },
          "Action": "sns:Publish",
          "Resource": "arn:aws:sns:eu-west-1:123456789876:my-topic"
        }
      ]
    }'

    **注意:**將 eu-west-1 替換為您的 AWS 區域,將 123456789876 替換為您的帳戶 ID,將 my-topic 替換為您的主題名稱。

  3. 若要使用您的電子郵件訂閱 SNS 主題,請執行以下訂閱命令:

    aws sns subscribe \
        --topic-arn arn:aws:sns:eu-west-1:123456789876:my-topic \
        --protocol email \
        --notification-endpoint example@example.com

    **注意:**將 eu-west-1 替換為您的地區,將 123456789876 替換為您的帳戶 ID,將 my-topic 替換為您的主題名稱,並將 example@example.com 替換為您的電子郵件地址。

  4. 在您收到的訂閱確認電子郵件中,選擇 Confirm subscription (確認訂閱)。

  5. 若要為服務部署失敗事件建立 EventBridge 規則,請執行以下 put-rule 命令:

    aws events put-rule \
      --name "EcsServiceDeploymentFailed" \
      --event-pattern "{\"source\":[\"aws.ecs\"],\"detail-type\":[\"ECS Deployment State Change\"],\"detail\":{\"eventName\":[\"SERVICE_DEPLOYMENT_FAILED\"]}}"
  6. 若要將 SNS 主題新增為 EventBridge 規則的目標,請執行以下 put-targets 命令:

    aws events put-targets \
        --rule EcsServiceDeploymentFailed --targets "Id"="1","Arn"="arn:aws:sns:eu-west-1:123456789876:my-topic"

    **注意:**將 eu-west-1 替換為您的區域,將 123456789876 替換為您的帳戶 ID,並將 my-topic 替換為您的主題名稱。

測試部署失敗情境

請完成下列步驟:

  1. 建立一個 JSON 檔案,其任務定義包含不正確的映像檔標籤,類似於以下內容:

    {
        "family": "my-task",
        "containerDefinitions": [
            {
                "name": "sample-container",
                "image": "nginx:wrong-image-tag",
                "essential": true
            }
        ],
        "executionRoleArn": "arn:aws:iam::123456789876:role/ecsTaskExecutionRole",
        "networkMode": "awsvpc",
        "requiresCompatibilities": [
            "FARGATE"
        ],
        "cpu": "256",
        "memory": "512"
    }

    **注意:**將 sample-container 替換為您的容器執行個體,將 nginx:wrong-image-tag 替換為不正確的映像檔標籤,並將 123456789876 替換為您的帳戶 ID。不正確的映像檔標籤會導致部署失敗。

  2. 若要註冊任務定義,請執行以下 register-task-definition 命令:

    aws ecs register-task-definition --cli-input-json file://taskdef-failure.json

    **注意:**將 taskdef-failure.json 替換為您任務定義 JSON 檔案的標題。

  3. 若要使用新的任務定義更新服務並啟動新的部署,請執行以下 update-service 命令:

    aws ecs update-service --service my-sample-service --task-definition my-task:2

    **注意:**將 my-sample-service 替換為您的服務,並將 my-task:2 替換為您的任務。新的部署會失敗,因為任務無法提取映像檔。您將收到類似以下範例的輸出結果:

    {
        "version": "0",
        "id": "12345abc-2f7c-f86a-e544-a69218eb1446",
        "detail-type": "ECS Deployment State Change",
        "source": "aws.ecs",
        "account": "123456789876",
        "time": "2024-11-19T17:42:41Z",
        "region": "eu-west-1",
        "resources": [
            "arn:aws:ecs:eu-west-1:123456789876:service/default/my-sample-service"
        ],
        "detail": {
            "eventType": "ERROR",
            "eventName": "SERVICE_DEPLOYMENT_FAILED",
            "clusterArn": "arn:aws:ecs:eu-west-1:123456789876:cluster/default",
            "deploymentId": "ecs-svc/9876543210987654321",
            "updatedAt": "2024-11-19T17:42:40.73Z",
            "reason": "ECS deployment circuit breaker: tasks failed to start."
        }
    }
  4. 若要確認 Amazon ECS 服務是否已復原,請執行以下 describe-services 命令:

    aws ecs describe-services \
        --cluster default \
        --services my-sample-service | jq '.services[0].events[] | {message}'

    您將收到類似以下範例的輸出結果:

    {
      "message": "(service my-sample-service) has reached a steady state."
    }
    {
      "message": "(service my-sample-service) (deployment ecs-svc/1234567890123456789) deployment completed."
    }
    {
      "message": "(service my-sample-service) rolling back to deployment ecs-svc/1234567890123456789."
    }
    {
      "message": "(service my-sample-service) (deployment ecs-svc/9876543210987654321) deployment failed: tasks failed to start."
    }
    {
      "message": "(service my-sample-service) has started 1 tasks: (task b808c60616134ec1ac0c656a2bff1ef2)."
    }
    {
      "message": "(service my-sample-service) has started 1 tasks: (task 846c9aebd9224c2b832a38942cae5ea6)."
    }
    {
      "message": "(service my-sample-service) has started 1 tasks: (task 7143d03444574f2db2b567d75df3fe72)."
    }
    {
      "message": "(service my-sample-service) has started 1 tasks: (task 9a6a399770d940a2b442560c02a6a4c0)."
    }
    {
      "message": "(service my-sample-service) has reached a steady state."
    }
    {
      "message": "(service my-sample-service) (deployment ecs-svc/1234567890123456789) deployment completed."
    }
    {
      "message": "(service my-sample-service) has started 1 tasks: (task 2918eb15dd0f4d42affc2a3a07818abf)."
    }

相關資訊

Amazon ECS 部署斷路器如何偵測故障

宣布推出 Amazon ECS 部署斷路器

AWS 官方已更新 2 年前