Introduction to Vagrant
A vagrant is a tool for building and managing virtual machine environments during a single workflow. It is really easy-to-use because of its workflow and automation feature, Vagrant increases production parity, lowers development environment setup time, and makes the “works on my machine” excuse a relic of the past.
It provides Infrastructure as a code. Infrastructure as Code (IaC) is the management of infrastructure (networks, virtual machines, networking devices, and connection topology) in a representative model. We need to write code for configuring a real-time environment. For this, we need a virtualization platform.
Before you begin, make sure you have already got a virtualization solution on your system.
Solutions that work with Vagrant include VirtualBox, Docker, VMware, Hyper-V, and custom solutions. We are able to use any hypervisor for the Vagrant environment. Vagrant by default supports with VirtualBox which is Oracle solution. Download and Install VirtualBox from following link “https://www.virtualbox.org”
Why Vagrant?
Vagrant provides reproducible, simple configure and portable work environments designed on top of industry-standard technologies. It is controlled by single continuous progress to achieve maximize the productivity and flexibility of you and your team.
To achieve its magic, Vagrant takes support from the different virtualization tools. Machines area unit provisioned on top of VirtualBox, VMware, AWS, or the other provider. Then, industry-standard provisioning tools such as shell scripts, Chef, or Puppet, can automatically install and configure the so
For Developers
If you are working as a developer, Vagrant can isolate dependencies and their configuration at intervals a single disposable, consistent atmosphere, without sacrificing any of the tools you’re used to operating with (editors, browsers, debuggers, etc.). Once you or some other person creates one Vagrantfile, you only need to vagrant up and everything is installed and designed for you to work. Other members of your team produce their development environments from a similar configuration, therefore whether or not you’re performing on Linux, Mac OS X, or Windows, all of your team members are running code within the same environment, against similar dependencies, all designed a similar method. Say goodbye to “works on my machine” bugs.
For Operators
If you are working as a DevOps engineer or operations engineer Vagrant provides you a disposable environment and consistent workflow for developing and testing infrastructure management scripts. You can quickly check things like shell scripts, cook cookbooks, Puppet modules, and additional using local virtualization such as VirtualBox or VMware.
Then, with a similar configuration, you’ll test these scripts on remote clouds like AWS or RackSpace with a similar workflow. Ditch your custom scripts to recycle EC2 instances, stop juggling SSH prompts to various machines, and begin using Vagrant to bring saneness to your life.
For Designers
If you’re a designer, Vagrant can automatically set everything up that’s needed for that web app in order for you to focus on doing what you are doing best: design. Once a developer configures Vagrant, you are doing not need to worry about the way to get that app running ever again. No additional bothering different developers to help you fix your environment so you’ll be able to check designs. Just check out the code, vagrant up, and begin designing.
For Everyone
Now we all are aware of the Vagrant tool. The vagrant has used as the simple and fastest way to provide a virtualized environment! Because of these features nowadays everyone creates different virtual environments using vagrant.
For Free Demo classes Call: 7798058777
Registration Link: Click Here!
Vagrant Installation Process
- Download and the Virtualization tool. I am using VirtualBox.
Click here to download: https://www.oracle.com/virtualization/technologies/vm/downloads/virtualbox-downloads.html#vbox - To find the latest version of Vagrant, use a web browser to navigate to its official webpage: https://www.vagrantup.com/downloads.html
- You will see a list of all the different supported operating systems, with a 32-bit and 64-bit package for each. Download the appropriate file for your operating system, then run the installer.
Check if the installation was successful
We can use bellow command for checking if vagrant is installed or not
$ vagrant -v
$ vagrant –version
$ vagrant
Understanding Command-Line Interface
Almost all interaction with Vagrant is finished through the command-line interface.
The interface is on the market using the vagrant command and comes installed with Vagrant automatically. The vagrant command, in turn, has several subcommands, like vagrant up and vagrant destroy, etc.
If you run vagrant by itself, assistance will be displayed showing all available subcommands. In addition to the current, you’ll run any Vagrant command with the -h flag to output help that specific command. For example, try running vagrant init -h.
Init
Command: vagrant init [name [url]]
- This initializes the current directory to be a Vagrant environment by creating an initial Vagrantfile if one doesn’t exist already.
- If a primary argument is given, it’ll prepopulate the config.vm.box setting within the created Vagrantfile.
Validate
Command: vagrant validate
This command validates your Vagrantfile.
Plugins
Vagrant comes with several nice options out of the box to induce your environments up and running. Sometimes, however, you want to vary the way Vagrant will one thing or add further functionality to Vagrant. This can be done via Vagrant plugins.
Box
- This command is used to manage (add, list, remove, etc.) boxes.
Box Add
Command: vagrant box add ADDRESS
Box List
Command: vagrant box list
- This command lists all the boxes that are available into the Vagrant.
Box Prune
Command: vagrant box prune
- This command removes old versions of available boxes. If the box is currently in use by any VM which is created by vagrant then vagrant will ask for confirmation.
Box Remove
Command: vagrant box remove NAME
- This command is used to remove a box from Vagrant that matches the given name.
Box Update
Command: vagrant box update
- This command is used to updates the box for the current Vagrant environment if there are updates available.
Up
Command: vagrant up [name|id]
- This command is used to creates and configures guest machines according to your Vagrantfile.
- This is the 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 for creating the automatic virtual environment.
SSH
Command: vagrant ssh [name|id] [– extra_ssh_args]
- This will SSH into a running Vagrant machine and give you access to a terminal shell.
Halt
Command: vagrant halt [name|id]
- This command is used to shuts down the running virtual machine which is created by Vagrant.
- Vagrant will first attempt to gracefully shut down process for the virtual machine by running the guest OS shutdown mechanism. If this procedure will fail, or if the –force flag is specified, Vagrant will effectively just shut off power to the virtual machine.
Suspend
Command: vagrant suspend [name|id]
- This command is used to suspend the guest machine Vagrant is managing, rather than fully shutting it down or destroying it.
- A suspend command stores the exact point-in-time state of the virtual machine so that when you resume it later, it starts running immediately from that point, rather than start booting from the initial phase.
- This generally wants extra disk space to store all the contents of the RAM within your guest machine, but the machine no longer consumes the RAM of your host machine or CPU cycles while it is suspended.
Resume
Command: vagrant resume [name|id]
- This command is used to resumes a Vagrant managed machine that was previously suspended, perhaps with the suspend command.
Destroy
Command: vagrant destroy [name|id]
- This command, first of all, stops the running machine Vagrant is managing and then destroys all resources that were created during the machine creation process.
- After applying this command, your computer should be left at a clean state, as if you never created the guest machine in the first place.
Project Setup
The first step in configuring any Vagrant project and creating a virtual machine using Infrasture as code is to create a Vagrantfile. The purpose of the Vagrantfile is twofold:
For Free Demo classes Call: 7798058777
Registration Link: Click Here!
- Create a project directory to store your project settings and mark this directory as a root directory of your project. Many of the configuration options for creating virtual machines are relative to this root directory.
- Describe the kind of machine and resources you need to run your project with a dedicated environment, as well as what software to install and how you want to access it.
Vagrantfile
The primary function of the Vagrantfile is to describe the type of server machine which is 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 (casing does not matter unless your file system is running in a strict case sensitive mode)
Vagrant is designed to run with one Vagrantfile per project, and the Vagrantfile is supposed to be committed to version control. This functionality allows other developers involved in the project to check out the code, run vagrant up, and be on their way. Vagrantfiles are portable across every platform where the Vagrant tool is supported.
Vagrant has a built-in command for initializing a directory for creating project configuration with Vagrant: vagrant init. For setting up normal project configuration, please follow along in your terminal:
$ mkdir vagrant_getting_started
$ cd vagrant_getting_started
$ vagrant init
Working with Vagrant Boxes
Boxes are the package format for Vagrant environments which is used to create a guest virtual machine. Boxes are the same as images in the Docker concept. A box can be used by anyone on any platform that Vagrant supports to run an identical working environment.
The easiest way to use a box is to add a box from the publicly available remote location of the Vagrant boxes. We can log in with this remote boxes repository page and we can also add and share your own customized boxes on this website.
https://app.vagrantup.com/boxes/search
Adding and Listing Vagrant Boxes
Use the following commands to add and list vagrant boxes.
$ vagrant box add olbat/tiny-core-micro
$ vagrant box add centos/7
$ vagrant box add ubuntu/trusty64
$ vagrant box list
Removing Vagrant boxes
Use the following command for remove vagrant boxes.
$ vagrant box remove olbat/tiny-core-micro
Run Basic VM using Vagrantfile
VagrantFile contained:
Vagrant.configure(“2”) do |config|
config.vm.box = “centos/7”
config.vm.hostname = “WebServer1”
end
Advance Vagrantfile Configuration
Hostname & SYNC Physical machine folder with VMs
VagrantFile contained:
Vagrant.configure(“2”) do |config|
config.vm.box = “centos/7”
config.vm.hostname = “WebServer1”
config.vm.synced_folder “.”, “/sharedir”, type:”virtualbox”
end
Use vagrant reload command for reflecting configure environment
Test shared directory
Port Forwarding Concept
Host: Physical Windows Machine
Guest: Vagrant Virtual Machine
Vagrant.configure(“2”) do |config|
config.vm.box = “centos/7”
config.vm.hostname = “WebServer1”
config.vm.synced_folder “.”, “/sharedir”, type:”virtualbox”
config.vm.network :”forwarded_port”, guest:80,host:9090
end
Login WebServer and setup WebServer using following commands
$ yum install httpd -y
$ systemctl start httpd
$ systemctl enable httpd
$ echo “This is vagrant web page” >> /var/www/html/index.html
vagrant reload
Access localhost:9090 from physical machine browser
Note: If you are facing issues with mounting file system then follow
$ vagrant plugin install vagrant-vbguest
$ vagrant reload
Assigning Private IP address using DHCP
Vagrant.configure(“2”) do |config|
config.vm.box = “centos/7”
config.vm.hostname = “WebServer1”
config.vm.synced_folder “.”, “/sharedir”, type:”virtualbox”
config.vm.network :”forwarded_port”, guest:80,host:9090
config.vm.network “private_network”, type: “dhcp”
end
Testing for IP address
Configure Computing Resources
Check Memory and CPU core status
Vagrant.configure(“2”) do |config|
config.vm.box = “centos/7”
config.vm.hostname = “WebServer1”
config.vm.synced_folder “.”, “/sharedir”, type:”virtualbox”
config.vm.network :”forwarded_port”, guest:80,host:9090
config.vm.network “private_network”, type: “dhcp”
config.vm.provider “virtualbox” do |vb|
vb.memory = “2000”
vb.cpus = “4”
end
end
For Free Demo classes Call: 7798058777
Registration Link: Click Here!
Test Updated Computing Resources Status
Configuring DevOps Environment using Vagrant
1. Initialization of Vagrant repository
2. Install Plugins if necessary
1. Edit Vagrant file as per your configuration
Software Configuration
Install centos 7
Set Hostname
Set IP Address
Sync folder
Port Forwading
update the OS
Install java
Install git
Install maven
Install Ansible
Install Docker
Vagrant file contains
Vagrant.configure(“2”) do |config|
config.vm.define “master” do |master|
master.vm.provider :virtualbox
#Configuring Environment
master.vm.box = “centos/7”
master.vm.hostname = “DevOps”
#Assigning Ip address and Port forworing Rule
config.vm.network “private_network”, type: “dhcp”
config.vm.network :”forwarded_port”, guest:80,host:9090
#Assign your own VM machine name
config.vm.provider “virtualbox” do |v|
v.name = “DevOpsWebserver”
end
config.vm.synced_folder “.”, “/vagrant”, type: “virtualbox”
master.vm.provision “shell”, inline: <<-SHELL
sudo yum install httpd -y
echo testing vagrant page > /var/www/html/index.html
sudo systemctl start httpd
sudo yum install git -y
sudo yum install maven -y
sudo yum install ansible -y
sudo yum install docker -y
SHELL
end
end
1. Test Results
Use reload command and check all environment configuration is working fine or not.
Use the following commands.
$ vagrant reload –provision
$ vagrant ssh
$ curl localhost
Author:
Onkar Kubade
Call the Trainer and Book your free demo Class for now!!!
© Copyright 2019 | Sevenmentor Pvt Ltd.