Vagrant is a command line tool whose main objective is the creation of virtual machines in a
Vagrant is a command line tool whose main objective is the creation of virtual machines in a fast, flexible, and reproducible manner. It is developed in Ruby and is open source and multiplatform . Vagrant allows us to easily create an environment with the settings you need (OS, libraries, applications, etc.)
General Applications of vagrant
- Generate test environments to perform deploys
- Unify development environments of the members of a team
- Creating environments with different configurations to test the performance of our application.
- Create a version of a web application running in a virtual machine and is served in a port on my local machine
How to install?
Download the appropriate version for your operating system and follow the normal installation process.
In addition, we install a virtualization tool. We use VirtualBox , since it is free and available in all major platforms.
We´re going to show an example of how we use Vagrant to create a test environment where installing a web application served from apache. In our case we’re working on Ubuntu 12.04, using Vagrant in version 1.5.4 and 4.3.18 VirtualBox.
Creating our configuration
We will create a folder where we will store the settings required to initiate our virtual machine. This will be the root directory of your configuration in which we execute commands from Vagrant.
mkdir vagrant_prueba cd vagrant_prueba
The initial configuration of the virtual machine that will create will be defined in the Vagrantfile file. To create a sample Vagrantfile, with comments on the configuration that we can define and execute the command:
After running this command we must define the image base (base box) which will be used to clone our virtual machines. We can find images with different configurations in the following repositories:
If we find one with the settings you need (mainly the OS, the other applications can be installed later) we must define it within the Vagrantfile in the line:
config.vm.box = "puphpet/debian75-x64"
In this case we select a 64-bit Debian Server 7.5 , which is found in the official repository of Vagrant (that´s why we identify it by its name alone) . If we select an image that is in another repository, we must specify the URL from where to download it from and the name we will identify it as:
config.vm.box = "ubuntu-trusty-64bits" config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
Once we define this setting we can start our virtual machine with the command:
This command will first check if our system has discharged a version of the image that we define as a base. If not, it will download it and place it in a directory run by Vagrant (in Ubuntu ~ / .vagrant.d / boxes ). We can see the list of images downloaded on our system with the command:
vagrant box list
After downloading the image, Vagrant will clone it to create our virtual machine. Then finally, it will start the virtual machine. If the command is executed successfully, we can connect to it by ssh, running the command:
Once we connected we can make the changes necessary to install our application. Note that by default, the root directory where we start our VM has been shared within the same in the / vagrant (this facilitates the task of copying files from our computer to the virtual machine we created). These directories are synchronized, so that any changes to the files or folders of the same type will be reflected on both machines.
As we saw, we can install the software we need to run our application by logging in through ssh and installing “by hand.” A better way is to use the Vagrant providers that allow us to automate the installation and configuration process of the necessary applications. The simplest way is to provide a shell script that will launch once it executes the vagrant up.
For example, to update the system and install apache, we can create the following bash script in the root folder of the project:
#!/usr/bin/env bash apt-get update apt-get install -y apache2
Configure their execution within Vagrantfile with the following line:
config.vm.provision :shell, path: "update_and_install_apache.sh", privileged: true
If we change this configuration on a machine that is running, we can restart the machine taking this configuration with:
vagrant reload --provision
Besides shell we can use some other, more sophisticated tools such as: Ansible , Chef or Puppet .
So far, we have a virtual machine with which we can interact, share files and install the applications you need. The last thing we need to check if our web application works correctly is to be able to access it from our host machine.
The simplest way to do this is via Port Forwarding, to connect and relay the data to be sent from a port of our host machine to a port of our virtual machine.
If we continue to work with the newly created machine, we see that Apache is up and running by default on port 80. We can connect this port to port 8080 of our host machine with the following configuration in the Vagranfile:
config.vm.network :forwarded_port, host: 8080, guest: 80
Then, to take this new setting use vagrant up (or vagrant reload if the vm vagrant is already picked up). Then, we can access our host machine through http : // localhost : 8080 and we will see the sample page apache, which is being served from the vm .
The complete configuration of this example machine would be:
# -*- mode: ruby -*- # vi: set ft=ruby : # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "puphpet/debian75-x64" config.vm.network "forwarded_port", guest: 80, host: 8080 config.vm.provision :shell, path: "update_and_install_apache.sh", privileged: true end
Turning it off
Finally, we can turn our virtual machine, keeping the changes that have been made in it, with the command: vagrant halt. To start it up again we run vagrant up.
In case you want to stop our VM, saving the current state of implementation so that it can be summarized later and continue from that exact spot, use the commands: vagrant suspend y vagrant resume (respectively)
Finally, if we know that we will no longer work with this machine we can remove it with the command: vagrant destroy. This stops the virtual machine and destroy all the changes we have made, but not its configuration file (Vagrantfile), allowing us to later execute a vagrant up to start a new VM from a completely clean image.
We can check the state of our virtual machine with the command vagrant status .
In the context of a web application, it is common for one to be integrated by different processes, deployed on different servers and communicate in itself, within a network. Vagrant allows you to easily manage and define a configuration of this type. We discuss below an example with two virtual machines, one used to run the application and one for the server database.
# -*- mode: ruby -*- # vi: set ft=ruby : # Vagrantfile API/syntax version. Don't touch unless you know what you're doing! VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "puphpet/debian75-x64" config.vm.define "web" do |web| web.vm.provision :shell, path: "update_and_install_apache.sh", privileged: true web.vm.network "private_network", ip: "192.168.0.2" end config.vm.define "db" do |db| db.vm.provision :shell, path: "install_mysql.sh", privileged: true db.vm.network "private_network", ip: "192.168.0.3" end end
The config.vm.define option allows us to define a setting within the general configuration. This command takes as a parameter the name of a variable ( in this case ” web ” and ” db” ) . From these variables we can define the particular configuration that applies to each machine.
The directives that are outside of the particular configuration of each machine are shared in both instances. In this case both use the base “puphpet/debian75-x64″ image.
In addition to the configuration that we had set for the web server, we add both settings to create a private network between the host machine and the two virtual machines so they can communicate. To make a demand of the Apache server that is running web in this instance, we can access the url http://192.168.0.2
How Do We Use It?
In the FOP-ARSAT project, we had to develop and deploy a composite of different processes that would communicate with each other and could be installed and run on multiple servers with different application configurations. Each of these processes require different environments and applications to run. To test the proper functioning of the application, in addition to the unit testing and integration testing, we needed a way to test the proper installation and communication processes.
Vagrant allowed us to quickly and easily create, similar environments to those used by the customer for installation. These environments would be very useful for testing and debugging scripts in the installation and upgrade of our application.
Another requirement of the project was to provide the customer with a virtual machine in which he could run the application, which we can connect to, so as to be able to run it locally without internet access. We use Vagrant to unify and automate the process of creating these virtual machines.
Finally, because the client does not have a Unix environment to easily work on application development, we provided a virtual machine with the tools and skills necessary to perform this task applications. The process of creating these virtual machines was also implemented using Vagrant.
Comments? Contact us for more information. We’ll quickly get back to you with the information you need.