Vagrant安裝、使用

Vagrant安裝配置

實際上Vagrant只是一個讓你能夠方便設置你想要的虛擬機的便攜式工具,它底層支持VirtualBox、VMware甚至AWS做爲虛擬機系統,本書中咱們將使用VirtualBox來進行說明,因此第一步須要先安裝Vagrant和VirtualBox。php

VirtualBox安裝

VirtualBox是Oracle開源的虛擬化系統,它支持多個平臺,因此你能夠到官方網站:https://www.virtualbox.org/wiki/Downloads/ 下載適合你平臺的VirtualBox最新版本並安裝,它的安裝過程都很傻瓜化,一步一步執行就能夠完成安裝了。html

Vagrant安裝

最新版本的Vagrant已經沒法經過gem命令來安裝,由於依賴庫太多了,因此目前沒法使用gem來安裝,目前網絡上面不少教程仍是相似這樣的命令,那些都是錯誤的。目前惟一安裝的辦法就是到官方網站下載打包好的安裝包:http://www.vagrantup.com/downloads.html 他的安裝過程和VirtualBox的安裝同樣都是傻瓜化安裝,一步一步執行就能夠完成安裝。nginx

儘可能下載最新的程序,由於VirtualBox常常升級,升級後有些接口會變化,老的Vagrant可能沒法使用。web

要想檢測安裝是否成功,能夠打開終端命令行工具,輸入vagrant,看看程序是否是已經能夠運行了。若是不行,請檢查一下$PATH裏面是否包含vagrant所在的路徑。shell

Vagrant配置

當咱們安裝好VirtualBox和Vagrant後,咱們要開始考慮在VM上使用什麼操做系統了,一個打包好的操做系統在Vagrant中稱爲Box,即Box是一個打包好的操做系統環境,目前網絡上什麼都有,因此你不用本身去製做操做系統或者製做Box:vagrantbox.es上面有你們熟知的大多數操做系統,你只須要下載就能夠了,下載主要是爲了安裝的時候快速,固然Vagrant也支持在線安裝。ubuntu

創建開發環境目錄

個人開發機是Mac,因此我創建了以下的開發環境目錄,讀者能夠根據本身的系統不一樣創建一個目錄就能夠:瀏覽器

/Users/astaxie/vagrant

下載box

前面講了box是一個操做系統環境,實際上它是一個zip包,包含了Vagrant的配置信息和VirtualBox的虛擬機鏡像文件.咱們這一次的實戰使用官方提供了一個box:Ubuntu lucid 64 http://files.vagrantup.com/lucid64.box服務器

固然你也能夠選一個本身團隊在用的系統,例如CentOS、Debian等,咱們能夠經過上面說的地址下載開源愛好者們製做好的box。固然你本身作一個也行,下一節我會講述如何本身製做包。網絡

添加box

添加box的命令以下:架構

vagrant box add base 遠端的box地址或者本地的box文件名

vagrant box add 是添加box的命令

base是box的名稱,能夠是任意的字符串,base是默認名稱,主要用來標識一下你添加的box,後面的命令都是基於這個標識來操做的。

例子:

vagrant box add base http://files.vagrantup.com/lucid64.box
vagrant box add base https://dl.dropbox.com/u/7225008/Vagrant/CentOS-6.3-x86_64-minimal.box
vagrant box add base CentOS-6.3-x86_64-minimal.box
vagrant box add "CentOS 6.3 x86_64 minimal" CentOS-6.3-x86_64-minimal.box

我在開發機上面是這樣操做的,首先進入咱們的開發環境目錄/Users/astaxie/vagrant,執行以下的命令

vagrant box add base lucid64.box

安裝過程的信息:

Downloading or copying the box...
Extracting box...te: 47.5M/s, Estimated time remaining: --:--:--)
Successfully added box 'base' with provider 'virtualbox'!

box中的鏡像文件被放到了:/Users/astaxie/.vagrant.d/boxes/,若是在window系統中應該是放到了: C:\Users\當前用戶名\.vagrant.d\boxes\目錄下。

經過vagrant box add這樣的方式安裝遠程的box,可能很慢,因此建議你們先下載box到本地再執行這樣的操做。

初始化

初始化的命令以下:

vagrant init

若是你添加的box名稱不是base,那麼須要在初始化的時候指定名稱,例如

vagrant init "CentOS 6.3 x86_64 minimal"

初始化過程的信息:

A `Vagrantfile` has been placed in this directory.
You are now ready to `vagrant up` your first virtual environment!
Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.

這樣就會在當前目錄生成一個 Vagrantfile的文件,裏面有不少配置信息,後面咱們會詳細講解每一項的含義,可是默認的配置就能夠開箱即用。

啓動虛擬機

啓動虛擬機的命令以下:

vagrant up

啓動過程的信息:

Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'base'...
[default] Matching MAC address for NAT networking...
[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] Mounting shared folders...
[default] -- /vagrant

鏈接到虛擬機

上面已經啓動了虛擬機,以後咱們就能夠經過ssh來鏈接到虛擬機了。好比在個人開發機中能夠像這樣來鏈接:

vagrant ssh

鏈接到虛擬機後的信息以下:

Linux lucid64 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: Fri Sep 14 07:31:39 2012 from 10.0.2.2

這樣咱們就能夠像鏈接到一臺服務器同樣進行操做了。

