ブルー/グリーンデプロイを使用したサービスの作成 - Amazon Elastic Container Service

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

ブルー/グリーンデプロイを使用したサービスの作成

以下のチュートリアルでは、 AWS CLIで ブルー/グリーンデプロイタイプを使用する Fargate タスクを含む、Amazon ECS サービスを作成する方法を示します。

注記

ブルー/グリーンデプロイを実行するためのサポートが AWS CloudFormationに追加されています。詳細については、「 ユーザーガイド」の「 CodeDeploy を使用して Amazon ECS ブルー/グリーンデプロイ AWS CloudFormationを実行する」を参照してください。 AWS CloudFormation

前提条件

このチュートリアルでは、以下の前提条件をすでに満たしているものとします。

ステップ 1: Application Load Balancer の作成

ブルー/グリーンデプロイタイプを使用する Amazon ECS サービスは、Application Load Balancer または Network Load Balancer のいずれかを使用する必要があります。このチュートリアルでは、Application Load Balancer を使用します。

Application Load Balancer を作成するには
  1. create-load-balancer コマンドを使用して、Application Load Balancer を作成します。異なるアベイラビリティーゾーンにある 2 つのサブネット、およびセキュリティグループを指定します。

    aws elbv2 create-load-balancer \ --name bluegreen-alb \ --subnets subnet-abcd1234 subnet-abcd5678 \ --security-groups sg-abcd1234 \ --region us-east-1

    出力には、次の形式でロードバランサーの Amazon リソースネーム (ARN) が含まれます。

    arn:aws:elasticloadbalancing:region:aws_account_id:loadbalancer/app/bluegreen-alb/e5ba62739c16e642
  2. create-target-group コマンドを使用してターゲットグループを作成します。このターゲットグループは、サービスで設定されている元のタスクにトラフィックをルーティングします。

    aws elbv2 create-target-group \ --name bluegreentarget1 \ --protocol HTTP \ --port 80 \ --target-type ip \ --vpc-id vpc-abcd1234 \ --region us-east-1

    出力には、以下の形式でターゲットグループの ARN が含まれます。

    arn:aws:elasticloadbalancing:region:aws_account_id:targetgroup/bluegreentarget1/209a844cd01825a4
  3. ターゲットグループにリクエストを転送するデフォルトルールが関連付けられた、ロードバランサーのリスナーを作成するには、create-listener コマンドを使用します。

    aws elbv2 create-listener \ --load-balancer-arn arn:aws:elasticloadbalancing:region:aws_account_id:loadbalancer/app/bluegreen-alb/e5ba62739c16e642 \ --protocol HTTP \ --port 80 \ --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:region:aws_account_id:targetgroup/bluegreentarget1/209a844cd01825a4 \ --region us-east-1

    出力には、以下の形式でリスナーの ARN が含まれます。

    arn:aws:elasticloadbalancing:region:aws_account_id:listener/app/bluegreen-alb/e5ba62739c16e642/665750bec1b03bd4

ステップ 2: Amazon ECS クラスターを作成する

[create-cluster]コマンドを使用して、使用するtutorial-bluegreen-clusterという名前のクラスターを作成します。

aws ecs create-cluster \ --cluster-name tutorial-bluegreen-cluster \ --region us-east-1

出力には、以下の形式でクラスターの ARN が含まれます。

arn:aws:ecs:region:aws_account_id:cluster/tutorial-bluegreen-cluster

ステップ 3: タスク定義を登録する

register-task-definition コマンドを使用して、Fargate と互換性のあるタスク定義を登録します。これには、awsvpc ネットワークモードを使用する必要があります。このチュートリアルで使用するタスク定義の例を以下に示します。

まず、以下の内容で fargate-task.json というファイルを作成します。タスク実行ロールの ARN を使用していることを確認します。詳細については、「Amazon ECS タスク実行IAM ロール」を参照してください。

