本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 為 EC2 啟動類型建立 Amazon ECS 任務 AWS CLI
以下步驟會協助您使用 AWS CLI在 Amazon ECS 中設定叢集、註冊任務定義、執行任務,以及執行其他常見案例。使用最新版本的 AWS CLI。如需如何升級至最新版本的詳細資訊,請參閱安裝或更新至最新版本的 AWS CLI。
主題
必要條件
本教學課程假設已完成下列先決條件:
-
AWS CLI 已安裝並設定最新版本的 。如需安裝或升級 的詳細資訊 AWS CLI,請參閱安裝或更新至最新版本的 AWS CLI。
-
已完成「設定以使用 Amazon ECS。」中的步驟。
-
您的 AWS 使用者具有 IAM AmazonECS_FullAccess 政策範例中指定的必要許可。
-
您已建立 VPC 和安全群組。如需詳細資訊,請參閱建立 Virtual Private Cloud。
-
(選用) AWS CloudShell 是一種工具,可為客戶提供命令列,而不需要建立自己的 EC2 執行個體。如需詳細資訊,請參閱AWS CloudShell 《 使用者指南》中的什麼是 AWS CloudShell?。
步驟 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-instancescontainer_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 執行個體 ID,用來在 Amazon EC2 主控台中監控執行個體,或搭配 aws ec2 describe-instances --instance-id
instance_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 儲存為 檔案,並使用
選項傳遞。或者,您可以逸出 JSON 中的引號,然後在命令行上傳遞 JSON 容器定義,如下範例所示。如果您選擇用命令列傳送容器定義,您的命令需要使用額外的 --cli-input-json
file://path_to_file.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 命令隨時列出您帳戶的任務定義。這個命令的輸出會顯示 family
和 revision
值,可在呼叫 run-task 或 start-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-definitionsleep360:1
--count1
輸出:
{
"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
--tasktask_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": []
}
]
}
]
}