為 Fargate 啟動類型創建 Amazon ECS Windows 任務 AWS CLI - Amazon Elastic Container Service

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

為 Fargate 啟動類型創建 Amazon ECS Windows 任務 AWS CLI

下列步驟可協助您設定叢集、註冊任務定義、執行 Windows 任務,以及在 Amazon 中ECS使用 AWS CLI. 請務必使用最新版本的 AWS CLI。如需如何升級至最新版本的詳細資訊,請參閱安裝 AWS Command Line Interface

必要條件

本教學課程假設已完成下列先決條件。

  • 已安裝並設定最新版本的。 AWS CLI 如需有關安裝或升級的詳細資訊 AWS CLI,請參閱安裝 AWS Command Line Interface.

  • 已完成「設置使用 Amazon ECS」中的步驟。

  • 您的 AWS 使用者具有在Amazon ECS _ FullAccessIAM策略範例中指定的必要權限。

  • 您已建立要使用的 VPC AND 安全性群組。本教學課程使用託管於 Docker Hub 的容器映像,因此任務必須有網際網路連線。若要將網際網路的路由提供給任務,請使用下列其中一個選項。

    • 使用具有彈性 IP 位址之NAT閘道的私有子網路。

    • 使用公有子網路,然後將公有 IP 地址指派給任務。

    如需詳細資訊,請參閱建立 Virtual Private Cloud

    如需有關安全群組和規則的資訊,請參閱 Amazon Virtual Private Cloud 使用者指南中的預設安全群組VPCs範例規則

  • (選擇性) AWS CloudShell 這項工具可為客戶提供命令列,而不需要建立自己的EC2執行個體。如需詳細資訊,請參閱什麼是 AWS CloudShell?《AWS CloudShell 使用者指南》中。

步驟 1:建立叢集

您的帳戶預設會得到 default 叢集。

注意

使用為您提供之 default 叢集的優點是,您不必在後續的命令中指定 --cluster cluster_name 選項。如果您建立的是自己的叢集,不是預設叢集,您必須在打算對該叢集使用的每個命令中指定 --cluster cluster_name

使用下列命令以唯一的名稱建立您自己的叢集:

aws ecs create-cluster --cluster-name fargate-cluster

輸出:

{ "cluster": { "status": "ACTIVE", "statistics": [], "clusterName": "fargate-cluster", "registeredContainerInstancesCount": 0, "pendingTasksCount": 0, "runningTasksCount": 0, "activeServicesCount": 0, "clusterArn": "arn:aws:ecs:region:aws_account_id:cluster/fargate-cluster" } }

步驟 2:註冊 Windows 任務定義

您必須先註冊任務定義,才能在 Amazon ECS 叢集上執行 Windows 任務。任務定義是分在一組的容器清單。以下範例是建立 Web 應用程式的簡單任務定義。如需可用之任務定義參數的詳細資訊,請參閱「Amazon ECS 任務定義」。

{ "containerDefinitions": [ { "command": ["New-Item -Path C:\\inetpub\\wwwroot\\index.html -Type file -Value '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </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>'; C:\\ServiceMonitor.exe w3svc"], "entryPoint": [ "powershell", "-Command" ], "essential": true, "cpu": 2048, "memory": 4096, "image": "mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019", "name": "sample_windows_app", "portMappings": [ { "hostPort": 80, "containerPort": 80, "protocol": "tcp" } ] } ], "memory": "4096", "cpu": "2048", "networkMode": "awsvpc", "family": "windows-simple-iis-2019-core", "executionRoleArn": "arn:aws:iam::012345678910:role/ecsTaskExecutionRole", "runtimePlatform": {"operatingSystemFamily": "WINDOWS_SERVER_2019_CORE"}, "requiresCompatibilities": ["FARGATE"] }

上述範例JSON可以透過兩種方式傳遞給:您可以將工作定義儲存JSON為檔案,並使用--cli-input-json file://path_to_file.json選項來傳遞。 AWS CLI

若要將JSON檔案用於容器定義:

aws ecs register-task-definition --cli-input-json file://$HOME/tasks/fargate-task.json

register-task-definition 命令會在完成註冊後傳回任務定義的描述。

步驟 3:列出任務定義

您可以使用 list-task-definitions 命令隨時列出您帳戶的任務定義。這個命令的輸出會顯示 familyrevision 值,可在呼叫 run-taskstart-task 時一起使用。

aws ecs list-task-definitions

輸出:

{ "taskDefinitionArns": [ "arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate-windows:1" ] }

步驟 4:建立服務

為您的帳戶註冊任務之後,您就可以在您的叢集中建立已註冊任務的服務。在此範例中,您將透過在叢集中執行之 sample-fargate:1 任務定義的一個執行個體建立服務。該任務需要網際網路的路由,因此您可透過兩種方法達成。一種方法是使用在公共子網中使用具有彈性 IP 地址的網NAT關配置的私有子網絡。另一種方法是,使用公有子網路並將公有 IP 地址指派至任務。以下提供這兩種範例。

