Slide 1

Slide 1 text

Sandeep  Parikh   Technical  Product  Marke4ng   [email protected]   Ge#ng  Started  with  MongoDB   and  Amazon  Web  Services  

Slide 2

Slide 2 text

What  We’ll  Cover   •  MongoDB  components  »  AWS  instances   •  Deployment  configura4ons   •  Storage  configura4on   •  Produc4on  4ps   •  SeFng  up  a  three-­‐node  replica  set  

Slide 3

Slide 3 text

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  configura4ons   • Minimal  data  needs   • 64-­‐bit  instances   config  

Slide 4

Slide 4 text

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  

Slide 5

Slide 5 text

DEPLOYMENT  CONFIGURATIONS  

Slide 6

Slide 6 text

Single  Node   •  mongod   –  64-­‐bit  EC2  instance   •  Storage   –  Mul4ple  EBS  volumes   –  RAID  10   –  Configured  using  mdadm   mongod   RAID  10  

Slide 7

Slide 7 text

Replica  Set   mongod   primary   RAID  10   mongod   secondary   RAID  10   mongod   secondary   RAID  10  

Slide 8

Slide 8 text

Replica  Set  –  Using  Zones   mongod   primary   RAID  10   mongod   secondary   RAID  10   mongod   secondary   RAID  10   Zone  3   Zone  2   Zone  1  

Slide 9

Slide 9 text

Replica  Set  –  Mul4ple  Regions   app   Region 1 Region 2 mongod   primary   RAID  10   mongod   secondary   RAID  10   mongod   secondary   RAID  10  

Slide 10

Slide 10 text

Replica  Set  –  Security  Groups   mongod   primary   RAID  10   mongod   secondary   RAID  10   mongod   secondary   RAID  10   app   “application” “database”

Slide 11

Slide 11 text

Sharded  Deployment   Shard  1   Shard  2   Shard  3   config   config   config   app  server   mongos   app  server   mongos   app  server   mongos  

Slide 12

Slide 12 text

CONFIGURING  STORAGE  

Slide 13

Slide 13 text

Recommenda4ons   •  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

Slide 14

Slide 14 text

DEPLOYMENT  NOTES  

Slide 15

Slide 15 text

Some  Tips   •  Know  your  deployment   –  Security   –  Instances   –  Storage   •  Configure  filesystem  as  Ext4  or  XFS   •  Reduce  I/O  overhead   –  noa4me,  nodira4me   •  Raise  file  descriptor  limits  

Slide 16

Slide 16 text

More  Tips   •  EBS  snapshots  are  an  easy  way  to  back  up   data   •  Deploy  in  a  trusted  environment   – Consider  authen4ca4on  

Slide 17

Slide 17 text

REPLICA  SET  DEPLOYMENT  STEPS  

Slide 18

Slide 18 text

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  

Slide 19

Slide 19 text

Single  Node   •  We’ll  use  this  as  a   star4ng  point   •  Single  EC2  instance   •  4  EBS  volumes   •  RAID10   –  Example:   •  100  GiB  total   •  50  GiB  usable     mongod   RAID  10  

Slide 20

Slide 20 text

Crea4ng  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  

Slide 21

Slide 21 text

Storage  Configura4on   $ 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  

Slide 22

Slide 22 text

Storage  Configura4on   $ 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

Slide 23

Slide 23 text

Install  MongoDB   $ echo "[10gen]! name=10gen Repository! baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64! gpgcheck=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

Slide 24

Slide 24 text

MongoDB  Configura4on   $ 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 $ mongo! MongoDB shell version: 2.0.2! connecting to: test! >! Connect to MongoDB

Slide 25

Slide 25 text

Adding  Addi4onal  Nodes   •  For  each  node,  first   repeat  steps  for   –  Crea4ng  instances  and   volumes   –  Configuring  storage   •  Include  replica  set   parameter  in  MongoDB   configura4on   •  Start  mongod  

Slide 26

Slide 26 text

MongoDB  Configura4on   $ sudo nano /etc/mongod.conf! ...! dbpath=/data! replSet=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  

Slide 27

Slide 27 text

Ini4alize  Replica  Set   $ mongo! MongoDB shell version: 2.0.2! connecting 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  

Slide 28

Slide 28 text

Ini4alize  Replica  Set   >! The mongo prompt should go from this PRIMARY>! To this SECONDARY>! Or this

Slide 29

Slide 29 text

BACKUP/RESTORE  

Slide 30

Slide 30 text

Backing  Up  Your  Data   $ mongo! SECONDARY> use admin! SECONDARY> 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   descrip4on   Create a snapshot for each attached volume $ mdadm --detail /dev/md0p1! Run mdadm and note the UUID

Slide 31

Slide 31 text

Backing  Up  Your  Data   $ mongo! SECONDARY> db.$cmd.sys.unlock.findOne();! { "ok" : 1, "info" : "unlock requested" }! Unlock the database

Slide 32

Slide 32 text

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/md0! mdadm: /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  

Slide 33

Slide 33 text

Restoring  Your  Data   Update 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! $ mongo! MongoDB shell version: 2.0.2! connecting 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

Slide 34

Slide 34 text

Recommended  Backup  Methods   •  Create  backups  on  secondary  servers  when   using  replica4on   – Doesn’t  block  on  the  primary   – Secondary  updates  once  unlocked   •  Use  mongodump  to  backup  live  data   – Collec4ons,  databases,  everything   – Restore  with  mongorestore  

Slide 35

Slide 35 text

QUESTIONS?  

Slide 36

Slide 36 text

Learn  More   •  MongoDB  on  Amazon  Web  Services  white  paper:   hnp://www.10gen.com/white-­‐papers   •  Amazon  EC2  info  at  MongoDB  wiki:   hnp://www.mongodb.org/display/DOCS/Amazon+EC2   •  Scripts  for  working  with  EBS  (crea4ng,  anaching,  etc.):   hnps://gist.github.com/1482182      

Slide 37

Slide 37 text

Contact  Info   •  MongoDB   –  hnp://www.mongodb.org   –  Downloads,  Docs,  Forums,  etc.   •  10gen   –  hnp://www.10gen.com/contact   –  [email protected]   –  Consul4ng,  Support,  etc.   •  Sandeep  Parikh   –  [email protected]