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。