ECS Cluster
- Clusters are Region-specific.
- Logical Grouping of Tasks and Services .
- A cluster may contain a mix of tasks hosted on AWS Fargate, Amazon EC2 instances, or external instances.
- ECS Agent (Docker Container) allows container instances to connect to your cluster.
- ECS container agent is included in the Amazon ECS-optimized AMIs, but you can also install it on any Amazon EC2 instance that supports the Amazon ECS specification.
- ECS container agent is only supported on Amazon EC2 instances.
ECS Cluster Setup
Step1:-Create A Role for the service -> Elastic Container Service
It will show below use case
Select your use case as per predefined policies
- EC2 Role for Elastic Container Service:-Allows EC2 instances in an ECS cluster to access ECS.
Policy:- AmazonEC2ContainerServiceforEC2Role
Role Name:-ecs_ec2_Role1 - Elastic Container Service:-Allows ECS to create and manage AWS resources on your behalf.
Policy:- AmazonEC2ContainerServiceRole
Role Name:-ecs_resource_Role2 - Elastic Container Service Autoscale:-Allows Auto Scaling to access and update ECS services.
Policy:- AmazonEC2ContainerServiceAutoscaleRole
Role Name:-ecs_autoscaling_Role3 - Elastic Container Service Task:-Allows ECS tasks to call AWS services on your behalf.
Policy:- AmazonECSTaskExecutionRolePolicy
Role Name:-ecs_task_Role4
Step2:- Create AWS Infrastructure
Create a cloudformation file to create the following VPC Infrastructure
- VPC
- 2 subnets in 2 different AZs
- Internet Gateway
- Routing Tables
Create below cloudformation file to setup above infrastructure. file name is core-infrastructure-setup.yml
AWSTemplateFormatVersion: '2010-09-09'
Description: VPC and subnets as base for an ECS cluster
Parameters:
EnvironmentName:
Type: String
Default: ecs-course
Mappings:
SubnetConfig:
VPC:
CIDR: '172.16.0.0/16'
PublicOne:
CIDR: '172.16.0.0/24'
PublicTwo:
CIDR: '172.16.1.0/24'
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
EnableDnsSupport: true
EnableDnsHostnames: true
CidrBlock: !FindInMap ['SubnetConfig', 'VPC', 'CIDR']
PublicSubnetOne:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone:
Fn::Select:
- 0
- Fn::GetAZs: {Ref: 'AWS::Region'}
VpcId: !Ref 'VPC'
CidrBlock: !FindInMap ['SubnetConfig', 'PublicOne', 'CIDR']
MapPublicIpOnLaunch: true
PublicSubnetTwo:
Type: AWS::EC2::Subnet
Properties:
AvailabilityZone:
Fn::Select:
- 1
- Fn::GetAZs: {Ref: 'AWS::Region'}
VpcId: !Ref 'VPC'
CidrBlock: !FindInMap ['SubnetConfig', 'PublicTwo', 'CIDR']
MapPublicIpOnLaunch: true
InternetGateway:
Type: AWS::EC2::InternetGateway
GatewayAttachement:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref 'VPC'
InternetGatewayId: !Ref 'InternetGateway'
PublicRouteTable:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref 'VPC'
PublicRoute:
Type: AWS::EC2::Route
DependsOn: GatewayAttachement
Properties:
RouteTableId: !Ref 'PublicRouteTable'
DestinationCidrBlock: '0.0.0.0/0'
GatewayId: !Ref 'InternetGateway'
PublicSubnetOneRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnetOne
RouteTableId: !Ref PublicRouteTable
PublicSubnetTwoRouteTableAssociation:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PublicSubnetTwo
RouteTableId: !Ref PublicRouteTable
Outputs:
VpcId:
Description: The ID of the VPC that this stack is deployed in
Value: !Ref 'VPC'
Export:
Name: !Sub ${EnvironmentName}:VpcId
PublicSubnetOne:
Description: Public subnet one
Value: !Ref 'PublicSubnetOne'
Export:
Name: !Sub ${EnvironmentName}:PublicSubnetOne
PublicSubnetTwo:
Description: Public subnet two
Value: !Ref 'PublicSubnetTwo'
Export:
Name: !Sub ${EnvironmentName}:PublicSubnetTwo
Command to create above cloudformation stack
aws cloudformation create-stack --capabilities CAPABILITY_IAM --stack-name ecs-core-infrastructure --template-body file://./core-infrastructure-setup.yml
Step 3:- Setup a Cluster - EC2 Instance
- Create Cluster ( by Clicking on Cluster and then click on Create Cluster Button )
- Select cluster template:-> Select EC2 Linux + Networking
- Provide Following Details to template
1. Provide Cluster name ( I am giving cluster name as mycluster )
2. Provisioning Model :- On-Demand Instance
3. EC2 instance type* :- t2.small ( you can change as per your requirement)
4. Number of instances*:- 1 ( you can change as per your requirement)
5. EC2 AMI ID*:- AMAZON LINUX 2 AMI
6. Root EBS Volume Size (GiB):- 30 GB
7. Key pair : (Select any valid key pair)
8. VPC : (Select the VPC which get created above with cloud formation)
9. Subnet: (Select the subnet which get created for VPC mentioned in previous step)
10. Auto assign public IP : Use Subnet Settings
11. Security group: Create new security group
12. Security group inbound rules CIDR:- 0.0.0.0/0 Port Range:- 80
13. Select IAM Role ecs_ec2_Role1 (Which we have created for ECS earlier)
14. Click on Create Button
15. Wait for Cluster to Create and eventually it will be created
Step 4: Setup a Cluster - Fargate
- Create Cluster ( by Clicking on cluster and then click on Create Cluster Button)
- Select cluster template:- Select Networking only
- Cluster Name:- mycluster-fargate
Click on Create Button to create cluster - Select Task Definition
- Select Launch Type as Fargate and Click on Next Step
- Fill the following Details related to Task Definition
1. Task Definition Name :- Task Name ( I am creating Task1)
2. Task Role :- ecs_task_Role4 ( This role was created on the top)
3. Task execution role :- ecs_task_Role4 ( This role was created on the top)
4. Task memory (GB) :- Memory used by container ( I am taking 0.5 GB because I am going to run nginx service)
5. Task CPU (vCPU):- Cpu Capcity ( I am taking 0.25)
6. Click on Add Container and give image name ( I am using nginx ) and container port as well as the security group
7. Click on Create Button to Create Task Definition. - Select Fargate Cluster ( in my case it is mycluster-fargate)
- Click on Run new task Button
- Add Following Details to the Task
1. Launch Type:- Fargate
2. Task Definition:- Select the Task definition which you have created in step5 (Task1 )
3. Number of tasks:- 1 ( you can select with your choice or requirement)
4. VPC :- Select the VPC which you have created for cluster
5. Subnet:- select all the subnet under this VPC
6. Click on Run Task Button
7. Task will be create and Running with public IP address
8. Use public IP address and check on browser that nginx page is accessible or not - Select Fargate Cluster ( in my case it is mycluster-fargate)
- Click on Services and Click on Create Button
- Add Following Details to the Task
1. Launch Type:- Fargate
2. Task Definition:- Select the Task definition which you have created in step5 (Task1 )
3. Number of tasks:- 2 ( you can select with your choice or requirement)
4. Service Name:- nginx-service
5. Service Type:- Replica
6. VPC :- Select the VPC which you have created for cluster
7. Subnet:- select all the subnet under this VPC
8. Click on Create Service
9. Task will be create and Running with public IP address
10. Use public IP address and check on browser that nginx page is accessible or not
COMMENTS