top of page
  • Writer's pictureplusforum. in

Introduction to Virtualization automation using Vagrant - Infrastructure as a Code (IAC)

In many of the configuration management and provisioning tools available today, building a VM quickly and repeatedly with consistency of base image is the main focus. Vagrant along with VirtualBox or other virtualization platform serves this purpose to the expectation.

Vagrant is a tool that helps to build VMs, (called ‘Box’ in Vagrant terminology) quickly and with great consistency. So, in no time one can bring up a development or a test environment that consumes least resources of course that depends on the base image, applications part of the base image etc., Also at the same time one can scrap the environment to release resources. The Box or the Vagrant instance, in other words, can also be customized as per environment requirements, build from an existing Box available at ‘vagrantBoxes.es’ or Hashicorp’s repository and also from a VM created using VirtualBox.

‘vagrantfile’ is the file which is used to configure the VagrantBox.

Best practice is to create a dedicated folder for the project and create VagrantBox and then run the vagrant init command. The vagrant file gets created automatically while we run this command.

VirtualBox is a virtualization tool that is available on the term of GPL. The latest version available is 5.1.

Presently VirtualBox supports various guest operating systems like, Windows, Linux, Mac, Unix variants etc.

VirtualBox is equally capable like its counter part, Hyper-V, VMware etc.

Let’s walk thru the installation and basic functionality part of VirtualBox. I have Ubuntu on my laptop, so we will install and run the VirtualBox on Ubuntu. It is equally simple to install VirtualBox on other OS platforms.

To start with using Vagrant for VM automation, let's start with below commands.

$ mkdir vagrantProject

$ cd vagrantproject

$ vagrant init

This command with create the vagrant configuration file, 'vagrantfile'.

One can also create a text file manually and name it as ‘Vagrantfile’ in order the Vagrant to use it.

let’s check the vagrantfile and see what all settings and configurations can be done.

--------------

Vagrant.configre(“2”) do |config|

config.vm.box = “centos/8”

config.vm.box_version = “2011.0”

end

---------

once we have the ‘vagrantfile’ ready to use, use below commands to start the Box.

$ vagrant up

Upon completion of the creating the box, which usually finishes within 2-3 minutes, use below command to connect to the Box.

$ vagrant ssh

So in a matter of 2-3 minutes and typing few commands we were able to brought up fully functional Virtual machine.

To terminate the SSH session, type CTRL+D.

Also show that the newly created Box appears in VirtualBox UI as available VM.

If you want to remove the VM, one can run the command,

$ vagrant destroy

This command will remove the VM and all the resources used by the VM will be freed. This command does not delete the Vagrant Box. In order to remove the Box as well use the command,

$ vagrant box remove

Visit repository for vagrant box files at https://app.vagrantup.com/boxes/search There are similar public repository of vagrant box files at http://www.vagrantbox.es

Let's explore the vagrantfile.

The ‘Synched Folder’ functionality in vagrant is very useful by which one can synch files and folders between, the Host and the Guest machine.

‘/home/vagrant’ on guest machine and ‘/vagrant’ on host machine gets synched automatically with this functionality.

We can also use Vagrant to configure or provision machine with required software pre-installed, this can be done under the provision section.

Also, one can create a shell script for software installation as below. And then call the script inside Vagrantfile.

#!usr/bin/env bash

yum update

yum install -y httpd

if ! [ -L /var/www ]; then

rm -rf /var/www

ln -fs /vagrant /var/www

fi

name this file as bootstrap.sh and place it in the root directory ‘/vagrant’

Now, configure vagrantfile to use this script.

Vagrant.configre(“2”) do |config|

config.vm.box = “centos/8”

config.vm.box_version = “2011.0”

config.vm.provision :shell, path: “bootstrap.sh”

end

After this is done, we just have to run the ‘vagrant up’ command and the VM gets created with mentioned characteristics with Apache webservice installed.

If a guest machine is already configured and running on previous version, then to reconfigure the machine use below command.

$ vagrant reload --provision

In order to access a particular port on guest machine, one has to use port forwarding. By this we can assign a port on host machine to the port on guest machine. e.g., guest: 80, host: 4567.

Vagrant.configure("2") do |config|

config.vm.box = "centos/8"

config.vm.provision :shell, path: "bootstrap.sh"

config.vm.network :forwarded_port, guest: 80, host: 4567

end

Vagrant up and Vagrant reload commands can be used to implement the changes in Vagrantfile.

In order to configure the network part such as IP address, gateways etc., one should modify the Vagrantfile as shown below.

There are two ways to configure, one is to mentioned it as Private network and other as Public Network.

Vagrant.configure("2") do |config|

config.vm.box = "hashicorp/precise64"

config.vm.provision :shell, path: "bootstrap.sh"

config.vm.network “private_network”, type: “dhcp”

or,

config.vm.network “private_network”, ip:”192.168.1.12”

config.vm.network :forwarded_port, guest: 80, host: 4567

end

If we want to configure Ipv6, then the IP address has to mentioned specifically as DHCP does not support Ipv6.

For manually configuring network, we have to disable auto configure feature.

config.vm.network “private_network”, ip:”192.168.1.12”

auto_config: false

Public network

for Public network access configuration, where there is not restriction on access security, the easiest way to configure network is using DHCP. Here we do not need to mention the DHCP verbiage separately.

Vagrant.configure("2") do |config|

config.vm.network "public_network"

end

When DHCP is used IPAddress can be determined by using ‘ifconfig’ command.

While DHCP is used, to use the DHCP assigned default route,

Vagrant.configure("2") do |config|

config.vm.network "public_network",

use_dhcp_assigned_default_route: true

end

Network Interfaces

Incase if more than one network interfaces are available vagrant will ask you to chose which network to bridge to.

Config.vm.network “public network”, bridge: “env1: wifi (personal)”

Here the network name is ‘wifi (personal)’.

If there are multiple networks available, we can configre the vagrantfile to use it with below syntax,

Config.vm.network “public network”, bridge: [“env1: wifi (personal)”“env2: NetXtreme adaptor”]

The guest machine will connect to anyone of the network which it connect to successfully.

Share the VM environment over Internet

One can share the VM environment on internet to access it from anywhere, irrespective of which IP address it has been assigned.

This can be done only after having account on Vagrant cloud.

$ vagrant login

username or email:

password:

You are now logged in

$ Vagrant share

This provides URL that can be shared.

Destroy Vagrant VM

$ Vagrant suspend

This command will save the current state and stop the running VM. To start the VM, use the ‘Vagrant up’ command.

To perform a clean shut down of VM, use

$ Vagrant halt

To remove a VM and delete all related resource, use

$ Vagrant destroy

To rebuild a VM just use

$ Vagrant up

For Vagrant to run with any provider one has to mentioned the same as below.

$ vagrant up –-provider=vmware_fusion.

$ vagrant up –-provider=aws


Package

To package a Box for re-use we can use command,

$ vagrant package [name|id]

This command can only be used with some of the providers like, VirtualBox, Hyper-V and Docker for now. Soon this will be supported for other providers as well.

Options are as,

-- base NAME : NAME should be name or UUID of the machine from VirtualBox GUI.

-- output NAME : resulting package will be saved as NAME.

-- include x,y,z : Additional files that will be packaged with box that can be used by ‘Vagrantfile’.

--vagrantfile FILE: packages a vagrantfile that is used by vagrantfile load order when resulting box is used.

$ Vagrant port [name|id]

This command displays network ports on guest machine mapped to the host machine.

$ vagrant powershell

option -c <command>

This command is used to run a PowerShell command on VagrantBox, where PowerShell is supported.

PROVISIOING: CONFIGURATION MANAGEMENT.

Vagrant supports most of the provision / configuration management tools. Requirement is to have Ansible installed on the host machine.

Using ANSIBLE:

Vagrant.configure("2") do |config|

# Run Ansible from the Vagrant Host

config.vm.provision "ansible" do |ansible|

ansible.playbook = "playbook.yml"

end

end


The fact is VMs build using vagrant can be provisioned using all leading config management tools. Writing and using the cookbooks in chef or the manifest file in Puppet or similar ones (playbook) in Ansible is out of scope for this course. It is very easy to use these tools though.

Similarly Docker can also be used to provision container inside a guest VM.

The vagrantfile can be written with the code to bootstrap the provisioning tools.

The Vagrantfile can be used for multiple machines as well. So a single file is used to bring up multiple machines. The syntax could be something as mentioned below.

Vagrant.configure("2") do |config|

config.vm.provision "shell", inline: "echo Hello"

config.vm.define "web" do |web|

web.vm.box = "apache"

end

config.vm.define "db" do |db|

db.vm.box = "mysql"

end

end

Vagrant Push: The vagrant Push feature can be used to push files, code into your developement environment created using Vagrant Box VMs.

The syntax for the same is,

config.push.define "ftp" do |push|

push.host = "ftp.company.com"

push.username = "username"

push.password = "password"

end

Options available with this feature are as,

dir: base directory containing files to upload

destination: target directory, default is /.

exclude: exclude files from the code to be pushed to remote folder.

include: include files to be pushed to remote folder

To push the application to remote server, use command,

$ vagrant push

For help on any individual command run `vagrant COMMAND -h`

Common commands:

box manages boxes: installation, removal, etc.

connect connect to a remotely shared Vagrant environment

destroy stops and deletes all traces of the vagrant machine

global-status outputs status Vagrant environments for this user

halt stops the vagrant machine

help shows the help for a subcommand

init initializes a new Vagrant environment by creating a Vagrantfile

login log in to HashiCorp's Atlas

package packages a running vagrant environment into a box

plugin manages plugins: install, uninstall, update, etc.

port displays information about guest port mappings

powershell connects to machine via PowerShell remoting

provision provisions the vagrant machine

push deploys code in this environment to a configured destination

rdp connects to machine via RDP

reload restarts vagrant machine, loads new Vagrantfile configuration

resume resume a suspended vagrant machine

share share your Vagrant environment with anyone in the world

snapshot manages snapshots: saving, restoring, etc.

ssh connects to machine via SSH

ssh-config outputs OpenSSH valid configuration to connect to the machine

status outputs status of the vagrant machine

suspend suspends the machine

up starts and provisions the vagrant environment

version prints current and latest Vagrant version



36 views0 comments

コメント


bottom of page