Create your very own Development Environment with Vagrant and Packer

67
Create your very own Development Environment with Vagrant and Packer … and a little bit of Puppet Frank Stelzer https://www.flickr.com/photos/eiriknewth/238681391

description

Presentation about Vagrant and Packer held on PHP Usergroup Duesseldorf http://www.meetup.com/PHP-Usergroup-Duesseldorf/events/176868772/

Transcript of Create your very own Development Environment with Vagrant and Packer

Page 1: Create your very own Development Environment with Vagrant and Packer

Create your very own Development Environment with Vagrant and Packer

… and a little bit of PuppetFrank Stelzer https://www.flickr.com/photos/eiriknewth/238681391

Page 2: Create your very own Development Environment with Vagrant and Packer

Créateur de

Agenda• Motivation

• Vagrant Introduction

• Base Boxes

• Creating an own Base Box with Packer

• Demo

• Fat vs. Thin Boxes

• Provisioning with Puppet

Page 3: Create your very own Development Environment with Vagrant and Packer

Créateur de

Vagrant

PuppetPacker

Agenda

Sorry, nothing about Docker in this talk!

Page 4: Create your very own Development Environment with Vagrant and Packer

Créateur de

Who's speaking?

Frank Stelzer

Software Architect at

SensioLabs Deutschland GmbH

@frastel

Page 5: Create your very own Development Environment with Vagrant and Packer

Créateur de

Motivation

http://www.flickr.com/photos/mrmoaks/8199740598/

Page 6: Create your very own Development Environment with Vagrant and Packer

Créateur de

Development Environments

Page 7: Create your very own Development Environment with Vagrant and Packer

Créateur de

I don't know my dev env, it's just working!

Page 8: Create your very own Development Environment with Vagrant and Packer

Créateur de

Development Environments

Shared Development ServerXAMPP/MAMP

VagrantGolden Image

some undefined VM/local system

Vagrant with Provisioning

Page 9: Create your very own Development Environment with Vagrant and Packer

Créateur de

Criteria for good development environments

• Isolated

Page 10: Create your very own Development Environment with Vagrant and Packer

Créateur de

Development Environments

Shared Development ServerXAMPP/MAMP

VagrantGolden Image

some undefined VM/local system

Vagrant with Provisioning

Page 11: Create your very own Development Environment with Vagrant and Packer

Créateur de

Criteria for good development environments

• Isolated

• Same tools for production

Page 12: Create your very own Development Environment with Vagrant and Packer

Créateur de

Development Environments

XAMPP/MAMP

VagrantGolden Image

some undefined VM/local system

Vagrant with Provisioning

Page 13: Create your very own Development Environment with Vagrant and Packer

Créateur de

Criteria for good development environments

• Isolated

• Same tools for production

• Reproducible

Page 14: Create your very own Development Environment with Vagrant and Packer

Créateur de

Development Environments

VagrantGolden Image

some undefined VM/local system

Vagrant with Provisioning

Page 15: Create your very own Development Environment with Vagrant and Packer

Créateur de

Criteria for good development environments

• Isolated

• Same tools for production

• Reproducible

• EASY!

Page 16: Create your very own Development Environment with Vagrant and Packer

Créateur de

Development Environments

Golden Image

Vagrant with Provisioning

Page 17: Create your very own Development Environment with Vagrant and Packer

Créateur de

VAGRANT

http://www.hashicorp.com/ http://www.vagrantup.com/

Page 18: Create your very own Development Environment with Vagrant and Packer

Créateur de

Vagrant

VirtualBox

VM!

dev.smoov.io

Vagrant

Vagrantfile

"name = dev.smoov.io"

project $ vagrant up

Base Box21

Page 19: Create your very own Development Environment with Vagrant and Packer

Créateur de

!

Vagrantfile

Vagrant.configure("2") do |config| ! config.vm.box = "precise64" config.vm.network "private_network", ip: "192.168.56.10" config.vm.hostname = "dev.smoov.io" ! config.vm.provider :virtualbox do |virtualbox| virtualbox.customize ["modifyvm", :id, "--name", "dev.smoov.io"] virtualbox.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] virtualbox.customize ["modifyvm", :id, "--memory", "1024"] end ! config.ssh.keep_alive = true config.ssh.forward_agent = true !end

assign provider specific configs

