37
Sandeep Parikh Technical Product Marketing [email protected] Getting Started with MongoDB and Amazon Web Services

Getting Started with MongoDB on Amazon Web Services

Embed Size (px)

Citation preview

Page 1: Getting Started with MongoDB on Amazon Web Services

Sandeep ParikhTechnical Product [email protected]

Getting Started with MongoDB and Amazon Web Services

Page 2: Getting Started with MongoDB on Amazon Web Services

What We’ll Cover

• MongoDB components » AWS instances• Deployment configurations• Storage configuration• Production tips• Setting up a three-node replica set

Page 3: Getting Started with MongoDB on Amazon Web Services

MongoDB Components

• 64-bit instances• Where your data lives• Needs RAM and Disk I/O

mongod

• Stateless request router• Lives on your app server• Minimal data needs

mongos

• Metadata server for sharded configurations• Minimal data needs• 64-bit instances

config

Page 4: Getting Started with MongoDB on Amazon Web Services

Amazon EC2 Components• mongod

– Standard: Large or Extra Large (typical)

– Hi-Memory: XL, XXL or 4XL (large data sets)

– Cluster instances provide increased capacity and bandwith

• mongos– Deploy onto your app server (32

or 64-bit)• config

– Standard instances– Micro instance is sufficient

Page 5: Getting Started with MongoDB on Amazon Web Services

DEPLOYMENT CONFIGURATIONS

Page 6: Getting Started with MongoDB on Amazon Web Services

Single Node

• mongod– 64-bit EC2 instance

• Storage– Multiple EBS volumes– RAID 10– Configured using mdadm

mongod

RAID 10

Page 7: Getting Started with MongoDB on Amazon Web Services

Replica Set

mongodprimary

RAID 10

mongodsecondary

RAID 10

mongodsecondary

RAID 10

Page 8: Getting Started with MongoDB on Amazon Web Services

Replica Set – Using Zones

mongodprimary

RAID 10

mongodsecondary

RAID 10

mongodsecondary

RAID 10

Zone 3Zone 2Zone 1

Page 9: Getting Started with MongoDB on Amazon Web Services

Replica Set – Multiple Regions

app

Region 1 Region 2

mongodprimary

RAID 10

mongodsecondary

RAID 10

mongodsecondary

RAID 10

Page 10: Getting Started with MongoDB on Amazon Web Services

Replica Set – Security Groups

mongodprimary

RAID 10

mongodsecondary

RAID 10

mongodsecondary

RAID 10

app “application”

“database”

Page 11: Getting Started with MongoDB on Amazon Web Services

Sharded Deployment

Shard 1 Shard 2 Shard 3

config

config

config

app server

mongos

app server

mongos

app server

mongos

Page 12: Getting Started with MongoDB on Amazon Web Services

CONFIGURING STORAGE

Page 13: Getting Started with MongoDB on Amazon Web Services

Recommendations

• EBS-backed storage vs. instance-based– Persistent vs. ephemeral

• RAID 10: “striped mirrors”

• 4-8 EBS volumes for best performance

http://en.wikipedia.org/wiki/Nested_RAID_levels#RAID_1_.2B_0

Page 14: Getting Started with MongoDB on Amazon Web Services

DEPLOYMENT NOTES

Page 15: Getting Started with MongoDB on Amazon Web Services

Some Tips

• Know your deployment– Security– Instances– Storage

• Configure filesystem as Ext4 or XFS• Reduce I/O overhead– noatime, nodiratime

• Raise file descriptor limits

Page 16: Getting Started with MongoDB on Amazon Web Services

More Tips

• EBS snapshots are an easy way to back up data

• Deploy in a trusted environment– Consider authentication

Page 17: Getting Started with MongoDB on Amazon Web Services

REPLICA SET DEPLOYMENT STEPS

Page 18: Getting Started with MongoDB on Amazon Web Services

Security Groups

• Create groups for the following– app servers– MongoDB components

• Ports to remember– mongod: 27017, 28017 (web-based status)– mongod shard server: 27018 – mongos: 27017– config: 27019

• Security groups act as ACLs• Instances are accessible via SSH keys

Page 19: Getting Started with MongoDB on Amazon Web Services

Single Node

• We’ll use this as a starting point

• Single EC2 instance• 4 EBS volumes• RAID10

– Example:• 100 GiB total• 50 GiB usable

mongod

RAID 10

Page 20: Getting Started with MongoDB on Amazon Web Services

Creating the Components

$ ec2-run-instances ami-41814f28 -n 1 -g database -k cluster-keypair -t m1.large -z us-east-1a

AMI

count

group

keypair

size

zone

Create an instance

Create storage volumes (4x)$ ec2-create-volume –s 25 -z us-east-1a

size

zone

Page 21: Getting Started with MongoDB on Amazon Web Services

Storage Configuration

$ ec2-attach-volume vol-e796108a -i i-11eee072 -d /dev/sdh1

volume

instance

device

Attach storage (4x)

Start the RAID$ sudo mdadm --create -l10 -n4 /dev/md0 /dev/sdh*

type

new device

devices

Page 22: Getting Started with MongoDB on Amazon Web Services

Storage Configuration

$ sudo fdisk /dev/md0$ sudo mkfs.ext4 /dev/md0p1

Partition device and make filesystem

