vagrant

Vagrant常用命令

Vagrant Cmd:

  • 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虛擬機(默認密碼是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.ubuntu.com/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.ubuntu.com/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)都可以,唯一限制你的就是你主機的硬件配置了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,967評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,273評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,870評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,742評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 71,527評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,010評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,108評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,250評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,769評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,656評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,853評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,371評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,103評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,472評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,717評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,487評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 47,815評論 2 372

推薦閱讀更多精彩內(nèi)容

  • 1,介紹Vagrant 我們做web開發(fā)的時候經(jīng)常要安裝各種本地測試環(huán)境,比如apache,php,mysql,r...
    meng_philip123閱讀 2,533評論 0 12
  • 什么是Vagrant簡單來說,Vagrant 就是一個虛擬機的集成管理器。 我們用它可以快速創(chuàng)建虛擬機,可以快速部...
    嗝喯唲閱讀 660評論 0 0
  • Vagrant學(xué)習(xí)筆記 簡介 vagrant用于快速創(chuàng)建基于VirtualBox、VMware、AWS的虛擬機,提...
    200cc閱讀 8,424評論 0 15
  • 本次會議根據(jù)錦囊討論了本周六班級會議的流程,困惑,以及確定會議角色和各自準備的工作。要點如下: 1,本次會議流程去...
    王昌倫閱讀 409評論 0 1
  • 如果你只做一件事情,一定可以干的很好
    農(nóng)爸爸閱讀 149評論 0 0