天天看點

Vagrant配置與使用

Vagrant是一個基于Ruby的工具,用于建立和部署虛拟化開發環境。它 使用Oracle的開源VirtualBox虛拟化系統,使用 Chef建立自動化虛拟環境。

​​Vagrant​​ is an amazing tool for managing virtual machines via a simple to use command line interface. With a simple ​

​vagrant up​

​ you can be working in a clean environment based on a standard template.

https://www.vagrantup.com/

These standard templates are called ​​base boxes​​.

Create and configure lightweight, reproducible可再生的, and portable development environments.

官網上寫的步驟:

SET UP

Download and install Vagrant within minutes on Mac OS X, Windows, or a popular distribution of Linux. No complicated setup process, just a simple to use OS-standard installer.

CONFIGURE

Create a single file for your project to describe the type of machine you want, the software that needs to be installed, and the way you want to access the machine. Store this file with your project code.

WORK

Run a single command — "vagrant up" — and sit back as Vagrant puts together your complete development environment. Say goodbye to the "works on my machine" excuse as Vagrant creates identical development environments for everyone on your team.

1. 安裝 VirtualBox

虛拟機還是得依靠 VirtualBox 來搭建,免費小巧。

下載下傳位址:​​https://www.virtualbox.org/wiki/Downloads​​

* 雖然 Vagrant 也支援 VMware,不過 VMware 是收費的,對應的 Vagrant 版本也是收費的

2. 安裝 Vagrant

下載下傳位址:​​http://downloads.vagrantup.com/​​ 根據提示一步步安裝。

此外,還得下載下傳官方封裝好的基礎鏡像:

Ubuntu precise 32 VirtualBox ​​http://files.vagrantup.com/precise32.box​​

Ubuntu precise 64 VirtualBox ​​http://files.vagrantup.com/precise64.box​​

如果你要其他系統的鏡像,可以來這裡下載下傳:​​http://www.vagrantbox.es/​​

* 鑒于國内網速,我們将下載下傳鏡像的步驟單獨剝離出來了

3. 添加鏡像到 Vagrant

假設我們下載下傳的鏡像存放路徑是 ​

​~/box/precise64.box​

​,在終端裡輸入:

$ vagrant box add hahaha ~/box/precise64.box
      

​hahaha​

​ 是我們給這個 box 命的名字,​

​~/box/precise64.box​

​ 是 box 所在路徑

(laravel的homestead

當 VirtualBox / VMware 和 Vagrant 安裝完成後,你可以在終端機以下列指令将 'laravel/homestead' 封裝包安裝進你的 Vagrant 安裝程式中。下載下傳封裝包會花你一點時間,時間長短将依據你的網絡速度決定:

vagrant box add laravel/homestead
      

如果這個指令失敗了, 你可能安裝的是一個老版本的 Vagrant 需要指定一個完整的 URL:

vagrant box add laravel/homestead https://atlas.hashicorp.com/laravel/boxes/homestead      

指令行下下載下傳速度太慢的話可以利用工具下載下傳以下連結加速.

​​https://atlas.hashicorp.com/laravel/boxes/homestead/versions/0.2.7/providers/virtualbox.box​​

留意連結中的​

​0.2.7​

​,這是homestead box的版本号,修改url中版本号可以下載下傳不同版本的homestead box.

還有一種方法是,先​

​vagrant init minimal/centos6​

​,然後直接啟動​

​vagrant up --provider virtualbox​

​。當然這些都與下載下傳boxes到本地效果是一樣的,下載下傳方法就是在vagrantcloud.com上點開你所需要的box版本,然後再URL裡加入​

​/providers/virtualbox.box​

​便得到檔案位址,如 ​​https://atlas.hashicorp.com/hashicorp/boxes/precise64​​ 對應的檔案為https://atlas.hashicorp.com/hashicorp/boxes/precise64/providers/virtualbox.box?spm=5176.100239.blogcont47306.14.i9YtVn&file=virtualbox.box ,

 )

下載下傳後執行以下指令, 注意修改 ​

​/path/to/virtualbox.box​

​ 為正确的 path.

vagrant box add laravel/homestead /path/to/virtualbox.box       

首先在本地建立好工作目錄,并在指令行下切換到對應目錄

vagrant box add base CentOS-6.3-x86_64-minimal.box

base 表示指定預設的box,也可以為box指定名稱,比如 centos63 ,使用base時,之後可以直接使用 vagrant init 進行初始化,如果自行指定名稱,則初始化的時候需要指定box的名稱。

CentOS-6.3-x86_64-minimal.box 是box對應的檔案名,這裡可以是本地儲存box的路徑,也可以是可以下載下傳box的網址,如果是網址的話,Vagrant會自動啟動下載下傳。

[vagrant] Downloading with Vagrant::Downloaders::File...

[vagrant] Copying box to temporary location...

[vagrant] Extracting box...

