CSC/ECE 517 Spring 2015/ch1a 1 DZ: Difference between revisions
(Created page with "= Vagrant = '''Vagrant''' is a computer software that helps create, configure and manage virtual development or work environments. With its great portability and compatibility, t...") |
|||
(26 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
= Vagrant | <font size="5"><b>Vagrant</b></font><br> | ||
'''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. | '''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 == | == Background == | ||
Vagrant works with mainstream virtualization softwares like [https://www.virtualbox.org/ VirtualBox], [http://www.vmware.com/ VMware] and [http://aws.amazon.com/ AWS] or | Vagrant works with mainstream virtualization softwares like [https://www.virtualbox.org/ VirtualBox], [http://www.vmware.com/ VMware] and [http://aws.amazon.com/ 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. <code>'''vagrant box'''</code> is the utility that downloads and manages different types of boxes.<br> | |||
<code>'''vagrant box add {url}'''</code> can be executed under both Windows and Unix operating system to add a new box to Vagrant. <code>'''url'''</code> is the network location of the box. <br> | |||
For example: <br> | |||
<code>'''vagrant box add hashicorp/precise64'''</code> downloads a standard Ubuntu 12.04 LTS 64-bit box from [https://atlas.hashicorp.com/boxes/search?utm_source=vagrantcloud.com&vagrantcloud=1 HashiCorp's Atlas box catalog].<br> | |||
<code>'''vagrant box add https://dl.dropboxusercontent.com/u/29173892/vagrant-boxes/debian7.3.0-vbox4.3.6-puppet3.4.1.box'''</code> downloads a Debian 7.3.0 64-bit Puppet 3.4.1 box which is shared by personal user.<br> | |||
By default, Vagrant automatically check for updates during any <code>vagrant up</code>. You can also disable this by adding <code>config.vm.box_check_update = false</code> in your Vagrantfile. | |||
=== 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: <br> | |||
<pre> | |||
Vagrant.configure(2) do |config| | |||
config.vm.box = "hashicorp/precise32" | |||
# other configuration here | |||
end | |||
</pre> | |||
The "2" in the first line is the version of configuration object <code>config</code>. This number is either "1" for Vagrant 1.0.x, or "2" for Vagrant 1.1+. <br> | |||
This file allows you to configure some basic settings (including shared folder location, cpu core number and etc.) of the virtual machine. For example: <br> | |||
<pre> | |||
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 | |||
</pre> | |||
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. <br> | |||
Some more examples: <br> | |||
<pre> | |||
config.vm.synced_folder "src/", "/srv/website" | |||
</pre> | |||
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. <br> | |||
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: | |||
<pre> | |||
config.vm.synced_folder "src/", "/srv/website" | |||
owner: "root", group: "root" | |||
</pre> | |||
=== Provision === | |||
Provision in Vagrant is a process of automatically install your own softwares and modify configurations during the VM booting up.<br> | |||
Also, this process needs no human interaction and therefore makes it very convenient and repeatable. <br> | |||
Every provisioner is configured within your Vagrantfile using the <code>config.vm.provision</code> method call, for example: | |||
<pre> | |||
config.vm.provider "virtualbox" do |vb| | |||
config.vm.provision :shell, path: "bootstrap.sh" | |||
end | |||
</pre> | |||
This section of code enables <code>'''bootstrap.sh'''</code> every time the VM is initialized or reloaded. <br> | |||
Typically, provisioners are only run once, during the first <code>vagrant up</code> since the last <code>vagrant destroy</code>. If you want to run the provisioners on every <code>vagrant up</code>, simply set the <code>run</code> option to "always", as shown below: | |||
<pre> | |||
config.vm.provider "virtualbox" do |vb| | |||
config.vm.provision :shell, path: "bootstrap.sh" | |||
run: "always" | |||
end | |||
</pre> | |||
== Why use it? == | |||
Many people would ask why use vagrant rather than use Virtualbox or VMware directly to build several virtual machines?<br> | |||
=== Easy to set up === | |||
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: | |||
<li>Install Vagrant and Virtualbox | |||
<li><code>'''git clone git@github.com:<git repo name>.git'''</code> | |||
<li><code>'''vagrant up'''</code><br> | |||
Very simple and easy, isn't it? The whole process takes maybe only 15 minutes if you had fast Internet. | |||
=== 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. [https://www.virtualbox.org/wiki/Downloads VirtualBox Download Page])<br> | |||
'''Step 2: '''Download [http://www.vagrantup.com/downloads Vagrant] | |||
=== Configure === | |||
'''Step 1: ''' <code>'''vagrant box add hashicorp/precise32'''</code> to add a Ubuntu<ref>Using Ubuntu Linux/Introduction: http://en.wikibooks.org/wiki/Using_Ubuntu_Linux/Introduction</ref> 32bit box<br> and "hashicorp/precise32" change be change to other Vagrant image name.<br> | |||
'''Step 2: ''' <code>'''vagrant init hashicorp/precise32'''</code><br> | |||
=== Work === | |||
'''Step 1: ''' <code>'''vagrant up'''</code> or <code>'''vagrant up --provision'''</code> if you want to run the VM in provision mode.<br> | |||
'''Step 2: ''' <code>'''vagrant ssh'''</code> 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, add the CentOS box. | |||
<pre>vagrant box add chef/centos-6.5</pre> | |||
Then to select the VM provider. Because I have both Virtualbox and VMware Desktop installed, there are two options on my screen | |||
<pre> | |||
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 | |||
</pre> | |||
Then it will start downloading the box | |||
<pre> | |||
==> 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'! | |||
</pre> | |||
For Ubuntu box, do the same procedures with Ubuntu's box name | |||
<pre>vagrant box add ubuntu/trusty64 | |||
</pre> | |||
Then we can check whether both boxes are successfully installed. | |||
<pre>vagrant box list</pre> | |||
=== Configure Vagrantfile === | |||
Run <code>'''vagrant init'''</code> to create a Vagrantfile or manually create one, then replace the content with the following code: | |||
<pre> | |||
# -*- 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 | |||
</pre> | |||
=== Boot up === | |||
Then run | |||
<pre>vagrant up</pre> | |||
After the machines are booted up, you can do ssh to any one of them using: | |||
<pre> | |||
vagrant ssh host1 or vagrant ssh host2 | |||
</pre> | |||
So let's ssh to host1 by <code>'''vagrant ssh host1'''</code>, then I can ssh to host2 inside the host1: | |||
<pre>[vagrant@host1 ~]$ ssh 11.11.1.11</pre> | |||
The default password is 'vagrant' (no single quotes). | |||
Finally, if you can see <pre>vagrant@host2:~$</pre> in your terminal, host2 is successfully connected! | |||
=== Halt Vagrant VMs === | |||
<pre>vagrant halt host1 or vagrant halt host2</pre> to halt anyone of them or | |||
<pre>vagrant halt</pre> to halt both of them at once. | |||
== References == | == References == | ||
<references/> |
Latest revision as of 17:06, 2 March 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.
By default, Vagrant automatically check for updates during any vagrant up
. You can also disable this by adding config.vm.box_check_update = false
in your Vagrantfile.
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 set up
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:
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 had fast Internet.
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, 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 seevagrant@host2:~$in your terminal, host2 is successfully connected!
Halt Vagrant VMs
vagrant halt host1 or vagrant halt host2to halt anyone of them or
vagrant haltto halt both of them at once.
References
<references/>