The key aspects of maintaining a container orchestration platform is to configure autoscaling of the services running on it. Here this blog is my experiences on that.
Setup:
Before I explain to you how to create a capacity provider, we need to create an ECS cluster. Follow the steps mentioned below to create a new AWS ECS cluster which we will use later:
- Log in to your AWS console using your admin account and search for ECS in the search bar.
- Click on Create cluster which will open the cluster creation wizard for you. This is where you can select if you want to use AWS Fargate (serverless) as the underlying infrastructure or if you want to manage your infrastructure using EC2.
- Select EC2 Linux + Networking as we want to demonstrate our capacity provider which will scale in or scale out our underlying infrastructure based on the rules we define later.
- Fill in the below details in the wizard and click next.
Cluster name: clusterDemo (or any name you want to use)
EC2 instance type: t2.micro (to use free tier eligible instance type)
Number of instances: 1
Key pair: "Select your key pair you want to use to login into this host"
VPC: Select existing VPC for this demo as you don’t need to create a new VPC for this exercise.
Subnets: Select the subnets you want these EC2 instances to use in case they increase in number. This will allow you to achieve high availability.
Security Group: Create new security group.
Container instance IAM role: ecsInstanceRole (This role has an IAM policy which allows ECS agents to communicate with the ECS cluster)
- After filling in the details mentioned above, click ‘Create’ and wait for the cluster to be created.
Task Definition:
After our ECS cluster has been provisioned, let's prepare another prerequisite to have a task definition ready which we can deploy on our cluster. To do this, follow the steps below:
- Open the ECS service console, and click on 'Task definitions' from the left menu.
- Click on 'Create new task definition' and enter the details given below:
task definition family: httpd-task
name: httpd-demo
Image URI: httpd:latest (this will pull image from docker hub)
- Select the app environment where this task will run. Select EC2 from the drop-down menu.
- Specify Memory size for this task: use a smaller value like .2 GB.
- Click 'Next' and then 'create'.
Creating a capacity provider
Now comes the main part which involves the creation of a capacity provider to manage our underlying EC2 infrastructure. To do this, follow the steps below:
- Before we create the capacity provider, make sure you are using the classic console UI as the new UI does not allow us to create a capacity provider yet.
- Click on 'Clusters' in the ECS dashboard and select the cluster we created above.
- Click 'Capacity Providers' and then click 'Create' and use the details given below:
Capacity provider name: clusterDemoCapacityProvider
Auto Scaling Group: Select the one from the drop-down which was created during ECS cluster creation. The name will start with EC2ContainerService-*.
Target Capacity %: 80 (this means that EC2 instances will be auto-scaled when they are more than 80% utilized)
Managed termination Protection: Disabled
- Click 'Create'.
- Now you will see that this provider has been successfully created.
- Update the ECS cluster to use the capacity provider we created.
- Open 'clusterDemo' cluster and click 'Update cluster'.
- Change default capacity provider strategy to use the provider we created.
Deploying
Now that we have our cluster configured, we can start deploying our tasks/containers and we can test if underlying EC2 instances are being scaled or not.
- To demonstrate this, first, go to the EC2 dashboard and go to ‘Auto Scaling Groups’, and locate our capacity provider’s ASG.
- Now, change the max capacity to 3 so that when this ASG scale-out EC2 instances, the max instances it is allowed to create is 3.
- Now, deploy the task definition we created earlier to run multiple httpd containers on this cluster. To do this, open the ECS cluster dashboard and click the cluster we created.
- Within the Services tab, click 'Create'.
- Use the details similar to the screenshot below. The key thing to note is that we are asking this service to run 15 instances of the task definition.
- Skip auto-scaling for the service as we just want to test auto-scaling for underlying EC2 instances.
- Create service.
- Now, if you click on 'View Service' you will notice that the ECS cluster is provisioning the task instances we defined i.e. 15 in total.
Finally, to see if our capacity provider provisioned the EC2 instances needed to host 15 instances of our task, go to the EC2 dashboard and verify that you see 3 EC2 instances created by the provider.
Clean up
To make sure you are not charged and do not cross the allowed free tier quota, delete all the resources created in this demo i.e. ECS capacity provider, cluster, Auto Scaling Group, and EC2 instances from EC2 dashboard, etc.
That's it! Thanks for reading!