日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Vgrant使用入門

 天才白癡書館 2015-07-17

Vgrant使用入門

前面我們已經(jīng)學(xué)會了如何安裝并配置Vagrant,而且也已經(jīng)按照默認(rèn)的方式開啟了,那么這一小節(jié)就給大家介紹一下Vagrant的高級應(yīng)用。

Vagrant常用命令

前面講了Vagrant的幾個命令:

  • vagrant box add 添加box的操作
  • vagrant init 初始化box的操作
  • vagrant up 啟動虛擬機的操作
  • vagrant ssh 登錄擬機的操作

Vagrant還包括如下一些操作:

  • vagrant box list

    顯示當(dāng)前已經(jīng)添加的box列表

    $ vagrant box list
    base (virtualbox)
  • vagrant box remove

    刪除相應(yīng)的box

    $ vagrant box remove base virtualbox
    Removing box 'base' with provider 'virtualbox'...
  • vagrant destroy

    停止當(dāng)前正在運行的虛擬機并銷毀所有創(chuàng)建的資源

    $ vagrant destroy
    Are you sure you want to destroy the 'default' VM? [y/N] y
    [default] Destroying VM and associated drives...
  • vagrant halt

    關(guān)機

    $ vagrant halt
    [default] Attempting graceful shutdown of VM...
  • vagrant package

    打包命令,可以把當(dāng)前的運行的虛擬機環(huán)境進行打包

    $ vagrant package
    [default] Attempting graceful shutdown of VM...
    [default] Clearing any previously set forwarded ports...
    [default] Creating temporary directory for export...
    [default] Exporting VM...
    [default] Compressing package to: /Users/astaxie/vagrant/package.box
  • vagrant plugin

    用于安裝卸載插件

  • vagrant provision

    通常情況下Box只做最基本的設(shè)置,而不是設(shè)置好所有的環(huán)境,因此Vagrant通常使用Chef或者Puppet來做進一步的環(huán)境搭建。那么Chef或者Puppet稱為provisioning,而該命令就是指定開啟相應(yīng)的provisioning。按照Vagrant作者的說法,所謂的provisioning就是"The problem of installing software on a booted system"的意思。除了Chef和Puppet這些主流的配置管理工具之外,我們還可以使用Shell來編寫安裝腳本。

    例如: vagrant provision --provision-with chef

  • vagrant reload

    重新啟動虛擬機,主要用于重新載入配置文件

    $ vagrant reload
    [default] Attempting graceful shutdown of VM...
    [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 VM to boot. This can take a few minutes.
    [default] VM booted and ready for use!
    [default] Setting hostname...
    [default] Mounting shared folders...
    [default] -- /vagrant
  • vagrant resume

    恢復(fù)前面被掛起的狀態(tài)

    $vagrant resume
    [default] Resuming suspended VM...
    [default] Booting VM...
    [default] Waiting for VM to boot. This can take a few minutes.
    [default] VM booted and ready for use!
  • vagrant ssh-config

    輸出用于ssh連接的一些信息

    $vagrant ssh-config
    Host default
      HostName 127.0.0.1
      User vagrant
      Port 2222
      UserKnownHostsFile /dev/null
      StrictHostKeyChecking no
      PasswordAuthentication no
      IdentityFile "/Users/astaxie/.vagrant.d/insecure_private_key"
      IdentitiesOnly yes
      LogLevel FATAL
  • vagrant status

    獲取當(dāng)前虛擬機的狀態(tài)

    $vagrant status
    Current machine states:
    
    default                   running (virtualbox)
    
    The VM is running. To stop this VM, you can run `vagrant halt` to
    shut it down forcefully, or you can run `vagrant suspend` to simply
    suspend the virtual machine. In either case, to restart it again,
    simply run `vagrant up`.
  • vagrant suspend

    掛起當(dāng)前的虛擬機

    $ vagrant suspend
    [default] Saving VM state and suspending execution...

模擬打造多機器的分布式系統(tǒng)

前面這些單主機單虛擬機主要是用來自己做開發(fā)機,從這部分開始的內(nèi)容主要將向大家介紹如何在單機上通過虛擬機來打造分布式造集群系統(tǒng)。這種多機器模式特別適合以下幾種人:

  1. 快速建立產(chǎn)品網(wǎng)絡(luò)的多機器環(huán)境,例如web服務(wù)器、db服務(wù)器
  2. 建立一個分布式系統(tǒng),學(xué)習(xí)他們是如何交互的
  3. 測試API和其他組件的通信
  4. 容災(zāi)模擬,網(wǎng)絡(luò)斷網(wǎng)、機器死機、連接超時等情況

Vagrant支持單機模擬多臺機器,而且支持一個配置文件Vagrntfile就可以跑分布式系統(tǒng)。

現(xiàn)在我們來建立多臺VM跑起來,並且讓他們之間能夠相通信,假設(shè)一臺是應(yīng)用服務(wù)器、一臺是DB服務(wù)器,那么這個結(jié)構(gòu)在Vagrant中非常簡單,其實和單臺的配置差不多,你只需要通過config.vm.define來定義不同的角色就可以了,現(xiàn)在我們打開配置文件進行如下設(shè)置:

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è)置和前面我們單機設(shè)置配置類似,只是我們使用了:web以及:db分別做了兩個VM的設(shè)置,并且給每個VM設(shè)置了不同的hostname和IP,設(shè)置好之后再使用vagrant up將虛擬機跑起來:

$ 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登錄虛擬機了,但是這次和上次使用的不一樣了,這次我們需要指定相應(yīng)的角色,用來告訴ssh你期望連接的是哪一臺:

$ vagrant ssh web
vagrant@web:~$

$ vagrant ssh db
vagrant@db:~$

是不是很酷!現(xiàn)在接下來我們再來驗證一下虛擬機之間的通信,讓我們先使用ssh登錄web虛擬機,然后在web虛擬機上使用ssh登錄db虛擬機(默認(rèn)密碼是vagrant):

$ 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:~$

通過上面的信息我們可以看到虛擬機之間通信是暢通的,所以現(xiàn)在開始你偉大的架構(gòu)設(shè)計吧,你想設(shè)計怎么樣的架構(gòu)都可以,唯一限制你的就是你主機的硬件配置了。

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多