Top Banner
Introduction to Vagrant Marcelo Correia Pinheiro Friday, March 29, 13
37
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Vagrant

Introduction to VagrantMarcelo Correia Pinheiro

Friday, March 29, 13

Page 2: Introduction to Vagrant

Friday, March 29, 13

Page 3: Introduction to Vagrant

Friday, March 29, 13

Page 4: Introduction to Vagrant

What is Vagrant?A tool to build development environments based on virtual machines

Focused to create environments that are similar as possible or identical with production servers

Created by Mitchell Hashimoto

Written in Ruby

Initially builted on top of VirtualBox API, today offers VMWare Fusion support (as $79 per licence)

Friday, March 29, 13

Page 5: Introduction to Vagrant

How I install Vagrant?

Get VirtualBox first

Download installer on Vagrant site (Debian, CentOS, Windows, OSX, other OS’s)

Get a Vagrant box

Friday, March 29, 13

Page 6: Introduction to Vagrant

What is a Vagrant Box?

Is a previously builted Vagrant virtual machine image, ready-to-run

Available in a lot of platforms (Linux, Windows, BSD)

You can create one! :)

Friday, March 29, 13

Page 7: Introduction to Vagrant

How I add a box?

Great box repository: www.vagrantbox.es

Run this command:

$ vagrant box add <name> <url> <provider> # virtualbox

Friday, March 29, 13

Page 8: Introduction to Vagrant

How I create a environment?

Inside your project, create a Vagrantfile:

$ vagrant init <your box name>

Friday, March 29, 13

Page 9: Introduction to Vagrant

How I create a environment?

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

Vagrant.configure("2") do |config| # All Vagrant configuration is done here. The most common configuration # options are documented and commented below. For a complete reference, # please see the online documentation at vagrantup.com.

# Every Vagrant virtual environment requires a box to build off of. config.vm.box = "my_precious_box"

# ... # A list of options here # ...end

Friday, March 29, 13

Page 10: Introduction to Vagrant

How I start to use it?

Simply run this command:

$ vagrant up

Friday, March 29, 13

Page 11: Introduction to Vagrant

How I connect to it?

Easy:

$ vagrant ssh

Friday, March 29, 13

Page 12: Introduction to Vagrant

How I stop it?

Easy:

$ vagrant halt

Friday, March 29, 13

Page 13: Introduction to Vagrant

How I restart it?

Easy:

$ vagrant reload

Friday, March 29, 13

Page 14: Introduction to Vagrant

How I access it?You need to set forwarding ports between guest and host to work (bind on 0.0.0.0!)

Just add the following code in your Vagrantfile, restart server and access in browser:

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

Vagrant.configure("2") do |config| # ...

config.vm.network :forwarded_port, guest: 3000, host: 3000

# ...end

Friday, March 29, 13

Page 15: Introduction to Vagrant

How I customize it?You can change memory, CPU cores and other things in Vagrantfile

Just see VBoxManage options

Example:

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

Vagrant.configure("2") do |config| # ...

config.vm.provider :virtualbox do |vb| vb.customize [ 'modifyvm', :id, '--memory', '1024' ] vb.customize [ 'modifyvm', :id, '--cpus', '4' ] end

# ...end

Friday, March 29, 13

Page 16: Introduction to Vagrant

That’s it?Of course, no! :)

It’s time to configure environment using available provisioners to install required software:

Chef Solo

Chef Server

Puppet Apply

Puppet Server

Shell

CFEngine (experimental)

Friday, March 29, 13

Page 17: Introduction to Vagrant

Using Chef Solo

First, run chef-solo installation based on Opscode website in your guest:

$ sudo true && curl -L https://www.opscode.com/chef/install.sh | sudo bash

Friday, March 29, 13

Page 18: Introduction to Vagrant

Using Chef SoloGet all necessary recipes from Opscode site

Great repo with a lot of recipes

Manual recipe dependency resolution sometimes

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

Vagrant.configure("2") do |config| # ...

config.vm.provision :chef_solo do |chef| chef.add_recipe 'apt' chef.add_recipe 'build-essential' chef.add_recipe 'ruby1.9' chef.add_recipe 'mondodb' chef.add_recipe 'redis' end

# ...end

Friday, March 29, 13

Page 19: Introduction to Vagrant

Using Chef SoloOr Install berkshelf

Vagrant plugin very similar to Bundler

generate a Berksfile in app root and run:

$ berks install --path vendor/cookbooks

source :opscode

cookbook 'mysql'cookbook 'nginx', '~> 0.101.5'

Friday, March 29, 13

Page 20: Introduction to Vagrant

Using Chef ServerConfigure Vagrantfile to use a Chef Server as a repository

Requires a URL and a PEM key to connect

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

Vagrant.configure("2") do |config| # ...

