How to force AWS ECS migrate containers to another ASG?

前端 未结 2 452
Happy的楠姐
Happy的楠姐 2021-01-07 04:45

I\'m using user_data to do initial configuration of the hosts used in ECS to run containers. I want to be able to tell AWS ECS to migrate containers to a newly created hosts

2条回答
  •  伪装坚强ぢ
    2021-01-07 05:32

    I don't think ECS has a built-in way to do this. As a result, it usually requires a fairly tedious & manual process (albeit one that could be scripted). There are a few different ways to do it, but this is typically the simplest one:

    1. Make your change to user_data.
    2. Run terraform apply.
    3. For each EC2 Instance in your ASG that has the old user_data:
      1. Terminate that EC2 Instance. You can do this via the AWS CLI or via the EC2 web console.
      2. After a little while, the ASG will automatically launch a new EC2 Instance, with your new user_data, to replace the terminated EC2 Instance.
      3. After a little while, ECS will automatically launch new copies of any ECS Tasks that happened to be running on the terminated EC2 Instance.

    Once you have gone through this process, all of the Instances in the ASG will be running the new user_data. Note that this can be done with zero-downtime for your ECS Tasks as long as:

    1. There are at least 2 copies of each ECS Task, each one on a separate EC2 Instance in your ASG.
    2. You wait enough time between terminating EC2 Instances for the ECS Task(s) to relaunch.

    If you can't meet those requirements, then you may have some downtime, or you may need to pursue a messier option that involves doubling the size of the ASG, waiting for the new EC2 Instances (which will have the new user_data) to deploy in the ASG, doubling the number of ECS Tasks, waiting for those new ECS Tasks to deploy (they will typically deploy onto the new EC2 Instances), and then reducing each by half again (in theory, the old ECS Tasks and old EC2 Instances will be terminated, leaving just the new ones behind).

提交回复
热议问题