-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathMongoDbArbiterServer.template
372 lines (333 loc) · 16.5 KB
/
MongoDbArbiterServer.template
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "MongoDB Arbiter Server (Amazon Linux)",
"Parameters": {
"KeyName": {
"Description": "The name of an existing key pair to enable SSH access to Amazon EC2 instances",
"Type": "String",
"Default": ""
},
"InstanceType": {
"Description": "MongoDB Node Type (EC2 Instance)",
"Type": "String",
"Default": "t2.micro",
"AllowedValues": [
"t2.micro",
"t2.small",
"t2.medium"
],
"ConstraintDescription": "Must be a valid Amazon EC2 instance type."
},
"AccessKeyId" : {
"Description" : "Access Key ID",
"Type" : "String",
"Default" : ""
},
"SecretAccessKey" : {
"Description" : "Secret Access Key for the specified Access Key ID",
"Type" : "String",
"Default" : ""
},
"ReplicaSetName" : {
"Description" : "Name of the MongoDB replica set e.g. s1r (shard 1 replica)",
"Type" : "String",
"Default" : "s1r"
},
"MongoArbiterVpcId": {
"Description": "The VPC of the MongoDB Arbiter Server",
"Type" : "AWS::EC2::VPC::Id",
"Default": ""
},
"MongoArbiterSubnetId": {
"Description": "The subnet of the MongoDB Arbiter Server",
"Type" : "AWS::EC2::Subnet::Id",
"Default": ""
},
"MongoArbiterGroupId": {
"Description": "The security group for the MongoDB Arbiter Server",
"Type": "AWS::EC2::SecurityGroup::Id",
"Default": ""
},
"UpdateRoute53": {
"Description": "Add record set to AWS Route53 service. Note: don't select this option if you want to update the Route53 table separately",
"Type": "String",
"Default": "True",
"AllowedValues": [ "True", "False" ]
},
"Route53HostedZone": {
"Description": "Route 53 hosted zone for updating internal DNS (Only applicable if the parameter [ UpdateRoute53 ] = \"true\"",
"Type": "AWS::Route53::HostedZone::Id",
"Default": ""
},
"InitMongoReplicaSet" : {
"Description": "Initilise the Replica-Set. This assumes that the 2 replica data nodes already exist. If the 'ReplicaSetName' = 's1r' this will expect 2 replica instances to exist and the internal DNS (via Route53) configed as `mongo-s1r1.<Route53HostedZone name>`, `mongo-s1r2.<Route53HostedZone name>` and `mongo-s1r-arb.<Route53HostedZone name>` (i.e. this server) ",
"Type": "String",
"Default": "True",
"AllowedValues": [ "True", "False" ]
}
},
"Mappings": {
"AWSInstanceType2Arch": {
"t2.micro" : { "Arch": "64HVM" },
"t2.small" : { "Arch": "64" },
"t2.medium" : { "Arch": "64" }
},
"AWSRegionArch2AMI": {
"eu-west-1" : { "64" : "ami-2918e35e", "64HVM" : "ami-4b18e33c" },
"us-east-1" : { "64" : "ami-fb8e9292", "64HVM" : "ami-978d91fe" },
"us-west-1" : { "64" : "ami-7aba833f", "64HVM" : "ami-5aba831f" }
}
},
"Resources": {
"MongoDbArbiterServer": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"Comment1" : "This creates an instance (generally a micro) and installs a MongoDB arbiter server",
"configSets" : {
"Install" : [ "Update-Route53", "Metric-Uploading-Config", "Install-MongoDB", "Init-MongoDB-RepliaSet" ]
},
"Update-Route53" : {
"files" : {
"/usr/local/bin/cli53" : {
"source" : "https://github.com/barnybug/cli53/releases/download/0.6.3/cli53-linux-amd64",
"mode" : "000755",
"owner" : "root",
"group" : "root"
},
"/tmp/update_route53.sh" : {
"content" : { "Fn::Join" : ["", [
"#!/bin/bash\n\n",
"PRIVATE_IP=`curl http://169.254.169.254/latest/meta-data/local-ipv4/`\n",
"UPDATE_ROUTE_53=", {"Ref": "UpdateRoute53" }, "\n",
"if [ $UPDATE_ROUTE_53 == 'True' ] ; then \n",
" /usr/local/bin/cli53 rrcreate ", {"Ref": "Route53HostedZone" },
" \"mongo-", { "Ref" : "ReplicaSetName" }, "-arb 300 A $PRIVATE_IP\" --replace --wait ; \n",
"fi\n",
"echo Route53 Update Finished!"
] ] },
"mode" : "000755",
"owner" : "root",
"group" : "root"
}
},
"commands" : {
"01_UpdateRoute53" : {
"command" : "/tmp/update_route53.sh > /tmp/update_route53.log 2>&1"
}
}
},
"Metric-Uploading-Config" : {
"packages" : {
"yum" : {
"perl-Sys-Syslog" : [],
"perl-Switch" : [],
"perl-URI" : [],
"perl-IO-Compress" : [],
"perl-Bundle-LWP" : []
}
},
"sources" : {
"/home/ec2-user" : "http://ec2-downloads.s3.amazonaws.com/cloudwatch-samples/CloudWatchMonitoringScripts-v1.1.0.zip"
},
"files" : {
"/home/ec2-user/crontab": {
"content" : { "Fn::Join" : ["", [
"*/1 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl --aws-iam-role=MonitorRole --mem-used --memory-units=megabytes --mem-util --disk-space-util --disk-space-used --disk-space-avail --disk-path=/","\n"
]]},
"mode" : "000600",
"owner" : "ec2-user",
"group" : "ec2-user"
}
},
"commands" : {
"01_YumUpdateMonitoring" : {
"command" : "yum update -y"
},
"02_MonitoringCron" : {
"command" : "chmod +x /home/ec2-user/aws-scripts-mon/*.pl && crontab -u ec2-user /home/ec2-user/crontab && rm /home/ec2-user/crontab"
}
}
},
"Install-MongoDB" : {
"packages" : {
"yum" : {
"mdadm" : [],
"sysstat" : []
}
},
"files" : {
"/etc/yum.repos.d/mongodb-org-3.0.repo" : {
"content" : { "Fn::Join" : ["", [
"[mongodb-org-3.0]\n",
"name=MongoDB Repository\n",
"baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.0/x86_64/\n",
"gpgcheck=0\n",
"enabled=1\n"
] ] },
"mode" : "000644",
"owner" : "root",
"group" : "root"
},
"/etc/mongod.conf" : {
"content" : { "Fn::Join" : ["", [
"net:\n",
" bindIp: 0.0.0.0\n",
" port: 27017\n",
" http:\n",
" enabled: true\n",
" RESTInterfaceEnabled: true\n\n",
"systemLog:\n",
" destination: file\n",
" path: /var/log/mongodb/mongod.log\n",
" logAppend: true\n\n",
"storage:\n",
" dbPath: /var/lib/mongo\n",
" journal:\n",
" enabled: false\n",
" mmapv1:\n",
" smallFiles: true\n\n",
"processManagement:\n",
" fork: true\n",
" pidFilePath: /var/run/mongodb/mongod.pid\n\n",
"replication:\n",
" replSetName: ", { "Ref" : "ReplicaSetName" }, "\n\n",
"sharding:\n",
" clusterRole: shardsvr\n"
] ] },
"mode" : "000644",
"owner" : "root",
"group" : "root"
}
},
"commands" : {
"01_YumUpdateMongo" : {
"command" : "yum update -y\n"
},
"02_InstallMongoDBServerAndClient" : {
"command" : "yum install -y mongodb-org-server mongodb-org-shell > /tmp/yum-mongo.log 2>&1\n"
}
},
"services" : {
"sysvinit" : {
"mongod" : { "enabled" : "true", "ensureRunning" : "true" }
}
}
},
"Init-MongoDB-RepliaSet" : {
"files" : {
"/tmp/init_mongo_replicaset.sh" : {
"content" : { "Fn::Join" : ["", [
"#!/bin/bash\n\n",
"# This script automates the initialisation of a MongoDB replica-set - see https://docs.mongodb.org/manual/reference/method/rs.initiate\n",
"# Assumes that the 2 main replica data nodes and internal DNS is configured properly.\n\n",
"INIT_MONGO_REPLICA_SET=", {"Ref": "InitMongoReplicaSet" }, "\n",
"ZONE_NAME=`aws route53 get-hosted-zone --id ", {"Ref": "Route53HostedZone" }, " | jp 'HostedZone.Name' | sed 's/\"//g' | sed 's/\\.$//'`\n",
"REPLICA_SET=", {"Ref": "ReplicaSetName" }, "\n\n",
"if [ $INIT_MONGO_REPLICA_SET == 'True' ] ; then \n",
"cat << END > /tmp/init_mongo_replicaset.js\n",
"var cfg = { \n",
" _id : \"${REPLICA_SET}\",\n",
" members : [\n",
" {_id : 0, host : \"mongo-${REPLICA_SET}1.$ZONE_NAME:27017\"},\n",
" {_id : 1, host : \"mongo-${REPLICA_SET}2.$ZONE_NAME:27017\"},\n",
" {_id : 2, host : \"mongo-${REPLICA_SET}-arb.$ZONE_NAME:27017\", arbiterOnly: true},\n",
" ]\n",
"}\n",
"var error = rs.initiate(cfg)\n",
"printjson(error);\n",
"END\n\n",
"mongo mongo-${REPLICA_SET}1.$ZONE_NAME:27017/admin /tmp/init_mongo_replicaset.js\n\n",
"fi\n",
"echo MongoDB ReplicaSet initialisation script finished!\n"
] ] },
"mode" : "000755",
"owner" : "root",
"group" : "root"
}
},
"commands" : {
"01_init_mongo_replica" : {
"command" : "/tmp/init_mongo_replicaset.sh > /tmp/init_mongo_replicaset.log 2>&1"
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"AWSRegionArch2AMI",
{ "Ref": "AWS::Region" },
{ "Fn::FindInMap": [
"AWSInstanceType2Arch",
{ "Ref": "InstanceType" },
"Arch"
]}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroupIds": [
{
"Ref" : "MongoArbiterGroupId"
}
],
"KeyName": {
"Ref": "KeyName"
},
"SubnetId": {
"Ref": "MongoArbiterSubnetId"
},
"IamInstanceProfile": "MonitorRole",
"Monitoring" : "true",
"Tags": [
{
"Key": "Stack",
"Value": "Production"
},
{
"Key": "Name",
"Value": { "Fn::Join" : ["", [
"Mongo-", { "Ref" : "ReplicaSetName" }, "-arb"
]]}
}
],
"UserData": { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash\n",
"yum update -y aws-cfn-bootstrap\n",
"## Error reporting helper function\n",
"function error_exit\n",
"{\n",
" /opt/aws/bin/cfn-signal --exit-code 1 --reason \"$1\" '", { "Ref" : "WaitHandleArbiterServer" }, "'\n",
" exit 1\n",
"}\n",
"## Initialize CloudFormati\n",
"/opt/aws/bin/cfn-init -v ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource MongoDbArbiterServer ",
" --configsets Install ",
" --access-key ", { "Ref" : "AccessKeyId" },
" --secret-key ", {"Ref": "SecretAccessKey" },
" --region ", { "Ref" : "AWS::Region" },
" > /tmp/cfn-init.log 2>&1 || error_exit $(</tmp/cfn-init.log)\n",
"## CloudFormation signal that setup is complete\n",
"/opt/aws/bin/cfn-signal --exit-code 0 --reason \"MongoDB Config Server instance setup complete\" '", { "Ref" : "WaitHandleArbiterServer" }, "'\n"
]]}}
}
},
"WaitHandleArbiterServer" : {
"Type" : "AWS::CloudFormation::WaitConditionHandle",
"Properties" : {}
},
"WaitConditionArbiterServer" : {
"Type" : "AWS::CloudFormation::WaitCondition",
"DependsOn" : "MongoDbArbiterServer",
"Properties" : {
"Handle" : { "Ref" : "WaitHandleArbiterServer" },
"Timeout" : "900"
}
}
}
}