Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we measure the vCPU usage for Amazon ECS/Ec2 instances?

I am going to migrate my webapp from Amazon EC2 to ECS. (docker) but in ECS, we need to allocate memory and vCPU for the process.

But i am not sure how much vCPU should be assigned for the task. ( as well as memory)

How can i measure how much vCPU and memory is needed for a process?

Thanks

like image 988
TheOneTeam Avatar asked May 18 '18 02:05

TheOneTeam


People also ask

What is ECS vCPU?

Amazon ECS uses a standard unit of measure for CPU resources called CPU units. 1024 CPU units is the equivalent of 1 vCPU. For example, 2048 CPU units is equal to 2 vCPU. Note: When defining task definitions, you can also use 1 vCPU instead of 1024.

How do I find my vCPU quota AWS?

(Optional) To view the instances that are already running, from the EC2 Dashboard, under Resources, choose Instances (running). In the navigation pane, Choose Limits. Choose Calculate vCPU limit to access the vCPU limits calculator.

Which tool would you use to monitor the CPU usage of an EC2 instance?

Use Task Manager to identify the source of high CPU utilization.


2 Answers

When it comes to your task definition, there are two ways of specifying Memory. The memory setting is a hard limit. If the containers memory usage hits this amount, the container will be terminated. If on the other hand, you specify memoryReservation, that much memory will be reserved for the task, but it can use more, up to the total amount of the machine. Check out the Task Definition documentation for further details.

An important consideration here is that only one of memory and memoryReservation are required. If both are used, memoryReservation should be less than memory. If you are only going to specify one of these, I'd recommend memoryReservation, as it will allow your task to use up to the total memory on the machine. If both are used, the memoryReservation will be used in calculating the amount of memory consumed by a task.

As for cpu, this value is not required, and if not specified, all tasks on an instance are allowed an equal portion of the CPU available on the system. If only one task is on the instance, it can use the entire CPU of the instance by default.

To get a little bit more concrete, 1vCPU/1GB Mem EC2 equals to 1024 on ECS.

It's safe to say that your service will roughly use the same CPU/Memory as it does on EC2. You can log in into your EC2 instance and check this with various tools (htop, ps, ...) or check this out on CloudWatch.

Example: If your current app runs on a t2.medium (Which has 2vCPU and 4GB mem) and uses only half of the cpu/memory, then your task would use 1024 CPU and 2048 Memory.

like image 154
ThomasVdBerge Avatar answered Sep 20 '22 23:09

ThomasVdBerge


To Answer your question about measuring: Your ECS will need at least as much CPU and memory as your current EC2. The best way to measure and monitor your EC2 CPU and memory usage is to use AWS CloudWatch. For your specific needs, this is a good news/bad news answer.

Good news first: The CPU usage metric is already tracked with CloudWatch automatically.

Follow these steps to see what your current CPU usage is as a graphed metric for your instance.

Go into your AWS console, then select CloudWatch under Management Tools. On the main CloudWatch screen, ensure you have the region where your EC2 instance is hosted selected in the top right of the console. Now click Metrics in the left and you will be presented with a timeline graph. Below the graph are the metrics you can choose from. Click EC2->Per Instnace Metrics-> CPUUtilization for the instance you want to view metrics for. Change the time to custom -> 4 weeks. This will give you a graph of the average cpu utilization for the past month for this instance.

I would suggest to look at the maximum CPUUtilization graph for the last month. This can give you an idea of the absolute top CPU needed to cover the same type of CPU activity in your new ECS, where an average metric might not be the best guide.

To view the maximum on the graph, in the bottom pane, click on the graphed metrics tab. In the statistic column, change the setting pull down for your row/instance from Average, to Maximum.

My graph for max CPU the last 4 weeks is shown here. You can see the top barely hits 25% of the CPU for T2.micro instance. This should help you determine what size and percent CPU you will need for ECS.

enter image description here

Now the bad news: monitoring memory usage with CloudWatch is not set up automatically like CPU is. In short, getting a memory metric involves using legacy perl scripts or the new recommended way of installing the CloudWatch agent. With the agent you can choose up to 10 memory metrics (listed here) tracked for Linux to report on and set up the correct IAM settings so the agent can report the memory metrics back to CloudWatch.

EDIT: I forgot that I had written an SO answer in 2016 with the steps to setup the memory monitoring scripts and an example dashboard with memory metrics displayed in CloudWatch. The answer mentions setting up Cloudwatch alarms, but the scripts that run and report to CloudWatch display memory usage. Please refer to that answer on how to set these up (just ignore the alarm part of the answer).

But as good DEVOPS practice you will want to set this up on all of your Linux instances (EC2 and ECS). This will allow you to have the memory metrics proactively and readily available from the console. You want to avoid the need to SSH in to check it. Once tracked, you can use the console to graph the memory usage over a period of time to determine how much you would need for your ECS migration.

Hope this helps.

like image 37
Taterhead Avatar answered Sep 21 '22 23:09

Taterhead