使用ECS在AWS上运行Docker容器
ECS代表弹性容器服务。
它是可以运行docker容器的托管容器服务。
尽管AWS还使用Kubernetes(EKS)提供了容器管理,但它也有其专有的解决方案(ECS)。
该教程将涵盖以下内容:创建ECS群集,提供镜像注册表(ECR)并将docker镜像推送到注册表,使用任务和服务定义将容器部署到群集,创建管道以更新ECS群集上运行的服务。
本教程将介绍如何创建ECS集群。
要求/准备工作
在继续阅读本教程之前,读者/用户必须具有:
一个AWS账户。
在该账户上创建一个用户,以允许对该账户进行资源调配;生成一个EC2 SSH密钥对;创建一个VPC和子网(可选)。
在以下链接上找到:使用CloudFormation创建AWS VPC
创建AWS ECS集群
我们可以创建两种类型的ECS集群。
带Fargate集群的ECS:该集群允许用户运行容器,而无需担心配置和管理EC2实例/服务器。
它基本上是一种无服务器的体系结构。
带有EC2实例集群的ECS:该集群允许用户在预配置的EC2实例上运行容器。
因此需要管理服务器。
使用Fargate选项创建AWS ECS集群
要创建集群,我们可以使用CloudFormation或者Terraform手动或者自动进行。
对于本文,我将使用CloudFormation手动创建集群。
CloudFormation:
下面的CloudFormation模板创建了一个具有容量提供商的ECS集群,并启用了Fargate和Container Insights。
读者应修改模板设置以自定义其特定要求。
更改的主要方面将是:标签。
群集设置。
AWSTemplateFormatVersion: "2010-09-09"
Description: "Create ECS fargate cluster"
Parameters:
Name:
Type: String
Description: The name of the ECS Cluster
Resources:
ECSCluster:
Type: 'AWS::ECS::Cluster'
Properties:
ClusterName: !Ref Name
ClusterSettings:
- Name: containerInsights
Value: enabled
CapacityProviders:
- FARGATE
Tags:
-
Key: "Name"
Value: "test-ecs"
-
Key: "CreatedBy"
Value: "Maureen Barasa"
-
Key: "Environment"
Value: "test"
Outputs:
ECS:
Description: The created ECS Cluster
Value: !Ref ECSCluster
手动:
要手动创建集群,请执行以下步骤。
在Elastic Container Services控制台上,单击创建集群。
然后,对于群集模板,选择Powered by Fargate作为选项。
单击下一步。
最后,配置群集设置,然后单击创建群集。
N/B:我们可以为集群创建一个新的VPC。
但这是可选的。
现在,ECS Fargate集群正在运行。
使用EC2选件创建AWS ECS集群
我们还可以使用CloudFormation,Terraform或者手动执行此操作。
CloudFormation:
对于CloudFormation,请使用以下模板。
AWSTemplateFormatVersion: "2010-09-09"
Description: "Create ECS fargate cluster"
Parameters:
Name:
Type: String
Description: The name of the ECS Cluster
VPC:
Type: String
Description: The vpc to launch the service
Default: vpc-ID
PrivateSubnet01:
Type: String
Description: The subnet where to launch the ecs instances
Default: subnet-ID
PrivateSubnet02:
Type: String
Description: The subnet where to launch the ecs instances
Default: subnet-ID
InstanceType:
Type: String
Description: The EC2 instance type
Default: "t2.micro"
MinSize:
Type: String
Description: The subnet where to launch the ec2
Default: 1
MaxSize:
Type: String
Description: The subnet where to launch the ec2
Default: 2
DesiredSize:
Type: String
Description: The subnet where to launch the ec2
Default: 1
Resources:
IAMInstanceRole:
Type: 'AWS::IAM::Role'
Properties:
Description: The ECS Instance Role
RoleName: ecsInstanceRole2
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role
- arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy
- arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
Tags:
-
Key: "Environment"
Value: "test"
-
Key: "createdBy"
Value: "Maureen Barasa"
-
Key: "Name"
Value: "ecsInstanceRole2"
IAMInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
InstanceProfileName: ecsInstanceRole2
Roles:
- !Ref IAMInstanceRole
ECSSecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: "Security Group to control access to the ECS cluster"
GroupName: "test-ECS-SG"
VpcId: !Ref VPC
SecurityGroupIngress:
-
CidrIp: 0.0.0.0/0
FromPort: 80
IpProtocol: "tcp"
ToPort: 80
-
CidrIp: 0.0.0.0/0
FromPort: 443
IpProtocol: "tcp"
ToPort: 443
Tags:
-
Key: "Name"
Value: "test-ECS-SG"
-
Key: "CreatedBy"
Value: "Maureen Barasa"
-
Key: "Environment"
Value: "test"
ECSCluster:
Type: 'AWS::ECS::Cluster'
Properties:
ClusterName: !Ref Name
ClusterSettings:
- Name: containerInsights
Value: enabled
Tags:
-
Key: "Name"
Value: "test-ecs"
-
Key: "CreatedBy"
Value: "Maureen Barasa"
-
Key: "Environment"
Value: "test"
ECSAutoScalingGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
VPCZoneIdentifier:
- !Ref PrivateSubnet01
- !Ref PrivateSubnet02
LaunchConfigurationName: !Ref LaunchConfiguration
MinSize: !Ref MinSize
MaxSize: !Ref MaxSize
DesiredCapacity: !Ref DesiredSize
HealthCheckGracePeriod: 300
Tags:
-
Key: "Name"
Value: "test-ecs"
PropagateAtLaunch: true
-
Key: "CreatedBy"
Value: "MaureenBarasa"
PropagateAtLaunch: true
-
Key: "Environment"
Value: "test"
PropagateAtLaunch: true
LaunchConfiguration:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
ImageId: "ami-ID"
SecurityGroups:
- !Ref ECSSecurityGroup
InstanceType: !Ref InstanceType
IamInstanceProfile: !Ref IAMInstanceProfile
KeyName: "test-key"
UserData:
Fn::Base64: !Sub |
#!/bin/bash
echo ECS_CLUSTER=test-ecs >> /etc/ecs/ecs.config;echo ECS_BACKEND_HOST= >> /etc/ecs/ecs.config;
BlockDeviceMappings:
-
DeviceName: "/dev/xvda"
Ebs:
Encrypted: false
VolumeSize: 20
VolumeType: "gp2"
DeleteOnTermination: true
Outputs:
IAMProfile:
Description: The created EC2 Instance Role
Value: !Ref IAMInstanceProfile
AutoScalingGroup:
Description: The ECS Autoscaling Group
Value: !Ref ECSAutoScalingGroup
ECS:
Description: The created ECS Cluster
Value: !Ref ECSCluster
在模板的参数部分,输入集群的特定自定义输入。
其中包括:VPC和子网。
实例类型和Auto Scaling组的扩展要求。
在资源部分下。
请注意自定义:资源名称和标签。
EC2启动配置的密钥名称(使用我们生成的密钥对)。
EC2实例的AMI-ID。
(在账户上使用经过Amazon ECS优化的AMI)。
在CloudFormation完成执行堆栈之后,我们现在应该拥有一个带有已注册的活动EC2容器实例的ECS集群。
见下文:
手动:
要手动创建集群,请执行以下步骤:使用以下AWS托管策略创建ECS实例角色:AmazonS3ReadOnlyAccessCloudWatchAgentServerPolicyAmazon EC2ContainerServiceforEC2Role编辑角色信任关系并添加以下JSON信任策略。
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
然后,在Elastic Container Service控制台上,单击"创建集群"。
这次在选择集群模板窗口上,而不是选择Powered by Fargate,而是选择EC2 Linux + Networking选项。
单击下一步。
接下来,我们需要配置集群。
关于集群配置,我们有3个主要部分:集群名称实例配置网络集群名称:
实例配置:
确保根据要求选择正确的实例配置。
其中有一个选项可以创建一个新的VPC,如果用户尚未创建自己的子网,则可以创建子网。
如果他们已经创建了网络体系结构,则读者可以使用现有的设置。
这同样适用于群集的安全组。
可以创建一个新角色,也可以选择一个现有角色。
然后,对于容器实例" IAM角色",选择我们在上面创建的角色。
通过选中启用容器洞察力旁边的框来启用容器洞察力。
最后单击,创建集群。
我们将在数分钟内启动并运行群集。

