Vagrant : quickly deploy virtual machines

From Deimos.fr / Bloc Notes Informatique
Jump to: navigation, search
Vagrant

Software version
Operating System Debian 7
Website Vagrant Website
Last Update 13/03/2014
Others VirtualBox

1 Introduction

Vagrant provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility of you and your team.

To achieve its magic, Vagrant stands on the shoulders of giants. Machines are provisioned on top of VirtualBox, VMware, AWS, or any other provider. Then, industry-standard provisioning tools such as shell scripts, Chef, or Puppet, can be used to automatically install and configure software on the machine.

2 Installation

We need to install those prerequisites in order to install VirtualBox in the wished version :

Command aptitude
aptitude install libsdl-ttf2.0-0:amd64 gcc-4.6-base:amd64 cpp-4.6 dkms gcc-4.6 linux-headers-amd64 linux-kbuild-3.2

Then get this VirtualBox and vagrant version ot avoid incompatibility versions :

Command wget
wget http://download.virtualbox.org/virtualbox/4.2.12/virtualbox-4.2_4.2.12-84980~Debian~wheezy_amd64.deb
wget http://files.vagrantup.com/packages/7e400d00a3c5a0fdf2809c8b5001a035415a607b/vagrant_1.2.2_x86_64.deb

3 Usage

Each created instances should have it's own folder. For example you can do this kind of hierarchy :

.
|-- Vagrant
|   |-- vm1
|   |-- vm2
|   `-- vm3

3.1 Add an image

The first thing you have to do is to add an image. You can find several ones here[1] or to the official Vagrant Cloud[2]. Let's take a Debian Squeeze for example (change squeeze name if you want) :

Command vagrant
vagrant box add squeeze http://www.emken.biz/vagrant-boxes/debsqueeze64.box

This will download and store the image in ~/.vagrant.d/boxes/.

Here is an Wheezy image :

Command
vagrant box add deimosfr/debian-wheezy

Or Jessie:

Command
vagrant box add deimosfr/debian-jessie

3.2 Deploy an image

To deploy a downloaded box image, simply run that :

Command vagrant
vagrant init <squeeze>

Replace squeeze by what the name of the box. This will create a Vagrantfile file.

3.3 Start the image

To start an image, it's simple :

Command vagrant
vagrant up

3.4 Stop the image

You can shutdown :

Command vagrant
vagrant halt

3.5 Connect to the image

To connect through ssh, it's simple :

Command vagrant
vagrant ssh

3.6 List all boxes machines

To list all available boxes on your machine :

Command vagrant
> vagrant box list
squeeze (virtualbox)

4 Plugins

4.1 VirtualBox Guest Additions

To avoid reinstalling manually each new version of guests, here is a plugin that will do it for you each time you boot a vm ! Install the plugin:

Command
vagrant plugin install vagrant-vbguest

That's it :-). Now start a VM and if VirtualBox Guests Additions are not at the latest version, they will automatically be updated.

5 Example

5.1 Ceph

Here is an example for 6 VMs with 2 interfaces :

Configuration File Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
ENV['LANG'] = 'C'
 
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
 
# Insert all your Vms with configs
boxes = [
    { :name => :mon1, :role => 'mon'},
    { :name => :mon2, :role => 'mon'},
    { :name => :mon3, :role => 'mon'},
    { :name => :osd1, :role => 'osd', :ip => '192.168.33.31'},
    { :name => :osd2, :role => 'osd', :ip => '192.168.33.32'},
    { :name => :osd3, :role => 'osd', :ip => '192.168.33.33'},
]
 
$install = <<INSTALL
wget -q -O- 'https://ceph.com/git/?p=ceph.git;a=blob_plain;f=keys/release.asc' | sudo apt-key add -
echo deb http://ceph.com/debian/ $(lsb_release -sc) main | sudo tee /etc/apt/sources.list.d/ceph.list
aptitude update
aptitude -y install ceph ceph-deploy openntpd
INSTALL
 
Vagrant::Config.run do |config|
  # Default box OS
  vm_default = proc do |boxcnf|
    boxcnf.vm.box       = "deimosfr/debian-wheezy"
  end
 
  # For each VM, add a public and private card. Then install Ceph
  boxes.each do |opts|
    vm_default.call(config)
    config.vm.define opts[:name] do |config|
        config.vm.network   :bridged, :bridge => "eth0"
        config.vm.host_name = "%s.vm" % opts[:name].to_s
        config.vm.provision "shell", inline: $install
        # Create 8G disk file and add private interface for OSD VMs
        if opts[:role] == 'osd'
            config.vm.network   :hostonly, opts[:ip]
            file_to_disk = 'osd-disk_' + opts[:name].to_s + '.vdi'
            config.vm.customize ['createhd', '--filename', file_to_disk, '--size', 8 * 1024]
            config.vm.customize ['storageattach', :id, '--storagectl', 'SATA', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', file_to_disk]
        end
    end
  end
end

To boot them, simply "vagrant up".

6 Conclusion

I've wrote here basics commands as an introduction but you can do more than that with Vagrant. Read the documentation for more informations[3].

7 FAQ

7.1 VirtualBox is complaining that the kernel module is not loaded

If you got this kind of error message while doing a "vagrant up" command :

VirtualBox is complaining that the kernel module is not loaded. Please
run `VBoxManage --version` to see the error message which should contain
instructions on how to fix this error.

You need to install your kernel sources or kernel headers package and launch modules compilation :

Command
> /etc/init.d/vboxdrv setup
[ ok ] Stopping VirtualBox kernel modules:.
[ ok ] Recompiling VirtualBox kernel modules:.
[ ok ] Starting VirtualBox kernel modules:.

8 References

  1. ^ http://www.vagrantbox.es/
  2. ^ https://vagrantcloud.com/discover/featured
  3. ^ http://docs.vagrantup.com