Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MongoDB and Amazon Web Services - LinuxFestNorthWest

MongoDB and Amazon Web Services - LinuxFestNorthWest

Slides from my talk on deploying MongoDB on Amazon Web Services. Includes production tips and information on sample AWS CloudFormation templates that can be used to automate MongoDB deployments.

Sandeep Parikh

April 28, 2012
Tweet

More Decks by Sandeep Parikh

Other Decks in Technology

Transcript

  1. Sandeep  Parikh   Technical  Product  Manager   [email protected]   @crcsmnky

      MongoDB     and     Amazon  Web  Services  
  2. Agenda   •  MongoDB  basics   – Components   – Deployment  types

      – Storage   •  Deploying  a  Replica  Set   •  CloudFormaCon   – Sample  template  background   – Template  walkthrough  
  3. MongoDB  Components   mongod   Core   database   server

      process   mongos   Request   router  for   sharded   deployments   config   Metadata   server  for   sharded   deployments  
  4. Amazon  EC2  Components   •  mongod  (64-­‐bit)   –  Standard:

     L,  XL     –  Hi-­‐Memory:  XL,  XXL  or  4XL   –  Cluster:  increased  capacity   and  bandwidth   •  mongos   –  Deploy  onto  your  app  server   •  config   –  Standard  instances  sufficient  
  5. Replica  Set   mongod   primary   RAID  10  

    mongod   secondary   RAID  10   mongod   secondary   RAID  10  
  6. Replica  Set  –  MulCple  Zones   app   Zone 1

    Zone 3 mongod   primary   RAID  10   mongod   secondary   RAID  10   mongod   secondary   RAID  10   Zone 2
  7. Replica  Set  –  MulCple  Regions   app   Region 1

    Region 2 mongod   primary   RAID  10   mongod   secondary   RAID  10   mongod   secondary   RAID  10  
  8. Replica  Set  –  Security  Groups   mongod   primary  

    RAID  10   mongod   secondary   RAID  10   mongod   secondary   RAID  10   app   “application” “database”
  9. Sharded  Deployment   Shard  1   Shard  2   Shard

     3   config   config   config   app  server   mongos   app  server   mongos   app  server   mongos  
  10. Storage  ConsideraCons   •  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
  11. Deployment  Tips   •  Know  your  deployment   –  Security

     groups   –  Instance  sizes   –  Storage  needed  now  and  later   •  Configure  filesystem  as  ext4  or  XFS   •  Reduce  I/O  overhead   •  Raise  file  descriptor  limits   •  Set  read-­‐ahead  
  12. Single  Node   •  We’ll  use  this  as  a  

    starCng  point   •  Single  EC2  instance   •  4  EBS  volumes   •  RAID10   –  Example:   •  100  GiB  total   •  50  GiB  usable     mongod   RAID  10  
  13. CreaCng  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  
  14. Storage  ConfiguraCon   $ 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  
  15. Storage  ConfiguraCon   $ sudo pvcreate /dev/md0! $ sudo vgcreate

    vg0 /dev/md0! $ sudo lvcreate -n data vg0! $ sudo mkfs.ext4 /dev/vg0/data! Partition device and make filesystem $ sudo mkdir /data! $ sudo chown mongod:mongod /data! Create mount point and set ownership $ echo ‘/dev/vg0/data /data ext4 defaults,auto,noatime,noexec 0 0’ | sudo tee –a /etc/fstab! $ sudo mount /data! Update the filesystem table and mount
  16. 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 mongod off! Change auto-start settings
  17. MongoDB  ConfiguraCon   $ 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.4! connecting to: test! >! Connect to MongoDB
  18. Adding  AddiConal  Nodes   •  For  each  node,  first  

    repeat  steps  for   –  CreaCng  instances  and   volumes   –  Configuring  storage   •  Include  replica  set   parameter  in  MongoDB   configuraCon   •  Start  mongod  
  19. MongoDB  ConfiguraCon   $ 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  
  20. IniCalize  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  
  21. IniCalize  Replica  Set   >! The mongo prompt should go

    from this PRIMARY>! To this SECONDARY>! Or this
  22. AWS  CloudFormaCon   •  Gives  devs  and  admins  easy  way

     to  create   and  manage  AWS  resources   •  Create  text-­‐based  templates  which  describe   resources  in  your  stack   •  Templates  are  JSON  and  can  be  managed  like   code  or  stored  in  S3  
  23. MongoDB  plus  CloudFormaCon   •  Reference  templates  for  building  your

     own   deployment   – Single  node  for  test  and  development   – 3-­‐node  replica  set  for  producCon-­‐level   deployments   •  Customize  them  for  your  own  needs  
  24. Sample  Templates   •  Single-­‐node   – Single  mongod  with  4

     EBS  volumes  in  a  RAID10   •  Replica  set   – Each  node  similar  to  single  node   – Primary  configured  in  “parent”  template   – Secondary  configured  in  “child”  template   – MulCple  secondaries  are  created/referenced  in   “parent”  template  
  25. Resources  Created   •  Based  on  Amazon  Linux  (for  your

     region)   •  New  security  group   – Port  22:  open  to  all   – Port  27017:  open  to  instances  in  group   •  IAM  resource   •  4  EBS  volumes   •  4  EBS  volume  aiachments   •  10-­‐minute  Cmeout  for  the  whole  thing  
  26. Security  Groups  –  Gotchas   •  Don’t  forget:   – App

     servers  need  to  talk  to  MongoDB   – Sharding  uses  addiConal  ports  (27018,  27019)   – Status  page  is  visible  
  27. Instance  ConfiguraCon     •  Init  instance   –  Install

     packages   –  Add  10gen  repo   •  Wait  for  EBS  volumes  to  aiach   •  Install  MongoDB   •  Storage  setup   –  Create  RAID10   –  Blockdev  read-­‐ahead  for  each  device   –  LVM  setup   •  MongoDB  configuraCon   •  Finish  
  28. Instance  ProperCes   •  Based  on  the  region  you’re  running

     in  and   instance  size  input,  choose  an  AMI   •  KeyName  is  an  input  parameter   •  UserData  holds  configuraCon  script  
  29. Script  –  Gemng  Started   •  Install  AWS  CloudFormaCon  tools

      •  Error  helper  funcCon   •  Access  and  Secret  Keys  from  IAM  resource  
  30. Script  –  LVM,  FS,  Mounts   x 3 x 3

    •  Data,  Log  and  Journal  each  have     –  their  own  volume   –  Mount  path/pount  
  31. Replica  Set  Member   •  Child  templates  must  be  in

     S3   •  Note:  these  are  set  to  be  in  a  different  AZ  
  32. Replica  Set  ConfiguraCon   •  Almost  the  same  as  single

     node   •  Added  “replSet”  parameter  to  mongod.conf   •  All  Replica  Set  nodes  have  this  
  33. Replica  Set  IniCalizaCon   •  Created  Replica  Set  init  script

      •  $HOSTNAME  pulled  from  EC2  instance  metadata   •  Run  on  the  instance  from  ReplicaSetStack  
  34. Adding  More  Storage   •  Add  Volume  resource   • 

    Add  VolumeAiachment  resource   •  Add  sleep  condiCon  in  script   •  Edit  mdadm  for  addiConal  volumes  in  script  
  35. Adding  More  Instances   •  Edit  the  ReplicaSetStack  template  

    •  Add  addiConal  “ReplicaSetMember”  instances   •  Update  “replicaSetConfigInit.js”   •  Spread  across  mulCple  zones   – Note:  spreading  across  regions  may  require   addiConal  edits  and/or  manual  inputs  
  36. Using  Templates   •  Go  to  AWS  Management  Console  >

     AWS   CloudFormaCon  >  Create  New  Stack   •  ReplicaSetMember  template  must  be  in  S3   •  ReplicaSetStack  template  needs  that  S3  URL   – See  “TemplateURL”  
  37. Learn  More   •  AutomaCng  Deployment  on  AWS  with  CloudFormaCon

      hip://www.mongodb.org/display/DOCS/AutomaCng +Deployment+with+CloudFormaCon   •  Amazon  EC2  info  (incl.  in-­‐depth  guide):   hip://www.mongodb.org/display/DOCS/Amazon+EC2   •  MongoDB  AMI  on  AWS  Marketplace   hips://aws.amazon.com/marketplace/pp/B007IBMJPI/   •  MongoDB  on  Amazon  Web  Services  white  paper:   hip://www.10gen.com/white-­‐papers  
  38. QuesCons  &  Comments   •  Sandeep  Parikh   –  [email protected]

      –  @crcsmnky   •  MongoDB   –  hip://www.mongodb.org   –  Downloads,  Docs,  Forums,  etc.   •  10gen   –  hip://www.10gen.com/contact   –  [email protected]   –  ConsulCng,  Support,  etc.