Did you know that you can navigate the posts by swiping left and right?

Does a docker container see multiple cpu's on AWS ECS?

05 Apr 2018 . category: internals . Comments
#aws #docker #terraform

General

Imagine that we have a docker container running on AWS ECS. The host machine has a total of 4 cores available. The ECS task allows usage of 4096 CPU units. How would the docker container see the CPU? As 4 CPU’s, or as one big CPU?

I imagine that the AWS ECS Agent uses the docker --cpuset-cpus parameter to limit the amount of CPU’s that can be used.

The easiest way to find an answer to our question is to simply try it out! To try this out, we will use the code of one of the previous blogposts that can be found on github. We will make a few changes to that code.

  • In the variables file, change the value for aws_instance_type to the following:
    aws_instance_type="m5.xlarge"
  • Alter the aws_ecs_task_cpu block to the following:
    variable "aws_ecs_task_cpu" {
      description = ""
      type = "map"
      default = {
        t2.nano = 1024
        t2.micro = 1024
        t2.small = 1024
        m5.xlarge = 4096
      }
    }

That’s it! Apply the terraform plan to spin everything up. Once everything is online, ssh into the machine and execute docker ps. This will give us an overview of all running containers.

docker-ps

As we can see in the screeshot, the container id is 0c0bb92e4013. Write it down and go inside the docker container. This can be done with the command docker exec -it 0c0bb92e4013 /bin/bash.

Once inside the docker container, install htop with the command yum install -y htop. Afterwards, execute the command htop. This will give us the following result:

htop

This clearly shows us that the 4 cores are perfectly mapped, 1 on 1. But what if we adjust the settings again to something less arbitrary.

  • Alter the aws_ecs_task_cpu block to the following:
    variable "aws_ecs_task_cpu" {
      description = ""
      type = "map"
      default = {
        t2.nano = 1024
        t2.micro = 1024
        t2.small = 1024
        m5.xlarge = 3500
      }
    }

Apply terraform again and go inside the docker container again. Make note that the container id will have changed from last time. Install htop again and execute it. htop

There is no change! ECS equally devides it. The formula for this would look a bit like this: (CPUUNITS/CEIL(CPUUNITS/1024)).

Conclusion

AWS ECS makes it very easy for us to deploy new docker containers. We must however make sure that each and every docker container we want to run, has software running on it which supports multiple CPU’s (multi threaded).


Me

Thomas Van den Berge is a DevOps Cloud Engineer/Senior Backend Developer based in Belgium near the beautiful city of Ghent. In his spare time he likes to spend time with his daughter, work on his fishtanks and travel.