mongodb in aws (mongodb as a dbaas) - the …return back to cloudformation as custom resource •...

28
MongoDB in AWS (MongoDB as a DBaaS) Jing Wu Zhang Lu April 2017

Upload: others

Post on 11-Jul-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

MongoDB in AWS (MongoDB as a DBaaS)

Jing Wu Zhang Lu

April 2017

Page 2: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

2

Goals

• Automatically build MongoDB cluster

• Flexible scaling options

• Automatically recover from resource failures

Page 3: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

3

Utilizing CloudFormation Template• CloudFormation : Infrastructure As

Code

• CloudFormation template: JSON Formatted file

• Cloudformation Stack: A collection of AWS resources created by AWS CloudFormation.

Page 4: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

4

Utilizing CloudFormation Template

• In CloudFormation template, there are no loop/math functions available

• CloudFormation templates are mainly for stateless applications, thus cannot be used directly for building scalable database servers, which are stateful

Page 5: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

5

Utilizing Lambda Function

• AWS Lambda lets you run code without provisioning or managing servers. With Lambda, you can run code for virtually any type of application or backend service - all with zero administration

• The code you run on AWS Lambda is uploaded as Lambda function

Page 6: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

6

How Lambda Function is used

• Allow users to specify number of shards and nodes per shard as input parameters. The total instances in Cluster will be calculated in Lambda function and return back to CloudFormation as custom resource

• Assign instance initial roles in the Cluster for building process

• Save roles information in DynamoDB table

Page 7: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

7

Auto Scaling Group (ASG)

• Monitor the health of database nodes and keep number of healthy nodes constant

• Two ASGs are defined in CFN template. One is for mongod nodes and the other one is for config nodes in the cluster

Page 8: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

8

DynamoDB table• Store Cluster metadata information (ip, subnet,

volume ) for all instances provisioned by AWS CFN template

• Also store additional information like memberid, roles in MongoDB cluster, build stage and status

• Provide role information during initial cluster build

• Replacing node will reuse resources from old node stored in DynamoDB to save recovery time

Page 9: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

9

Build and Recovery Processes

Page 10: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

10

Initial Build of MongoDB Cluster• Lambda function to calculate total instances for

mongod and pre-assign roles and save into DynamoDB table

• ASG for mongod to launch instances based on the number return back form Lambda function

• ASG for config to launch instances based on input parameter for config servers

• Each newly provisioned instance goes through build stage 1-6 to establish a cluster

Page 11: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

11

MongoDB Cluster Build Stage 1• Get roles information from DynamoDB table

• Create EBS volumes and configure filesystems

• Create ENI and attach to instance

• Create config files for mongo processes and startup scripts

• Bring up mongo processes

• Update status in DynamoDB table for build stage 1 to complete

Page 12: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

12

MongoDB Cluster Build Stage2• For mongod secondary nodes, wait for status to be

updated to stage 5 complete

• For monod primary nodes, initialize replSet, and update status of all nodes in the same replSet to stage 2 complete

• For config node, the 1st config node will be used as primary, initialize the config replSet and update status of all config nodes to stage 2 complete

• The other config nodes, wait for status to be updated to stage 5 complete

Page 13: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

13

MongoDB Cluster Build Stage 3

• All mongod and config instances do nothing except pulling DynamoDB table for stage status changes

• On 1st config node, create additional config file and startup scripts for mongos, then start mongos process

• The build process of mongos updates status of all nodes to stage 3 complete.

Page 14: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

14

MongoDB Cluster Build Stage 4

• The build process of mongos pulls each replSet info from DynamoDB table, add each replSet into Cluster.

• All mongod and config instances doing nothing except pulling DynamoDB table for stage status changes

• Update status of all nodes in DynamoDB to stage 4 complete.

Page 15: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

15

MongoDB Cluster Build Stage 5

• The build process for mongod primary node of each shard creates all logins and update status of all nodes in the same replSet to stag 5 complete

• The build process for mongos creates all logins and update status of all config nodes to stage 5 complete in DynamoDB table

• The build processes of mongod secondary nodes and config nodes wait for stage status change.

Page 16: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

16

MongoDB Cluster Build Stage 6

• All nodes update their own config files to enable authentication and SSL.

• All nodes restart their processes.

• Update status of DynamoDB table to stage 6 complete

• Return success signal to CloudFormation

Page 17: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

17

Page 18: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

18

What Happens When Node Is Down?• ASG will bring up a new instance to replace the failed

instance

• Each new instance will look at DynamoDB table to find the instance to be replaced

• If they are in the same AZ, will re-attach the volumes and ENI, bring node back into cluster

• if it’s in different AZ, cleanup old volumes and ENI, rebuild node, bring it back to cluster and update DynamoDB table

Page 19: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

19

Other AWS services used • KMS: to encrypt and decrypt passwords

• ENI: static IPs, re-attach to the new instance. This is to let inter cluster communication to use dedicated channels and also avoid re-config replSets

• SNS/SQS: Database notification and monitor

• IAM: EC2 instance profiles and roles

• S3: Installation scripts EBS : Database storage

Page 20: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

20

Page 21: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

21

Other Components And Services Used

• Consul: DNS service for registry/discovery to provide one end point for applications

• Enable SSL for MongoDB Cluster during build

• Setup backup using ec2-consistant snapshot

Page 22: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

22

Cross Region Replication

• Adding nodes on different regions using the CFN Template for Cross-region

• Cross Region template will access DynamoDB table in primary region for Cluster information, then add new secondary in different region

• The metadata for new secondary will be saved in DynamoDB table in primary region

Page 23: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

23

Template Parameters

Page 24: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

24

Template Parameters (continued)

Page 25: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

25

Template Parameters (continued)

Page 26: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

26

Template Parameters (continued)

Page 27: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

27

Template Parameters (continued)

Page 28: MongoDB in AWS (MongoDB as a DBaaS) - The …return back to CloudFormation as custom resource • Assign instance initial roles in the Cluster for building process • Save roles information

28

Q/A

Jing [email protected]

Zhang [email protected]

Contacts