Monday, December 28, 2015

Virtualization with Vagrant and VirtualBox - Quick Start


Getting Ready

  • Install Vagrant & VirtualBox
  • Visit http://www.vagrantbox.es/ and select a Vagrant Box you are interested. (Click on Copy to copy the URL)
  • mkdir -p vagrant/centos65
  • cd vagrant/centos65

Action!!!

$ vagrant init centos65 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.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.



$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos65' 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 'centos65' (v0) for provider: virtualbox
    default: Downloading: https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
    default:
==> default: Successfully added box 'centos65' (v0) for 'virtualbox'!
==> default: Importing base box 'centos65'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: centos65_default_1451366382642_77025
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (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:
    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.6
    default: VirtualBox Version: 5.0
==> default: Mounting shared folders...
    default: /vagrant => C:/vagrant/centos65


$vagrant ssh
You'll get the ssh console access to the new Virtual mahcine

Alternatively you can login into it from VirtualBox as well  ( Select the VM and Click 'show')

Login ID: root
Password: vagrant

This configures network as well

Just run ..

$ ping google.com

to check ..


$vagrant destroy
To reclaim the memory, hard disk from VM.

Repeat the steps from 'vagrant up' to re-use this set-up later

$ vagrant suspend
will save the current running state of the machine and stop it. 

$ vagrant halt
will gracefully shut down the guest operating system and power down the guest machine. The benefit of this method is that it will cleanly shut down your machine, preserving the contents of disk, and allowing it to be cleanly started again.

Configuring Network

By default vagrant configures network, but we can't access this VM from host(laptop) or from other vagrant created VM's.  To enable access to our vagrant VM from other machine, make the below changes in Vagrantfile

#Uncomment the below line
config.vm.network "public_network" 

This enables DHCP to assign a public IP. The drawback is that IP may change in the next boot.
After making above change, run either "vagrant up" or "vagrant reload".





No comments: