/>Intro to Vagrant
Because I quite often like to test different applications locally, I use virtualbox a lot. I have recently found that using Vagrant can simplify the deployment of setting up a local development environment and replaces it with almost a single config file where you can pick and choose what features you want your server to have. No longer are you stuck using XAMMP, configuring virtual environments manually, or working remotely in your editor via FTP. Vagrant is quick, easy, and helps you manage multiple development environments at one time.
Imagine you’re developing an app with a team of say 15 people. This app is crazy awesome! It uses NodeJS ,ExpressJS , AngularJS , MongoDB and a bunch of othe modules.
Ideally, you’re going to want all 15 people on the team working on this app to have as identical development environemts. Not all the developers on the team are going to have the expertise of Sysops or being a System Admin. Getting the development environments identically setup can be a huge undertaking. On top of that, some people use Mac while others Use Linux or Windows. Before you know it, developers will be throwing computers through walls exhausted from constantly configuring and configuring.
Vagrant manages all this for you so everyone can focus on coding rather than maintaining their development environement.
Install Necessary Dependencies
To Help find a ready box you can search on the Vagrant website. These are usually prebuild virtual machines for Vegrant that are build from basic distros.
For our example i have downloaded and installed Vegrant for debian on my Ubuntu machine on whcih i am running virtualbox. I will be getting the Vegrant box Ubuntu 14.04 which will be downloaded automatically by Vagrant. So in order to download it you run the following command:
vagrant init ubuntu/trusty64
To initalize the vm you run the following
vagrant up --provider virtualbox
I order to connect to the vagrant machine , which is essentially ssh strapped onto vegrant, run the following command:
Looks simple right,well thats the whole point.
A run down of the most common commands
Vagrant CommandsThere’s a ton of commands you can use to talk to Vagrant. For a full list see the official docs here, but I’ll go over the more common ones.
vagrant upThis is the command you just ran to boot the box up and provision the server based on the configuration file Vagrantfile. I use this for starting as well as resuming my environments.
vagrant suspendUse this command to “pause” your virtual environment. Make sure you do this before shutting down your computer to safely be able to restore the environment later.
vagrant up. The state will be saved and everything should be where you left it off from including your database changes.
vagrant haltwill cleanly shutdown the virtual machine.Adding the --force ooption will sforce shutdown
vagrant destroyThis permanently removes the virtual environment from your machine.
vagrant reload --provisionIf your environment suddenly stops working, it’s useful to reboot by running this command. If you add the
--provisionflag, it will reprovision the box as well. This is useful with removing or adding things to the server via Cookbooks or Puppet.
vagrant sshAnything you edit on your computer in the public folder is automatically shared to the virtual environment without having to SSH in. However, if for whatever reason you need to connect to the virtual server, just run this command.
vagrant ssh-configThis lets you see the detailed login credentials of how you could connect to the virtual environment.
vagrant box listWill list the vegrant boxes that have been downloaded and are available on your directoty
vagrant statusThis will tell you the state of the machines Vagrant is managing.
It is quite easy, especially once you get comfortable with Vagrant, to forget whether your Vagrant machine is running, suspended, not created, etc. This command tells you the state of the underlying guest machine.
Networking BasicsModify the Vagrantfile and add the following line. This will cause port 8080 on the host machine to get forwarded to the virtual machine.
config.vm.network "forwarded_port", guest: 80, host:8080To enable run vegrant reload on the running machine
Provisioning and ProvisionersProvisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the
vagrant upprocess. This is useful since boxes typically are not built perfectly for your use case. Of course, if you want to just use
vagrant sshand install the software by hand, that works. But by using the provisioning systems built-in to Vagrant, it automates the process so that it is repeatable.
Provisioners can be used for Chef and Puppet.
In order to use provision we create a bash script that we place in out vagrant directory. I will use an example where i will install apache and link the apacje /var/www directory to our local directory in the vagrant directory.
So inside our vegrant directory we create a file called "provision.sh" and inside we add the following
#!/usr/bin/env bash echo "Installing Apache" apt-get update >/dev/null 2>&1 apt-get install -y apache2 rm -rf /var/www ln -fs /vagrant /var/www
One more thing is t add a reference to this file inside the Vagrantfile that exists in the same directory. we can add this under the line we previously modified to forward port 8080.
config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.provision "shell", path: "provision.sh"
Then we just use vegrant up and we start our vm that will install and configure apache
During startup you will see :
==> default: Reading package lists... ==> default: Building dependency tree... ==> default: Reading state information... ==> default: The following extra packages will be installed: ==> default: apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3... .......
Using Chef and Vagrant togetherI have written another tutorial on chef .This guide will provision for chef solo.destro
Firstly we can edit the vagrant file and add the following
config.vm.provision "chef_solo" do |chef| chef.add_recipe "vagrant_la" #you can add the path to your chef-repo here
what we are doing is that we are pointing to a chef recipe and telling vagrant to run that recipe through chef .
So we need to create the cookbook vagrant_la
If we have knife installed then we can do this by running
knife cookbook create vagrant_la
otherwise just create the file called default.rb /cookbooks/vagrant_la/recipes/default.rb and add that inside your vagrant folder.
Then inside the default.rd add the following text
execute "apt-get update" package "apache2" execute "rm -rf /var/www/html" link "/var/www" do to "/vagrant" end
Thejust run vegrant up and you will notice the chef installation will run and execute the apache installation and configuration.
There are thousand of ready cookbooks for chef and these cookbooks can all be used in conjunction with vagrant to deploy virtual machines.
Private NetworksVagrant private networks allow you to access your guest machine by some address that is not publicly accessible from the global internet. In general, this means your machine gets an address in the private address space.
DHCPThe easiest way to use a private network is to allow the IP to be assigned via DHCP.
Vagrant.configure("2") do |config| config.vm.network "private_network", type: "dhcp" end
Static IPYou can also specify a static IP address for the machine. This lets you access the Vagrant managed machine using a static, known IP. The Vagrantfile for a static IP looks like this:
Vagrant.configure("2") do |config| config.vm.network "private_network", ip: "192.168.50.4" end
Disable Auto-ConfigurationIf you want to manually configure the network interface yourself, you can disable Vagrant's auto-configure feature by specifying
Vagrant.configure("2") do |config| config.vm.network "private_network", ip: "192.168.50.4", auto_config: false end
Public NetworksNetwork identifier:
Vagrant public networks are less private than private networks, and the exact meaning actually varies from provider to provider, hence the ambiguous definition.
DHCPThe easiest way to use a public network is to allow the IP to be assigned via DHCP. In this case, defining a public network is trivially easy:
Vagrant.configure("2") do |config| config.vm.network "public_network" end
Using the DHCP Assigned Default RouteSome cases require the DHCP assigned default route to be untouched. In these cases one may specify the
use_dhcp_assigned_default_routeoption. As an example:
Vagrant.configure("2") do |config| config.vm.network "public_network", use_dhcp_assigned_default_route: true end
Default Network InterfaceIf more than one network interface is available on the host machine, Vagrant will ask you to choose which interface the virtual machine should bridge to. A default interface can be specified by adding a
:bridgeclause to the network definition.
config.vm.network "public_network", bridge: "en1: Wi-Fi (AirPort)"