virtualization,

Vagrant,
Provisioning

@codekipple

Carl Hughes

Straw poll

Who has used vagrant before?

Getting started... install these

  • git [1]
  • Virtualbox [2]
  • Vagrant [3]
  1. http://git-scm.com
  2. https://www.virtualbox.org
  3. https://www.vagrantup.com

Notes

Clone the codehub site

git clone https://github.com/CodeHubOrg/CodeHub codehub

It's important that the folder you clone into is called 'codehub' as that is presumed later in our vagrant setup.

Clone the codehub vagrant

git clone https://github.com/CodeHubOrg/codehub-vagrant
git submodule init
git submodule update

It's important that you clone it so the codehub-vagrant and the codehub site both reside in the same directory.

Add vagrant box

vagrant box add codekipple/ubuntu-trusty32-latestpuppet ubuntu-trusty32-puppetlatest.box
vagrant box list

Use vagrant box list to see your newly added vagrant box

Normally this step can be skipped but i created a custom box for use in this workshop. [1]

Once you have downloaded it add it as an available box to vagrant

1. https://www.wetransfer.com/downloads/8d73bf346b6ee7f2c6611dca6831768020150301232619/f3cd7a208c87a3fa8ecff14983367a2920150301232619/0bdff2

Notes

Vagrant up!

Navigate into the codehub vagrant directory and get this thing started.

vagrant up

This may take some time

While that's happening I'll cover what vagrant is, why you might want to use it.

Evolution of my development environment

  • FTP Commando - as coined by Chris Coyier [1]
  • Installers (XAMMP)
  • Virtualisation
  • Version controlled virtualisation with Vagrant

1. https://www.flickr.com/photos/59136411@N04/8480701963/

Notes

What is virtualisation?

Run another operating system as a guest on your host machine.

1. https://www.modern.ie/en-us/virtualization-tools

Notes

A popular use of virtualisation is testing older versions of internet explorer. [1]

Pick a provider

  1. https://www.virtualbox.org
  2. http://www.vmware.com
  3. http://www.parallels.com

Notes

FIrst you going to need a tool to virtualise the guest OS. Vagrant calls these providers.

  • Virtualbox [1]
  • VMware [2]
  • Parallels [3]

Pick a server OS

Next your going to need to decide what server OS you want to use.

Once you have downloaded one you can go through the providers GUI following the steps to create a new virtual machine.

Good news / Bad news

Vagrant is easy!

Sysadmin is hard.

Rabbit hole ahead...

Working with the VM

sudo apt-get install ...
  • Setup networking
  • Install software (apache, mysql ect)
  • Setup sharing (NFS if available)

What if you mess the VM up?

... Start again

Vagrant

Automate the creation of Virtual Machines

Vagrant reads a Vagrantfile and builds a machine based on a template called a box.

Basics

  • Boxes
  • Vagrantfile
  • Vagrant command line

Boxes

Normally a bare set of software. An example Linux box may contain the following:

  • Pre-installed OS
  • SSH user so Vagrant can connect
  • A provisioner (Chef, Puppet, etc) but not strictly required.

Each provider may require additional software. E.g. if you're making a box for VirtualBox, you'll want to include the VirtualBox guest additions [1] so that shared folders work properly.
Software is than provisioned on top of it. Although it does not have to work this way, you could pre install as much as you want [2]

  1. https://www.virtualbox.org/manual/ch04.html
  2. http://laravel.com/docs/4.2/homestead

Notes

Vagrant Cloud [1]

  1. https://vagrantcloud.com

Notes

  • Find boxes to use
  • Store your own boxes for free.

Vagrantfile

Configuration file read by Vagrant

  • machine properties
  • networks
  • what providers / provisioners to use 
     

Ruby based, you can use ruby syntax to assign variables and add some logic if you need it.

Vagrant command line

  1. http://docs.vagrantup.com/v2/cli/index.html

Notes

The docs are great [1]

Commonly used commands

  • vagrant up
  • vagrant ssh
  • vagrant halt
  • vagrant provision
  • vagrant reload
  • vagrant destroy

What if i break my vagrant managed VM?

vagrant destroy
vagrant up

Take a break while you VM rebuilds.

Vagrant file, closer look

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "codekipple/ubuntu-trusty32-latestpuppet"

  if ENV['vm_stages'] == "no"
    _stages = "no"
  else
    _stages = "yes"
  end

  # Networking
  config.vm.network "private_network", type: "dhcp"

  # Due to some issues with the ssh keys that arises when a
  # VM becomes disassociated with vagrant we are just using
  # username and password for now even though it's not as secure
  config.ssh.username = "vagrant"
  config.ssh.password = "vagrant"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder.
  config.vm.synced_folder "../", "/var/www"

  # Provider (VirtualBox, VMWare, ect) configuration
  # Using VirtualBox:
  config.vm.provider :virtualbox do |vb|
    # Boot without headless mode
    vb.gui = true

    # Use VBoxManage to customize the VM. For example to change memory:
    vb.customize ["modifyvm", :id, "--memory", "2024"]
    vb.customize ["modifyvm", :id, "--cpus", "1"]

    # Via http://blog.liip.ch/archive/2012/07/25/vagrant-and-node-js-quick-tip.html
    vb.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
  end

  # Provisioning (puppet, chef, ansible, salt, .sh ect) configuration
  # Using Puppet
  config.vm.provision :puppet do |puppet|
    puppet.manifests_path = "manifests"
    puppet.manifest_file  = "base.pp"
    puppet.module_path = "modules"
    puppet.hiera_config_path = 'hiera/hiera.yaml' # path on host machine
    puppet.working_directory = '/vagrant' # Relative path for hiera data directories

    # use facter to pass a hash of variables to puppet set as facter variables
    puppet.facter = {
      "stages" => _stages
    }

    # puppet.options = "--verbose --debug"
  end