keep this in mind!

Page 20: Create your very own Development Environment with Vagrant and Packer

Créateur de

vagrant destroy is your friend!

Page 21: Create your very own Development Environment with Vagrant and Packer

Créateur de

Available Base Boxes• Ubuntu Lucid 32 Bit (10.04)

• Ubuntu Lucid 64 Bit (10.04)

• Ubuntu Precise 32 Bit (12.04)

• Ubuntu Precise 64 Bit (12.04)

• https://github.com/mitchellh/vagrant/wiki/Available-Vagrant-Boxes

• https://cloud-images.ubuntu.com/vagrant/

• other inofficial userland boxes: vagrantboxes.es, puphpet.com …

What is in there!?

Page 22: Create your very own Development Environment with Vagrant and Packer

Créateur de

Native supported base boxes are not up-to-date in the most cases

PHP 5.3

nginx 0.9

Apache 1

not node compatible

http://www.flickr.com/photos/yoshimov/13909857/PPAs :(

Page 23: Create your very own Development Environment with Vagrant and Packer

Créateur de

Can I haz the new things please?

PHP 5.5.9

nginx 1.6

Apache 2

node!

http://www.flickr.com/photos/chaosandcreations/2325239486/PPAs not needed

Page 24: Create your very own Development Environment with Vagrant and Packer

Créateur de

Can I? Pleeeease!

Page 25: Create your very own Development Environment with Vagrant and Packer

Créateur de

Sure!

Page 26: Create your very own Development Environment with Vagrant and Packer

Créateur de

Wait until new base boxes will be supported!

http://www.flickr.com/photos/bogenfreund/556656621/

Page 27: Create your very own Development Environment with Vagrant and Packer

Créateur de

Not really!

Page 28: Create your very own Development Environment with Vagrant and Packer

Créateur de

Just build your own base box if you need to!

Page 29: Create your very own Development Environment with Vagrant and Packer

Créateur de

… and that's now!

Page 30: Create your very own Development Environment with Vagrant and Packer

Créateur de

Packer!

http://www.hashicorp.com/ http://www.packer.io/

Page 31: Create your very own Development Environment with Vagrant and Packer

Créateur de

Follow @mitchellh for updates!

Page 32: Create your very own Development Environment with Vagrant and Packer

Créateur de

