Vagrant常用命令


Vagrant常用命令

Vagrant的幾個命令:

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

Vagrant還包括如下一些操作:

  • vagrant box list

    顯示當前已經添加的box列表

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

    刪除相應的box

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

    停止當前正在運行的虛擬機並銷毀所有創建的資源

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

    關機

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

    打包命令,可以把當前的運行的虛擬機環境進行打包

    $ 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只做最基本的設置,而不是設置好所有的環境,因此Vagrant通常使用Chef或者Puppet來做進一步的環境搭建。那么Chef或者Puppet稱為provisioning,而該命令就是指定開啟相應的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

    恢復前面被掛起的狀態

    $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

    獲取當前虛擬機的狀態

    $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

    掛起當前的虛擬機

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

模擬打造多機器的分布式系統

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

  1. 快速建立產品網絡的多機器環境,例如web服務器、db服務器
  2. 建立一個分布式系統,學習他們是如何交互的
  3. 測試API和其他組件的通信
  4. 容災模擬,網絡斷網、機器死機、連接超時等情況

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

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

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

這里的設置和前面我們單機設置配置類似,只是我們使用了:web以及:db分別做了兩個VM的設置,並且給每個VM設置了不同的hostname和IP,設置好之后再使用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登錄虛擬機了,但是這次和上次使用的不一樣了,這次我們需要指定相應的角色,用來告訴ssh你期望連接的是哪一台:

$ vagrant ssh web
vagrant@web:~$

$ vagrant ssh db
vagrant@db:~$

是不是很酷!現在接下來我們再來驗證一下虛擬機之間的通信,讓我們先使用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:~$

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM