Wednesday, December 27, 2017

Introduction to 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 production parity.

 Vagrant
• Vagrant is a open source tool for working with virtual environments. Vagrant acts as a wrapper and sits on top of the virtualization solution (VirtualBox, Hyper-V, Docker, VMware, KVM and AWS) and provides a simple command line interface to manage the VM's with the custom configuration.
• Create and configure lightweight, reproducible, and portable development environments.
• Vagrant is capable to executing configuration management software such as Ansible, Chef, Salt, and Puppet.
• Public clouds e.g. AWS, DigitalOcean can be providers too.
• Focused to create environments that are similar to production servers.
• Vagrant is designed for everyone as the easiest and fastest way to create a virtualized environment.
• Crease new VMs quickly and easily -- only one command! Vagrant up
• Keep the number of VMs under control.
• Developer can create/destroy multiple deployment environments in minutes. 

Production Parity
The cost of fixing a bug exponentially increases the closer it gets to production. Vagrant aims to mirror production environments by providing the same operating system, packages, users, and configurations, all while giving users the flexibility to use their favorite editor, IDE, and browser. Vagrant also integrates with your existing configuration management tooling like Chef, Puppet, Ansible, or Salt, so you can use the same scripts to configure Vagrant as production.

How Vagrant work
Create a VM using base images.
Configuration the virtual machine specific settings and configuration using the Vagrantfile.
Automates the configuration management of the virtual machine using the configuration management software.

What is a Vagrant Box
A box is a packaged Vagrant environment, typically a virtual machine.
Is a previously builted Vagrant virtual machine image for ready-to-run.
Vagrant is available in a lot of platforms (Linux, Windows, Mac)

What is a Provider
Provider is basically the software which provides virtualization support.
There are two type of provide
Local      : VirtualBox, Hyper-V, Docker, VMware.
Remote :  Aws Cloud and Openstack --- There are remote providers

What is a Vagrantfile :
Vagrantfile is heart, Basically all the configuration save in Vagrantfile
What kindly of configuration save in the Vagrantfile
  1. Which base image to use.
  2. What will be the hostname of target machine
  3. Which particular network to be use
  4. Any specific compute (customize hardware configuration)
  5. Provisioner config. (Shell/Automation tool)

What is a Provisioner  :
A provisioner is a basically automation tool or configuration management tool and  provisioner  can be used Shell script
and also used automation tool like Chef , Ansible or Puppet

 


Prerequisites Software
• Virtualbox
• Vagrant

Install Vagrant
Download VirtualBox first & install ( version 5, 5.1)
Download installer on Vagrant site for Linux, windows and MacOS
Link : vagrantup.com
Link : virtualbox.org

To verify vagrant and virtualbox install correctly.

Run : vagrant and vboxmanage ----  command on terminal

SETTING UP THE BOX
Follow these steps once Virtualbox and Vagrant are installed.

How to add a box
Great box repository :
 www.vagrantbox.es    

$ vagrant box add  < name > <url> <provider>  # virtualbox information  ubuntu/trusty64

 vagrant box add  ubuntu/trusty64
                 or
 vagrant box add  centos/7
                    Or

• vagrant up

 ---------------------------------------------------------------------------------------------------------------------
Vagrant basic command 
Vagrant ssh-config   -------------- how to work ssh

Vagrant globle-status  ----------- to check install machine

Vagrant status   ----- show status , running or stopped

Vagrant ip-show   --- show ip add

Vagrant stop    ------ stooped

Vagrant reload  -----  stopped and start

Vagrant destroy   --- remove or delete

Vagrant halt    ------ shutdown system


  
vagrant list-commands
Below is a listing of all available Vagrant commands and a brief description of what they do

  • box             manages boxes: installation, removal, etc.
  • cap             checks and executes capability
  • connect         connect to a remotely shared Vagrant environment
  • destroy         stops and deletes all traces of the vagrant machine
  • docker-exec     attach to an already-running docker container
  • docker-logs     outputs the logs from the Docker container
  • docker-run      run a one-off command in the context of a container
  • 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
  • list-commands   outputs all available Vagrant subcommands, even non-primary ones
  • login           log in to HashiCorp's Vagrant Cloud
  • 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
  • provider        show provider for this environment
  • 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
  • rsync           syncs rsync synced folders to remote machine
  • rsync-auto      syncs rsync synced folders automatically when files change
  • 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
  • validate        validates the Vagrantfile
  • vbguest         plugin: vagrant-vbguest: install VirtualBox Guest Additions to the machine
  • version         prints current and latest Vagrant version

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

