使用 為EC2啟動類型建立 Amazon ECS任務 AWS CLI - Amazon Elastic Container Service

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

使用 為EC2啟動類型建立 Amazon ECS任務 AWS CLI

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

必要條件

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

步驟 1:建立叢集

當您啟動第一個容器執行個體時,您的帳戶預設會得到 default 叢集。

注意

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

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

aws ecs create-cluster --cluster-name MyCluster

輸出:

{ "cluster": { "clusterName": "MyCluster", "status": "ACTIVE", "clusterArn": "arn:aws:ecs:region:aws_account_id:cluster/MyCluster" } }

步驟 2:使用 Amazon 啟動執行個體 ECS AMI

您必須先在叢集中擁有 Amazon ECS容器執行個體,才能在叢集上執行任務。如果您的叢集中沒有任何容器執行個體,請參閱 啟動 Amazon ECS Linux 容器執行個體 以取得詳細資訊。

步驟 3:列出容器執行個體

在啟動容器執行個體的幾分鐘內,Amazon ECS代理程式會將執行個體註冊至您的預設叢集。您可以執行下列命令列出叢集中的容器執行個體:

aws ecs list-container-instances --cluster default

輸出:

{ "containerInstanceArns": [ "arn:aws:ecs:us-east-1:aws_account_id:container-instance/container_instance_ID" ] }

步驟 4:描述您的容器執行個體

在您擁有容器執行個體的 ARN或 ID 之後,您可以使用 describe-container-instances命令來取得執行個體的寶貴資訊,例如剩餘和已註冊CPU和記憶體資源。

aws ecs describe-container-instances --cluster default --container-instances container_instance_ID

輸出:

{ "failures": [], "containerInstances": [ { "status": "ACTIVE", "registeredResources": [ { "integerValue": 1024, "longValue": 0, "type": "INTEGER", "name": "CPU", "doubleValue": 0.0 }, { "integerValue": 995, "longValue": 0, "type": "INTEGER", "name": "MEMORY", "doubleValue": 0.0 }, { "name": "PORTS", "longValue": 0, "doubleValue": 0.0, "stringSetValue": [ "22", "2376", "2375", "51678" ], "type": "STRINGSET", "integerValue": 0 }, { "name": "PORTS_UDP", "longValue": 0, "doubleValue": 0.0, "stringSetValue": [], "type": "STRINGSET", "integerValue": 0 } ], "ec2InstanceId": "instance_id", "agentConnected": true, "containerInstanceArn": "arn:aws:ecs:us-west-2:aws_account_id:container-instance/container_instance_ID", "pendingTasksCount": 0, "remainingResources": [ { "integerValue": 1024, "longValue": 0, "type": "INTEGER", "name": "CPU", "doubleValue": 0.0 }, { "integerValue": 995, "longValue": 0, "type": "INTEGER", "name": "MEMORY", "doubleValue": 0.0 }, { "name": "PORTS", "longValue": 0, "doubleValue": 0.0, "stringSetValue": [ "22", "2376", "2375", "51678" ], "type": "STRINGSET", "integerValue": 0 }, { "name": "PORTS_UDP", "longValue": 0, "doubleValue": 0.0, "stringSetValue": [], "type": "STRINGSET", "integerValue": 0 } ], "runningTasksCount": 0, "attributes": [ { "name": "com.amazonaws.ecs.capability.privileged-container" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.17" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18" }, { "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19" }, { "name": "com.amazonaws.ecs.capability.logging-driver.json-file" }, { "name": "com.amazonaws.ecs.capability.logging-driver.syslog" } ], "versionInfo": { "agentVersion": "1.5.0", "agentHash": "b197edd", "dockerVersion": "DockerVersion: 1.7.1" } } ] }

您也可以在 Amazon EC2主控台中或透過 aws ec2 describe-instances --instance-id instance_id命令找到可用來監控執行個體的 Amazon EC2執行個體 ID。

步驟 5:註冊任務定義

您必須先註冊任務定義,才能在ECS叢集上執行任務。任務定義是分在一組的容器清單。以下範例是一種簡單的任務定義,使用 Docker Hub 的 busybox 映像,只睡眠 360 秒。如需可用之任務定義參數的詳細資訊,請參閱「Amazon ECS任務定義」。

{ "containerDefinitions": [ { "name": "sleep", "image": "busybox", "cpu": 10, "command": [ "sleep", "360" ], "memory": 10, "essential": true } ], "family": "sleep360" }

