Vagrant

2,302 views

Published on

Published in: Technology
  • Be the first to comment

Vagrant

  1. 1. Vagrant: virtualise yourdevelopment environmentMichael PeacockAugust 2012, SuperMondays
  2. 2. About Me @michaelpeacockHead Developer @ Ground SixLeading the development team and managing thedevelopment processWe are a tech investment company: you bring ideas, wepartner and build the productAuthorOccasional Speaker
  3. 3. Virtualisation
  4. 4. Virtualisation Abstraction layer for your hardware
  5. 5. Virtualisation Abstraction layer for your hardware Lets multiple Operating Systems run concurrently, each running on their own virtual hardware
  6. 6. Virtualisation Abstraction layer for your hardware Lets multiple Operating Systems run concurrently, each running on their own virtual hardware Limited by your own physical hardware and your host machine
  7. 7. VirtualBox
  8. 8. VirtualBox www.virtualbox.org
  9. 9. VirtualBox www.virtualbox.org Virtualisation software tool
  10. 10. VirtualBox www.virtualbox.org Virtualisation software tool Provides the hardware abstraction layer and an interface for running VMs
  11. 11. Vagrant
  12. 12. Vagrantwww.vagrantup.com “Create and configure lightweight, reproducable, and portable development environments”
  13. 13. Vagrantwww.vagrantup.com “Create and configure lightweight, reproducable, and portable development environments”Set of command line tools which interacts with andautomates VirtualBox
  14. 14. Vagrantwww.vagrantup.com “Create and configure lightweight, reproducable, and portable development environments”Set of command line tools which interacts with andautomates VirtualBoxDesigned around portability
  15. 15. PuppetProvisioning Service
  16. 16. Why?
  17. 17. Why?Quick start time for new team members Vagrant up: you have a development environment
  18. 18. Why?Quick start time for new team members Vagrant up: you have a development environmentServer configuration can be version controlled
  19. 19. Why?Quick start time for new team members Vagrant up: you have a development environmentServer configuration can be version controlledCross-platform: work from any machine
  20. 20. Why?Quick start time for new team members Vagrant up: you have a development environmentServer configuration can be version controlledCross-platform: work from any machineWork on multiple projects? Easy to pickup an old, legacyproject with its funky server configs
  21. 21. Why?Quick start time for new team members Vagrant up: you have a development environmentServer configuration can be version controlledCross-platform: work from any machineWork on multiple projects? Easy to pickup an old, legacyproject with its funky server configsCan run multiple VMs independently
  22. 22. Why?Quick start time for new team members Vagrant up: you have a development environmentServer configuration can be version controlledCross-platform: work from any machineWork on multiple projects? Easy to pickup an old, legacyproject with its funky server configsCan run multiple VMs independentlyDoesn’t mess with your local, host machine
  23. 23. Some terms
  24. 24. Some termsHost: Your physical machine
  25. 25. Some termsHost: Your physical machineGuest: The VM
  26. 26. Overview
  27. 27. Overview1.Project specific configuration file
  28. 28. Overview1.Project specific configuration file2.Import base box
  29. 29. Overview1.Project specific configuration file2.Import base box3.Boot up Virtual Machine 1.Forward Ports 2.Share folders
  30. 30. Overview1.Project specific configuration file2.Import base box3.Boot up Virtual Machine 1.Forward Ports 2.Share folders4.Configure / provision software
  31. 31. 3 lines: up and running
  32. 32. 3 lines: up and running # download a “base” VM image and give it the name# base for future usevagrant box add base http://files.vagrantup.com/precise64.box# Initialise a new vagrant project# i.e. create a new vagrant filevagrant init# Boot the VM based off the config file we createdvagrant up
  33. 33. 3 lines: up and running # download a “base” VM image and give it the name# base for future usevagrant box add base http://files.vagrantup.com/precise64.box# Initialise a new vagrant project# i.e. create a new vagrant filevagrant init# Boot the VM based off the config file we createdvagrant up
  34. 34. 3 lines: up and running # download a “base” VM image and give it the name# base for future usevagrant box add base http://files.vagrantup.com/precise64.box# Initialise a new vagrant project# i.e. create a new vagrant filevagrant init# Boot the VM based off the config file we createdvagrant up
  35. 35. 3 lines: up and running # download a “base” VM image and give it the name# base for future usevagrant box add base http://files.vagrantup.com/precise64.box# Initialise a new vagrant project# i.e. create a new vagrant filevagrant init# Boot the VM based off the config file we createdvagrant up
  36. 36. 3 lines: up and running # download a “base” VM image and give it the name# base for future usevagrant box add base http://files.vagrantup.com/precise64.box# Initialise a new vagrant project# i.e. create a new vagrant filevagrant init# Boot the VM based off the config file we createdvagrant up
  37. 37. 3 lines: up and running # download a “base” VM image and give it the name# base for future usevagrant box add base http://files.vagrantup.com/precise64.box# Initialise a new vagrant project# i.e. create a new vagrant filevagrant init# Boot the VM based off the config file we createdvagrant up
  38. 38. 3 lines: up and running # download a “base” VM image and give it the name# base for future usevagrant box add base http://files.vagrantup.com/precise64.box# Initialise a new vagrant project# i.e. create a new vagrant filevagrant init# Boot the VM based off the config file we createdvagrant up
  39. 39. 3 lines: up and running # download a “base” VM image and give it the name# base for future usevagrant box add base http://files.vagrantup.com/precise64.box# Initialise a new vagrant project# i.e. create a new vagrant filevagrant init# Boot the VM based off the config file we createdvagrant up
  40. 40. 3 lines: up and running # download a “base” VM image and give it the name# base for future usevagrant box add base http://files.vagrantup.com/precise64.box# Initialise a new vagrant project# i.e. create a new vagrant filevagrant init# Boot the VM based off the config file we createdvagrant up
  41. 41. 3 lines: up and running # download a “base” VM image and give it the name# base for future usevagrant box add base http://files.vagrantup.com/precise64.box# Initialise a new vagrant project# i.e. create a new vagrant filevagrant init# Boot the VM based off the config file we createdvagrant up
  42. 42. 3 lines: up and running # download a “base” VM image and give it the name# base for future usevagrant box add base http://files.vagrantup.com/precise64.box# Initialise a new vagrant project# i.e. create a new vagrant filevagrant init# Boot the VM based off the config file we createdvagrant up
  43. 43. Let’s try it...
  44. 44. SSH into the boxvagrant ssh
  45. 45. Whats happened?
  46. 46. Whats happened?Downloaded a base box of Ubuntu, configured forvagrant
  47. 47. Whats happened?Downloaded a base box of Ubuntu, configured forvagrantCreated a new project Configured to use this base box by default
  48. 48. Whats happened?Downloaded a base box of Ubuntu, configured forvagrantCreated a new project Configured to use this base box by defaultBooted the VM: vagrant up
  49. 49. Whats happened?Downloaded a base box of Ubuntu, configured forvagrantCreated a new project Configured to use this base box by defaultBooted the VM: vagrant upConnected into the VM: vagrant ssh
  50. 50. Managing the VM
  51. 51. Managing the VMSuspend vagrant suspend
  52. 52. Managing the VMSuspend vagrant suspendResume vagrant resume
  53. 53. Managing the VMSuspend vagrant suspendResume vagrant resumeShutdown vagrant halt
  54. 54. Managing the VMSuspend vagrant suspendResume vagrant resumeShutdown vagrant haltTurn on vagrant up (re-runs any provisioning tasks)
  55. 55. Lets try it
  56. 56. Suspend
  57. 57. Resume
  58. 58. Shutdown
  59. 59. When shit hits the fan vagrant destroy
  60. 60. vagrant destroy: a warning
  61. 61. vagrant destroy: a warning The VM is completely wiped
  62. 62. vagrant destroy: a warning The VM is completely wiped Anything that can’t be replicated from: Your project folder (shared folder) Vagrant base box Vagrant File Puppet Manifests
  63. 63. vagrant destroy: a warning The VM is completely wiped Anything that can’t be replicated from: Your project folder (shared folder) Vagrant base box Vagrant File Puppet Manifests WILL BE LOST FOREVER E.g. Databases (hint: use a DB patching or versioning tool in your project folder)
  64. 64. vagrant initCreates a new Vagrantfile, which instructsvagrant what too boot and how to do it. Let’stake a peak.
  65. 65. # -*- mode: ruby -*-# vi: set ft=ruby :Vagrant::Config.run do |config| config.vm.box = "base"end
  66. 66. Box to use# use the box called baseconfig.vm.box = "base"# Fallback URL, if we dont have a box called base, download from hereconfig.vm.box_url = "http://files.vagrantup.com/precise64.box"
  67. 67. Networking
  68. 68. NetworkingHost only VM is only visible to the host machine config.vm.network :hostonly, "192.168.33.10"
  69. 69. NetworkingHost only VM is only visible to the host machine config.vm.network :hostonly, "192.168.33.10"Bridged Network connection to the VM is bridged, letting it appear as a device on the network config.vm.network :bridged
  70. 70. Ports
  71. 71. PortsEasiest way to use the VM is to forward ports Forwards port X on the host To port Y on the guest
  72. 72. PortsEasiest way to use the VM is to forward ports Forwards port X on the host To port Y on the guestconfig.vm.forward_port 80, 4567
  73. 73. Shared Folders
  74. 74. Shared FoldersDefault: Current project (folder containing Vagrantfile) is mapped to /vagrant
  75. 75. Shared FoldersDefault: Current project (folder containing Vagrantfile) is mapped to /vagrantNew shared folders: config.vm.share_folder "name", "/guestmount", "../ hostfolder"
  76. 76. Provisioning So far: we can build and d
  77. 77. Provisioning with Puppetconfig.vm.provision :puppet do |puppet| puppet.manifests_path = "provision/manifests" puppet.manifest_file = "default.pp" puppet.module_path = "provision/modules"end
  78. 78. What can Puppet do
  79. 79. What can Puppet doChecks conditions:
  80. 80. What can Puppet doChecks conditions:Package installed
  81. 81. What can Puppet doChecks conditions:Package installedFile exists / contains / etc
  82. 82. What can Puppet doChecks conditions:Package installedFile exists / contains / etcExec: Run an ad-hock command** doesn’t sit well with Puppets “checking” nature.Execs get re-run (unless you use special conditions) onvagrant up
  83. 83. Packagepackage { "apache2": ensure => present, require => Exec[apt-get update] }
  84. 84. Service service { "apache2": ensure => running, require => Package["apache2"] }
  85. 85. Filefile { /etc/apache2/sites-available/default: source => /vagrant/provision/modules/apache/files/default, owner => root, group => root }
  86. 86. Exec exec { apt-get update: command => /usr/bin/apt-get update, require => Exec[preparenetworking] }
  87. 87. Users & Groups group { "wheel": ! ensure => "present", ! } ! ! exec { "/bin/echo "%wheel ALL=(ALL) ALL" >> /etc/sudoers": ! require => Group["wheel"] ! }
  88. 88. Users & Groups user { "developer": ! ! ensure => "present", ! ! gid => "wheel", ! ! shell => "/bin/bash", ! ! home => "/home/developer", ! ! managehome => true, ! ! password => "passwordtest", ! ! require => Group["wheel"] ! }
  89. 89. Stringing it together class apache { package { "apache2": ensure => present, require => Exec[apt-get update] } file { /var/www/src: ! ensure => link, ! target => /vagrant/src, ! require => Package[apache2] ! } ! ! file { /etc/apache2/sites-available/default: ! ! source => /vagrant/provision/modules/apache/files/default, ! ! owner => root, ! ! group => root ! } service { "apache2": ensure => running, require => Package["apache2"] } }
  90. 90. Using Puppet Modules import "apache" include apache
  91. 91. Explicit ordering stage { first: before => Stage[main] } class {apache: stage => first}
  92. 92. Vagrant up
  93. 93. Multiple VMsWithin the vagrantfile, we can define multiple VMs E.g. need a separate VM for web and db server to mimic production environment? Vagrant::Config.run do |config| config.vm.define :web do |web_config| web_config.vm.box = "web" web_config.vm.forward_port 80, 8080 end config.vm.define :db do |db_config| db_config.vm.box = "db" db_config.vm.forward_port 3306, 3306 end end
  94. 94. Chefwww.opscode.com/chefProvisioning service with Ruby based configurations
  95. 95. Plugins Backing up and snapshotting a VM Automated base box creators
  96. 96. Exporting BoxAlways using the same stack / versions on your box?Slow un-reliable Internet connection means puppettakes an age to run? Export the base box with thesepre-installedvagrant package
  97. 97. Creating your own boxVirtualbox configured: Guest additionsSSH & key based authentication for the vagrant userRuby, RubyGems, Puppet and ChefExport the boxvagrantup.com/v1/docs/base_boxes.html
  98. 98. The FutureMore than just VirtualBox
  99. 99. Caveats
  100. 100. CaveatsFunky permissions Files created on the host shared folder show without permissions on the guest and are inaccessible Solution: upgrade virtualbox guest additions on guest
  101. 101. Caveats Funky permissions Files created on the host shared folder show without permissions on the guest and are inaccessible Solution: upgrade virtualbox guest additions on guest Can’t chmod files within shared folderconfig.vm.share_folder("v-root", "/vagrant", ".", :extra => dmode=777,fmode=777) Thanks @AnthonySterling & @ChrisDKemper
  102. 102. Caveats Funky permissions Files created on the host shared folder show without permissions on the guest and are inaccessible Solution: upgrade virtualbox guest additions on guest Can’t chmod files within shared folderconfig.vm.share_folder("v-root", "/vagrant", ".", :extra => dmode=777,fmode=777) Thanks @AnthonySterling & @ChrisDKemper Waiting for VM to boot Open VirtualBox Cancel, halt the VM and try again
  103. 103. Caveats Funky permissions Files created on the host shared folder show without permissions on the guest and are inaccessible Solution: upgrade virtualbox guest additions on guest Can’t chmod files within shared folderconfig.vm.share_folder("v-root", "/vagrant", ".", :extra => dmode=777,fmode=777) Thanks @AnthonySterling & @ChrisDKemper Waiting for VM to boot Open VirtualBox Cancel, halt the VM and try again
  104. 104. ConclusionVirtualise your development environment Easy to add new team members Easy to work from other machines So long “works on my Machine”
  105. 105. Cheers!Any questions?@michaelpeacockwww.michaelpeacock.co.uk

×