Amazon ECS
AWS Fargate 用户指南 (API 版本 2014-11-13)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

示例任务定义

此部分提供了一些任务定义示例,您可以使用这些示例来开始创建自己的任务定义。有关更多信息,请参阅任务定义参数创建任务定义

示例:Web 服务器

下面是用于设置 Web 服务器的示例任务定义 (使用 Fargate 启动类型):

{ "containerDefinitions": [ { "command": [ "/bin/sh -c \"echo '<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> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\"" ], "entryPoint": [ "sh", "-c" ], "essential": true, "image": "httpd:2.4", "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group" : "/ecs/fargate-task-definition", "awslogs-region": "us-east-1", "awslogs-stream-prefix": "ecs" } }, "name": "sample-fargate-app", "portMappings": [ { "containerPort": 80, "hostPort": 80, "protocol": "tcp" } ] } ], "cpu": "256", "executionRoleArn": "arn:aws:iam::012345678910:role/ecsTaskExecutionRole", "family": "fargate-task-definition", "memory": "512", "networkMode": "awsvpc", "requiresCompatibilities": [ "FARGATE" ] }

示例:awslogs 日志驱动程序

以下示例演示如何在使用 Fargate 启动类型的任务定义中使用 awslogs 日志驱动程序。nginx 容器将其日志发送到 ecs-log-streaming 区域中的 us-west-2 日志组。有关更多信息,请参阅 使用 awslogs 日志驱动程序

{ "containerDefinitions": [ { "memory": 128, "portMappings": [ { "hostPort": 80, "containerPort": 80, "protocol": "tcp" } ], "essential": true, "name": "nginx-container", "image": "nginx", "logConfiguration": { "logDriver": "awslogs", "options": { "awslogs-group": "ecs-log-streaming", "awslogs-region": "us-west-2", "awslogs-stream-prefix": "fargate-task-1" } }, "cpu": 0 } ], "networkMode": "awsvpc", "executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole", "memory": "2048", "cpu": "1024", "requiresCompatibilities": [ "FARGATE" ], "family": "example_task_1" }

示例:splunk 日志驱动程序

以下示例演示如何在任务定义中使用 splunk 日志驱动程序,以将日志发送到远程服务。Splunk 令牌参数指定为密钥选项,因为它可能被视为敏感数据。有关更多信息,请参阅 指定敏感数据

"containerDefinitions": [{ "logConfiguration": { "logDriver": "splunk", "options": { "splunk-url": "https://cloud.splunk.com:8080", "tag": "tag_name", }, "secretOptions": [{ "name": "splunk-token", "valueFrom": "arn:aws:secretsmanager:region:aws_account_id:secret:splunk-token-KnrBkD" }],

示例:fluentd 日志驱动程序

以下示例演示如何在任务定义中使用 fluentd 日志驱动程序,以将日志发送到远程服务。fluentd-address 值被指定为密钥选项,因为它可能会视为敏感数据。有关更多信息,请参阅 指定敏感数据

"containerDefinitions": [{ "logConfiguration": { "logDriver": "fluentd", "options": { "tag": "fluentd demo" }, "secretOptions": [{ "name": "fluentd-address", "valueFrom": "arn:aws:secretsmanager:region:aws_account_id:secret:fluentd-address-KnrBkD" }] }, "entryPoint": [], "portMappings": [{ "hostPort": 80, "protocol": "tcp", "containerPort": 80 }, { "hostPort": 24224, "protocol": "tcp", "containerPort": 24224 }] }],

示例:gelf 日志驱动程序

以下示例演示如何在任务定义中使用 gelf 日志驱动程序,以将日志发送到远程主机(此主机运行将 Gelf 日志作为输入的 Logstash)。有关更多信息,请参阅 logConfiguration

"containerDefinitions": [{ "logConfiguration": { "logDriver": "gelf", "options": { "gelf-address": "udp://logstash-service-address:5000", "tag": "gelf task demo" } }, "entryPoint": [], "portMappings": [{ "hostPort": 5000, "protocol": "udp", "containerPort": 5000 }, { "hostPort": 5000, "protocol": "tcp", "containerPort": 5000 } ] }],

示例:容器依赖项

此示例展示了具有多个容器(其中指定了容器依赖项)的任务定义的语法。在以下任务定义中,envoy 容器必须达到正常运行状态(由必需的容器运行状况检查参数决定),然后 app 容器才能启动。有关更多信息,请参阅容器依赖项

{ "family": "appmesh-gateway", "proxyConfiguration":{ "type": "APPMESH", "containerName": "envoy", "properties": [ { "name": "IgnoredUID", "value": "1337" }, { "name": "ProxyIngressPort", "value": "15000" }, { "name": "ProxyEgressPort", "value": "15001" }, { "name": "AppPorts", "value": "9080" }, { "name": "EgressIgnoredIPs", "value": "169.254.170.2,169.254.169.254" } ] }, "containerDefinitions": [ { "name": "app", "image": "application_image", "portMappings": [ { "containerPort": 9080, "hostPort": 9080, "protocol": "tcp" } ], "essential": true, "dependsOn": [ { "containerName": "envoy", "condition": "HEALTHY" } ] }, { "name": "envoy", "image": "111345817488.dkr.ecr.us-west-2.amazonaws.com/aws-appmesh-envoy:v1.9.1.0-prod", "essential": true, "environment": [ { "name": "APPMESH_VIRTUAL_NODE_NAME", "value": "mesh/meshName/virtualNode/virtualNodeName" }, { "name": "ENVOY_LOG_LEVEL", "value": "info" } ], "healthCheck": { "command": [ "CMD-SHELL", "echo hello" ], "interval": 5, "timeout": 2, "retries": 3 } } ], "executionRoleArn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole", "networkMode": "awsvpc" }