[vagrant] Verifying box...

[vagrant] Cleaning up downloaded box...

設定好box之後,在目前工作目錄運作

vagrant init

生成對應的Vagrantfile

4. 初始化開發環境

建立一個開發目錄(比如:​

​~/dev​

​),你也可以使用已有的目錄,切換到開發目錄裡,用 ​

​hahaha​

​ 鏡像初始化目前目錄的環境:

$ cd ~/dev  # 切換目錄
$ vagrant init hahaha  # 初始化
$ vagrant up  # 啟動環境
      

你會看到終端顯示了啟動過程,啟動完成後,我們就可以用 SSH 登入虛拟機了,剩下的步驟就是在虛拟機裡配置你要運作的各種環境和參數了。

$ vagrant ssh  # SSH 登入
$ cd /vagrant  # 切換到開發目錄,也就是主控端上的 `~/dev`
      

​~/dev​

​ 目錄對應虛拟機中的目錄是 ​

​/vagrant​

Windows 使用者注意:Windows 終端并不支援 ssh,是以需要安裝第三方 SSH 用戶端,比如:Putty、Cygwin 等。

5. 其他設定

Vagrant 初始化成功後,會在初始化的目錄裡生成一個 ​

​Vagrantfile​

​ 的配置檔案,可以修改配置檔案進行個性化的定制

 預設配置檔案:

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://atlas.hashicorp.com/search.
  config.vm.box = "base"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
  # such as FTP and Heroku are also available. See the documentation at
  # https://docs.vagrantup.com/v2/push/atlas.html for more information.
  # config.push.define "atlas" do |push|
  #   push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
  # end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  # config.vm.provision "shell", inline: <<-SHELL
  #   sudo apt-get update
  #   sudo apt-get install -y apache2
  # SHELL
end      

我們要查找錯誤,将與vb.gui有關的注釋去掉.

config.vm.provider "virtualbox" do |vb|
  #   # Don't boot with headless mode
      vb.gui = true
  #
  #   # Use VBoxManage to customize the VM. For example to change     memory:
  #   vb.customize ["modifyvm", :id, "--memory", "1024"]
   end      

Vagrant 預設是使用端口映射方式将虛拟機的端口映射本地進而實作類似 ​

​http://localhost:80​

​ 這種通路方式,這種方式比較麻煩,新開和修改端口的時候都得編輯。相比較而言,host-only 模式顯得友善多了。打開 ​

​Vagrantfile​

​,将下面這行的注釋去掉(移除 ​

​#​

​)并儲存:

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

重新開機虛拟機,這樣我們就能用 ​

​192.168.33.10​

​ 通路這台機器了,你可以把 IP 改成其他位址,隻要不産生沖突就行。

6. 打包分發

當你配置好開發環境後,退出并關閉虛拟機。在終端裡對開發環境進行打包:

$ vagrant package
      

打包完成後會在目前目錄生成一個 ​

​package.box​

​ 的檔案,将這個檔案傳給其他使用者,其他使用者隻要添加這個 box 并用其初始化自己的開發目錄就能得到一個一模一樣的開發環境了。

7. 常用指令

$ vagrant init  # 初始化
$ vagrant up  # 啟動虛拟機
$ vagrant halt  # 關閉虛拟機
$ vagrant reload  # 重新開機虛拟機
$ vagrant ssh  # SSH 至虛拟機
$ vagrant status  # 檢視虛拟機運作狀态
$ vagrant destroy  # 銷毀目前虛拟機
      

更多内容請查閱官方文檔 ​​http://docs.vagrantup.com/v2/cli/index.html​​

 我

vagrant up報錯:

The guest machine entered an invalid state while waiting for it

to boot. Valid states are 'starting, running'. The machine is in the

'poweroff' state. Please verify everything is configured

properly and try again.

從提示看不出錯在哪裡,要去virtualbox目錄下看錯誤日志。日志在

'C:\Users\Administrator\VirtualBoxVMs\下面。

我打開日志看到錯誤:

00:00:00.747736 ERROR [COM]: aRC=E_FAIL (0x80004005) aIID={872da645-4a9b-1727-bee2-5585105b9eed} aComponent={ConsoleWrap} aText={VT-x is disabled in the BIOS for all CPU modes (VERR_VMX_MSR_ALL_VMX_DISABLED)}, preserve=false aResultDetail=0

00:00:00.747940 Console: Machine state changed to 'PoweredOff'

00:00:00.753476 Power up failed (vrc=VERR_VMX_MSR_ALL_VMX_DISABLED, rc=E_FAIL (0X80004005))