上述範例JSON可以透過 AWS CLI 兩種方式傳遞至 :您可以將任務定義儲存JSON為檔案,並搭配 --cli-input-json file://path_to_file.json選項傳遞。或者,您可以逸出 中的引號,JSON並傳遞命令列上的JSON容器定義,如下列範例所示。如果您選擇用命令列傳送容器定義,您的命令需要使用額外的 --family 參數,以保留多個彼此相關聯的任務定義版本。

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

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

若要使用容器定義的JSON字串:

aws ecs register-task-definition --family sleep360 --container-definitions "[{\"name\":\"sleep\",\"image\":\"busybox\",\"cpu\":10,\"command\":[\"sleep\",\"360\"],\"memory\":10,\"essential\":true}]"

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

{ "taskDefinition": { "volumes": [], "taskDefinitionArn": "arn:aws:ec2:us-east-1:aws_account_id:task-definition/sleep360:1", "containerDefinitions": [ { "environment": [], "name": "sleep", "mountPoints": [], "image": "busybox", "cpu": 10, "portMappings": [], "command": [ "sleep", "360" ], "memory": 10, "essential": true, "volumesFrom": [] } ], "family": "sleep360", "revision": 1 } }

步驟 6:列出任務定義

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

aws ecs list-task-definitions

輸出:

{ "taskDefinitionArns": [ "arn:aws:ec2:us-east-1:aws_account_id:task-definition/sleep300:1", "arn:aws:ec2:us-east-1:aws_account_id:task-definition/sleep300:2", "arn:aws:ec2:us-east-1:aws_account_id:task-definition/sleep360:1", "arn:aws:ec2:us-east-1:aws_account_id:task-definition/wordpress:3", "arn:aws:ec2:us-east-1:aws_account_id:task-definition/wordpress:4", "arn:aws:ec2:us-east-1:aws_account_id:task-definition/wordpress:5", "arn:aws:ec2:us-east-1:aws_account_id:task-definition/wordpress:6" ] }

步驟 7:執行任務

在您註冊帳戶任務,並啟動已向叢集註冊的容器執行個體之後,您就可以在叢集中執行註冊的任務。在此範例中,您會在您的預設叢集中放置 sleep360:1 任務定義的單一執行個體。

aws ecs run-task --cluster default --task-definition sleep360:1 --count 1

輸出:

{ "tasks": [ { "taskArn": "arn:aws:ecs:us-east-1:aws_account_id:task/task_ID", "overrides": { "containerOverrides": [ { "name": "sleep" } ] }, "lastStatus": "PENDING", "containerInstanceArn": "arn:aws:ecs:us-east-1:aws_account_id:container-instance/container_instance_ID", "clusterArn": "arn:aws:ecs:us-east-1:aws_account_id:cluster/default", "desiredStatus": "RUNNING", "taskDefinitionArn": "arn:aws:ecs:us-east-1:aws_account_id:task-definition/sleep360:1", "containers": [ { "containerArn": "arn:aws:ecs:us-east-1:aws_account_id:container/container_ID", "taskArn": "arn:aws:ecs:us-east-1:aws_account_id:task/task_ID", "lastStatus": "PENDING", "name": "sleep" } ] } ] }

步驟 8:列出任務

列出您叢集的任務。您應該會看到您在先前一節所執行的任務。您可以取得從此命令傳回的任務 ID ARN 或完整,稍後再使用它來描述任務。

aws ecs list-tasks --cluster default

輸出:

{ "taskArns": [ "arn:aws:ecs:us-east-1:aws_account_id:task/task_ID" ] }

步驟 9:描述執行中的任務

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

aws ecs describe-tasks --cluster default --task task_ID

輸出:

{ "failures": [], "tasks": [ { "taskArn": "arn:aws:ecs:us-east-1:aws_account_id:task/task_ID", "overrides": { "containerOverrides": [ { "name": "sleep" } ] }, "lastStatus": "RUNNING", "containerInstanceArn": "arn:aws:ecs:us-east-1:aws_account_id:container-instance/container_instance_ID", "clusterArn": "arn:aws:ecs:us-east-1:aws_account_id:cluster/default", "desiredStatus": "RUNNING", "taskDefinitionArn": "arn:aws:ecs:us-east-1:aws_account_id:task-definition/sleep360:1", "containers": [ { "containerArn": "arn:aws:ecs:us-east-1:aws_account_id:container/container_ID", "taskArn": "arn:aws:ecs:us-east-1:aws_account_id:task/task_ID", "lastStatus": "RUNNING", "name": "sleep", "networkBindings": [] } ] } ] }