aws ecs使用ecs-cli进行集群管理

想快速测试一下aws ecs但是懒得点控制台,通过cli工具快速启动和定制任务和服务

ecs-cli

目前该项目已经不维护了,新的项目迁移到了AWS Copilot CLI,但是感觉copilot反而因为封装太多导致很难用。

配置ecs cli,分为凭证配置和集群配置

  • 凭证配置,存放路径为~/.ecs/credentails
# 凭证配置
#ecs-cli configure profile --profile-name profile_name --access-key $AWS_ACCESS_KEY_ID --secret-key $AWS_SECRET_ACCESS_KEY --session-token AWS_SESSION_TOKE
cat ~/.ecs/credentails
version: v1
default: foo
ecs_profiles:
  default:
    aws_access_key_id: xxxxxxxxxxxxxxx
    aws_secret_access_key: xxxxxxxxxxxxxxx
  foo:
    aws_access_key_id: xxxxxxxxxxxxxxx
    aws_secret_access_key: xxxxxxxxxxxxxxx
  • 集群配置,存放路径为~/.ecs/config,可以设置启动类型(默认为EC2),但是启动服务或任务时仍旧能覆盖。
# 集群配置
# ecs-cli configure --cluster cluster_name --region region_name --config-name configuration_name
ecscli configure --region cn-north-1 --cluster worklearn
cat ~/.ecs/config
version: v1
default: foo
clusters:
  default:
    cluster: xxxxxxxxx
    region: cn-north-1
    default_launch_type: ""
  foo:
    cluster: xxxxxxxxx
    region: cn-north-1
    default_launch_type: ""

默认凭证获取顺序为:ecscli profile -> env -> ecs config -> default aws profile -> ec2 role

创建集群

如果非空集群,默认创建以下资源

  • Autoscaling Group
  • Autoscaling Launch Configuration
  • EC2 VPC
  • EC2 Internet Gateway
  • EC2 VPC Gateway Attachment
  • EC2 Route Table
  • EC2 Route
  • 2 Public EC2 Subnets
  • 2 EC2 SubnetRouteTableAssocitaions
  • EC2 Security Group
# 空集群
ecs-cli up --cluster myCluster --empty
# 指定资源创建集群
ecs-cli up --keypair cluster-key --capability-iam --size 2 --vpc vpc-086d798b56f59e2ae --subnets subnet-077cf5772b9302a37,subnet-027025e9d9760acdd --security-group sg-096df1a0cb9a6d7e9 --size 1
# fargate类型
ecs-cli up --launch-type FARGATE
# 指定userdata
$ ecs-cli up \
  --capability-iam \
  --extra-user-data my-shellscript \
  --extra-user-data my-cloud-boot-hook \
  --extra-user-data my-mime-multipart-archive \
  --launch-type EC2
# 扩容
ecs-cli scale --capability-iam --size 3 --cluster worklearn
# 关闭集群,实际上删除堆栈
ecs-cli down --cluster <clustername>

注意:创建集群默认会使用.ecs路径下config中默认配置的cluster以及credential中配置的默认凭证。如果集群已经存在,会报错InvalidParameterException: Arguments on this idempotent request are inconsistent with arguments used in previous request(s).

创建集群后资源的命名规则如下

  • amazon-ecs-cli-setup--EcsInstanceProfile-xxxxxxx
  • amazon-ecs-cli-setup--EcsInstanceRole-xxxxxx
  • amazon-ecs-cli-setup--EcsInstanceAsg-xxxxxx

启动任务

ecscli可以使用docker compose启动任务

关于docker-compose.yml的配置,Compose specification

创建compose.yaml

version: '2'
services:
  web:
    image: amazon/amazon-ecs-sample
    ports:
     - "80:80"

管理任务

compose : Executes docker-compose-style commands on an ECS cluster.

注意:创建task定义默认会以所在folder的名称命名

# 通过compose文件创建task definition
ecs-cli compose create --create-log-groups --cluster xxxx
# 创建并启动任务
ecs-cli compose up
# 启动任务,不会创建service
ecs-cli compose start
# 停止任务
ecs-cli compose down
# 扩展任务数量为2
ecs-cli compose scale 2
# 查看任务
ecs-cli compose ps
ecs-cli compose ps  --desired-status RUNNING

管理服务

注意:创建service定义默认会以所在folder的名称命名

# 通过compose文件创建task definition
ecs-cli compose service create --create-log-groups --cluster xxxx
# 创建并启动服务,会创建task定义
ecs-cli compose service up
# 启动服务
ecs-cli compose service start
# 停止任务
ecs-cli compose service down
# 扩展任务
ecs-cli compose service scale --size 2
# 查看任务
ecs-cli compose service ps
# 删除服务
ecs-cli compose service rm

由于ecs中很多参数在docker compose中并不存在,因此可以直接通过文件指定这些参数。可以指定的参数参考,在启动任务或服务的时候指定即可

service参数比较重要,和docker compose文件中的container对应

services correspond to the services listed in your docker compose file, with service_name matching the name of the container you wish to run. Its fields will be merged into an ECS Container Definition.

ecs-cli compose --ecs-params my-ecs-params.yml up

本地运行任务

ecs-cli能够将任务定义转换为 docker compose 文件

ecs-cli local create

不指定参数会尝试从本地task-definition.json中获取并生成docker-compose.ecs-local.ymldocker-compose.ecs-local.override.yml文件

You can also specify a different output file using the --output or -o flag. To skip the overwrite confirmation prompt, use the --force flag.

通过 -f--task-def-file指定task任务定义文件,--task-def-remote-t指定已经注册的任务,通过--output-o指定输出文件

$ ecs-cli local create -t demo:2
$ cat docker-compose.ecs-local.yml
version: "3.4"
services:
  web:
    environment:
      AWS_CONTAINER_CREDENTIALS_RELATIVE_URI: /creds
      ECS_CONTAINER_METADATA_URI: http://169.254.170.2/v3
    image: amazon/amazon-ecs-sample
    labels:
      ecs-local.task-definition-input.type: remote
      ecs-local.task-definition-input.value: demo:2
    networks:
      ecs-local-network: null
    ports:
    - target: 80
      published: 80
      protocol: tcp
networks:
  ecs-local-network:
    external: true

启动任务

This command will also create the local end Amazon ECS Local Endpoints Container and the network, ecs-local-network

$ ecs-cli local up -t demo:2
$ ecs-cli local ps -t demo:2
$ ecs-cli local down -t demo:2

此外

  • 使用私有仓库,https://github.com/aws/amazon-ecs-cli#using-private-registry-authentication

  • 查看日志

    $ ecs-cli logs --task-id xxxxxxx
    
  • 查看实例和任务的属性缺失

    ecs-cli check-attributes --container-instances xxxxxxxxxxxx --task-def demo:2 --cluster xxxxxxx
    Container Instance  Missing Attributes
    worklearn           None
    
  • 上传和拉取镜像

    ecs images
    # 自动认证创建ecr仓库
    ecs push alpine:latest
    ecs pull alpine:latest
    

你可能感兴趣的:(AWS,aws,云计算,java)