config.vm.provision :chef_client do |chef| chef.chef_server_url = 'https://your-chef-server.devops.com' chef.validation_key_path = 'your-private-key.pem' end

# ...end

Friday, March 29, 13

Page 21: Introduction to Vagrant

Using Puppet Apply

Install Puppet from official repository in your guest VM:

# wget http://apt.puppetlabs.com/puppetlabs-release-squeeze.deb# dpkg -i puppetlabs-release-squeeze.deb

Friday, March 29, 13

Page 22: Introduction to Vagrant

Using Puppet Apply

Change Vagrantfile to use Puppet manifest files:

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

Vagrant.configure("2") do |config| # ...

config.vm.provision :puppet do |puppet| puppet.manifests_path = 'manifests' puppet.manifest_file = 'my-devops-puppet-manifest.pp' end

# ...end

Friday, March 29, 13

Page 23: Introduction to Vagrant

Using Puppet Apply

Write Puppet manifest with all software that your app needs

Depending of what you need, some additional configuration is required

Friday, March 29, 13

Page 24: Introduction to Vagrant

Using Puppet ServerChange Vagrantfile to connect on a Puppet Server

Set node hostname if you need

Add some options too

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

Vagrant.configure("2") do |config| # ...

config.vm.provision :puppet_server do |puppet| puppet.puppet_server = 'http://your-master-of-puppets.devops.com' puppet.puppet_node = 'my-precious-puppet.devops.com' puppet.options = '--verbose --debug' end

# ...end

Friday, March 29, 13

Page 25: Introduction to Vagrant

Using ShellCreate a single bash script that installs all you need:

#!/bin/bash

apt-get update

# baseapt-get install --yes python nginx mongodb-server redis-server

# othersapt-get install --yes curl tmux htop

(...)

# some additional configuration here

(...)

Friday, March 29, 13

Page 26: Introduction to Vagrant

Installing software

Easy:

$ vagrant provision

Friday, March 29, 13

Page 27: Introduction to Vagrant

Creating a custom boxYou can create custom boxes to distribute between development teams

Requires a fresh installation of a virtual machine based on Vagrant conventions and some manual configuration

Awesome advantage: you can repackage a existent Vagrant package after updating a existent VM

Next steps are based on Debian distro as VM with VirtualBox as provider

Friday, March 29, 13

Page 28: Introduction to Vagrant

Creating a custom box

Installation steps:

set root password: vagrant

create a user with login vagrant and pwd vagrant

machine name: vagrant-debian-squeeze

machine host: vagrantup.com

Friday, March 29, 13

Page 29: Introduction to Vagrant

Creating a custom boxPost-installation steps:

Install sudo on virtual machine

Add a group permission with visudo:

%admin ALL=NOPASSWD: ALL

Download SSH insecure pair files:

https://github.com/mitchellh/vagrant/tree/master/keys/

Save public key on GUEST in ~/.ssh/authorized_keys and all keys in HOST

Or generate a custom pair of SSH keys and distribute it

Friday, March 29, 13

Page 30: Introduction to Vagrant

Creating a custom box

Post-installation steps:

Install VirtualBox Guest Additions with /Cmd|Ctrl/-D

Remove pre-installed packages:

# apt-get remove --purge virtualbox-ose-*

Friday, March 29, 13

Page 31: Introduction to Vagrant

Creating a custom box

Post-installation steps:

VirtualBox needs xorg drivers, kernel headers and gcc to correctly build Guest Additions kernel module. Run:

# apt-get install linux-headers-$(uname -r) build-essential xorg

Friday, March 29, 13

Page 32: Introduction to Vagrant

Creating a custom box

# mount /media/cdrom# sh /media/cdrom/VBoxLinuxAdditions.run

Post-installation steps:

Run VirtualBox Guest Additions installer:

Friday, March 29, 13

Page 33: Introduction to Vagrant

Creating a custom box

After all steps, shutdown your VM

Execute in host:

$ vagrant package <vm-name> --base <package-name> --output <box-file>

$ vagrant box add <package-name> <box-file> virtualbox

Friday, March 29, 13

Page 34: Introduction to Vagrant

Creating a custom box

If you don’t want to build step-by-step, try veewee

https://github.com/jedi4ever/veewee

Supports VMWare Fusion, VirtualBox and KVM

Enable boxing based on a ISO file

Run as a Vagrant Plugin

Friday, March 29, 13

Page 35: Introduction to Vagrant

Performance TipsSlow I/O on Guest

Enable Host I/O cache on SATA Controller

Slow with CPU-bound tasks

Set Motherboard Chipset to ICH9

Still searching for a solution to slow webserver bootstrap (Ruby / Python)

Anomalous kernel CPU execution time while loading

Friday, March 29, 13

Page 36: Introduction to Vagrant

FAQ Time

It’s time to make a question! :)

Friday, March 29, 13

Page 37: Introduction to Vagrant

Thank you! :)

Friday, March 29, 13