ECS集成composefile示例

预计阅读时间:6分钟

撰写文件样本-ECS特定

服务

服务映射可以定义Docker映像,运行时约束和容器要求。

services:
  test:
    image: "image"
    command: "command"
    entrypoint: "entrypoint"
    environment:
      - "FOO=BAR"
    cap_add:
      - SYS_PTRACE
    cap_drop:
      - SYSLOG
    init: true
    user: "user"
    working_dir: "working_dir"
任务大小

设置资源限制,将其转换为Fargate任务大小值:

services:
  test:
    image: nginx
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 2048M
IAM角色

将现有用户角色分配给任务:

services:
  test:
    x-aws-policies:
      - "arn:aws:iam::aws:policy/AmazonS3FullAccess"
IAM政策

为任务分配嵌入式IAM策略:

services:
  test:
    x-aws-role:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Action: sqs:*
          Resource: arn:aws:sqs:us-east-1:12345678:myqueue
记录中

将选项传递给awslogs驱动程序

services:
  foo:
    image: nginx
    logging:
      options:
        awslogs-datetime-pattern: "FOO"

x-aws-logs_retention: 10
自动缩放

设定CPU百分比目标

services:
  foo:
    image: nginx
    deploy:
      x-aws-autoscaling: 
        cpu: 75
显卡

generic_resources为需要加速器作为GPU的服务设置。

services:
  learning:
    image: tensorflow/tensorflow:latest-gpus
    deploy:
      resources:
        reservations:
          memory: 32Gb
          cpus: "32"
          generic_resources:
            - discrete_resource_spec:
                kind: gpus
                value: 2
负载均衡器

当撰写文件中的服务公开端口时,将创建负载平衡器并将其配置为在所有容器之间分配流量。

可以创建2种类型的负载均衡器。对于公开非HTTP端口/协议的服务,将创建网络负载平衡器(NLB)。具有http / https端口/协议的服务将获得应用程序负载平衡器(ALB)

仅为Compose堆栈创建/配置了一个负载均衡器。如果在compose堆栈中为服务配置了两个http /非http端口,则会创建一个NLB。

下面的撰写文件仅配置了http端口,因此在部署时会创建一个ALB。

services:
  app:
    image: nginx
    ports:
      - 80:80

为非HTTP端口创建了NLB

services:
  app:
    image: nginx
    ports:
      - 8080:8080

要将http协议与自定义端口一起使用并获取ALB,请使用x-aws-protocolport属性。

services:
  test:
    image: nginx
    ports:
      - target: 8080
        x-aws-protocol: http

要重新使用外部负载均衡器并避免创建专用的负载均衡器,请x-aws-loadbalancer按如下所示设置顶层属性:

x-aws-loadbalancer: "LoadBalancerName"
services:
  app:
    image: nginx
    ports:
      - 80:80

类似地,外部VPCCluster可重复使用:

x-aws-vpc: "vpc-25435e"
x-aws-cluster: "ClusterName"

services:
  app:
    image: nginx
    ports:
      - 80:80

请记住,外部资源不会作为组合堆栈生命周期的一部分进行管理。

卷数

services:
  app:
    image: nginx
    volumes:
      - data:/test
volumes:
  data:

要使用先前创建的外部卷,请将其ID / ARN设置为名称:

services:
  app:
    image: nginx
    volumes:
      - data:/test

volumes:
  data:
    external: true
    name: "fs-f534645"

通过以下方式自定义卷配置 driver_opts

services:
  test:
    image: nginx
volumes:
  db-data:
    driver_opts:
        backup_policy: ENABLED
        lifecycle_policy: AFTER_30_DAYS
        performance_mode: maxIO
        throughput_mode: provisioned
        provisioned_throughput: 1024

网路

网络被映射到安全组。

services:
  test:
    image: nginx
networks:
  default:

使用外部网络/安全组:

services:
  test:
    image: nginx
networks:
  default:
    external: true
    name: sg-123abc

机密

机密会以字符串形式存储在AWS SecretsManager中,并会安装到下方的容器中/run/secrets/

services:
  app:
    image: nginx
    ports:
      - 80:80
    secrets:
      - mysecret

secrets:
  mysecret:
    file: ./secrets/mysecret.txt

使用外部机密时,请ARNname属性下设置有效机密:

services:
  app:
    image: nginx
    secrets:
      - foo_bar

secrets:
  foo_bar:
    name: "arn:aws:secretsmanager:eu-west-3:xxx:secret:foo_bar"
    external: true

访问私人图片

当服务配置有来自Docker Hub上的私有存储库中的映像时,请确保在部署Compose堆栈之前已正确配置了提取凭据。

要创建提取凭证,请创建具有以下内容的文件:

$ cat creds.json
{
  "username":"DockerHubID",
  "password":"GeneratedHubTokenOrPassword"
}

要创建拉取凭据并检索ARN/ID要在撰写文件中使用的凭据,请运行:

$ docker secret create pullcred /path/to/creds.json
arn:aws:secretsmanager:eu-west-3:xxx:secret:pullcred

使用ARN输出到设置x-aws-pull_credentials服务属性如下:

services:
  app:
    image: DockerHubID/privateimage
    x-aws-pull_credentials: arn:aws:secretsmanager:eu-west-3:xxx:secret:pullcred
    ports:
      - 80:80
DockerAmazon集成ECS编写cli部署样本