Slide 1

Slide 1 text

Application deployment with Chef Igor Afonov

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Configuration Management

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Opscode Chef

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Crash course

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

cookbook recipe chef-repository resource resource resource Code structure

Slide 13

Slide 13 text

Resource

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Provider

Slide 17

Slide 17 text

package apt pacman macports Resource Providers Interface Implementation

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Recipe

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Cookbook

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Node

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Role

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Chef repository

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

๏ 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

Slide 33

Slide 33 text

Deployment

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Real-world example

Slide 36

Slide 36 text

internet nginx unicorn unicorn unicorn Simple Rails deployment

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

Rails application 'copycopter' do ... rails do gems ['bundler'] database do username node['mysql']['username'] password node['mysql']['password'] database 'copycopter_production' end end end

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

๏ before_deploy ๏ before_migrate ๏ before_symlink ๏ before_restart ๏ after_restart Callbacks

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

๏ 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

Slide 51

Slide 51 text

FAQ

Slide 52

Slide 52 text

When you should automate your infrastructure?

Slide 53

Slide 53 text

Which tool should you use?

Slide 54

Slide 54 text

What are benefits of using automation tools?

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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