Tuesday, February 17, 2015

Vagrant up and running



Until very recently, I have had little exposure to Linux, my knowledge of Linux was limited to a few commands such as “cd, ls, cp”. I had little need to use Linux in my work, so never felt the need to learn it. But a couple of months ago, I caught the fever of Docker, and wanted to put the application my team was working on in Docker. My journey took me to learn Vagrant, Python, Linux, Docker, zookeeper and ansible. It has been a very interesting journey, but like many cutting-edge technologies, it is hard to find solutions for problems that pop up in my experiment, I had to google, try, google, try until I hit upon the missing piece. So I want to write down the most tricky problems I have encountered so you that do not have to scratch your head and wonder how something seemingly so simple can be so hard to solve. 

This blog is about installing Vagant. 

There have been a lot of interesting discussions about whether Docker renders Vagrant unnecessary, to me, the discussion is moot - it is naturally to use them both. My physical computer runs Win7, I want to have a complete Linux OS, and Vagrant is perfect in managing virtual box.  The state of my machines will be:


Install VirtualBox

Download Virtualbox for windows from http://download.virtualbox.org/virtualbox/4.3.22/VirtualBox-4.3.22-98236-Win.exe and install it.

 

Install Vagrant

Download Vagrant for windows from https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.2.msi and install it.


I also need to install some plugins. The plugin I have to install is vagrant-proxyconf. Living in mainland China, I have to use proxy to connect to the outside world. 2000 years ago, we built the Great Wall to stop invasion from the north; today, we’ve built GFW (Great Firewall) to stop mind corruption from the west. We haven’t really evolved.

Open windows command line, and install vagrant plugins:
vagrant plugin install vagrant-proxyconf
vagrant plugin install vagrant-timezone
vagrant plugin list

Install Ubuntu box

There are many public boxes available in http://www.vagrantbox.es/.

Install a base box is deadly simple, but wait, again I need to configure proxies for my win7 machine with two environmental variables, otherwise I won't be able to download boxes:

HTTP_PROXY=https://<user>:<password>@<proxy-host>:<proxy-port>
HTTPS_PROXY=https://<user>:<password>@<proxy-host>:<proxy-port> 

Then I just need to run a few vagrant commands:
cd c:\devops\
mkdir ubuntu14.04.1
vagrant init ubuntu14.04.1
vagrant up
vagrant ssh


Let us see what each step does. 


Vagrant will download the box into C:\Users\apple\.vagrant.d\boxes\ubuntu14.04.1, which is only 382MB.


vagrant init ubuntu14.04.1

Vagrant will create a default Vagrant file in the current directory using the box ubuntu14.04.1.


vagrant up

This command throws the following error:

    default: No guest additions were detected on the base box for this VM! Guest

    default: additions are required for forwarded ports, shared folders, host on
ly
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default:
    default: This is not an error message; everything may continue to work prope
rly,
    default: in which case you may ignore this message.
==> default: Mounting shared folders...
    default: /vagrant => C:/devops/ubuntu14.04.1
Failed to mount folders in Linux guest. This is usually because the "vboxsf" file system is not available. Please verify that the guest additions are properly installed in the guest and can work properly.

But despite the error, an Ubuntu virtual VM is up and running, how cool is that!
VirtualBox stores the VM image at C:\Users\apple\VirtualBox VMs, which is 1.31GB.


vagrant ssh

SSH to Ubuntu using user/password vagrant/vagrant. If you met the following issue:

`ssh` executable not found in any directories in the %PATH% variable. Is an SSH client installed? Try installing Cygwin, MinGW or Git, all of which contain an SSH client.

Just following the instruction, and install something that contains SSH, and put the path into windows %PATH%.

Note, if you install Cygwin, make sure you install SSH with Cygwin, SSH is by default not installed. Cygwin 64bit is very slow in my computer, and vagrant ssh takes a long time to logon. So I use Git.

 

Install Guest Addition on Ubuntu

The error thrown out by vagrant up is because vagrant fails to share folders between the host (my Win7 physical machine) and the guest (the Ubuntu). To support folder sharing, the Ubuntu has to install guest addition, here is how.

Start Ubuntu inside Virtual Box, click on Device/ Insert guest additions cd image:


Check Ubuntu settings, make sure the guest addition is mounted:

Now run the following commands (you can run them after you vagrant ssh Ubuntu, or logon Ubuntu directly using vagrant/vagrant and run them inside Ubuntu): 

sudo mount /dev/cdrom /mnt;cd /mnt;sudo ./VBoxLinuxAdditions.run;sudo reboot

Package Ubuntu box

I’ve gone to a lot of trouble to install guest additions on Ubuntu, surely I do not want to go through it for every new Ubuntu box I download. I can package this Ubuntu as a basebox and reuse it.

cd c:\devops\ubuntu14.04.1
vagrant package

This will create a package.box.

Now you can use the packaged box to create other VMs:

cd c:\devops
mkdir ubuntu14.04.2
cd ubuntu14.04.2
vagrant box add ubuntu14.04.2 C:\devops\ubuntu14.04.1\package.box
vagrant init

Before you run vagrant up, you need to change config.vm.box  in ubuntu14.04.2\Vagrant file. By default, it is “base”, change it to “ubuntu14.04.2”.

No comments:

Post a Comment