window機器不支持這樣的命令,必須使用第三方客戶端來進行鏈接,例如putty、Xshell4等.

putty爲例:

主機地址: 127.0.0.1

端口: 2222

用戶名: vagrant

密碼: vagrant

 

系統信息

進入系統以後咱們能夠看一下系統的基礎信息:

vagrant@lucid64:/vagrant$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/lucid64-root
                       78G  945M   73G   2% /
none                  179M  176K  179M   1% /dev
none                  184M     0  184M   0% /dev/shm
none                  184M   64K  184M   1% /var/run
none                  184M     0  184M   0% /var/lock
none                  184M     0  184M   0% /lib/init/rw
none                   78G  945M   73G   2% /var/lib/ureadahead/debugfs
/dev/sda1             228M   17M  199M   8% /boot
/vagrant              298G   76G  222G  26% /vagrant

/vagrant這個目錄是自動映射的,被映射到/Users/astaxie/vagrant,這樣就方便咱們之後在開發機中進行開發,在虛擬機中進行運行效果測試了。

Vagrantfile配置文件詳解

在咱們的開發目錄下有一個文件Vagrantfile,裏面包含有大量的配置信息,主要包括三個方面的配置,虛擬機的配置、SSH配置、Vagrant的一些基礎配置。Vagrant是使用Ruby開發的,因此它的配置語法也是Ruby的,可是咱們沒有學過Ruby的人仍是能夠跟着它的註釋知道怎麼配置一些基本項的配置。

  1. box設置

    config.vm.box = "base"

    上面這配置展現了Vagrant要去啓用那個box做爲系統,也就是上面咱們輸入vagrant init Box名稱時所指定的box,若是沒有輸入box名稱的話,那麼默認就是base,VirtualBox提供了VBoxManage這個命令行工具,可讓咱們設定VM,用modifyvm這個命令讓咱們能夠設定VM的名稱和內存大小等等,這裏說的名稱指的是在VirtualBox中顯示的名稱,咱們也能夠在Vagrantfile中進行設定,在Vagrantfile中加入以下這行就能夠設定了:

    config.vm.provider "virtualbox" do |v|
       v.customize ["modifyvm", :id, "--name", "astaxie", "--memory", "512"]
     end

    這行設置的意思是調用VBoxManage的modifyvm的命令,設置VM的名稱爲astaxie,內存爲512MB。你能夠相似的經過定製其它VM屬性來定製你本身的VM。

  2. 網絡設置

    Vagrant有兩種方式來進行網絡鏈接,一種是host-only(主機模式),意思是主機和虛擬機之間的網絡互訪,而不是虛擬機訪問internet的技術,也就是隻有你一個人自High,其餘人訪問不到你的虛擬機。另外一種是Bridge(橋接模式),該模式下的VM就像是局域網中的一臺獨立的主機,也就是說須要VM到你的路由器要IP,這樣的話局域網裏面其餘機器就能夠訪問它了,通常咱們設置虛擬機都是自high爲主,因此咱們的設置通常以下:

    config.vm.network :private_network, ip: "11.11.11.11"

    這裏咱們虛擬機設置爲hostonly,而且指定了一個IP,IP的話建議最好不要用192.168..這個網段,由於頗有可能和你局域網裏面的其它機器IP衝突,因此最好使用相似11.11..這樣的IP地址。

  3. hostname設置

    hostname的設置很是簡單,Vagrantfile中加入下面這行就能夠了:

    config.vm.hostname = "go-app"

    設置hostname很是重要,由於當咱們有不少臺虛擬服務器的時候,都是依靠hostname來作識別的,例如Puppet或是Chef,都是經過hostname來作識別的,既然設置那麼簡單,因此咱們就別偷懶,設置一個。

  4. 同步目錄

    咱們上面介紹過/vagrant目錄默認就是當前的開發目錄,這是在虛擬機開啓的時候默認掛載同步的。咱們還能夠經過配置來設置額外的同步目錄:

    config.vm.synced_folder  "/Users/astaxie/data", "/vagrant_data"

    上面這個設定,第一個參數是主機的目錄,第二個參數是虛擬機掛載的目錄

  5. 端口轉發

    config.vm.network :forwarded_port, guest: 80, host: 8080

    上面這句配置可厲害了,這一行的意思是把對host機器上8080端口的訪問請求forward到虛擬機的80端口的服務上,例如你在你的虛擬機上使用nginx跑了一個php應用,那麼你在host機器上的瀏覽器中打開http://localhost:8080時,Vagrant就會把這個請求轉發到VM裏面跑在80端口的nginx服務上,所以咱們能夠經過這個設置來幫助咱們去設定host和VM之間,或是VM和VM之間的信息交互。

          修改完Vagrantfile的配置後,記得要用vagrant reload命令來重啓VM以後才能使用VM更新後的配置

 

 

Vgrant使用入門

前面咱們已經學會了如何安裝並配置Vagrant,並且也已經按照默認的方式開啓了,那麼這一小節就給你們介紹一下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:~$

經過上面的信息咱們能夠看到虛擬機之間通訊是暢通的,因此如今開始你偉大的架構設計吧,你想設計怎麼樣的架構均可以,惟一限制你的就是你主機的硬件配置了。

相關文章
相關標籤/搜索