In this article, I am going to teach
you about the quick introduction of the Vagrant and the settings,
configurations, and managing the vagrant Environments.
What
is Vagrant?
Vagrant is an open source tool for
working with virtual environments. Vagrant acts as a wrapper and sits on top of
the virtualization solution (VMware, Hyper-V, VirtualBox or libvirt) and
provides a simple command line interface to manage the VM’s with the custom
configuration.
Why
we use Vagrant?
§ No need to learn different CLI command of Virtualization providers,
vagrant takes cares to manage the underlying VM’s with its easy CLI interface.
§ Well defined environments as configuration used to create the
environments are in simple text files. The user can recreate as many
Vagrant Instances (VMs) using the same Vagrant file.
§ Vagrant is capable of executing configuration management software like
Puppet/Ansible/Chef once the base system is ready (using the box). This help o
setup the environment (System + Application) on the target machines in an
automated way.
§ Developer can create/destroy multiple development environments in
minutes.
§ As vagrant is wrapper only, you can choose the guest OS images supported
by the virtualization platform you choice. Example on CentOS 7, you may run
CentOS 6 VM.
How
does Vagrant work?
Vagrant performs the following tasks
with the single command:
§ Creates a VM using base images.
§ Configures the virtual machine specific settings and configuration using
the Vagrantfile.
§ Automates the configuration management of the virtual machine using the
configuration management software.
Workflow: the below image will explain how Vagrant environments will looks like.
Vagrant
Terminology:
Box: A box is a packaged Vagrant environment, typically a virtual
machine.
Provider: A provider provides virtualization support. Providers can be of 02
types;
§ Local: VirtualBox,VMware,Docker,Hyper-V
§ Remote: AWS Cloud, Openstack
Vagrantfile: Configuration file for the target machine’s config. Contains
information like
§ Which Base Image to use.
§ What will be the hostname?
§ What will be the network?
§ Any specific compute.
§ Provisioner Config (Shell/Automation Tools)
Provisioner: A provisioner is a tool to set up the virtual environment
and can be as simple as a shell script, also it can be automation
toll-like Chef, Ansible or Puppet.
The
example of Shell Provisioner:
1
2
3
4
5
|
config.vm.provision
"shell", inline: <<-SHELL
useradd demouser
yum install httpd
systemctl start httpd.service
SHELL
|
The
Example of Automation Tool Provisioner:
1
2
3
4
5
|
config.vm.provision
"ansible" do |ansible|
ansible.verbose =
"v"
ansible.playbook =
"playbook.yml"
end
end
|
LAB
Details and Pre-Req’s:
In this session, I am going to
demonstrate vagrant on a Linux machine. Details are:
Host: CentOS 7.3 x86_64
Host Platform: VMware
Workstation 12
Hardware Virtualization:
AMD-v/Intel VT-x
Host Network: VM have 02
vNICs
§ vNIC1 : Host Only (Host Only Connectivity) 90.10.10.10/24
§ vNIC2 : Bridged (Internet
Connectivity) 192.168.1.20/24
VirtualBox Version
: 5.1.x86_64
Vagrant Version: 1.9.3
Update the system and install the
pre-req packages for VirtualBox to work.
1
|
# yum update –y
|
1
|
# yum install gcc
make kernel-devel -y
|
Not mandatory, but let’s install “X
Windows”, to easily look into the VirtualBox issues if any.
1
|
# yum
groupinstall "X Window System"
|
Take a reboot of the system.
1
|
# init 6
|
Vagrant
Installation:
1. Download the latest available
versions of Virtualbox and Vagrant software from the download pages of the
products.
§ VirtualBox: https://www.virtualbox.org/wiki/Linux_Downloads
§ Vagrant: https://www.vagrantup.com/downloads.html
2. Once software installers
downloaded (they are rpm’s generally), transfer those to the machine and it is
going to be configured as a Vagrant server.
3. Install the Virtualbox and Vagrant
RPM.
1
|
# yum
localinstall /software/VirtualBox-5.1-5.1.18_114002_el7-1.x86_64.rpm
|
1
|
# yum
localinstall /software/vagrant_1.9.3_x86_64.rpm
|
1
|
#
/sbin/vboxconfig
|
“vboxconfig” will install, configure
the virtualbox modules and start the service.
Validate
the Installation:
1
|
# vboxmanage
--version
|
1
|
# vagrant
--version
|
vboxmanage is the CLI provided by the
VirtualBox package.
Downloading
Base Image:
Though you
can build a virtual machine from scratch, but that is going to a time taking
process. You can find different images from Atlas ( Vagrant
Repo) for your Virtualization Provider.
Vagrant uses a base image to quickly
clone a virtual machine. These base images are known as “boxes”.
Vagrant
Image Repository:
To
download the base Image
1
|
# vagrant box add
centos/7
|
Note – In the above example, we are
downloading base image with name “centos/7”.
Image download will take some time
depending on your internet connectivity as Image will download the Atlas repo.
Once box downloads successful, you
can validate it by running the below command.
1
|
# vagrant box
list
|
Set
the Path of Virtual Machine
1
|
# vboxmanage
setproperty machinefolder /app_store/virtual_machines
|
Initializes
a new Vagrant environment:
1
|
# mkdir
/path/to/directory
|
1
|
# cd
/path/to/directory
|
1
|
# vagrant init
<box_image>
|
Example:
1
|
# mkdir
/app_store/centos7_demo
|
1
|
# cd
/app_store/centos7_demo
|
1
|
# vagrant init
centos/7
|
“vagrant init “ creates Vagrantfile
in the current directory, you can customize the file as per requirement.
Start
and provision the vagrant environment:
1
|
# vagrant up
|
Connect
to the machine via SSH:
1
2
3
4
5
|
# vagrant ssh
Or
# ssh -p
<port> 127.0.0.1 -l vagrant
|
Please
Note – when you run multiple vagrant
machines, vagrant allocates new SSH port forwarding for every machine.
Default credentials for the Vagrant
machine are generally “vagrant/vagrant”.
1.
List all the VMs:
1
|
# vagrant
global-status
|
2.
To know the vagrant port mapping:
1
|
# vagrant port
<vagrant machine>
|
3.
To display IP address of guest machine:
1
|
# vagrant ip-show
|
Please Note – you need to install
“vagrant-ip-show” before you run the below-mentioned command.
1
|
# vagrant plugin
install vagrant-ip-show
|
Often
used Vagrant CLI commands: This below
commands will help you to handle Vagrant environments.
1.
Outputs status of the vagrant machine:
1
|
# vagrant status
|
2.
Outputs status Vagrant environments for this user:
1
|
# vagrant
global-status
|
3.
Starts and provisions the vagrant environment:
1
|
# vagrant up
|
4.
Stops the vagrant machine:
1
|
# vagrant halt
|
5.
Suspends the machine:
1
|
# vagrant suspend
|
6.
Stops and deletes all traces of the vagrant machine:
1
|
# vagrant destroy
|
7.
Restarts vagrant machine loads new Vagrantfile config:
1
|
# vagrant reload
|
8.
Displays information about guest port mappings:
1
|
# vagrant port
|
Summary: In this article, I have tried to explain you the Vagrant Environments
and how it will useful for everyone. if you have any suggestion/queries please
comment on below. I will help you out to provide the response as my best.