1> 簡介:
vagrant提供了易于配置,重複性好,便攜式的工作環境,這些對開發人員非常有用,它可以讓開發人員可以建立簡單且可重複使用的基于VirtualBox的虛拟機(現在也支援VMware和AWS等),這些虛拟機可以快速的建立和銷毀。vagrant也可以和puppet,chef等結合,實作虛拟機管理的自動化。vagrant的官網:http://www.vagrantup.com本文出自cclo的blog,轉載時請務必以超連結形式标明文章原始出處:http://xuclv.blog.51cto.com/5503169/1239250
2> 安裝:(OS:ubuntu12.04 vagrant:1.2.2)
$ sudo apt-get install virtualbox
$ sudo dpkg -i vagrant_1.2.2_x86_64.deb
版本1.0.x也可以這樣安裝(OS:ubuntu12.04)
sudo apt-get install vagrant
或
1:sudo apt-get install virtualbox
2:sudo apt-get install ruby1.9.1 rubygems
3:gem install vagrant
NOTE:在實體機的基礎上安裝virtualbox,如果用vm建立的虛拟機中再安裝virtualbox和vagrant,那麼vagrant将起不來。這裡http://downloads.vagrantup.com/下載下傳相應的vagrant版本,注意1.0.x版本和1.1+版本的配置有較大不同,稍後在做介紹。
3> 一個簡單的項目
版本1.1+
$ vagrant init precise32 http://files.vagrantup.com/precise32.box
$ vagrant up
版本1.0.x and 1.1+
$ vagrant box add precise32 http://files.vagrantup.com/precise32.box
$ vagrant init precise32
上述指令運作後,将有一個虛拟機VirtualBox運作Ubuntu 12.04 LTS 32位的系統。使用指令$ vagrant ssh進入該系統。
NOTE:"precise32"為虛拟機的名字,可以更改為你想用的名字。
box是一個zip包,包含了vagrant的配置資訊和VirtualBox的虛拟機鏡像檔案
"http://files.vagrantup.com/precise32.box"為鏡像所在的路徑,可以為本地路徑,是以建議将box下載下傳下來後,指定為本地路徑,速度會更快
這裡http://www.vagrantbox.es/有許多公共的base boxes可供下載下傳和使用,後續将會介紹如何建立一個base box。
4> 常用的vagrant指令:
$ vagrant box add NAME URL #添加一個box
$ vagrant box list #檢視本地已添加的box
$ vagrant box remove NAME virtualbox #删除本地已添加的box,如若是版本1.0.x,執行$ vagrant box remove NAME
$ vagrant init NAME #初始化,實質應是建立Vagrantfile檔案
$ vagrant up #啟動虛拟機
$ vagrant halt #關閉虛拟機
$ vagrant destroy #銷毀虛拟機
$ vagrant reload #重新開機虛拟機
$ vagrant package #目前正在運作的VirtualBox虛拟環境打包成一個可重複使用的box
$ vagrant ssh #進入虛拟環境
5> Vagrantfile
官方解釋是這樣的:The primary function of the Vagrantfile is to describe the type of machine required for a project, and how to configure and provision these machines。翻譯出來太生澀,簡單來說就是配置這個虛拟主機網絡連接配接方式,端口轉發,同步檔案夾,以及怎麼和puppet,chef結合的一個配置檔案。執行完$ vagrant init後,在工作目錄中,你會發現此檔案。
NOTE:配置版本說明:
Vagrant.configure("2") do |config|
# ...
end
目前支援的兩個版本:"1"和"2". "1":描述是Vagrant 1.0.x的配置(如看到Vagrant::Config.run do |config| 此也為Vagrant 1.0.x 的配置);"2":描述的是1.1+ leading up to 2.0.x的配置。vagrant 1.1+ 的Vagrantfiles能夠與vagrant 1.0.x的Vagrantfiles保持向後相容,也大幅引入新的功能和配置選項。
6> 配置網絡(本文将提供2種版本的常用配置,其中版本1的配置經過實踐驗證)
(1) 端口轉發:(假設虛拟機的80端口提供web服務,此處将通過通路實體機的8080端口轉發到虛拟機的80端口,來實作web的通路)
版本"2":
Vagrant.configure("2") do |config|
config.vm.network :forwarded_port, guest: 80, host: 8080
end
版本"1"
Vagrant::Config.run do |config|
# Forward guest port 80 to host port 8080
config.vm.forward_port 80, 8080
end
(2)橋接網絡(公共網絡,區域網路DHCP伺服器自動配置設定IP)
版本"2"
Vagrant.configure("2") do |config|
config.vm.network :public_network
end
版本"1"
Vagrant::Config.run do |config|
config.vm.network :bridged
end
$ VBoxManage list bridgedifs | grep ^Name #可通過此指令檢視本機的網卡
Name: eth0
指定網卡,配置可寫為如下:
Vagrant::Config.run do |config|
config.vm.network :bridged, :bridge => "eth0"
end
(3) 私有網絡:允許多個虛拟機通過主機通過網絡互相通信,vagrant允許使用者配置設定一個靜态IP,然後使用私有網絡設定。
Vagrant.configure("2") do |config|
config.vm.network :private_network, ip: "192.168.50.4"
end
Vagrant::Config.run do |config|
config.vm.network :hostonly, "192.168.50.4"
end
7> 同步檔案夾
預設的,vagrant将共享你的工作目錄(即Vagrantfile所在的目錄)到虛拟機中的/vagrant,是以一般不需配置即可,如你需要可配置:
版本"2"
Vagrant.configure("2") do |config|
# other config here
config.vm.synced_folder "src/", "/srv/website"
end
"src/":實體機目錄;"/srv/website"虛拟機目錄
8> vagrant和shell(實作在虛拟機啟動的時候自運作需要的shell指令或腳本)
版本"2"
内嵌腳本:
Vagrant.configure("2") do |config|
config.vm.provision :shell,
:inline => "echo Hello, World"
end
複雜點的調用如下:
$script = <<SCRIPT
echo I am provisioning...
date > /etc/vagrant_provisioned_at
SCRIPT
Vagrant.configure("2") do |config|
config.vm.provision :shell, :inline => $script
end
外部腳本:
Vagrant.configure("2") do |config|
config.vm.provision :shell, :path => "script.sh" #腳本的路徑相對于項目根,也可使用絕對路徑
end
腳本可傳遞參數:
Vagrant.configure("2") do |config|
config.vm.provision :shell do |s|
s.inline = "echo $1"
s.args = "'hello, world!'"
end
end
版本"1":
内部腳本:
Vagrant::Config.run do |config|
config.vm.provision :shell, :inline => "echo abc > /tmp/test"
end
外部腳本:
Vagrant::Config.run do |config|
config.vm.provision :shell, :path => "test.sh"
end
腳本參數:
Vagrant::Config.run do |config|
config.vm.provision :shell do |shell|
shell.inline = "echo $1 > /tmp/test"
shell.args = "'this is test'"
end
end
9> vagrant和puppet(如果不知道puppet,請看這裡http://xuclv.blog.51cto.com/blog/5503169/1154261)
(1) vagrant調用puppet單獨使用
Vagrant.configure("2") do |config|
config.vm.provision :puppet do |puppet|
puppet.manifests_path = "my_manifests"#路徑相對于項目根,如無配置此項,預設為manifests
puppet.manifest_file = "default.pp" #如無配置此項,預設為default.pp
puppet.module_path = "modules" #路徑相對于根
puppet.options = "--verbose --debug"
end
end
預設配置的目錄結構:
$ tree
.
|-- Vagrantfile
|-- manifests
| |-- default.pp
(2) vagrant讓puppet作為代理,連接配接Puppet master
Vagrant.configure("2") do|config|
config.vm.provision :puppet_server do|puppet|
puppet.puppet_server = "puppet.example.com"#master域名
puppet.puppet_node = "node.example.com"#傳遞給puppet伺服器節點的名稱。預設為”puppet“
puppet.options = "--verbose --debug"#選項
end
end
NOTE:
版本1配置差别不大,不再詳述,差別:Vagrant.configure("2") do |config|改為Vagrant::Config.run do |config|
以上Vagrantfile配置完畢後,可$ vagrant reload 重新開機虛拟機以來實作配置生效
官方給了一個例子(可嘗試玩玩):
1.進入工作目錄
2.修改Vagrantfile
$ vim Vagrantfile #啟用或添加如下行:
Vagrant.configure("2") do |config|
config.vm.provision :puppet #這裡沒有配置pp檔案等的路徑,全部采用預設
end
end
3.建立puppet的主目錄
$ mkdir manifests
4.配置pp檔案
$ vim manifests/default.pp
# Basic Puppet Apache manifest
class apache {
exec { 'apt-get update':
command => '/usr/bin/apt-get update'
}
package { "apache2":
ensure => present,
}
service { "apache2":
ensure => running,
require => Package["apache2"],
}
file { '/var/www':
ensure => link,
target => "/vagrant",
notify => Service['apache2'],
force => true
}
}
include apache
5.重新開機虛拟機
$ vagrant reload #重新開機後可看到虛拟機中已經安裝好了apache