Vagrant( "官網" 、 "github" )是一款構建虛擬開發環境的工具,支持 Window,Linux,Mac OS,Vagrant 中的 Boxes 概念類似於 Docker(實質是不同的),你可以把它看作是一個箱子,裡面裝了一些東西,Vagrant 創建虛擬機的時候,需要用到 Box ...
Vagrant(官網、github)是一款構建虛擬開發環境的工具,支持 Window,Linux,Mac OS,Vagrant 中的 Boxes 概念類似於 Docker(實質是不同的),你可以把它看作是一個箱子,裡面裝了一些東西,Vagrant 創建虛擬機的時候,需要用到 Box ,它裡面包含了虛擬機配置、虛擬機硬碟鏡像和 Vagrant 配置的壓縮包,有了 Box,你不需要再重新下載 ISO 鏡像文件、新建虛擬機、修改虛擬機等配置,而是直接運行你所需要的操作系統。
更多 Vagrant 概念,參考:Vagrant 是什麼,不是什麼。
Vagrant 支撐 VirtualBox、HyperV、VMWare 等虛擬機軟體,我 Mac 電腦裝的是 VMWare Fusion,但 Vagrant 支持是收費的(79 美元),好黑呀,不過 VirtualBox 是免費的,我又安裝了個 VirtualBox(大概 300 M),以便做示例。
使用 Vagrant 的目的,就是方便在虛擬機中做 Consul 的集群(Mac OS、Ubuntu 安裝及使用 Consul)。
安裝 Vagrant(使用 homebrew)
$ brew install vagrant
安裝好 Vagrant 之後,就可以使用初始化 Box 了,你可以使用別人封裝好的 Box,也可以自己封裝 Box,比如下麵命令:
$ vagrant box add ubuntu/trusty64
ubuntu/trusty64
是一個公開 Boxes(更多 Boxes)。運行上面第一行命令後,Vagrant 會在工作目錄下創建 Vagrantfile 配置文件。線上下載 Box 會比較慢,你可以先下載 Box 之後,再載入本地的 Box 進行初始化。
我使用的是 Ubuntu 64 Box:http://files.vagrantup.com/precise64.box,其他 Box 下載地址:http://www.vagrantbox.es/
下載好 Box 之後,你可以創建這樣的工作目錄:
$ tree
.
├── boxes
│ └── precise64.box
└── works
2 directories, 1 files
創建命令:
$ mkdir vagrant_projects
$ mkdir boxes
$ mkdir works
然後把下載好的 Box 放到 boxes 文件夾下,然後命令轉到 boxes 目錄下(cd boxes
),然後執行添加 Box 命令:
$ vagrant box add ubuntu precise64.box
添加後之後,可以查看所添加的 Box 列表:
$ vagrant box list
ubuntu64 (virtualbox, 0)
命令轉到 works 目錄下(cd works
)接著進行初始化虛擬機:
$ vagrant init ubuntu64
初始化完成後,會在當前目錄下生成一個 VagrantFile 配置文件,裡面是對虛擬機環境的一些配置(可以手動修改),然後啟動虛擬機:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: bridged
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: The guest additions on this VM do not match the installed version of
default: VirtualBox! In most cases this is fine, but in rare cases it can
default: prevent things such as shared folders from working properly. If you see
default: shared folder errors, please make sure the guest additions within the
default: virtual machine match the version of VirtualBox you have installed on
default: your host and reload your VM.
default:
default: Guest Additions Version: 4.2.0
default: VirtualBox Version: 5.1
==> default: Configuring and enabling network interfaces...
==> default: Mounting shared folders...
default: /vagrant => /Users/xishuai/vagrant_project/works
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
虛擬機啟動好之後(可以在 VirtualBox 中查看是否已啟動),就可以登錄虛擬機了:
$ vagrant ssh
Welcome to Ubuntu 12.04 LTS (GNU/Linux 3.2.0-23-generic x86_64)
* Documentation: https://help.ubuntu.com/
New release '14.04.5 LTS' available.
Run 'do-release-upgrade' to upgrade to it.
Welcome to your Vagrant-built virtual machine.
Last login: Tue Dec 27 10:16:06 2016 from 10.0.2.2
vagrant@precise64:~$
這個命令就表示已經進入了 Ubuntu 的系統,可以像在虛擬機中使用一樣使用它了。
當然也可以把你配好的 Box 導出出來,給其他人使用,執行命令:
$ cd ~/VirtualBox\ VMs/works_default_1482820841651_93029
$ vagrant package --output works_default_1482820841651_93029 --base ubuntu64.box
Vagrant 命令列表:
vagrant box list
:查看box列表vagrant add box box 名字 box地址
:添加box,自動幫你生成 Vagrantfilevagrant init box 名字
:初始化 Vagrantfilevagrant up
:啟動虛擬機vagrant ssh
:連接虛擬機vagrant halt
:關閉虛擬機vagrant reload
:重新載入 Vagrantfile 文件vagrant suspend
:暫時掛起虛擬機vagrant destroy
:銷毀虛擬機vagrant status
:查看虛擬機運行狀態vagrant package
:導出 Box
在使用 Vagrant 的時候,遇到了這樣一個問題:創建的 Ubuntu 虛擬機,需要訪問外部網路,所以需要將虛擬機的網路模式設置為橋接模式(Bridged),於是就使用 VirtualBox 進行設置,但設置成功之後,每次 Vagrant 啟動虛擬機的時候,都會進行網路模式重置,但如果用 VirtualBox 啟動的話,就沒有什麼問題。
這個問題搞了好久,最後的解決方案是修改 Vagrantfile 配置文件,添加如下配置:
config.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)"
config.vm.boot_timeout = 20
boot_timeout
是鏈接超時設置(20 秒),bridge
後面表示橋接的網路模式(WiFi 網路),如果不進行設置的話,每次啟動虛擬機的時候,會進行選擇網路模式:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Specific bridge 'en1: Wi-Fi (AirPort)' not found. You may be asked to specify
==> default: which network to bridge to.
==> default: Available bridged network interfaces:
1) en0: Wi-Fi (AirPort)
2) en1: Thunderbolt 1
3) en2: Thunderbolt 2
4) p2p0
5) awdl0
6) bridge0
7) vmnet1
8) vmnet8
==> default: When choosing an interface, it is usually the one that is
==> default: being used to connect to the internet.
default: Which interface should the network bridge to?
由於之前的 Ubuntu 版本太低(12.04),安裝 .NET Core 的時候,遇到了一些問題,後來又換了一個 Ubuntu Box(版本 14.04),但配置的時候,又遇到了下麵問題:
$ vagrant init ubuntu_server1
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.
xishuaideMacBook-Pro:ubuntu_server1 xishuai$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu_server1'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ubuntu_server1_default_1482924693668_66404
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
default:
default: Vagrant insecure key detected. Vagrant will automatically replace
default: this with a newly generated keypair for better security.
default:
default: Inserting generated public key within guest...
default: Removing insecure key from the guest if it's present...
default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Mounting shared folders...
default: /vagrant => /Users/xishuai/vagrant_project/ubuntu_server1
Vagrant was unable to mount VirtualBox shared folders. This is usually
because the filesystem "vboxsf" is not available. This filesystem is
made available via the VirtualBox Guest Additions and kernel module.
Please verify that these guest additions are properly installed in the
guest. This is not a bug in Vagrant and is usually caused by a faulty
Vagrant box. For context, the command attempted was:
mount -t vboxsf -o uid=1000,gid=1000 vagrant /vagrant
The error output from the command was:
mount: unknown filesystem type 'vboxsf'
解決方案(參考 Vagrant error : Failed to mount folders in Linux guest):
$ vagrant plugin install vagrant-vbguest
另外,附一些 Linux 常用命令:
command &
:將進程放在後臺執行ctrl + z
:暫停當前進程 並放入後臺jobs
:查看當前後臺任務bg( %id)
:將任務轉為後臺執行fg( %id)
:將任務調回前臺kill( %id)
:殺掉任務
參考資料:
- Vagrant 安裝配置
- 用 Vagrant 管理虛擬機
- 快速打造跨平臺開發環境 vagrant + virtualbox + box
- 開始使用 Vagrant
- windows 下 使用 vagrant 來管理 linux 虛機開發環境
- Vagrant PUBLIC NETWORKS