{ "family": "tutorial-task-def", "networkMode": "awsvpc", "containerDefinitions": [ { "name": "sample-app", "image": "httpd:2.4", "portMappings": [ { "containerPort": 80, "hostPort": 80, "protocol": "tcp" } ], "essential": true, "entryPoint": [ "sh", "-c" ], "command": [ "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #00FFFF;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\"" ] } ], "requiresCompatibilities": [ "FARGATE" ], "cpu": "256", "memory": "512", "executionRoleArn": "arn:aws:iam::aws_account_id:role/ecsTaskExecutionRole" }

次に、作成した fargate-task.json ファイルを使用して、タスク定義を登録します。

aws ecs register-task-definition \ --cli-input-json file://fargate-task.json \ --region us-east-1

ステップ 4: Amazon ECS サービスを作成する

サービスを作成するには、create-service コマンドを使用します。

まず、以下の内容で service-bluegreen.json というファイルを作成します。

{ "cluster": "tutorial-bluegreen-cluster", "serviceName": "service-bluegreen", "taskDefinition": "tutorial-task-def", "loadBalancers": [ { "targetGroupArn": "arn:aws:elasticloadbalancing:region:aws_account_id:targetgroup/bluegreentarget1/209a844cd01825a4", "containerName": "sample-app", "containerPort": 80 } ], "launchType": "FARGATE", "schedulingStrategy": "REPLICA", "deploymentController": { "type": "CODE_DEPLOY" }, "platformVersion": "LATEST", "networkConfiguration": { "awsvpcConfiguration": { "assignPublicIp": "ENABLED", "securityGroups": [ "sg-abcd1234" ], "subnets": [ "subnet-abcd1234", "subnet-abcd5678" ] } }, "desiredCount": 1 }

次に、作成した service-bluegreen.json ファイルを使用してサービスを作成します。

aws ecs create-service \ --cli-input-json file://service-bluegreen.json \ --region us-east-1

出力には、以下の形式でサービスの ARN が含まれます。

arn:aws:ecs:region:aws_account_id:service/service-bluegreen

次のコマンドを使用し、ロードバランサーの DNS 名を取得します。

aws elbv2 describe-load-balancers --name bluegreen-alb --query 'LoadBalancers[*].DNSName'

Web ブラウザに DNS 名を入力すると、サンプルアプリが青色の背景で表示された Web ページが表示されます。

ステップ 5: AWS CodeDeploy リソースを作成する

以下のステップを使用して、 CodeDeploy アプリケーション、 CodeDeploy デプロイグループの Application Load Balancer ターゲットグループ、デプロイ CodeDeploy グループを作成します。

CodeDeploy リソースを作成するには
  1. create-application コマンドを使用して CodeDeploy アプリケーションを作成します。ECS コンピューティングプラットフォームを指定します。

    aws deploy create-application \ --application-name tutorial-bluegreen-app \ --compute-platform ECS \ --region us-east-1

    出力には、以下の形式でアプリケーションの ID が含まれます。

    {
        "applicationId": "b8e9c1ef-3048-424e-9174-885d7dc9dc11"
    }
  2. create-target-group コマンドを使用して、 CodeDeploy デプロイグループを作成するときに使用される 2 番目の Application Load Balancer ターゲットグループを作成します。

    aws elbv2 create-target-group \ --name bluegreentarget2 \ --protocol HTTP \ --port 80 \ --target-type ip \ --vpc-id "vpc-0b6dd82c67d8012a1" \ --region us-east-1

    出力には、以下の形式でターゲットグループの ARN が含まれます。

    arn:aws:elasticloadbalancing:region:aws_account_id:targetgroup/bluegreentarget2/708d384187a3cfdc
  3. create-deployment-group コマンドを使用してデプロイ CodeDeploy グループを作成します。

    まず、以下の内容で tutorial-deployment-group.json というファイルを作成します。この例では、作成したリソースを使用します。にはserviceRoleArn、Amazon ECS CodeDeploy IAM ロールの ARN を指定します。詳細については、「Amazon ECS CodeDeploy IAM ロール」を参照してください。

    { "applicationName": "tutorial-bluegreen-app", "autoRollbackConfiguration": { "enabled": true, "events": [ "DEPLOYMENT_FAILURE" ] }, "blueGreenDeploymentConfiguration": { "deploymentReadyOption": { "actionOnTimeout": "CONTINUE_DEPLOYMENT", "waitTimeInMinutes": 0 }, "terminateBlueInstancesOnDeploymentSuccess": { "action": "TERMINATE", "terminationWaitTimeInMinutes": 5 } }, "deploymentGroupName": "tutorial-bluegreen-dg", "deploymentStyle": { "deploymentOption": "WITH_TRAFFIC_CONTROL", "deploymentType": "BLUE_GREEN" }, "loadBalancerInfo": { "targetGroupPairInfoList": [ { "targetGroups": [ { "name": "bluegreentarget1" }, { "name": "bluegreentarget2" } ], "prodTrafficRoute": { "listenerArns": [ "arn:aws:elasticloadbalancing:region:aws_account_id:listener/app/bluegreen-alb/e5ba62739c16e642/665750bec1b03bd4" ] } } ] }, "serviceRoleArn": "arn:aws:iam::aws_account_id:role/ecsCodeDeployRole", "ecsServices": [ { "serviceName": "service-bluegreen", "clusterName": "tutorial-bluegreen-cluster" } ] }

    次に、 CodeDeploy デプロイグループを作成します。

    aws deploy create-deployment-group \ --cli-input-json file://tutorial-deployment-group.json \ --region us-east-1

    出力には、以下の形式でデプロイグループの ID が含まれます。

    {
        "deploymentGroupId": "6fd9bdc6-dc51-4af5-ba5a-0a4a72431c88"
    }