使用私有子網路的範例。

aws ecs create-service --cluster fargate-cluster --service-name fargate-service --task-definition sample-fargate-windows:1 --desired-count 1 --launch-type "FARGATE" --network-configuration "awsvpcConfiguration={subnets=[subnet-abcd1234],securityGroups=[sg-abcd1234]}"

使用公有子網路的範例。

aws ecs create-service --cluster fargate-cluster --service-name fargate-service --task-definition sample-fargate-windows:1 --desired-count 1 --launch-type "FARGATE" --network-configuration "awsvpcConfiguration={subnets=[subnet-abcd1234],securityGroups=[sg-abcd1234],assignPublicIp=ENABLED}"

create-service 命令會在完成註冊後傳回任務定義的描述。

步驟 5:列出服務

列出您叢集的服務。您應該會看到您在先前一節所建立的服務。您可以獲取服務名稱或從此命令返回的完整ARN名稱,然後使用它來描述服務。

aws ecs list-services --cluster fargate-cluster

輸出:

{ "serviceArns": [ "arn:aws:ecs:region:aws_account_id:service/fargate-service" ] }

步驟 6:描述執行中的服務

使用之前擷取的服務名稱來描述服務,以取得任務的詳細資訊。

aws ecs describe-services --cluster fargate-cluster --services fargate-service

如果成功,這會傳回服務失敗和服務的描述。例如,在服務區段中,您將可以找到部署的相關資訊,例如執行中或擱置中的任務狀態。您也可以找到任務定義、網路組態和時間戳記事件的相關資訊。在失敗區段中,您將可以找到與呼叫相關聯的失敗 (如果有的話) 的相關資訊。如需故障診斷,請參閱服務事件訊息。如需服務描述的詳細資訊,請參閱描述服務

{ "services": [ { "status": "ACTIVE", "taskDefinition": "arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate-windows:1", "pendingCount": 2, "launchType": "FARGATE", "loadBalancers": [], "roleArn": "arn:aws:iam::aws_account_id:role/aws-service-role/ecs.amazonaws.com/AWSServiceRoleForECS", "placementConstraints": [], "createdAt": 1510811361.128, "desiredCount": 2, "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-abcd1234" ], "securityGroups": [ "sg-abcd1234" ], "assignPublicIp": "DISABLED" } }, "platformVersion": "LATEST", "serviceName": "fargate-service", "clusterArn": "arn:aws:ecs:region:aws_account_id:cluster/fargate-cluster", "serviceArn": "arn:aws:ecs:region:aws_account_id:service/fargate-service", "deploymentConfiguration": { "maximumPercent": 200, "minimumHealthyPercent": 100 }, "deployments": [ { "status": "PRIMARY", "networkConfiguration": { "awsvpcConfiguration": { "subnets": [ "subnet-abcd1234" ], "securityGroups": [ "sg-abcd1234" ], "assignPublicIp": "DISABLED" } }, "pendingCount": 2, "launchType": "FARGATE", "createdAt": 1510811361.128, "desiredCount": 2, "taskDefinition": "arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate-windows:1", "updatedAt": 1510811361.128, "platformVersion": "0.0.1", "id": "ecs-svc/9223370526043414679", "runningCount": 0 } ], "events": [ { "message": "(service fargate-service) has started 2 tasks: (task 53c0de40-ea3b-489f-a352-623bf1235f08) (task d0aec985-901b-488f-9fb4-61b991b332a3).", "id": "92b8443e-67fb-4886-880c-07e73383ea83", "createdAt": 1510811841.408 }, { "message": "(service fargate-service) has started 2 tasks: (task b4911bee-7203-4113-99d4-e89ba457c626) (task cc5853e3-6e2d-4678-8312-74f8a7d76474).", "id": "d85c6ec6-a693-43b3-904a-a997e1fc844d", "createdAt": 1510811601.938 }, { "message": "(service fargate-service) has started 2 tasks: (task cba86182-52bf-42d7-9df8-b744699e6cfc) (task f4c1ad74-a5c6-4620-90cf-2aff118df5fc).", "id": "095703e1-0ca3-4379-a7c8-c0f1b8b95ace", "createdAt": 1510811364.691 } ], "runningCount": 0, "placementStrategy": [] } ], "failures": [] }

步驟 7:清除

完成此教學課程時,建議您清除相關聯的資源,以免未使用的資源產生費用。

刪除服務。

aws ecs delete-service --cluster fargate-cluster --service fargate-service --force

刪除叢集。

aws ecs delete-cluster --cluster fargate-cluster