看樣子是vt-x沒有開啟。

  • 運作vagrant up 啟動 virtualbox 後,GUI會給出提示

    VT-x/AMD-V硬體加速在您的系統中不可用。您的64-位虛拟機将無法檢測到 64-位處理器,進而無法啟動。

  • 這是由于在BOIS中沒有開啟cpu虛拟化支援,重新開機F2或F10等進入BIOS設定Virtualization為Enable(我的Thinkpad是Security=>Virtualizatio設定為Enable);
  • 電腦重新開機後,再次vagrant up啟動虛拟機還是有一些問題,當時也沒有記錄下來錯誤資訊,隻記得解決方案是使用vagrant destroy将虛拟機從磁盤中删除,然後使用vagrant up指令重新建立。

常用配置:

Vagrant 三種網絡配置詳解

  • Forwarded port
  • Private network
  • Public network

Vagrant 中一共有三種網絡配置,下面我們将會詳解三種網絡配置各自優缺點。

端口映射(Forwarded port),顧名思義是指把宿主計算機的端口映射到虛拟機的某一個端口上,通路宿主計算機端口時,請求實際是被轉發到虛拟機上指定端口的。Vagrantfile中設定文法為:

config.vm.forwarded_port 80, 8080      

以上将通路宿主計算機8080端口的請求都轉發到虛拟機的80端口上進行處理。

預設隻轉發TCP包,UDP需要額外添加以下語句:

config.vm.forwarded_port 80, 8080, protocol: "udp"      

優點:

  • 簡單易了解
  • 容易實作外網通路虛拟機

缺點:

  • 如果一兩個端口需要映射很容易,但是如果有有很多端口,比如MySQL,MongoDB,tomcat等服務,端口比較多時,就比較麻煩。
  • 不支援在主控端器上使用小于1024的端口來轉發。比如:不能使用SSL的443端口來進行https連接配接。

私有網絡(Private network),隻有主機可以通路虛拟機,如果多個虛拟機設定在同一個網段也可以互相通路,當然虛拟機是可以通路外部網絡的。設定文法為:

config.vm.network "private_network", ip: "192.168.50.4"
      
  • 安全,隻有自己能通路
  • 因為私有的原因,是以團隊成員其他人不能和你寫作

公有網絡(Public network),虛拟機享受實體機器一樣的待遇,一樣的網絡配置,vagrant1.3版本之後也可以設定靜态IP。設定文法如下:

config.vm.network "public_network", ip: "192.168.1.120"      

公有網絡中還可以設定橋接的網卡,文法如下

config.vm.network "public_network", :bridge => 'en1: Wi-Fi (AirPort)'
      
  • 友善團隊協作,别人可以通路你的虛拟機
  • 需要有網絡,有路由器配置設定IP

設定共享目錄位置與讀寫權限

Virtualbox共享目錄需要VM上裝有Guest Additions。自己安裝到VM再打包成box檔案做基礎模闆,或者一開始就找好合适的box檔案(前文提到過),随便你怎麼整好。

vagrant 預設将主控端上Vagrantfile所載目錄共享至VM上的 /vagrant 目錄。

我們常需要的是指定共享某個目錄,為此vagrant 支援兩種方式:

(

預設的,vagrant将共享你的工作目錄(即Vagrantfile所在的目錄)到虛拟機中的/vagrant,是以一般不需配置即可,如你需要可配置:

版本"2"

1

2

3

4

​Vagrant.configure(​

​​

​"2"​

​) ​

​do​

​ ​

​|config|​

​# other config here​

​config.vm.synced_folder ​

​"src/"​

​, ​

​"/srv/website"​

​end​

  "src/":實體機目錄;"/srv/website"虛拟機目錄

)

簡單:vboxsf

vboxsf是 V irtual boxS hared F older(或 VboxS hared F older)的縮寫。很多人知道可以指定vboxsf作類型來mount,卻不知這個這個詞全稱,甚至誤解為“虛拟機檔案系統”而錯寫成vboxfs。

vagrant預設使用這種方式共享目錄。優點友善,缺點是性能不好。配置時,隻要在Vagrantfile裡指明:

config.vm.synced_folder "path/on/host", "/absolute/path/on/vm"
      
  • 前一個參數須是主控端上已存在的目錄,若為相對目錄,那是相對Vagrantfile所在目錄。
  • 後一個參數須是VM上的絕對路徑,若不存在,vagrant會在啟動VM時建好,多層的目錄也沒關系。

正經:NFS

NFS是正途,不過它也挑剔。

vagrant 内置了主控端和VM兩端對NFS的支援與配置的協調。如果你用windows,那vagrant當作你的配置不存在,直接忽略(我也覺得你在瞎胡鬧)。Vagrantfile裡很容易,标上标記就行了:

config.vm.synced_folder "path/on/host", "/absolute/path/on/vm", :nfs => true
      
  • 兩個目錄可以一樣,如都用 “/shared” ,藉此建立多個機器節點的共享存儲了。

NFS的共享目錄這種方式的挑剔之處在于目錄内的讀寫權限。

Linux安裝:

 apt-get install virtualbox

 apt-get install vagrant