我运行 aws fargate 容器,这些容器是使用
ecs-cli
启动的,如下所示:
ecs-cli configure --cluster ${cluster_name} --default-launch-type FARGATE --region ${AWS_REGION} --config-name ${config_name}
ecs-cli compose --project-name ${service_name} service up
我想使用
aws ecs execute
在这些容器中运行命令。我能找到的关于该主题的所有示例都使用 aws
命令行工具,而不是 ecs-cli
。文档指定,为了使用 aws ecs execute
,您必须使用 --enable-execute-command
启动服务。
此标志确实存在于
aws
命令行工具中 - 但 ecs-cli
没有相应的标志。 ecs-cli
通过 yaml 文件 ecs-params.yml
配置。但是,文档再次没有提及可在该文件中使用来启用执行命令的设置。这就是我的 ecs-params.yml
的样子:
version: 1
task_definition:
task_execution_role: ecsTaskExecutionRole
ecs_network_mode: awsvpc
task_size:
mem_limit: 4GB
cpu_limit: 2vCPU
run_params:
network_configuration:
awsvpc_configuration:
subnets:
- ${AWS_VPC_SUBNET_A}
- ${AWS_VPC_SUBNET_B}
security_groups:
- ${AWS_VPC_SECURITY_GROUP}
assign_public_ip: ENABLED
我在
ecs-cli
存储库上找到了一个拉取请求,将此标志添加到命令行(请参阅:https://github.com/aws/amazon-ecs-cli/pull/1135),这确认了它是目前不可用。但这并不意味着没有其他方法来指定启用执行命令。
作为替代方法,我尝试在容器启动后使用
aws ecs update-service --enable-execute-command
。然而,这不起作用,因为一旦容器启动,相应的任务定义就会被标记为非活动状态。
所以我的问题是:如何在使用
ecs-cli
启动的容器中启用命令执行?
上述命令中的一些错误
aws ecs update-service --service $SERVICE_NAME --cluster $CLUSTER \
--region eu-west-1 \
--enable-execute-command \
--force-new-deployment
尝试强制进行新的部署,这对我来说是成功的。
aws ecs update-service <SERVICE_NAME> --cluster <CLUSTER_NAME> \
--enable-execute-command \
--force-new-deployment
我找到了一个可能的解决方法。想法是从“服务启动”切换到直接任务运行。
首先,从 compose 配置创建任务定义,但不要运行它:
ecs-cli compose create...
从创建的定义运行新任务后(最后一个参数将起作用):
aws ecs run-task --cluster $cluser --task-definition $task --enable-execute-command
它将起作用,您将能够通过以下方式连接到容器:
aws ecs execute-command...
当您想在正在运行的任务中启用“execute_command”时,我正在研究这个问题,您需要强制进行新的部署。根据文档,在“使用 ECS Exec 的注意事项”部分中,它说“您无法为现有任务启用 ECS Exec,只能为新任务启用它。”因此,您需要强制进行新的部署。您可以在此链接上找到有关此内容的更多信息:https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ecs-exec.html.
这段代码对我有用:
aws ecs update-service --cluster {cluster} --service {service} --region {region} --enable-execute-command --force-new-deployment
我的工作命令:
aws ecs update-service \
--cluster <Name-Of-Cluster> \
--task-definition <Name-Of-TaskDefination:$Version> \
--service <Name-Of-Service \
--enable-execute-command \
--force-new-deployment \
--region <Region-code, ex. us-east-1>