CSC/ECE 517 Spring 2015/ch1a 1 DZ: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 119: Line 119:
Then we can check whether both boxes are successfully installed.
Then we can check whether both boxes are successfully installed.
<pre>vagrant box list</pre>
<pre>vagrant box list</pre>
=== Vagrantfile configuration ===
=== Configure Vagrantfile ===
Run <code>'''vagrant init'''</code> to create a Vagrantfile or manually create one, then replace the content with the following code:
Run <code>'''vagrant init'''</code> to create a Vagrantfile or manually create one, then replace the content with the following code:
<pre>
<pre>
Line 141: Line 141:
end
end
</pre>
</pre>
=== Boot up ===
=== Boot up ===
Then run
Then run

Revision as of 17:58, 8 February 2015

Vagrant

Vagrant is a computer software that helps create, configure and manage virtual development or work environments. With its great portability and compatibility, team members can share a common development environment. Therefore minimize the cost of time for deployment and maximize the productivity and flexibility.

Background

Vagrant works with mainstream virtualization softwares like VirtualBox, VMware and AWS or any other provider<ref>Other supported Virtualization softwares: https://docs.vagrantup.com/v2/providers/</ref>.

Virtualization Machine

Virtualization machine, aka Virtual Machine or VM, which is created by virtualization software, is an emulation of a real computer that provides a complete operating system. In other words, a virtual machine(VM) is a computer with a complete set of hardwares and operating system which runs in your own operating system. In that way, for example, one using Windows operating system can easily install Linux operating system in the VM rather than actually installing it on local machine. Therefore, any changes or modifications in the VM will not propagate to the operating system on which it runs.

Vagrant Box

Boxes are the package format for Vagrant environments. In other words, a box is a packed operating system used by Vagrant. vagrant box is the utility that downloads and manages different types of boxes.
vagrant box add {url} can be executed under both Windows and Unix operating system to add a new box to Vagrant. url is the network location of the box.
For example:
vagrant box add hashicorp/precise64 downloads a standard Ubuntu 12.04 LTS 64-bit box from HashiCorp's Atlas box catalog.
vagrant box add https://dl.dropboxusercontent.com/u/29173892/vagrant-boxes/debian7.3.0-vbox4.3.6-puppet3.4.1.box downloads a Debian 7.3.0 64-bit Puppet 3.4.1 box which is shared by personal user.

Vagrantfile

Vagrantfile is a configuration file<ref>Definition of configuration file: http://en.wikipedia.org/wiki/Configuration_file</ref> which exists in every single vagrant folder. It is written in Ruby, probably looks like this:

Vagrant.configure(2) do |config|
    config.vm.box = "hashicorp/precise32"
    # other configuration here
end

The "2" in the first line is the version of configuration object config. This number is either "1" for Vagrant 1.0.x, or "2" for Vagrant 1.1+.
This file allows you to configure some basic settings (including shared folder location, cpu core number and etc.) of the virtual machine. For example:

config.vm.provider "virtualbox" do |vb|
    vb.memory = "1024"
    vb.cpus="2"
    config.vm.network :forwarded_port, host: 8000, guest: 80
    config.vm.network :forwarded_port, host: 33060, guest: 33060
end

This section of code specifies the memory and CPU core number of the virtual machine. Also, any network traffic on localhost:8000 and localhost:33060 will be forwarded to port 80 and 33060, respectively.
Some more examples:

config.vm.synced_folder "src/", "/srv/website"

This command is used to sync folder. The first parameter is a path to a directory on the host machine, the second parameter must be an absolute path of where to share the folder within the guest machine.
By default, synced folders are mounted with the SSH user owner/group setting. You can mount them with a different owner and group using the configuration below:

config.vm.synced_folder "src/", "/srv/website"
  owner: "root", group: "root"

Provision

Provision in Vagrant is a process of automatically install your own softwares and modify configurations during the VM booting up.
Also, this process needs no human interaction and therefore makes it very convenient and repeatable.
Every provisioner is configured within your Vagrantfile using the config.vm.provision method call, for example:

config.vm.provider "virtualbox" do |vb|
  config.vm.provision :shell, path: "bootstrap.sh"
end

This section of code enables bootstrap.sh every time the VM is initialized or reloaded.
Typically, provisioners are only run once, during the first vagrant up since the last vagrant destroy. If you want to run the provisioners on every vagrant up, simply set the run option to "always", as shown below:

config.vm.provider "virtualbox" do |vb|
  config.vm.provision :shell, path: "bootstrap.sh"
    run: "always"
end

Why use it?

Many people would ask why use vagrant rather than use Virtualbox or VMware directly to build several virtual machines?