ステップ 6: デプロイを作成してモニタリングする CodeDeploy

CodeDeploy デプロイを作成する前に、commandfargate-task.json次のように のタスク定義を更新して、サンプルアプリケーションの背景色を緑色に変更します。

{ ... "containerDefinitions": [ { ... "command": [ "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #097969;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\"" ] } ], ... }

次のコマンドを使用し、更新されたタスク定義を登録します。

aws ecs register-task-definition \ --cli-input-json file://fargate-task.json \ --region us-east-1

次に、次の手順を使用して、アプリケーション仕様ファイル (AppSpec ファイル) と CodeDeploy デプロイを作成してアップロードします。

CodeDeploy デプロイを作成してモニタリングするには
  1. 次の手順を使用して、 AppSpec ファイルを作成してアップロードします。

    1. CodeDeploy デプロイグループの内容で appspec.yaml というファイルを作成します。この例では、更新されたタスク定義を使用しています。

      version: 0.0 Resources: - TargetService: Type: AWS::ECS::Service Properties: TaskDefinition: "arn:aws:ecs:region:aws_account_id:task-definition/tutorial-task-def:2" LoadBalancerInfo: ContainerName: "sample-app" ContainerPort: 80 PlatformVersion: "LATEST"
    2. s3 mb コマンドを使用して、 AppSpec ファイルの Amazon S3 バケットを作成します。

      aws s3 mb s3://tutorial-bluegreen-bucket
    3. s3 cp コマンドを使用して、 AppSpec ファイルを Amazon S3 バケットにアップロードします。

      aws s3 cp ./appspec.yaml s3://tutorial-bluegreen-bucket/appspec.yaml
  2. 以下の手順を使用して CodeDeploy デプロイを作成します。

    1. CodeDeploy デプロイのコンテンツcreate-deployment.jsonを含む という名前のファイルを作成します。この例では、チュートリアルの前半で作成したリソースを使用します。

      { "applicationName": "tutorial-bluegreen-app", "deploymentGroupName": "tutorial-bluegreen-dg", "revision": { "revisionType": "S3", "s3Location": { "bucket": "tutorial-bluegreen-bucket", "key": "appspec.yaml", "bundleType": "YAML" } } }
    2. デプロイを作成するには、create-deployment コマンドを使用します。

      aws deploy create-deployment \ --cli-input-json file://create-deployment.json \ --region us-east-1

      出力には、以下の形式でデプロイの ID が含まれます。

      {
          "deploymentId": "d-RPCR1U3TW"
      }
  3. get-deployment-target コマンドを使用して、前の出力deploymentIdから を指定して、デプロイの詳細を取得します。

    aws deploy get-deployment-target \ --deployment-id "d-IMJU3A8TW" \ --target-id tutorial-bluegreen-cluster:service-bluegreen \ --region us-east-1

    初期では、デプロイの状態は InProgress です。トラフィックは、BLUE の taskSetLabelPRIMARY の状態、100.0 の trafficWeight である元のタスクセットに送信されます。置換タスクセットには、GREEN の taskSetLabelACTIVE の状態、0.0 の trafficWeight があります。DNS 名を入力した Web ブラウザには、引き続き青い背景のサンプルアプリが表示されます。

    {
    "deploymentTarget": {
    "deploymentTargetType": "ECSTarget",
    "ecsTarget": {
        "deploymentId": "d-RPCR1U3TW",
        "targetId": "tutorial-bluegreen-cluster:service-bluegreen",
        "targetArn": "arn:aws:ecs:region:aws_account_id:service/service-bluegreen",
        "lastUpdatedAt": "2023-08-10T12:07:24.797000-05:00",
        "lifecycleEvents": [
            {
                "lifecycleEventName": "BeforeInstall",
                "startTime": "2023-08-10T12:06:22.493000-05:00",
                "endTime": "2023-08-10T12:06:22.790000-05:00",
                "status": "Succeeded"
            },
            {
                "lifecycleEventName": "Install",
                "startTime": "2023-08-10T12:06:22.936000-05:00",
                "status": "InProgress"
            },
            {
                "lifecycleEventName": "AfterInstall",
                "status": "Pending"
            },
            {
                "lifecycleEventName": "BeforeAllowTraffic",
                "status": "Pending"
            },
            {
                "lifecycleEventName": "AllowTraffic",
                "status": "Pending"
            },
            {
                "lifecycleEventName": "AfterAllowTraffic",
                "status": "Pending"
            }
        ],
        "status": "InProgress",
        "taskSetsInfo": [
            {
                "identifer": "ecs-svc/9223370493423413672",
                "desiredCount": 1,
                "pendingCount": 0,
                "runningCount": 1,
                "status": "ACTIVE",
                "trafficWeight": 0.0,
                "targetGroup": {
                    "name": "bluegreentarget2"
                },
                "taskSetLabel": "Green"
            },
            {
                "identifer": "ecs-svc/9223370493425779968",
                "desiredCount": 1,
                "pendingCount": 0,
                "runningCount": 1,
                "status": "PRIMARY",
                "trafficWeight": 100.0,
                "targetGroup": {
                    "name": "bluegreentarget1"
                },
                "taskSetLabel": "Blue"
            }
        ]
    }
    }
    }

    以下の出力に示すように、デプロイメントのステータスが Succeeded になるまで、コマンドを使用してデプロイメントの詳細の取得を続けます。これで、トラフィックは置換タスクセットにリダイレクトされ、PRIMARY の状態および 100.0 の trafficWeight になりました。ロードバランサーの DNS 名を入力したウェブブラウザを更新すると、背景が緑色のサンプルアプリが表示されます。

    {
    "deploymentTarget": {
    "deploymentTargetType": "ECSTarget",
    "ecsTarget": {
        "deploymentId": "d-RPCR1U3TW",
        "targetId": "tutorial-bluegreen-cluster:service-bluegreen",
        "targetArn": "arn:aws:ecs:region:aws_account_id:service/service-bluegreen",
        "lastUpdatedAt": "2023-08-10T12:07:24.797000-05:00",
        "lifecycleEvents": [
            {
                "lifecycleEventName": "BeforeInstall",
                "startTime": "2023-08-10T12:06:22.493000-05:00",
                "endTime": "2023-08-10T12:06:22.790000-05:00",
                "status": "Succeeded"
            },
            {
                "lifecycleEventName": "Install",
                "startTime": "2023-08-10T12:06:22.936000-05:00",
                "endTime": "2023-08-10T12:08:25.939000-05:00",
                "status": "Succeeded"
            },
            {
                "lifecycleEventName": "AfterInstall",
                "startTime": "2023-08-10T12:08:26.089000-05:00",
                "endTime":  "2023-08-10T12:08:26.403000-05:00",
                "status": "Succeeded"
            },
            {
                "lifecycleEventName": "BeforeAllowTraffic",
                "startTime": "2023-08-10T12:08:26.926000-05:00",
                "endTime":  "2023-08-10T12:08:27.256000-05:00",
                "status": "Succeeded"
            },
            {
                "lifecycleEventName": "AllowTraffic",
                "startTime": "2023-08-10T12:08:27.416000-05:00",
                "endTime": "2023-08-10T12:08:28.195000-05:00",
                "status": "Succeeded"
            },
            {
                "lifecycleEventName": "AfterAllowTraffic",
                "startTime": "2023-08-10T12:08:28.715000-05:00",
                "endTime":  "2023-08-10T12:08:28.994000-05:00",
                "status": "Succeeded"
            }
        ],
        "status": "Succeeded",
        "taskSetsInfo": [
            {
                "identifer": "ecs-svc/9223370493425779968",
                "desiredCount": 1,
                "pendingCount": 0,
                "runningCount": 1,
                "status": "ACTIVE",
                "trafficWeight": 0.0,
                "targetGroup": {
                    "name": "bluegreentarget1"
                },
                "taskSetLabel": "Blue"
            },
            {
                "identifer": "ecs-svc/9223370493423413672",
                "desiredCount": 1,
                "pendingCount": 0,
                "runningCount": 1,
                "status": "PRIMARY",
                "trafficWeight": 100.0,
                "targetGroup": {
                    "name": "bluegreentarget2"
                },
                "taskSetLabel": "Green"
            }
        ]
    }
    }
    }

