Yearly Archives: 2011

Vagrant: Productivity for both Developers and SysAdmins

Update Feb 22, 2012: I just found out another ‘cool’ tool at http://keminglabs.com/vagrant-ec2/ using vagrant for local image and with EC2 instance.

Vagrant is a productive tool in building and distributing virtual images for development.

1. Setup Virtualbox
2. Setup RVM
3. Install Ruby 1.9.2 inside RVM
4. Install Vagrant

$ gem install vagrant
$ vagrant box add base http://files.vagrantup.com/lucid32.box

5. Install chef (optional)

Once everything is set, a simple configuration on Vagrantfile can have the lines:

Vagrant::Config.run do |config|
  config.vm.box = "lucid32"
  config.vm.network "33.33.33.10"

end 

Start initializing and running our base image:

$ vagrant init base
$ vagrant up 
$ vagrant ssh

Now to start with chef-solo, you can download the getting started cookbooks:

Adding more cookbooks later, I need to generate the metadata.json.

$ cd /tmp
$ wget https://github.com/opscode/cookbooks/tarball/master -O cookbook-ops.tgz
$ tar -zxvf cookbook-ops.tgz
$ cd ops* 
$ knife cookbook metadata -a  -o ./ 

Generating metadata for 7-zip from /tmp/opscode-cookbooks-c156223/7-zip/metadata.rb
Generating metadata for activemq from /tmp/opscode-cookbooks-c156223/activemq/metadata.rb
Generating metadata for ant from /tmp/opscode-cookbooks-c156223/ant/metadata.rb
...

In my case, I copy only the nginx cookbook to vagrant cookbook directory, now the cookbook directory:

cookbooks/ 
    apache2      
	apt          
	nginx        
	vagrant_main

I need to edit the cookbooks/vagrant_main/recipes/default.rb for nginx:

require_recipe "apt"
require_recipe "nginx"

Now the new Vagrantfile will have the lines:

Vagrant::Config.run do |config|

  config.vm.box = "lucid32"
  config.vm.network "33.33.33.10"

  config.vm.provision :chef_solo do |chef|
     chef.log_level = "debug"
     chef.cookbooks_path = "cookbooks"
     chef.add_recipe "vagrant_main"
   end
end

And then we can start provisioning:

$ vagrant provision 

[default] Running provisioner: Vagrant::Provisioners::ChefSolo...
[default] Generating chef JSON and uploading...
[default] Running chef-solo...
[default] stdin: is not a tty
...

$ vagrant ssh
Linux lucid32 2.6.32-33-generic #70-Ubuntu SMP Thu Jul 7 21:09:46 UTC 2011 i686 GNU/Linux
Ubuntu 10.04.3 LTS

Welcome to Ubuntu!
 * Documentation:  https://help.ubuntu.com/
Last login: Fri Dec  2 01:27:30 2011 from 10.0.2.2
vagrant@lucid32:~$ sudo netstat -lp | grep www
tcp        0      0 *:www                   *:*                     LISTEN      739/nginx       
vagrant@lucid32:~$ 

Our nginx is running. 🙂 Let’s package our new nginx VM.

$ vagrant package --include Vagrantfile
[default] Attempting graceful shutdown of linux...
[default] Clearing any previously set forwarded ports...
[default] Cleaning previously set shared folders...
[default] Creating temporary directory for export...
[default] Exporting VM...
Progress: 0%Progress: 14%Progress: 28%Progress: 31%Progress: 34%Progress: 36%Progress: 46%Progress: 48%Progress: 57%Progress: 61%Progress: 68%Progress: 83%Progress: 86%Progress: 93%[default] Compressing package to: /Users/cocoy/vagrant/lucid32/package.box
[default] Packaging additional file: Vagrantfile

For distribution and restoring the new image:

$ vagrant box add newlucid package.box 
[vagrant] Downloading with Vagrant::Downloaders::File...
[vagrant] Copying box to temporary location...
[vagrant] Extracting box...
[vagrant] Verifying box...
[vagrant] Cleaning up downloaded box...

Note: We can edit the Vagrantfile to change the IP first if we need to.

$ vagrant up
[default] Importing base box 'newlucid'...
[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and more to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.
...

Distributing VM’s for developers to sync configs/software packages/files with production setup is easy.
Easy replication of servers via chef-solo recipes.
Testing integration with different parts of the system may it be database, cache servers, or web servers.

Good references about Vagrant:
Getting-the-most-out-of-Chef-with-Scalarium-and-vagrant
Vagrant-testing-testing-one-two
Vagrant Getting Started

Nginx here we go!

For the past few months, I have been busy with my personal and client’s projects.

I’ve moved the site to use Nginx + PHP-fpm and been running smoothly for several days now.
So far I’m happy with the site’s performance 🙂