end

Vagrantfile:Networking

  1. NAT - port forwarding
  2. Host-only networking - private network
  3. Bridged networking - public network, VM shows up as device on your network (good for mobile testing)
  4. Can mix and match
# NAT
config.vm.network "forwarded_port",
    guest: 80, host: 8080

# Host only
config.vm.network "private_network", 
    ip: "192.168.50.4"

# Bridged
config.vm.network "public_network"

Configures host/guest communication 

Vagrantfile:Synced folders

  • Virtualbox shared folders (guest additions)
  • VMware shared folders
  • NFS
  • rsync
  • Many more ...
config.vm.synced_folder "../", "/var/www"

Vagrantfile:Providers

Define your provider, pass in provider specific configuration.

  # Provider (VirtualBox, VMWare, ect) configuration
  # Using VirtualBox:
  config.vm.provider :virtualbox do |vb|
    # Boot without headless mode
    vb.gui = true

    # Use VBoxManage to customize the VM. For example to change memory:
    vb.customize ["modifyvm", :id, "--memory", "2024"]
    vb.customize ["modifyvm", :id, "--cpus", "1"]
  end

Vagrantfile:Provisioners

  # Provisioning (puppet, chef, ansible, salt, .sh ect) configuration
  # Using Puppet
  config.vm.provision :puppet do |puppet|
    puppet.manifests_path = "manifests"
    puppet.manifest_file  = "base.pp"
    puppet.module_path = "modules"
    puppet.hiera_config_path = 'hiera/hiera.yaml' # path on host machine
    puppet.working_directory = '/vagrant' # Relative path for hiera data directories
  end

puppet [1], chef [2], ansible [3] [4], salt [5], bash scripts .sh, Many more.

Vagrant can run multiple provisioners, each provisioner doing it's thing and then passing on to the next one [6]

  1. http://puppetlabs.com
  2. https://www.chef.io/chef
  3. http://www.ansible.com
  4. http://phansible.com
  5. http://saltstack.com
  6. http://docs.vagrantup.com/v2/provisioning/basic_usage.html

Notes

Provisioning

  1. http://kartar.net/2010/01/puppet-chef-deterministic-ordering-and-the-much-maligned-dsl/
  2. http://phansible.com/

Notes

Recommend trying ansible first. I've heard a lot of good things.
phansible.com [1] looks particularly appealing.

I went with puppet first, but i'm planning on trying ansible next to see if it works out better. Some reasons I found puppet hard.

  • Uses dependency graph [1] (a plus or negative depending on your perspective)
  • Can't handle recursive files without slowing down massively.
  • I found the syntax very confusing
  • Variable scope seems weird, might be my inexperience.

Puppet

  • hiera
  • manifests
  • modules

 

All configuration particular to puppet.
 

Getting the codehub site working

  • ssh into vagrant and load this dummy database. [1]


     
  • Edit your hosts file [2] to match the VM's ip

3. https://www.wetransfer.com/downloads/5bcfcb695e2265e9a738cd7c104d268820150301042603/f9839583a2ee0562ecc317a5336db42720150301042603/718577

2. http://www.rackspace.com/knowledge_center/article/how-do-i-modify-my-hosts-file

 

Notes

mysql -u root -ppassword codehub_dev < codehub.sql
172.28.128.3 dev.codehub.org.uk

Getting the codehub site working

Flush dns:-

  • Mac
     
  • Windows
     
  • Linux

Visit dev.codehub.org.uk in your browser

dscacheutil -flushcache
ipconfig /flushdns
service nscd restart

Vagrant in the wild ...

  • Laraval - Homestead [1]
  • Ghost [2]
  • WordPress - Varying Vagrant Vagrants [3]
  1. http://laravel.com/docs/4.2/homestead
  2. https://github.com/TryGhost/Ghost-Vagrant
  3. https://github.com/Varying-Vagrant-Vagrants/VVV

Notes

Terminology

  1. Providers - a piece of software used to create and run virtual machines
     
  2. Box - A base starting point to use whilst building a new virtual machine
     
  3. Provisioner - Software used to programatically setup a web server.
     
  4. Provisioning - The act of programatically setting up a web server.

Thanks

Any questions?

Acknowledgements

Mitchell Hashimoto - Not least for creating Vagrant but also for his talk [1] on Vagrant which I used whilst creating these slides.

  1. https://www.youtube.com/watch?v=Im4wNqlolqQ

 

Notes

Vagrant and virtualization

By codekipple

Vagrant and virtualization

  • 613
Loading comments...

More from codekipple