Vagrant是一个简单易用的部署工具,用英文说应该是orchestration tool。它能帮助开发人员迅速的构建一个开发环境,帮助测试人员构建测试环境。
Vagrant的基本工作原理大致如下:
我们使用VirtualBox作为虚拟化的Provider,下载并安装VirtualBox即可。https://www.virtualbox.org/wiki/Downloads
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脚本正好能完成这个工作。比如完成操作系统安装后自动安装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。