Vagrant Quickstart
What is Vagrant?
Vagrant is wrapper or collection of scripts for building and maintaining portable virtual development environment. We can say vagrant is command line script for VirtualBox (or any other individuals prefer VM environment). We will skip installation part of VirtualBox and Vagrant , as its very simple and easy, just follow instruction from there websites.
When to use ?
Basically vagrant is use to setup local development/testing environment. ex1) A developer 'A' written a code, he want to check how it work with PHP5 and PHP6 . Solution is simply deploy two guest machines (VM) with PHP5 and PHP6, Share same project dir with both VM and test it. A sysadmin 'B' written shell/python code to deploy LAMP environment, he want to test how it work with CentOS and how it work with Dabian.
Lets release the kraken
Assume vagrant is already installed. Search your favorite OS from here:- https://atlas.hashicorp.com/boxes/search. For example we use 'bento/centos-6.7'
$ mkdir projectA $ vagrant init bento/centos-6.7 $ ls ## Expected output 'Vagrantfile''vagrant init' command will generate 'Vagrantfile'. Vagrantfile is main configuration file for vagrant.
Lets launch our first guest host (VM)
Before that Note:- When first time we start vagrant then it will download box for interent, so while searching you box please check size of box and internet speed of your network. But its one time activity
$ vagrant up ## First time it will take some time depend upon network speed ## and size of box ## Please read output it will give you some valuable details , ## like ssh port, ssh address, directory shared etc.
Once vagrant is up, first check locally available vagrant box's. Locally available means in next project is use these boxes then it will launch quick by copying local copy
$ vagrant box list #Expected output #bento/centos-6.7 (virtualbox, 2.2.7) #Check Help for other opetions $ vagrant box help
Cool, now our guest os (vm) is running, lets explore what else we can do.
## Check Status $ vagrant status ## Login to VM $ vagrant ssh ## Exit from vm and back to host machine ## (assuming you already ssh in vm) $ exit ## Stop VM $ vagrant halt ## Start again $ vagrant up ## Destroy VM $ vagrant destroy ## Launch again $ vagrant up ## Create test file in your project directory ## (directory where Vagrantfile located) $ echo "Created in host machine" > helloWorld.txt ## Access helloWorld.txt inside guest os $ vagrant ssh $ cat /vagrant/helloWorld.txt
Feed the kraken
Since now our guest os is running lets deploy first application Apache.
## login to guest $ vagrant ssh ## Check apache available $ service httpd status ## expected output:- ## httpd: unrecognized service ##lets install apache $ sudo yum install -y httpd ## start apache $ sudo service httpd start ## Configure apache to start at boot time $ sudo chkconfig httpd on $ chkconfig --list | grep httpd $ exit ## Edit Vagrant file to access apache $ vi Vagrantfile ## Search and Comment below line:- ## config.vm.network "forwarded_port", guest: 80, host: 8080 ## Reload Guest OS $ vagrant relaod ## Open browser add access your apache running in guest machine:- ## http://localhost:8080/
Lets automate feeding of kraken
Since now we know how to install package in Guest OS, lets see how to automate any manual operation.
## Destroy Existing Guest OS $ vagrant destroy ## Check status $ vagrant status
Lets edit Vagrantfile and remove all commented section. After removing comment file will be look like:-
$ cat Vagrantfile # -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure(2) do |config| config.vm.box = "bento/centos-6.7" config.vm.network "forwarded_port", guest: 80, host: 8080 end
Now edit Vagrant file and add commands which we manually executed to install apache. Edited file should be look like below.
$ cat Vagrantfile # -*- mode: ruby -*- # vi: set ft=ruby : Vagrant.configure(2) do |config| config.vm.box = "bento/centos-6.7" config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.provision "shell", inline: <<-SHELL sudo yum install -y httpd sudo chkconfig httpd on sudo service httpd start sudo echo "Hello World" > /var/www/html/index.html SHELL end -shell>
In above configuration we use shell "provision" to install apache. If you are familiar with any other tool then you can use. For more details on provisioners check:- https://www.vagrantup.com/docs/provisioning/
Its time to Launch guest os again
$ vagrant up ## Open Browser and access url:- http://localhost:8080/
Genetic mutate kraken
Assume apache is basic software which you want to be available in all your future guest box. So to save time of installation we can save those changes in box
## Make sure you are in projectA directory $ cd projectA ## Create vagarnt box from curent guest os and name as 'cento-http-6.7' $ vagrant package --output cento-http-6.7 ## Check created box $ ls ## Expected output 'Vagrantfile and cento-http-6.7' ## Add box in local box repository $ vagrant box add --name cento-http-6.7 ./cento-http-6.7 ## Confirm Box added $ vagrant box list ## Delete box file $ rm cento-http-6.7 ## Created new project Directory $ mkdir ../projectB $ cd ../projectB ## Generate new Vagrant file for new box $ vagrant init cento-http-6.7 ## Edit generated Vagrant file and uncomment below line:- ## config.vm.network "forwarded_port", guest: 80, host: 8080 ## Save vagrant file ## Launch guest os $ vagrant up ## Open Browser and access readily available apache:- ## http://localhost:8080/
Summary
Hopefully this quick start introduction will allow all to start using this greatest tool 'Vagrant'
Comments
Post a Comment