$ sudo mkdir /data$ sudo chown `id -u` /data

Create mount point and set ownership

$ sudo echo ‘/dev/md0p1 /data auto noatime,noexec,nodiratime 0 0’ >> /etc/fstab$ sudo mount –a /dev/md0p1 /data

Update the filesystem table and mount

Page 23: Getting Started with MongoDB on Amazon Web Services

Install MongoDB

$ echo "[10gen]name=10gen Repositorybaseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64gpgcheck=0" | sudo tee -a /etc/yum.repos.d/10gen.repo

Update local repo settings

$ sudo yum -y install mongo-10gen-server$ sudo yum -y install sysstat

Install MongoDB and tools

$ sudo chkconfig --level 3 mongod off$ sudo chkconfig --level 5 mongod off

Change auto-start settings

Page 24: Getting Started with MongoDB on Amazon Web Services

MongoDB Configuration

$ sudo nano /etc/mongod.conf...dbpath=/data...

Set the dbpath

$ sudo chkconfig mongod on$ sudo /etc/init.d/mongod start

Set to autostart at boot and start the server now

$ mongoMongoDB shell version: 2.0.2connecting to: test>

Connect to MongoDB

Page 25: Getting Started with MongoDB on Amazon Web Services

Adding Additional Nodes

• For each node, first repeat steps for– Creating instances and

volumes– Configuring storage

• Include replica set parameter in MongoDB configuration

• Start mongod

Page 26: Getting Started with MongoDB on Amazon Web Services

MongoDB Configuration

$ sudo nano /etc/mongod.conf...dbpath=/datareplSet=replicaSetName...

Set the dbpath

$ sudo chkconfig mongod on$ sudo /etc/init.d/mongod start

Set to autostart at boot and start the server now

replica set name

Page 27: Getting Started with MongoDB on Amazon Web Services

Initialize Replica Set

$ mongoMongoDB shell version: 2.0.2connecting to: test>

Connect to MongoDB

> rs.initiate(){ "info2" : "no configuration explicitly specified -- making one", "me" : "ip-10-127-127-91:27017", "info" : "Config now saved locally. Should come online in about a minute.", "ok" : 1}

Initialize the replica set

> rs.add(“ec2-abc.def.amazonaws.com”){ “ok” : 1 }

Add each replica set member

external DNS name

Page 28: Getting Started with MongoDB on Amazon Web Services

Initialize Replica Set

>

The mongo prompt should go from this

PRIMARY>

To this

SECONDARY>

Or this

Page 29: Getting Started with MongoDB on Amazon Web Services

BACKUP/RESTORE

Page 30: Getting Started with MongoDB on Amazon Web Services

Backing Up Your Data

$ mongoSECONDARY> use adminSECONDARY> db.runCommand({fsync:1,lock:1});{ "info" : "now locked against writes, use db.$cmd.sys.unlock.findOne() to unlock", "ok" : 1}

Lock the database

$ ec2-create-snapshot vol-1234abcd --description "MongoDB RAID backup1"

volume description

Create a snapshot for each attached volume

$ mdadm --detail /dev/md0p1

Run mdadm and note the UUID

Page 31: Getting Started with MongoDB on Amazon Web Services

Backing Up Your Data

$ mongoSECONDARY> db.$cmd.sys.unlock.findOne();{ "ok" : 1, "info" : "unlock requested" }

Unlock the database

Page 32: Getting Started with MongoDB on Amazon Web Services

Restoring Your Data

$ ec2-create-volume --availability-zone us-east-1a --snapshot vol-1234abcd$ ec2-attach-volume vol-1234abcd -i i-aa3bc4c8 -d /dev/sdh1

Create and attach a volume from each snapshot

$ sudo mdadm --assemble --auto-update-homehost -u 07552c4d…a9c2f2fc --no-degraded /dev/md0mdadm: /dev/md0 has been started with 4 drives.

Reassemble the RAID

$ sudo mkdir /data2$ sudo chown `id -u` /data2

Create mount point and set ownership

UUID from mdadm

Page 33: Getting Started with MongoDB on Amazon Web Services

Restoring Your DataUpdate the filesystem table and mount

$ sudo echo ‘/dev/md0p1 /data2 auto noatime,noexec,nodiratime 0 0’ >> /etc/fstab$ sudo mount –a /dev/md0p1 /data2

$ mongod --dbpath /data2$ mongoMongoDB shell version: 2.0.2connecting to: test> use yourDb> db.yourCollection.validate({full:true})

Verify the restore (optional, requires time and disk space)

$ sudo cp /data2 /data

Set /data2 to /data

Page 34: Getting Started with MongoDB on Amazon Web Services

Recommended Backup Methods

• Create backups on secondary servers when using replication– Doesn’t block on the primary– Secondary updates once unlocked

• Use mongodump to backup live data– Collections, databases, everything– Restore with mongorestore

Page 35: Getting Started with MongoDB on Amazon Web Services

QUESTIONS?

Page 36: Getting Started with MongoDB on Amazon Web Services

Learn More

• MongoDB on Amazon Web Services white paper: http://www.10gen.com/white-papers

• Amazon EC2 info at MongoDB wiki: http://www.mongodb.org/display/DOCS/Amazon+EC2

• Scripts for working with EBS (creating, attaching, etc.): https://gist.github.com/1482182