!Base Box defined with simple JSON configuration

       "builders":  [  

               {  

                       "guest_os_type":  "Ubuntu_64",  

                       "iso_url":  "http://releases.ubuntu.com/14.04/ubuntu-­‐14.04-­‐server-­‐amd64.iso",  

                       "shutdown_command":  "echo  'vagrant'  |  sudo  -­‐S  shutdown  -­‐P  now",  

                       "type":  "virtualbox-­‐iso"  

               }  

       ],  

       "post-­‐processors":  [  

               {  

                       "only":  ["virtualbox-­‐iso"],  

                       "type":  "vagrant",  

                       "output":  "build/packer_ubuntu-­‐14.04_{{.Provider}}.box"

Page 33: Create your very own Development Environment with Vagrant and Packer

Créateur de

Provisioning - with plain shell scripts

"provisioners":  [  

       {  […]    

               "script":  "scripts/install.sh",  

               "type":  "shell"  

       […]  }  

]

# scripts/install.sh apt-get -y update apt-get -y -q install linux-headers-$(uname -r) build-essential dkms nfs-common !# ... install VBoxGuestAdditions, prepare Vagrant user etc. !# install everything for Puppet3+Hiera apt-get install -y puppet

Page 34: Create your very own Development Environment with Vagrant and Packer

Créateur de

Provisioning - with Puppet

"provisioners":  [  

       {  […]    

               "type":  "puppet-­‐masterless",  

               "manifest_file":  "puppet/manifests/lamp.pp",  

               "module_paths":  ["puppet/modules"]  

       […]  }  

]

• Provisioning types could be mixed • Puppet has to be installed within Shell Provisioning first

Page 35: Create your very own Development Environment with Vagrant and Packer

Créateur de

Resulting VM could be converted to a Vagrant box or whatever

"builders":  […],  

"post-­‐processors":  [  

       {  

               "only":  ["virtualbox-­‐iso"],  

               "type":  "vagrant",  

               "output":  "buildpacker_ubuntu-­‐14.04_{{.Provider}}.box"  

       }  

]

Page 36: Create your very own Development Environment with Vagrant and Packer

Créateur de

… and it comes with cool magic!

Page 37: Create your very own Development Environment with Vagrant and Packer

Créateur de

"builders": [ { "boot_command": [ "<esc><esc><enter><wait>", "/install/vmlinuz noapic preseed/url=http://{{ .HTTPIP }}:{{ .HTTPPort }}/preseed.cfg <wait>", "debian-installer=en_US auto locale=en_US kbd-chooser/method=us <wait>", "hostname={{ .Name }} <wait>", "fb=false debconf/frontend=noninteractive <wait>", "keyboard-configuration/modelcode=SKIP keyboard-configuration/layout=USA keyboard-configuration/variant=USA console-setup/ask_detect=false <wait>", "initrd=/install/initrd.gz -- <enter><wait>" ],

Page 38: Create your very own Development Environment with Vagrant and Packer

Créateur de

Demo: Generating a Base Box for

Vagrant with Packer in 3min

Page 39: Create your very own Development Environment with Vagrant and Packer

Créateur de

http://youtu.be/wgt9jUyuXVI

Page 40: Create your very own Development Environment with Vagrant and Packer

Créateur de

Base Box is generated! What's next?

Page 41: Create your very own Development Environment with Vagrant and Packer

Créateur de

Integrate it to your Vagrant setup!

vagrant  box  add  packer_ubuntu-­‐14.04_virtualbox  file://…

Page 42: Create your very own Development Environment with Vagrant and Packer

Créateur de

!

Vagrantfile

Vagrant.configure("2") do |config| ! config.vm.box = config.vm.network "private_network", ip: "192.168.56.10" config.vm.hostname = "dev.smoov.io" ! config.vm.provider :virtualbox do |virtualbox| virtualbox.customize ["modifyvm", :id, "--name", "dev.smoov.io"] virtualbox.customize ["modifyvm", :id, "--natdnshostresolver1", "on"] virtualbox.customize ["modifyvm", :id, "--memory", "1024"] end ! config.ssh.keep_alive = true config.ssh.forward_agent = true !end

"precise64""packer_ubuntu-14.04_virtualbox"

Page 43: Create your very own Development Environment with Vagrant and Packer

Créateur de

Packer + Vagrant

VirtualBox

VM!

dev.smoov.io

Vagrant

Base BoxVagrantfile

"name = dev.smoov.io"

project $

PackerBase BoxVM

1

2 3

vagrant up

Page 44: Create your very own Development Environment with Vagrant and Packer

Créateur de

What to put in Base Boxes?

Page 45: Create your very own Development Environment with Vagrant and Packer

Créateur de

Everything?

Page 46: Create your very own Development Environment with Vagrant and Packer

Créateur de

Fat Base Box

Fat Base Box

included:PHP 5MySQLRedis nginx

Page 47: Create your very own Development Environment with Vagrant and Packer

Créateur de

Fat Base Box• advantages:

• vagrant up -> ultrafast

• no internet connection needed during VM creation

• disadvantages:

• out-of-date very fast

• heavy maintenance needed

• one fat base box for every server type (webserver, database server, search server etc.)

• … or do you want PHP to be installed on a DB node?

Page 48: Create your very own Development Environment with Vagrant and Packer

Créateur de

Fat vs. Slim Base BoxFat Base Box

ISO

VBoxGuestAdditions

PHP 5MySQLRedisnginx

Slim Base Box

ISOVBoxGuestAdditions

+ Provisioning (Puppet)

Page 49: Create your very own Development Environment with Vagrant and Packer

Créateur de

Choose wisely according to your needs!

Page 50: Create your very own Development Environment with Vagrant and Packer

Créateur de

… but …

Page 51: Create your very own Development Environment with Vagrant and Packer

Créateur de

Slim Base Boxes should be preferred when infrastructure changes often!

Page 52: Create your very own Development Environment with Vagrant and Packer

Créateur de

UsageFat Base Box

!• no changes • System is well known • Golden Image way • Admins are defining the system

Slim Base Box

• frequent changes • System is not yet

defined • Developers are

defining the system

Page 53: Create your very own Development Environment with Vagrant and Packer

Créateur de

Slim Base Boxes need additional provisioning after Virtual Machine has

been installed!

Page 54: Create your very own Development Environment with Vagrant and Packer

Créateur de

Provisioning with Puppet

VM

Box!

dev.smoov.io

Vagrant

Base BoxVagrantfile

"name = dev.smoov.io"

project $

PackerBase BoxVM

1

2

4Puppet

3

vagrant up

Page 55: Create your very own Development Environment with Vagrant and Packer

Créateur de

VagrantfileVagrant.configure(VAGRANTFILE_API_VERSION)  do  |config|      config.vm.box  =  "packer_ubuntu-­‐14.04_virtualbox"            #  …  !    config.vm.provision  :puppet  do  |puppet|          puppet.manifest_file  =  "lamp.pp"          puppet.manifests_path  =  "puppet/manifests"          puppet.module_path  =  "puppet/modules"          puppet.options  =  ["-­‐-­‐verbose"]      end  end  

our generated Base Box from Packer

Provisioning with Puppet

Page 56: Create your very own Development Environment with Vagrant and Packer

Créateur de

Why Puppet?

Page 57: Create your very own Development Environment with Vagrant and Packer

Créateur de

Why Not?

Page 58: Create your very own Development Environment with Vagrant and Packer

Créateur de

Go for Chef or Ansible if you want!

Page 59: Create your very own Development Environment with Vagrant and Packer

Créateur de

Puppet <3

Page 60: Create your very own Development Environment with Vagrant and Packer

Créateur de

Puppet (2) - DSL Overview

Variables

Conditionals

Facts

Modules

Classes

Templates

Defined Types

Page 61: Create your very own Development Environment with Vagrant and Packer

Créateur de

Puppet – DSL - Modulespuppet/ |-- manifests | `-- lamp.pp `-- modules |-- apache2 | |-- files | | `-- staticfile.conf | |-- manifests | | |-- init.pp | | |-- mod.pp | | `-- vhost.pp | `-- templates | `-- vhost.erb |-- composer | `-- manifests | `-- init.pp

puppet/ |… `-- modules |… `-- php5 `-- manifests `-- init.pp

Page 62: Create your very own Development Environment with Vagrant and Packer

Créateur de

Puppet DSL - ClassDefinition:  class  composer(      $dir    =  '/usr/local/bin'  )  {      exec  {  'composer_install':          name        =>  'curl  -­‐s  getcomposer.org/installer  |  php',          cwd          =>  $dir,          creates  =>  '/usr/local/bin/composer.phar',          path        =>  '/usr/bin'      }      #  ...  }

Usage:  class  {  'composer':      dir  =>  $composer_dir  }

Page 63: Create your very own Development Environment with Vagrant and Packer

Créateur de

Puppet DSL - Defined TypeDefinition:  define  apache2::vhost(          $server_name      =  $name,          $priority            =  '50',          $document_root  =  '/var/www',          $index_file        =  'index.php',          $service              =  apache2:params:service  )  {      #  ...  }  

Usage:  apache2::vhost  {  'dev.smoov.io':      priority            =>  20,      document_root  =>  '/var/www/smoovio/web',      index_file        =>  'app_dev.php'  }  

Page 64: Create your very own Development Environment with Vagrant and Packer

Créateur de

Puppet DSL - Manifest

#  manifests/dev.pp      #  ...      class  {  'apache2':  }      -­‐>      apache2::vhost  {  'dev.smoov.io':          priority            =>  20,          document_root  =>  '/var/www/smoovio/web',          index_file        =>  'app_dev.php'      }      -­‐>class  {  'php5':  }    -­‐>class  {  'composer':  }      #  ...  

Page 65: Create your very own Development Environment with Vagrant and Packer

Créateur de

Infrastructure as Code

Page 66: Create your very own Development Environment with Vagrant and Packer

Créateur de

To sum up

• with Vagrant you can create Virtual Machines bases on Base Boxes very easy

• Packer adds the possibility to create your own Base Box for those VMs

• It's up to you if you create a Fat or a Thin Base Box

• After creation a Virtual Machine could be provisioned with Puppet (or place your preferred provisioning tool here)

Page 67: Create your very own Development Environment with Vagrant and Packer

Créateur de

Need more Information or Help!?

http://sensiolabs.de/workshops/vagrant/

packer build <your own base box>.json … now!

Frank Stelzer@frastel