Application deployment with Chef

Dd13a61bab3fe4243f3ebc683f9219eb?s=47 Igor Afonov
November 22, 2012

Application deployment with Chef

Agile tour Riga 2012

Dd13a61bab3fe4243f3ebc683f9219eb?s=128

Igor Afonov

November 22, 2012
Tweet

Transcript

  1. Application deployment with Chef Igor Afonov

  2. ‣ Ruby developer ‣ I’m participating in Chef development Developers

    Sys Admins DevOps I’m here About Me
  3. Configuration Management Chef Quick Intro Application Deployment FAQ Q &

    A Agenda
  4. Configuration Management

  5. "Keep track of all the stuff you do to take

    a system from 'bare metal' to 'doing its job'." - Adam Jacob
  6. ๏ Remember everything ๏ Keep notes in notepad ๏ Maintain

    wiki ๏ Bash scripts ๏ Automation frameworks
  7. Opscode Chef

  8. ๏ Configuration management framework ๏ Open source ๏ Created by

    Adam Jacob ๏ Maintained by Opscode
  9. ๏ Client-server architecture ๏ Idempotence ๏ Declarative ๏ Ruby everywhere

  10. Crash course

  11. chef-server push pull VCS chef-client node chef-client node chef-client node

    server chef repository developer Workflow
  12. cookbook recipe chef-repository resource resource resource Code structure

  13. Resource

  14. package 'apache2' do action :install provider Chef::Provider::Package::Apt version '2.4' end

  15. Cookbook File Cron Deploy Directory Env Execute* File Git Group

    HTTP Request Ifconfig Link Log Mount Ohai Package PowerShell Script User Remote Directory Remote File Route Ruby Block SCM Script Service Subversion Template Bundled resources
  16. Provider

  17. package apt pacman macports Resource Providers Interface Implementation

  18. service "redis" do action :start end Method call Parameter1 Parameter2

    # lib/chef/platform.rb - Chef::Platform :macosx => { :default => { ... :package => Chef::Provider::Package::Macports, :service => Chef::Provider::Service::Macosx, ... } } # lib/chef/provider/service/macosx.rb class Chef::Provider::Service::Macosx < Chef::Provider::Service::Simple def start if @current_resource.running Chef::Log.debug("#{@new_resource} already running, not starting") else shell_out!("launchctl load -w '#{@plist}'") end end end
  19. Recipe

  20. package "postfix" do action :install end %w{main master}.each do |config|

    template "/etc/postfix/#{config}.cf" do source "#{config}.cf.erb" owner "root" group "root" mode 0644 end end
  21. Cookbook

  22. cookbook |-attributes |-files |-libraries |-recipes |-templates |---metadata.rb

  23. ๏ Cookbooks should be data-driven ๏ Do not hardcode anything

    ๏ Control behavior via: ๏ Attributes ๏ Databags ๏ Search
  24. Node

  25. ๏ Host that runs chef-client ๏ Has attributes ๏ Has

    run list
  26. Role

  27. name "mail-server" description "Installs and configures postfix MTA" default_attributes :postfix

    => { "mydomain" => "domain.com", "myorigin" => "domain.com" } run_list "recipe[postfix]"
  28. Chef repository

  29. chef-repo |-cookbooks |-data_bags |-roles |-environments |---Rakefile https://github.com/opscode/chef-repo

  30. chef-server push pull VCS chef-client node chef-client node chef-client node

    server chef repository developer The big picture
  31. ๏ Authenticate node ๏ Synchronize cookbooks ๏ Compile ๏ Converge

    chef-client run
  32. ๏ Resource is a unit of work ๏ Provider take

    real action ๏ Recipe is collection of resources ๏ Cookbook is reusable set of recipes ๏ Code is pushed to chef-server ๏ Node pulls code from chef-server and runs it Recap
  33. Deployment

  34. ๏ Get the code from VCS ๏ Put the code

    to the right place ๏ Do some rituals Three easy* steps * Not true
  35. Real-world example

  36. internet nginx unicorn unicorn unicorn Simple Rails deployment

  37. Get the code & put it to the right place

    application 'copycopter' do repository 'git@github.com:iafonov/copycopter-server.git' revision 'master' path '/var/www/apps/copycopter' end
  38. Pick the SCM strategy application 'copycopter' do repository 'git@github.com:iafonov/copycopter-server.git' revision

    'master' path '/var/www/apps/copycopter' strategy :deploy_revision # :deploy_timestamped end
  39. Set the code owner application 'copycopter' do repository 'git@github.com:iafonov/copycopter-server.git' revision

    'master' path '/var/www/apps/copycopter' strategy :deploy_revision owner 'deploy' group 'deploy' end
  40. Install library dependencies application 'copycopter' do repository 'git@github.com:iafonov/copycopter-server.git' revision 'master'

    path '/var/www/apps/copycopter' strategy :deploy_revision owner 'deploy' group 'deploy' packages ['libxml2-dev', 'libxslt-dev'] end
  41. Run migrations application 'copycopter' do repository 'git@github.com:iafonov/copycopter-server.git' revision 'master' path

    '/var/www/apps/copycopter' strategy :deploy_revision owner 'deploy' group 'deploy' packages ['libxml2-dev', 'libxslt-dev'] migrate true end
  42. Restart service application 'copycopter' do repository 'git@github.com:iafonov/copycopter-server.git' revision 'master' path

    '/var/www/apps/copycopter' strategy :deploy_revision owner 'deploy' group 'deploy' packages ['libxml2-dev', 'libxslt-dev'] migrate true restart_command 'service copycopter restart' end
  43. ๏ rails ๏ java_webapp ๏ tomcat ๏ django ๏ ...

    Sub-resources
  44. Rails application 'copycopter' do ... rails do gems ['bundler'] database

    do username node['mysql']['username'] password node['mysql']['password'] database 'copycopter_production' end end end
  45. Unicorn application 'copycopter' do ... rails do ... end unicorn

    do worker_processes 4 port 8080 end end
  46. nginx application 'copycopter' do ... rails do ... end unicorn

    do ... end nginx_load_balancer do static_files '/public' => 'public' application_server_role 'copycopter_app_server' end end
  47. ๏ before_deploy ๏ before_migrate ๏ before_symlink ๏ before_restart ๏ after_restart

    Callbacks
  48. Run backups application 'copycopter' do ... before_deploy do execute 'run_backups'

    do command 'rsync ....' end end end
  49. Notify monitoring system application 'copycopter' do ... before_deploy do execute

    'run_backups' do command 'rsync ....' end end after_restart do StatsD.gauge("#{node.name}.deploy") end end
  50. ๏ Chef allows declaratively describe deploy process ๏ A lot

    of technology stacks are supported via sub-resources ๏ You can precisely control each step of process Recap
  51. FAQ

  52. When you should automate your infrastructure?

  53. Which tool should you use?

  54. What are benefits of using automation tools?

  55. Q & A afonov@gmail.com http://iafonov.github.com @iafonov

  56. Thank You! afonov@gmail.com http://iafonov.github.com @iafonov