4 Vagrant
4 Vagrant
Introduction to Vagrant
Vagrant is a tool for building and managing virtual machine environments in a single workflow.
With an easy-to-use workflow and focus on automation, Vagrant lowers development environment
setup time, increases productivity.
1. Why Vagrant?
If I put it in a single sentence, there is no such tool as vagrant for managing virtual machine
lifecycle. Vagrant gets integrated with hypervisors like VirtualBox and gives as a command line
interface to automate vm lifecycle.
Then, industry-standard provisioning tools such as shell scripts, Ansible, Chef, or Puppet, can
automatically install and configure software on the virtual machine.
As per Vagrant Documentation.
For Developers
If you are a developer, Vagrant will isolate dependencies and their configuration within a single
disposable, consistent environment, without sacrificing any of the tools you are used to working
with (editors, browsers, debuggers, etc.). Once you or someone else creates a single Vagrantfile,
you just need to vagrant up and everything is installed and configured for you to work. Other
members of your team create their development environments from the same configuration, so
whether you are working on Linux, Mac OS X, or Windows, all your team members are running
code in the same environment, against the same dependencies, all configured the same way. Say
goodbye to "works on my machine" bugs.
For Operators
If you are an operations engineer or DevOps engineer, Vagrant gives you a disposable
environment and consistent workflow for developing and testing infrastructure management scripts.
You can quickly test things like shell scripts, Chef cookbooks, Puppet modules, and more using
local virtualization such as VirtualBox or VMware. Then, with the same configuration, you can test
these scripts on remote clouds such as AWS or RackSpace with the same workflow. Ditch your
custom scripts to recycle EC2 instances, stop juggling SSH prompts to various machines, and start
using Vagrant to bring sanity to your life.
For Designers
If you are a designer, Vagrant will automatically set everything up that is required for that web app
in order for you to focus on doing what you do best: design. Once a developer configures Vagrant,
you do not need to worry about how to get that app running ever again. No more bothering other
developers to help you fix your environment so you can test designs. Just check out the code,
vagrant up, and start designing.
For Everyone
Vagrant is designed for everyone as the easiest and fastest way to create a virtualized environment!
Open git installable and follow the Installation wizard, take all the default settings in the
wizard.
4.Vagrant Cloud.
The biggest USP of vagrant is the vagrant cloud. There is no OS installation procedures when we
use vagrant. Vagrant solves that problem by giving us vagrant boxes.
Vagrant boxes are VM images which already has the OS and softwares installed in them. We just
need to download these boxes from vagrant cloud by using our vagrant CLI. Once the box is
downloaded you can create as many as VM’s from them.
https://atlas.hashicorp.com/boxes/search?utm_source=vagrantcloud.com&vagrantcloud=1
In the next exercise, we will setup an Ubuntu and Centos VM with vagrant.
5. Vagrantfile
The first step of setting up VM with vagrant is to create Vagrantfile. Vagrantfile has all the
information about the virtual machine, which vagrant will read and configure vm for us.
The primary function of the Vagrantfile is to describe the type of machine required for a project,
and how to configure and provision these machines. Vagrantfiles are called Vagrantfiles because
the actual literal filename for the file is Vagrantfile.
The syntax of Vagrantfiles is Ruby, but knowledge of the Ruby programming language is not
necessary to make modifications to the Vagrantfile, since it is mostly simple variable assignment. In
fact, Ruby is not even the most popular community Vagrant is used within, which should help show
you that despite not having Ruby knowledge, people are very successful with Vagrant.
Go to centos-vm folder and check the file => Open it with Notepad++ editor.
9. Vagrant up
Go to centos-vm folder from git bash and run “vagrant up” command.
Vagrant up commands read the configuration from the Vagrantfile.
This is the single most important command in Vagrant, since it is how any Vagrant machine
is created. Anyone using Vagrant must use this command on a day-to-day basis.
As we just specified the boxname its going to take other settings as defaults. Default settings
include RAM size, CPU, hard disk, network etc.
Follow the same procedure in ubuntu-vm folder, Open another git bash prompt.
1) cd /c/vagrant-vms/ubuntu-vm
2) vagrant init ubuntu/trusty64
3) vagrant up
Halting the virtual machine by calling vagrant halt will gracefully shut down the guest operating
system and power down the guest machine. You can use vagrant up when you are ready to boot
it again. 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. The downside is that it'll take
some extra time to start from a cold boot, and the guest machine still consumes disk space.
Destroying the virtual machine by calling vagrant destroy will remove all traces of the guest
machine from your system. It'll stop the guest machine, power it down, and remove all of the
guest hard disks. Again, when you are ready to work again, just issue v agrant up. The benefit of
this is that no cruft is left on your machine. The disk space and RAM consumed by the guest
machine is reclaimed and your host machine is left clean. The downside is that vagrant up to get
working again will take some extra time since it has to reimport the machine and re-provision
it.
Adding a box
We can also download a box and can be used later in our vagrantfile.
Boxes in vagrant cloud and different providers like virtualbox, libvirt, vmware desktop etc.
We may also need to select the provider of the box.
Vagrant reload
After changing any vm setting in the vagrantfile, you may need to reload its changes to an already
running vm.
Vagrant reload command will reboot the vm and load all the latest changes, you may need to
provide a --provision option to force the provisioners to run.
Memory/RAM modification.
➔ Open your vagrantfile and find config.vm.provider setting.
➔ Uncomment below mentioned three lines of code and change vb.memory as per requirement.
➔ Run vagrant up if the vm is halted or vagrant reload if the vm is already running. Verify the Base
memory setting from virtualbox.
➔ Login to vm and check its IP, you will see eth1 as bridged interface and will get IP from your
networks
16. Provisioning
Provisioning helps execute command or script as soon as the vm comes up.
Provisioners in Vagrant allow you to automatically install software, alter configurations, and more
on the machine as part of the vagrant up process.
This is useful since boxes typically are not built perfectly for your use case. Of course, if you want
to just use vagrant ssh and install the software by hand, that works. But by using the
provisioning systems built-in to Vagrant, it automates the process so that it is repeatable. Most
importantly, it requires no human interaction, so you can vagrant destroy and vagrant
up and have a fully ready-to-go work environment with a single command. Powerful.
Vagrant gives you multiple options for provisioning the machine, from simple shell scripts to more
complex, industry-standard configuration management systems.
end
Relative paths, such as above, are expanded relative to the location of the root Vagrantfile for your
project. Absolute paths can also be used, as well as shortcuts such as ~ (home directory) and
..(parent directory).
Configuration
Synced folders are configured within your Vagrantfile using the config.vm.synced_folder
method. Usage of the configuration directive is very simple:
end
The first parameter is a path to a directory on the host machine. If the path is relative, it is relative to
the project root. The second parameter must be an absolute path of where to share the folder within
the guest machine. This folder will be created (recursively, if it must) if it does not exist.
Vagrant.configure("2") do |config|
web.vm.box = "apache"
end
db.vm.box = "mysql"
end
end
As you can see, config.vm.define takes a block with another variable. This variable, such as
web above, is the exact same as the config variable, except any configuration of the inner
variable applies only to the machine being defined. Therefore, any configuration on web will only
affect the web machine.
Commands that only make sense to target a single machine, such as vagrant ssh, now require
the name of the machine to control. Using the example above, you would say vagrant ssh
web or vagrant ssh db.
Other commands, such as vagrant up, operate on every machine by default. So if you ran
vagrant up, Vagrant would bring up both the web and DB machine. You could also optionally
be specific and say vagrant up web or vagrant up db.
Vagrant forwarded ports allow you to access a port on your host machine and have all data
forwarded to a port on the guest machine, over either TCP or UDP.
For example: If the guest machine is running a web server listening on port 80, you can make a
forwarded port mapping to port 8080 (or anything) on your host machine. You can then open your
browser to localhost:8080 and browse the website, while all actual network data is being sent
to the guest.
end
This will allow accessing port 80 on the guest via port 8080 on the host.
web.vm.box = "ubuntu/trusty64"
web.vm.hostname = 'web'
end
end
db.vm.box = "ubuntu/trusty64"
db.vm.hostname = 'db'
end
end
end
Vagrantfile to setup three nodes with custom synced directory and private
network.
# README
# Getting Started:
# 2. vagrant up
# 8. vagrant ssh
Vagrant.configure(2) do |config|
config.hostmanager.enabled = true
h.vm.box = "nrel/CentOS-6.5-x86_64"
end
h.vm.hostname = 'puppetmaster'
end
h.vm.box = "nrel/CentOS-6.5-x86_64"
h.vm.hostname = 'lamp'
end
h.vm.box = "ubuntu/trusty64"
h.vm.hostname = 'lamptest'
end
end
# README
# Getting Started:
# 2. vagrant up
# 3. vagrant ssh
Vagrant.configure(2) do |config|
config.hostmanager.enabled = true
config.vm.box = "ubuntu/trusty64"
h.vm.hostname = 'autoserver'
if [ ! -f "/home/vagrant/.ssh/id_rsa" ]; then
fi
cp /home/vagrant/.ssh/id_rsa.pub /vagrant/autoserver.pub
Host *
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
SSHEOF
EOF
end
h.vm.hostname = 'nginx0101'
end
h.vm.hostname = 'apache01'
end
h.vm.hostname = 'apache02'
end
h.vm.hostname = 'mysql01'
end
end
Summary:
✓ Virtualization gave us the power of deploying multiple app in one physical server each
having their own OS.
✓ Hypervisor gives us power and interface to create multiple virtual server also known as VM
on one physical machine.
✓ There are two types of hypervisor, type1 which is installed directly on Hardware and type 2
which gets installed on a host OS.
✓ Virtualbox is type 2 hypervisor which can be installed on Linux and windows machine's.
✓ We can create & manage VM’s and their resources like virtual hard disk, Memory, Vcpu,
network etc through virtualbox interface.
✓ Creating & Managing lot vm from virtualbox interface is time consuming and mundane
task.
Conclusion:
Vagrant is a great tool for our day to day DevOps tasks Maybe you have written a script for
deployment or maybe you are learning any new devops tool, to test all these things you need VM’s.
With Vagrant, we can quickly set up multiple vm’s and start practicing. It’s part of our daily toolkit.
We can also use Ansible, Chef or Puppet code in provisioning part of the Vagrantfile, once we learn
these tools in later chapters you can go ahead and try those. Vagrant can also be used to provision
instances on AWS cloud.
If you are working in DevOps you will encounter with local virtual machines, always use Vagrant
to maintain your local vm setup.