$30 off During Our Annual Pro Sale. View Details »

The Joy of Cooking: Deploy MongoDB with Chef

The Joy of Cooking: Deploy MongoDB with Chef

A quick primer on Opscode Chef and an introduction to using it to deploy MongoDB.

Nathen Harvey

June 26, 2012
Tweet

More Decks by Nathen Harvey

Other Decks in Technology

Transcript

  1. The Joy of Cooking Deploying MongoDB with Chef Nathen Harvey,

    CustomInk.com https://github.com/nathenharvey/deploying- mongo-with-chef @nathenharvey @nathenharvey
  2. Agenda Infrastructure as Code Introduction to Chef Building a project

    in Chef Deploying MongoDB with Chef Additional resources @nathenharvey
  3. Evolution of Server Provisioning Just build it Keep notes in

    server.txt Migrate notes to wiki Custom shell scripts (in git) Systems integration framework @nathenharvey
  4. Infrastructure as Code Enable the reconstruction of the business from

    nothing but a source code repository an application data backup and bare metal resources -Jesse Robins, Opscode @nathenharvey
  5. Chef Declarative - What, not how Idempotent - Only take

    action if required Convergent - Takes care of itself @nathenharvey
  6. Developer Happiness MongoDB makes developers happy MongoDB makes operations happy

    Chef makes developers & operations happy, too! @nathenharvey
  7. Building a Chef Project First, come up with your policy

    / specification Abstract the resources in your spec @nathenharvey
  8. Resources package "mongodb-10gen" do version node['mongodb']['version'] action :install end user

    node["mongodb"]["user"] do comment "MongoDB Server" gid node["mongodb"]["group"] supports :manage_home => true home "/home/#{node["mongodb"]["user"]}" action [ :create, :manage ] end More resources... @nathenharvey
  9. Building a Chef Project First, come up with your policy

    / specification Abstract the resources in your spec Write recipes @nathenharvey
  10. Recipes include_recipe 'mongodb::users' service node['mongodb']['service_name'] do supports [:start, :stop, :restart]

    end template node['mongodb']['configfile'] do source "mongodb.conf.erb" cookbook "mongodb" variables( :dbpath => node['mongodb']['dbpath'], :logpath => node['mongodb']['logpath'], :port => node['mongodb']['port'] ) end @nathenharvey
  11. Building a Chef Project First, come up with your policy

    / specification Abstract the resources in your spec Write recipes Package recipes in cookbooks @nathenharvey
  12. Cookbooks cookbooks/mongodb |-- README.md |-- attributes | `-- default.rb |--

    metadata.rb |-- recipes | |-- 10gen_repo.rb | |-- configure.rb | |-- install.rb | |-- mms.rb | |-- replica_set.rb | `-- users.rb `-- templates `-- default `-- mongodb.conf.erb @nathenharvey
  13. Building a Chef Project First, come up with your policy

    / specification Abstract the resources in your spec Write recipes Package recipes in cookbooks Apply recipes to nodes @nathenharvey
  14. Nodes Representation of a host runs the Chef client has

    attributes has a list of recipes to be applied @nathenharvey
  15. Building a Chef Project First, come up with your policy

    / specification Abstract the resources in your spec Write recipes Package recipes in cookbooks Apply recipes to nodes Group things into roles @nathenharvey
  16. Roles mechanism for easily composing sets of functionality have attributes

    and a list of recipes to be applied @nathenharvey
  17. Roles name "mongodc" description "Sample MongoDB Replica Set for Mo

    default_attributes( "mongodb" => { "replicaset" => "mongodc" } ) run_list( "recipe[mongodb::install]", "recipe[mongodb::configure]", "recipe[mongodb::replica_set]" ) @nathenharvey
  18. Building a Chef Project First, come up with your policy

    / specification Abstract the resources in your spec Write recipes Package recipes in cookbooks Apply recipes to nodes Group things into roles @nathenharvey
  19. What is Chef? Server - API, search, Web UI Client

    - chef-client Command line tool - knife @nathenharvey
  20. knife $ knife help list bootstrap client configure cookbook cookbook-site

    data-bag environment exec index knife node role search shef ssh status tag @nathenharvey
  21. What is Chef? Server - API, search, Web UI Client

    - chef-client Command line tool - knife Inspection library - ohai @nathenharvey
  22. ohai Collects detailed, extensible information about a host. { "uptime":

    "13 days 06 hours 16 minutes 02 se "platform": "ubuntu", "os_version": "2.6.32-38-generic", "cpu": { "total": 3, "real": 0, "2": { "cache_size": "4096 KB", "model": "2", "family": "6", ... @nathenharvey
  23. What is Chef? Server - API, search, Web UI Client

    - chef-client Command line tool - knife Inspection library - ohai REPL - shef @nathenharvey
  24. What is Chef? Server - API, search, Web UI Client

    - chef-client Command line tool - knife Inspection library - ohai REPL - shef Community @nathenharvey
  25. community.opscode.com @nathenharvey

  26. Community Site Publish and share cookbooks @nathenharvey

  27. Community Site Publish and share plugins for Chef, Knife, and

    Ohai @nathenharvey
  28. chef-repo directory chef-repo |-- .chef | |-- knife.rb | |--

    rubynation-validator.pem | `-- rubynation.pem |-- README.md |-- Rakefile |-- certificates |-- config | `-- rake.rb |-- cookbooks |-- data_bags |-- environments `-- roles @nathenharvey
  29. Provision a server knife ec2 server create knife rackspace server

    create Vagrant @nathenharvey
  30. Vagrantfile Vagrant::Config.run do |config| config.vm.define :mongo01 do |mongo01| mongo01.vm.provision :chef_client

    do |chef chef.add_role("mongodc") chef.node_name = "mongo01" end end config.vm.define :mongo02 do |mongo02| mongo02.vm.provision :chef_client do |chef chef.add_role("mongodc") chef.node_name = "mongo02" end end end @nathenharvey
  31. Deploying MongoDB Install Configure Run Backup Monitor @nathenharvey

  32. Create a Cookbook $ knife cookbook create mongodb ** Creating

    cookbook mongodb ** Creating README for cookbook: mongodb ** Creating metadata for cookbook: mongodb @nathenharvey
  33. Write our recipes 10gen_repo.rb install.rb configure.rb users.rb replica_set.rb @nathenharvey

  34. Get the code https://github.com/nathenharvey/deploying- mongo-with-chef @nathenharvey

  35. 10gen Repo Recipe include_recipe "apt" execute "apt-get update" do action

    :nothing end apt_repository "10gen" do keyserver "keyserver.ubuntu.com" key "7F0CEB10" uri "http://downloads-distro.mongodb.org/rep action :add notifies :run, "execute[apt-get update]", end @nathenharvey
  36. Install Recipe include_recipe "mongodb::10gen_repo" package node['mongodb']['package_name'] do version node['mongodb']['version'] action

    :install end @nathenharvey
  37. Where does package_name come from? Attributes defined in the cookbook:

    case platform when 'debian', 'ubuntu' default['mongodb']['package_name'] = 'mongod when 'centos', 'redhat', 'fedora', 'amazon' default['mongodb']['package_name'] = 'mongo- end @nathenharvey
  38. Configure Recipe template node['mongodb']['configfile'] do variables( :dbpath => node['mongodb']['dbpath'], :logpath

    => node['mongodb']['logpath'], :port => node['mongodb']['port'], :journal => node['mongodb']['journal'], :replication_set => node['mongodb']['repli ) owner "root" group "root" mode "0644" notifies :restart, "service[#{node['mongodb' end @nathenharvey
  39. Configure Recipe The configuration template dbpath = <%= @dbpath %>

    logpath = <%= @logpath %>/mongodb.log logappend = true port = <%= @port %> noauth = true <% if @journal %> journal=true <% else -%> nojournal=true <% end -%> @nathenharvey
  40. Replication Recipe ruby_block "configure-replica-set" do block do require "rubygems" require

    "mongo" 10.times do |try| begin conn = Mongo::Connection.new( "localhost", node['mongodb']['port'], :slave_ok => true, :connect_timeout => rescue delay = 2 ** (try + 1) Chef::Log.info("Failed to connect to mongo sleep(delay) end end @nathenharvey
  41. Upload cookbooks to Chef server $ knife cookbook upload -a

    @nathenharvey
  42. Run chef-client Automatically knife ssh vagrant provision @nathenharvey

  43. Review Servers provisioned and communicating with the Chef API MongoDB

    running in a replica set @nathenharvey
  44. But wait, there's more! Encrypted databags Environments Lightweight Resources and

    Providers (LWRP) Exception and report handlers @nathenharvey
  45. Deploying MongoDB Deployment Preparedness MongoDB on Amazon EC2 Operations Best

    Practices Production Notes @nathenharvey
  46. MongoDB Cookbooks http://community.opscode.com/cookbooks/mong https://github.com/miketheman/fullstack @nathenharvey

  47. Want more? http://community.opscode.com http://wiki.opscode.com Opscode Training Materials #chef on irc.freenode.net

    @nathenharvey
  48. Want even more? http://foodfightshow.org Episode 5: Getting Started with Chef

    @nathenharvey
  49. Shameless Plugs DevOpsDC Washington DC MongoDB Users Group @nathenharvey

  50. Find Me @nathenharvey http://nathenharvey.com nharvey@customink.com @nathenharvey