Share
Embed
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
Transcript
50 slides
-
The Joy of Cooking Deploying MongoDB with Chef Nathen Harvey,
CustomInk.com https://github.com/nathenharvey/deploying- mongo-with-chef @nathenharvey @nathenharvey -
Agenda Infrastructure as Code Introduction to Chef Building a project
in Chef Deploying MongoDB with Chef Additional resources @nathenharvey -
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 -
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 -
Chef Declarative - What, not how Idempotent - Only take
action if required Convergent - Takes care of itself @nathenharvey -
Developer Happiness MongoDB makes developers happy MongoDB makes operations happy
Chef makes developers & operations happy, too! @nathenharvey -
Building a Chef Project First, come up with your policy
/ specification Abstract the resources in your spec @nathenharvey -
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 -
Building a Chef Project First, come up with your policy
/ specification Abstract the resources in your spec Write recipes @nathenharvey -
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 -
Building a Chef Project First, come up with your policy
/ specification Abstract the resources in your spec Write recipes Package recipes in cookbooks @nathenharvey -
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 -
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 -
Nodes Representation of a host runs the Chef client has
attributes has a list of recipes to be applied @nathenharvey -
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 -
Roles mechanism for easily composing sets of functionality have attributes
and a list of recipes to be applied @nathenharvey -
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 -
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 -
What is Chef? Server - API, search, Web UI Client
- chef-client Command line tool - knife @nathenharvey -
knife $ knife help list bootstrap client configure cookbook cookbook-site
data-bag environment exec index knife node role search shef ssh status tag @nathenharvey -
What is Chef? Server - API, search, Web UI Client
- chef-client Command line tool - knife Inspection library - ohai @nathenharvey -
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 -
What is Chef? Server - API, search, Web UI Client
- chef-client Command line tool - knife Inspection library - ohai REPL - shef @nathenharvey -
What is Chef? Server - API, search, Web UI Client
- chef-client Command line tool - knife Inspection library - ohai REPL - shef Community @nathenharvey -
Community Site Publish and share plugins for Chef, Knife, and
Ohai @nathenharvey -
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 -
Provision a server knife ec2 server create knife rackspace server
create Vagrant @nathenharvey -
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 -
Create a Cookbook $ knife cookbook create mongodb ** Creating
cookbook mongodb ** Creating README for cookbook: mongodb ** Creating metadata for cookbook: mongodb @nathenharvey -
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 -
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 -
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 -
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 -
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 -
Review Servers provisioned and communicating with the Chef API MongoDB
running in a replica set @nathenharvey -
But wait, there's more! Encrypted databags Environments Lightweight Resources and
Providers (LWRP) Exception and report handlers @nathenharvey -
Deploying MongoDB Deployment Preparedness MongoDB on Amazon EC2 Operations Best
Practices Production Notes @nathenharvey