ステップ 7: クリーンアップする

このチュートリアルが終了したら、使用していないリソースに対する料金が発生しないように、チュートリアルに関連付けられたリソースをクリーンアップします。

チュートリアルに関連付けられたリソースのクリーンアップ
  1. delete-deployment-group コマンドを使用して、 CodeDeploy デプロイグループを削除します。

    aws deploy delete-deployment-group \ --application-name tutorial-bluegreen-app \ --deployment-group-name tutorial-bluegreen-dg \ --region us-east-1
  2. delete-application コマンドを使用して、 CodeDeploy アプリケーションを削除します。

    aws deploy delete-application \ --application-name tutorial-bluegreen-app \ --region us-east-1
  3. [削除サービス]コマンドを使用して、Amazon ECS サービスを削除します。--force フラグを使用すると、タスクがゼロになっていなくてもサービスを削除できます。

    aws ecs delete-service \ --service arn:aws:ecs:region:aws_account_id:service/service-bluegreen \ --force \ --region us-east-1
  4. [delete-cluster] コマンドを使用して、Amazon ECS クラスターを削除します。

    aws ecs delete-cluster \ --cluster tutorial-bluegreen-cluster \ --region us-east-1
  5. s3 rm コマンドを使用して、Amazon S3 バケットから AppSpec ファイルを削除します。

    aws s3 rm s3://tutorial-bluegreen-bucket/appspec.yaml
  6. [s3 rb]コマンドを使用して、 Amazon S3 バケットを削除します。

    aws s3 rb s3://tutorial-bluegreen-bucket
  7. delete-load-balancer コマンドを使用して、Application Load Balancer を削除します。

    aws elbv2 delete-load-balancer \ --load-balancer-arn arn:aws:elasticloadbalancing:region:aws_account_id:loadbalancer/app/bluegreen-alb/e5ba62739c16e642 \ --region us-east-1
  8. delete-target-group コマンドを使用して、2 つの Application Load Balancer ターゲットグループを削除します。

    aws elbv2 delete-target-group \ --target-group-arn arn:aws:elasticloadbalancing:region:aws_account_id:targetgroup/bluegreentarget1/209a844cd01825a4 \ --region us-east-1
    aws elbv2 delete-target-group \ --target-group-arn arn:aws:elasticloadbalancing:region:aws_account_id:targetgroup/bluegreentarget2/708d384187a3cfdc \ --region us-east-1