How to use GUI mode

config.vm.provider "virtualbox" do |vb|       --------------------------------   for GUI Mode
  #   # Display the VirtualBox GUI when booting the machine
    vb.gui = true
 
---------------------------------------------------------------------------------------------------------------------
Start with a customized box

Create an environment

Start with a working dir.
• mkdir –p ~/Development/awesome_project
• cd ~/Development/awesome_project

• vagrant box add awesome_project http://boxes.example.com/supercool_drupal_project.box

Inside your project , create a Vagrantfile.

$ vagrant init < your box name>   ------ initializes a new Vagrant environment by creating a Vagrantfile

Initialize the Vagrant setup

• vagrant init awesome_project

Once Vagrantfile is created after that run below simple command for vagrant start.

• vagrant up

Connect to the box
• vagrant ssh


Configure the Vagrantfile.
– Hostname, network adapters, config paths…
Great docs at http://vagrantup.com/



 
Port forwarding, host name define and IP settings
Set forwarding ports between guest and host to work (bind on 0.0.0.0)
Just add the following code in your Vagraantfile, restart server and access in browser.

config.vm.network "private_network", ip: "192.168.56.12"
config.vm.hostname = "wls12c1.private"
config.hostmanager.enabled = true
config.hostmanager.ignore_private_ip = false  



Customize hardware settings
You can change memory, CPU cores and other things in Vagrantfile
Just see below VBoxManage options

 
Customize configuration
config.vm.provider "virtualbox" do |vb|     
vb.memory = "4096"   
vb.cpus = 4 
vb.name = “hostname" 
end

 

 --------------------------------------------------------------------------------------------------
 Sell provision

Install Apache & PHP though bash script
Add below line in Vagrantfile

config.vm.provision :shell, :path => File.join( "provision", "provision.sh" )


#!/bin/bash -eu
# provision.sh  -- Install Apache and a test php script

Sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-Centos-6
yum install -y httpd php

service httpd start
chkconfig httpd on

cat > /var/www/html/index.php << EOF
<?php phpinfo(); ?>
EOF
 --------------------------------------------------------------------------------------------------

Synced folders

Add to your Vagrantfile:

config.vm.synced_folder 'html', '/var/www/html/'
config.vm.synced_folder "www/", "/srv/www/", :owner => "www-data", :mount_options => [ "dmode=775", "fmode=774" ]

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

Ansible Provision

Add below line in Vagrantfile

Config.vm.provisioning 'ansible' do |ansible|
  ansible.inventory_path = "vagrant-inventory.ini"
 ansible.playbook = "vagrant-playbook.yml"                 
 ansible.extra_vars = { user: "vagrant" }
 ansible.sudo = true
 ansible.limit = 'all'
 ansible.verbose = "vvvv"

end
end

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


Set up Multiple VM's

Vagrant.configure("2") do |config|
   
config.vm.define :"prod" do |prod|
prod.vm.box = "ubuntu/trusty64"
prod.vm.hostname = "Prod"
prod.vm.network "private_network", ip: "192.168.10.1"
 end

    config.vm.define :"web" do |web|
web.vm.box = "centos/7"    ###### https://app.vagrantup.com/centos/boxes/7
web.vm.hostname = "web"
web.vm.network "private_network", ip: "192.168.10.2"
web.vm.network "forwarded_port", guest: 80, host: 8080
 end

  config.vm.define :"db" do |db|
db.vm.box = "hashicorp/precise64"
db.vm.hostname = "db"
db.vm.network "private_network", ip: "192.168.10.3"
db.vm.network "forwarded_port", guest: 3306, host: 3360
 end