本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 為 Fargate 建立 Amazon ECS Windows 任務 AWS CLI
以下步驟會協助您使用 AWS CLI在 Amazon ECS 中設定叢集、註冊任務定義、執行 Windows 任務,以及執行其他常見案例。請務必使用最新版本的 AWS CLI。如需有關如何升級至最新版本的詳細資訊,請參閱 Installing or updating to the latest version of the AWS CLI。
注意
您可以使用雙堆疊服務端點,透過 IPv4 和 IPv6 AWS CLI從 、 SDKs 和 Amazon ECS API 與 Amazon ECS 互動。如需詳細資訊,請參閱使用 Amazon ECS 雙堆疊端點。
先決條件
本教學課程假設已完成下列先決條件。
-
AWS CLI 已安裝並設定最新版本的 。如需安裝或升級 的詳細資訊 AWS CLI,請參閱安裝或更新至最新版本的 AWS CLI。
-
已完成「設定以使用 Amazon ECS。」中的步驟。
-
您的 IAM 使用者擁有 AmazonECS_FullAccess IAM 政策範例中指定的所需許可。
-
您已建立 VPC 和安全群組。本教學課程使用託管於 Docker Hub 的容器映像,因此任務必須有網際網路連線。若要將網際網路的路由提供給任務,請使用下列其中一個選項。
-
透過具有彈性 IP 地址的 NAT 閘道使用私有子網路。
-
使用公有子網路,然後將公有 IP 地址指派給任務。
如需詳細資訊,請參閱建立 Virtual Private Cloud。
如需有關安全群組和規則的資訊,請參閱《Amazon Virtual Private Cloud 使用者指南》中的 VPC 的預設安全群組和規則範例。
-
-
(選用) AWS CloudShell 是一種工具,可為客戶提供命令列,而不需要建立自己的 EC2 執行個體。如需詳細資訊,請參閱AWS CloudShell 《 使用者指南》中的什麼是 AWS CloudShell?。
步驟 1:建立叢集
您的帳戶預設會得到 default 叢集。
注意
使用為您提供之 default 叢集的優點是,您不必在後續的命令中指定 --cluster
選項。如果您建立的是自己的叢集,不是預設叢集,您必須在打算對該叢集使用的每個命令中指定 cluster_name--cluster 。cluster_name
使用下列命令以唯一的名稱建立您自己的叢集:
aws ecs create-cluster --cluster-namefargate-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 可以透過 AWS CLI 兩種方式傳遞至 :您可以將任務定義 JSON 儲存為 檔案,並使用 選項傳遞。--cli-input-json file://path_to_file.json
將 JSON 檔案用於容器定義中:
aws ecs register-task-definition --cli-input-jsonfile://$HOME/tasks/fargate-task.json
register-task-definition 命令會在完成註冊後傳回任務定義的描述。
步驟 3:列出任務定義
您可以使用 list-task-definitions 命令隨時列出您帳戶的任務定義。這個命令的輸出會顯示 family 和 revision 值,可在呼叫 run-task 或 start-task 時一起使用。
aws ecs list-task-definitions
輸出:
{
"taskDefinitionArns": [
"arn:aws:ecs:region:aws_account_id:task-definition/sample-fargate-windows:1"
]
}
步驟 4:建立服務
為您的帳戶註冊任務之後,您就可以在您的叢集中建立已註冊任務的服務。在此範例中,您將透過在叢集中執行之 sample-fargate:1 任務定義的一個執行個體建立服務。該任務需要網際網路的路由,因此您可透過兩種方法達成。一種方法是,使用透過 NAT 閘道所設定的私有子網路,而該閘道在公有子網路中具有彈性 IP 地址。另一種方法是,使用公有子網路並將公有 IP 地址指派至任務。以下提供這兩種範例。
使用私有子網路的範例。
aws ecs create-service --clusterfargate-cluster--service-namefargate-service--task-definitionsample-fargate-windows:1--desired-count1--launch-type "FARGATE" --network-configuration "awsvpcConfiguration={subnets=[subnet-abcd1234],securityGroups=[sg-abcd1234]}"
使用公有子網路的範例。
aws ecs create-service --clusterfargate-cluster--service-namefargate-service--task-definitionsample-fargate-windows:1--desired-count1--launch-type "FARGATE" --network-configuration "awsvpcConfiguration={subnets=[subnet-abcd1234],securityGroups=[sg-abcd1234],assignPublicIp=ENABLED}"
create-service 命令會在完成註冊後傳回任務定義的描述。
步驟 5:列出服務
列出您叢集的服務。您應該會看到您在先前一節所建立的服務。您可以記下這個命令傳回的服務名稱或完整的 ARN,稍後將用以描述服務。
aws ecs list-services --clusterfargate-cluster
輸出:
{
"serviceArns": [
"arn:aws:ecs:region:aws_account_id:service/fargate-service"
]
}
步驟 6:描述執行中的服務
使用之前擷取的服務名稱來描述服務,以取得任務的詳細資訊。
aws ecs describe-services --clusterfargate-cluster--servicesfargate-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 --clusterfargate-cluster--servicefargate-service--force
刪除叢集。
aws ecs delete-cluster --clusterfargate-cluster