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

Application deployment with Chef

Igor Afonov
November 22, 2012

Application deployment with Chef

Agile tour Riga 2012

Igor Afonov

November 22, 2012
Tweet

More Decks by Igor Afonov

Other Decks in Programming

Transcript

  1. Application
    deployment with Chef
    Igor Afonov

    View Slide

  2. ‣ Ruby developer
    ‣ I’m participating in Chef development
    Developers Sys Admins
    DevOps
    I’m
    here
    About Me

    View Slide

  3. Configuration Management
    Chef Quick Intro
    Application Deployment
    FAQ
    Q & A
    Agenda

    View Slide

  4. Configuration
    Management

    View Slide

  5. "Keep track of all the stuff you do to take a
    system from 'bare metal' to 'doing its job'."
    - Adam Jacob

    View Slide

  6. ๏ Remember everything
    ๏ Keep notes in notepad
    ๏ Maintain wiki
    ๏ Bash scripts
    ๏ Automation frameworks

    View Slide

  7. Opscode Chef

    View Slide

  8. ๏ Configuration management framework
    ๏ Open source
    ๏ Created by Adam Jacob
    ๏ Maintained by Opscode

    View Slide

  9. ๏ Client-server architecture
    ๏ Idempotence
    ๏ Declarative
    ๏ Ruby everywhere

    View Slide

  10. Crash course

    View Slide

  11. chef-server
    push
    pull
    VCS
    chef-client
    node
    chef-client
    node
    chef-client
    node
    server
    chef repository
    developer
    Workflow

    View Slide

  12. cookbook
    recipe
    chef-repository
    resource
    resource
    resource
    Code structure

    View Slide

  13. Resource

    View Slide

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

    View Slide

  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

    View Slide

  16. Provider

    View Slide

  17. package
    apt pacman macports
    Resource
    Providers
    Interface
    Implementation

    View Slide

  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

    View Slide

  19. Recipe

    View Slide

  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

    View Slide

  21. Cookbook

    View Slide

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

    View Slide

  23. ๏ Cookbooks should be data-driven
    ๏ Do not hardcode anything
    ๏ Control behavior via:
    ๏ Attributes
    ๏ Databags
    ๏ Search

    View Slide

  24. Node

    View Slide

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

    View Slide

  26. Role

    View Slide

  27. name "mail-server"
    description "Installs and configures postfix MTA"
    default_attributes :postfix => {
    "mydomain" => "domain.com",
    "myorigin" => "domain.com"
    }
    run_list "recipe[postfix]"

    View Slide

  28. Chef repository

    View Slide

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

    View Slide

  30. chef-server
    push
    pull
    VCS
    chef-client
    node
    chef-client
    node
    chef-client
    node
    server
    chef repository
    developer
    The big picture

    View Slide

  31. ๏ Authenticate node
    ๏ Synchronize cookbooks
    ๏ Compile
    ๏ Converge
    chef-client run

    View Slide

  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

    View Slide

  33. Deployment

    View Slide

  34. ๏ Get the code from VCS
    ๏ Put the code to the right place
    ๏ Do some rituals
    Three easy* steps
    * Not true

    View Slide

  35. Real-world
    example

    View Slide

  36. internet
    nginx
    unicorn
    unicorn
    unicorn
    Simple Rails deployment

    View Slide

  37. Get the code &
    put it to the right place
    application 'copycopter' do
    repository '[email protected]:iafonov/copycopter-server.git'
    revision 'master'
    path '/var/www/apps/copycopter'
    end

    View Slide

  38. Pick the SCM strategy
    application 'copycopter' do
    repository '[email protected]:iafonov/copycopter-server.git'
    revision 'master'
    path '/var/www/apps/copycopter'
    strategy :deploy_revision # :deploy_timestamped
    end

    View Slide

  39. Set the code owner
    application 'copycopter' do
    repository '[email protected]:iafonov/copycopter-server.git'
    revision 'master'
    path '/var/www/apps/copycopter'
    strategy :deploy_revision
    owner 'deploy'
    group 'deploy'
    end

    View Slide

  40. Install library dependencies
    application 'copycopter' do
    repository '[email protected]:iafonov/copycopter-server.git'
    revision 'master'
    path '/var/www/apps/copycopter'
    strategy :deploy_revision
    owner 'deploy'
    group 'deploy'
    packages ['libxml2-dev', 'libxslt-dev']
    end

    View Slide

  41. Run migrations
    application 'copycopter' do
    repository '[email protected]: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

    View Slide

  42. Restart service
    application 'copycopter' do
    repository '[email protected]: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

    View Slide

  43. ๏ rails
    ๏ java_webapp
    ๏ tomcat
    ๏ django
    ๏ ...
    Sub-resources

    View Slide

  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

    View Slide

  45. Unicorn
    application 'copycopter' do
    ...
    rails do
    ...
    end
    unicorn do
    worker_processes 4
    port 8080
    end
    end

    View Slide

  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

    View Slide

  47. ๏ before_deploy
    ๏ before_migrate
    ๏ before_symlink
    ๏ before_restart
    ๏ after_restart
    Callbacks

    View Slide

  48. Run backups
    application 'copycopter' do
    ...
    before_deploy do
    execute 'run_backups' do
    command 'rsync ....'
    end
    end
    end

    View Slide

  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

    View Slide

  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

    View Slide

  51. FAQ

    View Slide

  52. When you should
    automate your
    infrastructure?

    View Slide

  53. Which tool should
    you use?

    View Slide

  54. What are benefits
    of using
    automation tools?

    View Slide

  55. Q & A
    [email protected]
    http://iafonov.github.com
    @iafonov

    View Slide

  56. Thank You!
    [email protected]
    http://iafonov.github.com
    @iafonov

    View Slide