How to use Vagrant to create VM — Part 1

In this article I am going to show the usage of vagrant. I found this is the best tool to build your test environment. Especially in cases when you are working on clusters where you need to setup number of nodes to do the complete setup of cluster. Manual configuration may lead to issue, you can use the single command like “vagrant up” to bring up the whole cluster.

I have used Virtual box as a provider in this article. vagrant supports VMware workstation, VMware fusion and AWS also.

Step 1 : I have downloaded the centos 6.6 .box image which is compatible format while bringing up the VMs using vagrant. Along with box image I have downloaded the vagrant rpm. I have already virtual box installed on my machine. Everthying is available for free download.

[root@test /vagrantwork]# ll
total 427500
-rw-r–r– 1 root root 364979712 May 30 20:27 centos-6.6-x86_64.box
-rw-r–r– 1 root root  72766101 Jul 11 04:18 vagrant_1.7.3_x86_64.rpm

Step 2 : I installed the vagrant rpm using simple command like “rpm -ivh”. No dependency issue. Checking the version after the installation.

[root@test /vagrantwork]# vagrant –version
Vagrant 1.7.3

Step 3 : Creating a vagrant file using below command.

[root@test /vagrantwork]# vagrant init centosfile1 centos-6.6-x86_64.box
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

Step 4 : After issuing above command, a new file is created in current directory by default with name of “Vagrantfile”.

[root@test /vagrantwork]# ll Vagrantfile
-rw-r–r– 1 root root 2963 Aug 26 19:12 Vagrantfile

Step 5 : Lets check the content of that file. As we have used centos box image while using init command to create the file hence it’s showing the URL of the same in below output.

[root@test /vagrantwork]# awk ‘{$1=$1;print}’ Vagrantfile | egrep -v “^(#|$)”
Vagrant.configure(2) do |config|
config.vm.box = “centosfile1”
config.vm.box_url = “centos-6.6-x86_64.box”
end

Step 6 : Lets start the Virtual machine with one go. I have shown all the messages which i got on console while creating a new VM. It took only few minutes to bring up the new VM.

[root@test /vagrantwork]# vagrant up
Bringing machine ‘default’ up with ‘virtualbox’ provider…
==> default: Box ‘centosfile1’ could not be found. Attempting to find and install…
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Box file was not detected as metadata. Adding it directly…
==> default: Adding box ‘centosfile1’ (v0) for provider: virtualbox
default: Unpacking necessary files from: file:///vagrantwork/centos-6.6-x86_64.box
==> default: Successfully added box ‘centosfile1’ (v0) for ‘virtualbox’!
==> default: Importing base box ‘centosfile1’…
==> default: Matching MAC address for NAT networking…
==> default: Setting the name of the VM: vagrantwork_default_1440596686223_32361
==> default: Clearing any previously set forwarded ports…
==> default: Clearing any previously set network interfaces…
==> default: Preparing network interfaces based on configuration…
default: Adapter 1: nat
==> default: Forwarding ports…
default: 22 => 2222 (adapter 1)
==> default: Booting VM…
==> default: Waiting for machine to boot. This may take a few minutes…
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default: Warning: Connection timeout. Retrying…
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest…
default: Removing insecure key from the guest if it’s present…
default: Key inserted! Disconnecting and reconnecting using new SSH key…
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM…
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 4.3.28
default: VirtualBox Version: 5.0
==> default: Mounting shared folders…
default: /vagrant => /vagrantwork

Step 7 : I checked the status of running VM using below command.

[root@test /vagrantwork]# vagrant status
Current machine states:

default                   running (virtualbox)

The VM is running. To stop this VM, you can run `vagrant halt` to
shut it down forcefully, or you can run `vagrant suspend` to simply
suspend the virtual machine. In either case, to restart it again,
simply run `vagrant up`.

Step 8 : If you want to know about the provider like in this case, it’s virtual box, you may issue below command.

[root@test /vagrantwork]# vagrant global-status
id       name    provider   state   directory
————————————————————————
444be5a  default virtualbox running /vagrantwork

The above shows information about all known Vagrant environments
on this machine. This data is cached and may not be completely
up-to-date. To interact with any of the machines, you can go to
that directory and run Vagrant, or you can use the ID directly
with Vagrant commands from any directory. For example:
“vagrant destroy 1a2b3c4d”

Step 9 : As our VM is up and running fine. We can do the ssh without using any password to VM.

[root@test /vagrantwork]# vagrant ssh
Last login: Sat May 30 12:27:44 2015 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.

Step 10 : After logging into VM, I am checking the share filesystem. By default it will share the filesystem in which we have created a Vagrantfile.

[vagrant@localhost ~]$ df -h /vagrant/
Filesystem      Size  Used Avail Use% Mounted on
vagrant          92G   32G   61G  35% /vagrant
[vagrant@localhost ~]$ cd /vagrant/
[vagrant@localhost vagrant]$ ll
total 427500
-rw-r–r–. 1 vagrant vagrant 364979712 May 30 16:57 centos-6.6-x86_64.box
-rw-r–r–. 1 vagrant vagrant  72766101 Jul 11 00:48 vagrant_1.7.3_x86_64.rpm
-rw-r–r–. 1 vagrant vagrant      2963 Aug 26 15:42 Vagrantfile

Step 11 : if I am going to create any file in shared filesystem, changes will be reflected in both ways.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s