aws ecs使用ecs-cli进行集群管理
创始人
2024-02-13 08:32:25
0

想快速测试一下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: xxxxxxxxxxxxxxxaws_secret_access_key: xxxxxxxxxxxxxxxfoo:aws_access_key_id: xxxxxxxxxxxxxxxaws_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: xxxxxxxxxregion: cn-north-1default_launch_type: ""foo:cluster: xxxxxxxxxregion: cn-north-1default_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 

注意:创建集群默认会使用.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-sampleports:- "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: /credsECS_CONTAINER_METADATA_URI: http://169.254.170.2/v3image: amazon/amazon-ecs-samplelabels:ecs-local.task-definition-input.type: remoteecs-local.task-definition-input.value: demo:2networks:ecs-local-network: nullports:- target: 80published: 80protocol: 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
    

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...
美团联名卡审核成功待激活(美团... 今天百科达人给各位分享美团联名卡审核成功待激活的知识,其中也会对美团联名卡审核未通过进行解释,如果能...