-
Notifications
You must be signed in to change notification settings - Fork 2
/
phonebook-docker-swarm-cfn-template.yml
309 lines (290 loc) · 10.7 KB
/
phonebook-docker-swarm-cfn-template.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
AWSTemplateFormatVersion: 2010-09-09
Description: >
This CloudFormation Template deploys Phonebook Application to five Docker Machines on EC2 Instances
which are ready for Docker Swarm operations.
Parameters:
KeyPairName:
Type: AWS::EC2::KeyPair::KeyName
Description: Enter the name of your Key Pair for SSH connections.
ConstraintDescription: Must be one of the existing EC2 KeyPair
Resources:
InstanceConnectPolicy:
Type: "AWS::IAM::ManagedPolicy"
Properties:
PolicyDocument: #required
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- ec2-instance-connect:SendSSHPublicKey
Resource:
- !Sub arn:aws:ec2:${AWS::Region}:${AWS::AccountId}:instance/*
Condition: { "StringEquals": { "ec2:osuser": "ec2-user" } }
- Effect: Allow
Action:
- ec2:DescribeInstances
Resource: "*"
EC2InstanceConnect:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Statement:
- Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- "sts:AssumeRole"
ManagedPolicyArns:
- !Ref InstanceConnectPolicy
- arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryFullAccess
EC2ConnectProfile:
Type: "AWS::IAM::InstanceProfile"
Properties:
Roles: #required
- !Ref EC2InstanceConnect
DockerMachinesSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH and HTTP for Docker Machines
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 2377
ToPort: 2377
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 7946
ToPort: 7946
CidrIp: 0.0.0.0/0
- IpProtocol: udp
FromPort: 7946
ToPort: 7946
CidrIp: 0.0.0.0/0
- IpProtocol: udp
FromPort: 4789
ToPort: 4789
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 8080
ToPort: 8080
CidrIp: 0.0.0.0/0
DockerSwarmManagerLT:
Type: "AWS::EC2::LaunchTemplate"
Properties:
LaunchTemplateData:
ImageId: ami-0947d2ba12ee1ff75
InstanceType: t2.micro
KeyName: !Ref KeyPairName
IamInstanceProfile:
Arn: !GetAtt EC2ConnectProfile.Arn
SecurityGroupIds:
- !GetAtt DockerMachinesSecurityGroup.GroupId
UserData:
Fn::Base64: !Sub |
#! /bin/bash
yum update -y
amazon-linux-extras install docker -y
systemctl start docker
systemctl enable docker
usermod -a -G docker ec2-user
curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" \
-o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
yum install python3 -y
pip3 install ec2instanceconnectcli
eval "$(mssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
--region ${AWS::Region} ${DockerManager1} docker swarm join-token manager | grep -i 'docker')"
# uninstall aws cli version 1
rm -rf /bin/aws
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
./aws/install
yum install amazon-ecr-credential-helper -y
mkdir -p /home/ec2-user/.docker
cd /home/ec2-user/.docker
echo '{"credsStore": "ecr-login"}' > config.json
DockerSwarmWorkerLT:
Type: "AWS::EC2::LaunchTemplate"
Properties:
LaunchTemplateData:
ImageId: ami-0947d2ba12ee1ff75
InstanceType: t2.micro
KeyName: !Ref KeyPairName
IamInstanceProfile:
Arn: !GetAtt EC2ConnectProfile.Arn
SecurityGroupIds:
- !GetAtt DockerMachinesSecurityGroup.GroupId
UserData:
Fn::Base64: !Sub |
#! /bin/bash
yum update -y
amazon-linux-extras install docker -y
systemctl start docker
systemctl enable docker
usermod -a -G docker ec2-user
curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" \
-o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
yum install python3 -y
pip3 install ec2instanceconnectcli
eval "$(mssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no \
--region ${AWS::Region} ${DockerManager1} docker swarm join-token worker | grep -i 'docker')"
# uninstall aws cli version 1
rm -rf /bin/aws
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
./aws/install
yum install amazon-ecr-credential-helper -y
mkdir -p /home/ec2-user/.docker
cd /home/ec2-user/.docker
echo '{"credsStore": "ecr-login"}' > config.json
DockerManager1:
Type: AWS::EC2::Instance
DependsOn:
- DockerMachinesSecurityGroup
- InstanceConnectPolicy
- EC2InstanceConnect
Properties:
ImageId: ami-0947d2ba12ee1ff75
InstanceType: t2.micro
KeyName: !Ref KeyPairName
IamInstanceProfile: !Ref EC2ConnectProfile
SecurityGroupIds:
- !GetAtt DockerMachinesSecurityGroup.GroupId
Tags:
- Key: Name
Value: !Sub Docker Grand Master Manager 1st of ${AWS::StackName}
UserData:
Fn::Base64: !Sub
- |
#! /bin/bash
yum update -y
hostnamectl set-hostname Grand-Master
amazon-linux-extras install docker -y
systemctl start docker
systemctl enable docker
usermod -a -G docker ec2-user
curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" \
-o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker swarm init
aws ecr get-login-password --region ${AWS::Region} | docker login --username AWS --password-stdin ${ECR_REGISTRY}
docker service create \
--name=viz \
--publish=8080:8080/tcp \
--constraint=node.role==manager \
--mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock \
dockersamples/visualizer
yum install git -y
# uninstall aws cli version 1
rm -rf /bin/aws
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
./aws/install
yum install amazon-ecr-credential-helper -y
mkdir -p /home/ec2-user/.docker
cd /home/ec2-user/.docker
echo '{"credsStore": "ecr-login"}' > config.json
aws ecr create-repository \
--repository-name ${APP_REPO_NAME} \
--image-scanning-configuration scanOnPush=false \
--image-tag-mutability MUTABLE \
--region ${AWS::Region}
docker build --force-rm -t "${ECR_REGISTRY}/${APP_REPO_NAME}:latest" ${GITHUB_REPO}
docker push "${ECR_REGISTRY}/${APP_REPO_NAME}:latest"
mkdir -p /home/ec2-user/phonebook
cd /home/ec2-user/phonebook
cat << EOF | tee .env
ECR_REGISTRY=${ECR_REGISTRY}
APP_REPO_NAME=${APP_REPO_NAME}
EOF
curl -o "docker-compose.yml" -L ${GIT_FILE_URL}docker-compose.yml
curl -o "init.sql" -L ${GIT_FILE_URL}init.sql
docker-compose config | docker stack deploy --with-registry-auth -c - phonebook
- ECR_REGISTRY: !Sub ${AWS::AccountId}.dkr.ecr.${AWS::Region}.amazonaws.com
APP_REPO_NAME: infraform-repo/phonebook-app
GITHUB_REPO: https://github.com/infraform/docker-swarm-elastic-container-app.git
GIT_FILE_URL: https://raw.githubusercontent.com/infraform/docker-swarm-elastic-container-app/master/
DockerManager2:
Type: AWS::EC2::Instance
DependsOn:
- DockerManager1
Properties:
LaunchTemplate:
LaunchTemplateId: !Ref DockerSwarmManagerLT
Version: !GetAtt DockerSwarmManagerLT.LatestVersionNumber
Tags:
- Key: Name
Value: !Sub Docker Manager 2nd of ${AWS::StackName}
DockerManager3:
Type: AWS::EC2::Instance
DependsOn:
- DockerManager2
Properties:
LaunchTemplate:
LaunchTemplateId: !Ref DockerSwarmManagerLT
Version: !GetAtt DockerSwarmManagerLT.LatestVersionNumber
Tags:
- Key: Name
Value: !Sub Docker Manager 3rd of ${AWS::StackName}
DockerWorker1:
Type: AWS::EC2::Instance
DependsOn:
- DockerManager1
Properties:
LaunchTemplate:
LaunchTemplateId: !Ref DockerSwarmWorkerLT
Version: !GetAtt DockerSwarmWorkerLT.LatestVersionNumber
Tags:
- Key: Name
Value: !Sub Docker Worker 1st of ${AWS::StackName}
DockerWorker2:
Type: AWS::EC2::Instance
DependsOn:
- DockerWorker1
Properties:
LaunchTemplate:
LaunchTemplateId: !Ref DockerSwarmWorkerLT
Version: !GetAtt DockerSwarmWorkerLT.LatestVersionNumber
Tags:
- Key: Name
Value: !Sub Docker Worker 2nd of ${AWS::StackName}
Outputs:
1stDockerManagerDashboard:
Description: Dashboard for Docker Swarm Cluster
Value: !Sub
- http://${PublicAddress}:8080
- PublicAddress: !GetAtt DockerManager1.PublicDnsName
1stDockerManagerDNSName:
Description: Docker Manager 1st DNS Name
Value: !Sub
- ${PublicAddress}
- PublicAddress: !GetAtt DockerManager1.PublicDnsName
2ndDockerManagerDNSName:
Description: Docker Manager 2nd DNS Name
Value: !Sub
- ${PublicAddress}
- PublicAddress: !GetAtt DockerManager2.PublicDnsName
3rdDockerManagerDNSName:
Description: Docker Manager 3rd DNS Name
Value: !Sub
- ${PublicAddress}
- PublicAddress: !GetAtt DockerManager3.PublicDnsName
1stDockerWorkerDNSName:
Description: Docker Worker 1st DNS Name
Value: !Sub
- ${PublicAddress}
- PublicAddress: !GetAtt DockerWorker1.PublicDnsName
2ndDockerWorkerDNSName:
Description: Docker Worker 2nd DNS Name
Value: !Sub
- ${PublicAddress}
- PublicAddress: !GetAtt DockerWorker2.PublicDnsName