Vgrant使用入門前面我們已經(jīng)學(xué)會(huì)了如何安裝并配置Vagrant,而且也已經(jīng)按照默認(rèn)的方式開啟了,那么這一小節(jié)就給大家介紹一下Vagrant的高級(jí)應(yīng)用。 Vagrant常用命令前面講了Vagrant的幾個(gè)命令:
Vagrant還包括如下一些操作:
模擬打造多機(jī)器的分布式系統(tǒng)前面這些單主機(jī)單虛擬機(jī)主要是用來自己做開發(fā)機(jī),從這部分開始的內(nèi)容主要將向大家介紹如何在單機(jī)上通過虛擬機(jī)來打造分布式造集群系統(tǒng)。這種多機(jī)器模式特別適合以下幾種人:
Vagrant支持單機(jī)模擬多臺(tái)機(jī)器,而且支持一個(gè)配置文件Vagrntfile就可以跑分布式系統(tǒng)。 現(xiàn)在我們來建立多臺(tái)VM跑起來,並且讓他們之間能夠相通信,假設(shè)一臺(tái)是應(yīng)用服務(wù)器、一臺(tái)是DB服務(wù)器,那么這個(gè)結(jié)構(gòu)在Vagrant中非常簡(jiǎn)單,其實(shí)和單臺(tái)的配置差不多,你只需要通過 Vagrant.configure("2") do |config|
config.vm.define :web do |web|
web.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "web", "--memory", "512"]
end
web.vm.box = "base"
web.vm.hostname = "web"
web.vm.network :private_network, ip: "11.11.1.1"
end
config.vm.define :db do |db|
db.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "db", "--memory", "512"]
end
db.vm.box = "base"
db.vm.hostname = "db"
db.vm.network :private_network, ip: "11.11.1.2"
end
end 這里的設(shè)置和前面我們單機(jī)設(shè)置配置類似,只是我們使用了 $ vagrant up
Bringing machine 'web' up with 'virtualbox' provider...
Bringing machine 'db' up with 'virtualbox' provider...
[web] Setting the name of the VM...
[web] Clearing any previously set forwarded ports...
[web] Creating shared folders metadata...
[web] Clearing any previously set network interfaces...
[web] Preparing network interfaces based on configuration...
[web] Forwarding ports...
[web] -- 22 => 2222 (adapter 1)
[web] Running any VM customizations...
[web] Booting VM...
[web] Waiting for VM to boot. This can take a few minutes.
[web] VM booted and ready for use!
[web] Setting hostname...
[web] Configuring and enabling network interfaces...
[web] Mounting shared folders...
[web] -- /vagrant
[db] Setting the name of the VM...
[db] Clearing any previously set forwarded ports...
[db] Fixed port collision for 22 => 2222. Now on port 2200.
[db] Creating shared folders metadata...
[db] Clearing any previously set network interfaces...
[db] Preparing network interfaces based on configuration...
[db] Forwarding ports...
[db] -- 22 => 2200 (adapter 1)
[db] Running any VM customizations...
[db] Booting VM...
[db] Waiting for VM to boot. This can take a few minutes.
[db] VM booted and ready for use!
[db] Setting hostname...
[db] Configuring and enabling network interfaces...
[db] Mounting shared folders...
[db] -- /vagrant 看到上面的信息輸出后,我們就可以通過 $ vagrant ssh web
vagrant@web:~$
$ vagrant ssh db
vagrant@db:~$ 是不是很酷!現(xiàn)在接下來我們?cè)賮眚?yàn)證一下虛擬機(jī)之間的通信,讓我們先使用ssh登錄web虛擬機(jī),然后在web虛擬機(jī)上使用ssh登錄db虛擬機(jī)(默認(rèn)密碼是 $ vagrant ssh web
Linux web 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux
Ubuntu 10.04.4 LTS
Welcome to the Ubuntu Server!
* Documentation: http://www./server/doc
New release 'precise' available.
Run 'do-release-upgrade' to upgrade to it.
Welcome to your Vagrant-built virtual machine.
Last login: Thu Aug 8 18:55:44 2013 from 10.0.2.2
vagrant@web:~$ ssh 11.11.1.2
The authenticity of host '11.11.1.2 (11.11.1.2)' can't be established.
RSA key fingerprint is e7:8f:07:57:69:08:6e:fa:82:bc:1c:f6:53:3f:12:9e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '11.11.1.2' (RSA) to the list of known hosts.
vagrant@11.11.1.2's password:
Linux db 2.6.32-38-server #83-Ubuntu SMP Wed Jan 4 11:26:59 UTC 2012 x86_64 GNU/Linux
Ubuntu 10.04.4 LTS
Welcome to the Ubuntu Server!
* Documentation: http://www./server/doc
New release 'precise' available.
Run 'do-release-upgrade' to upgrade to it.
Welcome to your Vagrant-built virtual machine.
Last login: Thu Aug 8 18:58:50 2013 from 10.0.2.2
vagrant@db:~$ 通過上面的信息我們可以看到虛擬機(jī)之間通信是暢通的,所以現(xiàn)在開始你偉大的架構(gòu)設(shè)計(jì)吧,你想設(shè)計(jì)怎么樣的架構(gòu)都可以,唯一限制你的就是你主機(jī)的硬件配置了。 |
|