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

Chef - Automated Configuration Management

Chef - Automated Configuration Management

自動的なコンフィギュレーションマネージメント。

Avatar for Philipp Wollermann

Philipp Wollermann

June 01, 2012
Tweet

Other Decks in Technology

Transcript

  1. ίϯϑΟΪϡϨʔγϣϯͬͯ…Ͳ͜ʁ • Wiki? Redmine? • Mailing list? Blogs? • Source

    code? README file? • Knowledge base? FAQ? • ... ಉ྅ʁʮଞͷαʔόʔ͔Βίϐʔͯ͠Ͷʯʁ
  2. ී௨͸Shell scriptͰ͠ΐ͏ # Setup a simple apache2 apt-get install apache2

    cp /root/templates/httpd.conf /etc/apache2/httpd.conf apache2ctl restart
  3. ී௨͸Shell scriptͰ͠ΐ͏ # Setup a simple apache2 apt-get install apache2

    cp /root/templates/httpd.conf /etc/apache2/httpd.conf apache2ctl restart ඞཁͳ͍৔߹Ͱ΋apt-get install͢Δ ඞཁͳ͍৔߹Ͱ΋ApacheΛϦελʔτ͢Δ
  4. ී௨͸Shell scriptͰ͠ΐ͏ # Setup a simple apache2 aptitude search apache2

    | grep -P '^i\s+apache2\s+-\s' || apt-get install apache2 cd /etc/apache2 cp /root/templates/httpd.conf httpd.conf.new sed -i "s/KeepAlive.*/KeepAlive Off/g" httpd.conf.new sed -i "s/MaxClients.*/MaxClients 50/g" httpd.conf.new diff httpd.conf httpd.conf.new || (mv httpd.conf.new httpd.conf; apache2ctl restart)
  5. ී௨͸Shell scriptͰ͠ΐ͏ # Setup a simple apache2 aptitude search apache2

    | grep -P '^i\s+apache2\s+-\s' || apt-get install apache2 cd /etc/apache2 cp /root/templates/httpd.conf httpd.conf.new sed -i "s/KeepAlive.*/KeepAlive Off/g" httpd.conf.new sed -i "s/MaxClients.*/MaxClients 50/g" httpd.conf.new diff httpd.conf httpd.conf.new || (mv httpd.conf.new httpd.conf; apache2ctl restart) ࣮͸ɺRAMʹΑͬͯҧ͍·͢Ͷ… ΞϓϦέʔγϣϯαʔόʔ͚ͩͰ͢ΑͶ regex ftw!
  6. ී௨͸Shell scriptͰ͠ΐ͏ # Setup a simple apache2 aptitude search apache2

    | grep -P '^i\s+apache2\s+-\s' || apt-get install apache2 # Just a guess, should be fine RAM_PER_CLIENT=8192 RAM_KB=`cat /proc/meminfo | egrep '^MemTotal' | cut -d':' -f2 | tr -d ' kB'` MAXCLIENTS=$(($RAM_KB / $RAM_PER_CLIENT)) cd /etc/apache2 cp /root/templates/httpd.conf httpd.conf.new sed -i "s/KeepAlive.*/KeepAlive Off/g" httpd.conf.new sed -i "s/MaxClients.*/MaxClients $MAXCLIENTS/g" httpd.conf.new diff httpd.conf httpd.conf.new || (mv httpd.conf.new httpd.conf; apache2ctl restart) chown root:root /etc/httpd.conf chmod 0644 /etc/httpd.conf wtf ??
  7. Chefͷ৔߹ • Ruby • Resources: Package, Template, Service, ... •

    Conditions: Only change configuration if necessary • Subscriptions: If configuration file X changed, restart service Y
  8. # apache2/recipes/default.rb package "apache2" do action :install end template "/etc/apache2/httpd.conf"

    do owner "root" group "root" mode "0644" source "httpd.conf.erb" end service "apache2" do action [ :enable, :start ] end Recipe
  9. # apache2/recipes/default.rb package "apache2" do action :install end template "/etc/apache2/httpd.conf"

    do owner "root" group "root" mode "0644" source "httpd.conf.erb" notifies :restart, "service[apache2]" end service "apache2" do action [ :enable, :start ] end Recipe
  10. # apache2/recipes/default.rb package "apache2" do action :install end template "/etc/apache2/httpd.conf"

    do owner "root" group "root" mode "0644" source "httpd.conf.erb" notifies :restart, "service[apache2]" variables ( :keep_alive => node['apache2']['keep_alive'] ) end service "apache2" do action [ :enable, :start ] end Recipe
  11. package "apache2" do action :install end if node['apache2']['max_clients'] == nil

    memory = node['memory']['total'] max_clients = Integer(/\d+/.match(memory)[0]) / 8192 else max_clients = node['apache2']['max_clients'] end template "/etc/apache2/httpd.conf" do owner "root" group "root" mode "0644" source "httpd.conf.erb" notifies :restart, "service[apache2]" variables( :keep_alive => node['apache2']['keep_alive'], :max_clients => max_clients ) end service "apache2" do action [ :enable, :start ] end Recipe
  12. # apache2/templates/default/httpd.conf.erb # # This is the Apache 2 configuration

    file # KeepAlive <%= @keep_alive %> MaxClients <%= @max_clients %> Template
  13. Files in a cookbook cookbooks/ apache2/ attributes/ default.rb recipes/ default.rb

    templates/ default/ httpd.conf.erb templates/ default/ ports.conf.erb templates/ default/ ...
  14. Chef Solo solo.json { "apache2": { "max_clients": 50 }, "run_list":

    [ "recipe[apache2::default]" ] } solo.rb root = File.absolute_path(File.dirname(__FILE__)) file_cache_path root cookbook_path root + '/cookbooks' verbose_logging false # chef-solo -c solo.rb -j solo.json cookbooks/ apache2/ attributes/default.rb recipes/default.rb
  15. ͭ·ΓɺԿ͕ҧ͏ʁ shell script tell the computer how to do something.

    chef / puppet / cfengine tell the computer what you want.
  16. ͭ·ΓɺԿ͕ҧ͏ʁ wiki / blog / documentation small bits of knowledge.

    no standard form. actual configuration may differ. chef / puppet / cfengine contains all knowledge about the system. is “the truth”.
  17. Thanks! • Recommendations: • Chef Solo tutorial http://www.opinionatedprogrammer.com/2011/06/chef-solo- tutorial-managing-a-single-server-with-chef/ •

    “Executable Documentation” by Michael O’Brien http://speakerdeck.com/u/notbrien/p/executable-documentation