Followers

Create ECS Cluster Step by Step

 

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.

image

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
  • Step 5: Create Task Definition -Fargate

    • 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.

    Step 6: Create Task

    • 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

    Step 7: Create Service

    • 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

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,git quiz,1,Git Worksheet,1,ITIL,1,ITSM,1,Jira,3,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SDLC Quiz,5,SonarQube,3,Splunk,2,vagrant kubernetes,1,Windows,1,YAML Basics,1,
ltr
static_page
DevOpsWorld: Create ECS Cluster Step by Step
Create ECS Cluster Step by Step
DevOpsWorld
https://www.devopsworld.co.in/p/create-ecs-cluster-step-by-step.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/p/create-ecs-cluster-step-by-step.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content