Vagrant是一個簡單易用的部署工具,用英文說應該是orchestration tool。它能幫助開發人員迅速的構建一個開發環境,幫助測試人員構建測試環境。python
Vagrant的基本工做原理大體以下:linux
咱們使用VirtualBox做爲虛擬化的Provider,下載並安裝VirtualBox便可。https://www.virtualbox.org/wiki/Downloadsgit
Vagrant提供了windows,mac,deb和rpm的安裝包,下載最新版本1.3.5的安裝便可。Ubuntu軟件倉庫的版本是1.0.1的,比較老了,在讀取配置文件的時候可能會遇到問題,因此不建議直接從倉庫安裝。shell
http://downloads.vagrantup.com/ubuntu
建立一個文件夾,並進入vim
mkdir linux-dev cd linux-dev
初始化項目windows
vagrant init precise64 http://files.vagrantup.com/precise64.box
運行玩命令後,咱們應該會發如今當前目錄下出現了Vagrantfile文件,內容以下:ruby
# -*- 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鏡像。less
咱們一般在安裝完操做系統後但願能裝一些軟件或作一些配置,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
使用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。