Easy to setup

Actually what Vagrant needs to setup a Virtual Machine is only a Vagrantfile, which is small text file containing the configuration of the VM and can be easily distributed online via USB drive. For example, if I have a well-configured Vagrantfile on GitHub<ref>GitHub For Beginners: Don't Get Scared, Get Started: http://readwrite.com/2013/09/30/understanding-github-a-journey-for-beginners-part-1</ref>, the following 3 steps are all I need to do to have the VM setup:

  • Install Vagrant and Virtualbox
  • git clone git@github.com:<git repo name>.git
  • vagrant up Very simple and easy, isn't it? The whole process takes maybe only 15 minutes if you have a quick Internet access.

    Source Control

    The files in the Vagrant folder are those that appear in the Vagrant Virtual Machine. So it is very easy to know which file is modified by source control tool like Git. But if you are using Virtualbox directly, all files of the VM are stored in a single and large disk image, so it is impossible to perform source control to single files. Although Virtualbox offers Snapshot functionality to implement version restoration, it takes up much more spaces than to use Vagrant.

    How it works

    Set up

    Step 1: Install a mainstream VM software. (e.g. VirtualBox Download Page)
    Step 2: Download Vagrant

    Configure

    Step 1: vagrant box add hashicorp/precise32 to add a Ubuntu<ref>Using Ubuntu Linux/Introduction: http://en.wikibooks.org/wiki/Using_Ubuntu_Linux/Introduction</ref> 32bit box
    and "hashicorp/precise32" change be change to other Vagrant image name.
    Step 2: vagrant init hashicorp/precise32

    Work

    Step 1: vagrant up or vagrant up --provision if you want to run the VM in provision mode.
    Step 2: vagrant ssh to login to the virtual machine.

    Example

    For example, I need two Virtual Machines to run two different Linux distributions(CentOS-6.5<ref>CentOS 6.5 Release Notes: http://wiki.centos.org/Manuals/ReleaseNotes/CentOS6.5#head-5e50f026e9d40ab3ed3c0f504420c4542fd1719a</ref> and Ubuntu Server 14.04 LTS), and they can communicate with each other.

    Add Boxes

    First, to add the CentOS box.

    vagrant box add chef/centos-6.5

    Then to select the VM provider. Because I have both Virtualbox and VMware Desktop installed, there are two options on my screen

    vagrant box add chef/centos-6.5
    ==> box: Loading metadata for box 'chef/centos-6.5'
        box: URL: https://vagrantcloud.com/chef/centos-6.5
    This box can work with multiple providers! The providers that it
    can work with are listed below. Please review the list and choose
    the provider you will be working with.
    
    1) virtualbox
    2) vmware_desktop
    
    Enter your choice: 1
    

    Then it will start downloading the box

    ==> box: Adding box 'chef/centos-6.5' (v1.0.0) for provider: virtualbox
        box: Downloading: https://vagrantcloud.com/chef/boxes/centos-6.5/versions/1.0.0/providers/virtualbox.box
    ==> box: Successfully added box 'chef/centos-6.5' (v1.0.0) for 'virtualbox'!
    

    For Ubuntu box, do the same procedures with Ubuntu's box name

    vagrant box add ubuntu/trusty64
    

    Then we can check whether both boxes are successfully installed.

    vagrant box list

    Configure Vagrantfile

    Run vagrant init to create a Vagrantfile or manually create one, then replace the content with the following code:

    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    
    VAGRANTFILE_API_VERSION = "2"
    
    Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
      config.vm.define :host1 do |host1|
        host1.vm.box = "chef/centos-6.5"
        host1.vm.hostname = "host1"
        host1.vm.network :private_network, ip: "11.11.1.10"
      end
    
      config.vm.define :host2 do |host2|
        host2.vm.box = "ubuntu/trusty64"
        host2.vm.hostname = "host2"
        host2.vm.network :private_network, ip: "11.11.1.11"
      end
    end
    

    Boot up

    Then run

    vagrant up

    After the machines are booted up, you can do ssh to any one of them using:

    vagrant ssh host1      or       vagrant ssh host2
    

    So let's ssh to host1 by vagrant ssh host1, then I can ssh to host2 inside the host1:

    [vagrant@host1 ~]$ ssh 11.11.1.11

    The default password is 'vagrant' (no single quotes).

    Finally, if you can see
    vagrant@host2:~$
    in your terminal, host2 is successfully connected!

    Halt Vagrant VMs

    vagrant halt host1      or       vagrant halt host2
    to halt anyone of them or
    vagrant halt
    to halt both of them at once.

    References

    <references/>