因?yàn)槲⒎?wù)項(xiàng)目一般涉及的工程較多,所以在開發(fā)環(huán)境會(huì)把這些工程部署到虛擬機(jī)中,方便測試。
首先給出微服務(wù)開發(fā)虛擬機(jī)環(huán)境相關(guān)軟件清單:
- Vagrant 2.2.10
- VirtualBox 6.1.16
- Windows PowerShell(windows 10 自帶)
我們的目標(biāo)是在 windows 10 下,構(gòu)建出 Vagrant 管控的、 底層基于 VirtualBox 虛擬機(jī)技術(shù)的 CentOS 環(huán)境。
因?yàn)闀?huì)用到底層虛擬化技術(shù),所以我們需要首先在任務(wù)管理器的"性能"頁簽下,確認(rèn)是否已啟用虛擬化技術(shù),如果沒有需要在主板 Bios 中開啟。

1 Vagrant
Vagrant 是一個(gè)虛擬機(jī)管理工具,通過它,我們可以快速實(shí)現(xiàn)虛擬機(jī)鏡像分發(fā)和使用。
Vagrant 官網(wǎng)地址:https://www.vagrantup.com/
vagrant /?ve?ɡr?nt/
One who wanders from place to place without a permanent home or a means of livelihood.
(1)下載與安裝
從 Vagrant 的下載頁 找到對(duì)應(yīng)操作系統(tǒng)版本的 msi 安裝程序。

下載之后,雙擊 msi 即可進(jìn)行安裝。
安裝過程中,我們可以選擇 Vagrant 的安裝路徑:

其它沒有什么特別的注意點(diǎn),就是 Copying new files 過程會(huì)稍微久一些:

(2)驗(yàn)證
Vagrant 安裝成功后,在命令行輸入 vagrant version,就可以看到如下輸出:

也可以使用 vagrant -v命令來查看版本號(hào)。
2 VirtualBox
VirtualBox 是一款功能強(qiáng)大的虛擬機(jī)軟件,由于安裝過程較簡單,所以這里就不贅述咯。
因?yàn)殓R像文件占用很大的存儲(chǔ)空間,所以最好放在非系統(tǒng)盤中。
在 VirtualBox 中點(diǎn)擊管理 -》全局設(shè)定

進(jìn)入“常規(guī)”設(shè)置選項(xiàng)卡,在此可配置虛擬電腦位置:

3 安裝 Vagrant Box
Vagrant中有兩個(gè)重要概念:Box和Machine。Box指的是虛擬機(jī)應(yīng)用鏡像文件,比如 CentOS 應(yīng)用。Machine指的是處于運(yùn)行狀態(tài)的虛擬機(jī)。
這里用到的 CentOS 來源于 https://app.vagrantup.com/boxes/search。

安裝 box:
vagrant box add [box_name] file:///[box_path]
形如:vagrant box add --force --name centos7 C:\Users\86135\Documents\deniro-soft\MSA-CentOS7.box
注意:box 所在的 URI 必須是ascii 碼,所以 box 所在的文件夾不能出現(xiàn)中文名。
如果需要覆蓋已存在的 box,可以在之前的命令中加入 --force。

Vagrant2.x 的vagrant box add 命令說明摘錄如下:
Usage: vagrant box add [options] <name, url, or path>
Options:
-c, --clean Clean any temporary download files
-f, --force Overwrite an existing box if it exists
--insecure Do not validate SSL certificates
--cacert FILE CA certificate for SSL download
--capath DIR CA certificate directory for SSL download
--cert FILE A client SSL cert, if needed
--location-trusted Trust 'Location' header from HTTP redirects and use the same credentials for subsequent urls as for the initial one
--provider PROVIDER Provider the box should satisfy
--box-version VERSION Constrain version of the added box
The box descriptor can be the name of a box on HashiCorp's Vagrant Cloud,
or a URL, or a local .box file, or a local .json file containing
the catalog metadata.
The options below only apply if you're adding a box file directly,
and not using a Vagrant server or a box structured like 'user/box':
--checksum CHECKSUM Checksum for the box
--checksum-type TYPE Checksum type (md5, sha1, sha256)
--name BOX Name of the box
--[no-]color Enable or disable color output
--machine-readable Enable machine readable output
-v, --version Display Vagrant version
--debug Enable debug output
--timestamp Enable timestamps on log output
--debug-timestamp Enable debug output with timestamps
--no-tty Enable non-interactive output
-h, --help Print this help
使用 vagrant box remove <name>,可以刪除指定名稱的 box。
4 初始化 Vagrant Box
打開 PowerShell(win10 自帶,直接在搜索框中可以找到)

