Monday 19 June 2017

Vagrant Setup

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 provides easy to configure, reproducible, and portable work environments built on top of industry-standard technology and controlled by a single consistent workflow to help maximize the productivity and flexibility

For DevOps engineer, Vagrant gives a disposable environment and consistent workflow for developing and testing infrastructure management scripts.we 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, we can test these scripts on remote clouds such as AWS or RackSpace with the same workflow. 


Setting Vagrant Envioronment Step By Step:

To setup vagrant we first need to download the vagrant and oracle virtual box softwares as follow

Step1:
Install oracle virtual box.To install the virtual box  First go to the website virtualbox.org then download the version you prefer for your work.Then install it on your pc by following simple steps.



Step2:
Download the vagrant software from vagrantup.com and install it.it will ask for restart.


Step3:

Then we need to choose a box to install the centos on the vm .To download the centos on virtual machines follow below steps.

Click on FINDBOXES. Then type Centos 6 and select virtualbox and Enter.Click on Centos/6 (Depends on your requirement)

Step4:
Copy the highlated line(vagrant init centos/6) and paste in cmd. It will downloads the the required OS as per your command.





Step5:

Open command prompt and create a directory. In vagrant we place all the project related files in one folder.so it makes easier to find all the project files in a single directory.

C:\Users\user1> mkdir project1

C:\Users\user1>cd project1

C:\Users\user1\project1> vagrant init centos/6



Now Your Vagrant is ready


If you want to create multiple vms just follow as below
In Vagrant main configuration file is vagrant file.It will be used to configure the vagarant vms easily  with in minutes.To set up multiple vms we need to edit the vagrant file it will be opened in notepad from cmd as follow

C:\Users\user1\project1> notepad Vagrantfile

It will open a file as follow with little configuration .You can play with this configuration file.This is not default one i did some changes.By typing above command you will get default vagrant file with one vm configuration.You can increase  vms  by changing as follow.




# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "bento/centos-6.7"
  config.vm.define "centos1" do |centos1|
               centos1.vm.box= "bento/centos-6.7"
               centos1.vm.hostname="server1.abc.com" 
               centos1.vm.network "public_network", ip: "192.168.33.10"          
        end
   config.vm.define "centos2" do |centos2|
               centos2.vm.box= "bento/centos-6.7"
centos2.vm.hostname="server2.abc.com"
  centos2.vm.network "public_network", ip: "192.168.33.11" 
           
  end
  config.vm.define "centos3" do |centos3|
               centos3.vm.box= "bento/centos-6.7"
centos3.vm.hostname="server3.abc.com" 
centos3.vm.network "public_network", ip: "192.168.33.12"  
  end
  
  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  #config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  #config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
   #config.vm.network "public_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  #config.vm.network "public_network", type:"dhcp"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   apt-get update
  #   apt-get install -y apache2
  # SHELL

end




If you face any problem while practicing feel free to comment it and Bookmark this blog for quick reference.We will try to help you.

If you like this share with your friends.Follow us by email for our new posts 

Thanks

Devops Desk Team


No comments:

Post a Comment