Vagrant是一個簡單易用的部署工具,用英文說應該是orchestration tool。它能幫助開發人員迅速的構建一個開發環境,幫助測試人員構建測試環境。
Vagrant的基本工作原理大致如下:
- 首先,通過讀取配置文件,獲知用戶需要的環境的操作系統、網絡配置、基礎軟件等信息;
- 然后,調用虛擬化管理軟件的API(VMWare Fusion,Oracle VirtualBox, AWS, OpenStack等)為用戶創建好基礎環境;
- 最后,調用用戶定義的安裝腳本(shell,puppet,chef)安裝好相應的服務和軟件包;
Vagrant的主要應用場景
- 開發環境部署
作為開發人員可能會涉及到不同的開發語言和不同的包依賴,搭建開發環境總是一件很麻煩的事情,有些語言有強有力的項目構建工具支持,比如Java的Maven,而有些語言則沒有這么方便的工具,比如Python。特別是隨着時間的推移,開發環境也會變得很混亂。
Vagrant通過腳本文件的描述創建一個虛擬機實例,並通過shell腳本或puppet配置好開發環境,解決了開發環境的自動化搭建。同時,vagrant創建的開發環境也能被輕松的清理和共享,特別是對於一個團隊,構建標准的開發環境將變得很輕松。 - 測試環境部署
對於測試環節中的集成測試,特別是分布式系統的集成測試,測試環境的搭建也是一個費時費力的工作。Vagrant支持多個實例的部署,可以在單機上創建多個虛擬機實例進行自動化的集成測試。如果單機的測試環境還不夠大,也可以將這個工作交給AWS和OpenStack這樣的雲去完成。
Vagrant的主要概念
- Provider
Provider指的是為Vagrant提供虛擬化支持的具體軟件,比如vmware或virtual box。 - Box
Box代表虛擬機鏡像。Vagrant根據Porvider的不同提供了很多的基礎鏡像(通過url從s3上獲取),用戶可以根據自己的需求使用vagrant package制作屬於自己的box。 - Project
一個目錄和目錄中的Vagrantfile就組成了vagrant的一個項目,項目下可以有子項目,子項目中的Vagrantfile配置將繼承和重寫父項目的配置。項目的虛擬機實例並不會存儲在這個目錄(存儲在~/.vagrant.d/box下),所以可以通過git等版本管理工具來管理項目。 - Vagrantfile
Vagrant的配置文件,使用Ruby的語法描述。里面定義了項目所使用的box,網絡,共享目錄,provision腳本等。當vagrant up命令運行時,將讀取當前目錄的Vagrantfile。 - Provisioning
Provisioning指的是虛擬機實例啟動后,所需要完成的基礎配置工作,比如說安裝LAMP服務等。Vagrant支持使用shell,puppet,chef來完成provisioning工作。 - Plugin
Vagrant提供了插件機制,可以很好的擴展對宿主機OS, GuestOS,Provider,Provisioner的支持,比如vagrant的aws和openstack支持都是通過plugin來實現的。
QuickStart
安裝Provider
我們使用VirtualBox作為虛擬化的Provider,下載並安裝VirtualBox即可。https://www.virtualbox.org/wiki/Downloads
安裝Vagrant
Vagrant提供了windows,mac,deb和rpm的安裝包,下載最新版本1.3.5的安裝即可。Ubuntu軟件倉庫的版本是1.0.1的,比較老了,在讀取配置文件的時候可能會遇到問題,所以不建議直接從倉庫安裝。
http://downloads.vagrantup.com/
創建項目
創建一個文件夾,並進入
mkdir linux-dev cd linux-dev
初始化項目
vagrant init precise64 http://files.vagrantup.com/precise64.box
運行玩命令后,我們應該會發現在當前目錄下出現了Vagrantfile文件,內容如下:
# -*- 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| # All Vagrant configuration is done here. The most common configuration # options are documented and commented below. For a complete reference, # please see the online documentation at vagrantup.com. # Every Vagrant virtual environment requires a box to build off of. config.vm.box = "precise64" # The url from where the 'config.vm.box' box will be fetched if it # doesn't already exist on the user's system. config.vm.box_url = "http://files.vagrantup.com/precise64.box" ...
這個文件有詳細的注釋和說明,其中config.vm.box指定了所使用的box,如果該box不存在於本地,vagrant將會自動從config.vm.box_url處下載並添加到本地。
從名字可以看出這個box是一個ubuntu server 12.04 64位的virtual box鏡像。
配置provisioning腳本
我們通常在安裝完操作系統后希望能裝一些軟件或做一些配置,provisioning腳本正好能完成這個工作。比如完成操作系統安裝后自動安裝vim和git。
編輯Vagrantfile,添加一行
# The url from where the 'config.vm.box' box will be fetched if it # doesn't already exist on the user's system. config.vm.box_url = "http://files.vagrantup.com/precise64.box" # 添加下面的這行 config.vm.provision "shell", path: "provision.sh"
這一行指定了provision使用shell腳本,shell腳本位於與Vagrantfile同目錄下的provision.sh
創建provision.sh
sudo apt-get install vim git -y
啟動實例
在linux-dev目錄下運行vagrant up,vagran就會啟動由該目錄下Vagrantfile指定的虛擬機實例。
首先,vagrant會去本地查找box,如果沒有就從遠程下載(從s3上下載很慢,可以先用迅雷離線下載到本地,然后再通過vagrant box add命令來添加);
然后,vagrant就會啟動虛擬機,做一些網絡配置,並將當前目錄掛載到虛擬機的/vagrant下,使其能在虛擬機和物理機直接共享。
最后,vagrant會開始provisioning的過程,為虛擬機配置基礎的軟件(只在第一次啟動時進行,以后可通過vagrant provision命令觸發)。
AlexYang-mba:linux-dev alex$ vagrant up Bringing machine 'default' up with 'virtualbox' provider... [default] Importing base box 'precise64'... [default] Matching MAC address for NAT networking... [default] Setting the name of the VM... [default] Clearing any previously set forwarded ports... [default] Creating shared folders metadata... [default] Clearing any previously set network interfaces... [default] Preparing network interfaces based on configuration... [default] Forwarding ports... [default] -- 22 => 2222 (adapter 1) [default] Booting VM... [default] Waiting for machine to boot. This may take a few minutes... [default] Machine booted and ready! [default] The guest additions on this VM do not match the installed version of VirtualBox! In most cases this is fine, but in rare cases it can cause things such as shared folders to not work properly. If you see shared folder errors, please update the guest additions within the virtual machine and reload your VM. Guest Additions Version: 4.2.0 VirtualBox Version: 4.3 [default] Mounting shared folders... [default] -- /vagrant
vagrant provision [default] Running provisioner: shell... [default] Running: /var/folders/cy/jfbhmrh95bx7q5nqvg6h4qv00000gn/T/vagrant-shell20131023-1280-yxw9sy stdin: is not a tty Reading package lists... Building dependency tree... Reading state information... The following extra packages will be installed: git-man liberror-perl libgpm2 libpython2.7 patch vim-runtime
SSH登陸
使用vagrant ssh命令可以登陸到虛擬機上,進行相應的操作,比如:
vagrant@precise64:~$ ls -lah /vagrant/ total 16K drwxr-xr-x 1 vagrant vagrant 170 Oct 23 07:54 . drwxr-xr-x 24 root root 4.0K Oct 23 07:19 .. -rw-r--r-- 1 vagrant vagrant 32 Oct 23 07:54 provision.sh drwxr-xr-x 1 vagrant vagrant 102 Oct 23 05:51 .vagrant -rw-r--r-- 1 vagrant vagrant 4.6K Oct 23 07:45 Vagrantfile
關閉實例
關閉實例可以使用三種方式vagrant suspending, vagrant halt, vagrant destroy。
- suspending,暫停虛擬機,保存虛擬機當前的狀態(內存和磁盤均不釋放),可以使用vagrant up命令恢復運行;
- halt,關機,虛擬機停止運行,但是虛擬機實例保留,不銷毀,可以理解為是正常的關機;
- destroy,銷毀虛擬機,虛擬機的實例被銷毀;