Windows PowerShell 是一種命令行外殼程序和腳本環(huán)境。
通過該命令 vagrant box list我們可以查看剛才安裝的 CentOS Box:

切換到需要安裝虛擬機(jī)的路徑,然后運(yùn)行 vagrant init <box_name>,執(zhí)行初始化 Box 操作。

命令執(zhí)行成功后,會(huì)在當(dāng)前路徑下生成 Vagrantfile 配置文件。
5 vagrantfile 配置
在 vagrantfile 中,我們一般會(huì)配置以下這些參數(shù)。
(1)配置 box 名稱
config.vm.box = "centos7"
(2)配置私有網(wǎng)絡(luò)
私有網(wǎng)絡(luò)的意思是:只有當(dāng)前主機(jī)才可以訪問這個(gè)虛擬機(jī)。語法示例為:config.vm.network "private_network", ip: "192.168.33.10"
(3)配置共享文件夾
Vagrant 提供了將本機(jī)目錄掛載到虛擬機(jī)目錄的功能,默認(rèn)是將Vagrant 配置文件所在目錄掛載到虛擬機(jī) /vagrant 目錄下。語法示例為:config.vm.synced_folder "C:/VM/Vagrant/data", "/host_data"。注意:本機(jī)目錄(示例中的 C:/VM/Vagrant/data)必須預(yù)先創(chuàng)建好。
配置好后,就會(huì)在 vagrant up 命令啟動(dòng)后,看到輸出日志:

通過 vagrant ssh 登錄 CentOS 系統(tǒng)后,通過 cd / 進(jìn)入根目錄,就會(huì)看到主機(jī)所共享出來的目錄:

(4)配置虛擬機(jī)內(nèi)存與 CPU 數(shù)
vb.memory = "2048"
vb.cpus = 2
完整 vagrantfile 內(nèi)容如下:
# -*- 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://vagrantcloud.com/search.
config.vm.box = "centos7"
#config.vm.boot_timeout = 600
# 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.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# 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"
# config.vm.network "public_network",use_dhcp_assigned_default_route: true
# 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 "C:/VM/Vagrant/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.
config.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
vb.cpus = 2
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
# apt-get update
# apt-get install -y apache2
# SHELL
end
6 Vagrant 常用命令
| 命令 | 說明 |
|---|---|
| vagrant box list | 查看目前已創(chuàng)建的box |
| vagrant box add | 新增 box |
| vagrant box remove | 刪除 box |
| vagrant init | 初始化 box |
| vagrant up | 啟動(dòng)虛擬機(jī) |
| vagrant ssh | ssh 登錄虛擬機(jī) |
| vagrant suspend | 掛起虛擬機(jī) |
| vagrant reload | 重啟虛擬機(jī) |
| vagrant halt | 關(guān)閉虛擬機(jī) |
| vagrant status | 查看虛擬機(jī)狀態(tài) |
| vagrant destroy | 刪除虛擬機(jī) |
7 啟動(dòng)虛擬機(jī)
在 Windows PowerShell 中運(yùn)行 vagrant up,等待片刻,就可以在 VirtualBox 中看到虛擬機(jī)已經(jīng)啟動(dòng)起來了。

虛擬機(jī)啟動(dòng)成功的輸出日志為:

最后通過 vagrant ssh 命令就可以登錄虛擬機(jī)的 CentOS 系統(tǒng)。