超详细的Virtualbox结合Vagrant搭建本地开发和测试环境

技术开发人员使用Windows系统时,可能需要Linux系统的环境来学习和测试,如果直接购买服务器成本比较大,这里推荐搭建本地的虚拟机环境,这里详细介绍一下VirtaualBox与Vagrant结合来方便的搭建各种开发和测试环境。

安装Virtualbox

VirtualBox 是一个跨平台的虚拟化工具,支持多个操作系统,根据自己的情况选择对应的版本下载即可。

注意,除了主程序,还要把对应的扩展包程序也一并下载了。有些高级特性,比如 USB 3.0 等需要扩展包的支持。

本文章里使用的VirtualBox-7.0.4-154605-Win来安装的,具体下载可以到官网进行下载。

1234567

安装 Vagrant

在 Vagant官网根据自己的操作系统选择对应的版本下载即可,本文章用的是vagrant_2.3.4的版本。

89101112

安装完成后,重启计算机。

13

重启计算机后,在命令提示符里用vagrant version,如果提示对应的版本信息,说明已安装成功。

14

配置 VirtualBox

配置虚拟机的存放在位置,因为虚拟机占用空间比较大,所以需要放在空间比较的磁盘。

启动 VirtualBox 后,通过菜单 管理 -> 全局设定,在全局设定对话框中,修改 默认虚拟电脑位置,指定一个容量较大的磁盘。

15

配置 Vagrant

通过 Vagrant 创建虚机需要先导入镜像文件,也就是 box,它们默认存储的位置在用户目录下的 .vagrant.d 目录下,对于 Windows 系统来说,就是 C:\Users\用户名.vagrant.d。

如果后续可能会用到较多镜像,或者你的 C 盘空间比较紧缺,可以通过设置环境变量 VAGRANT_HOME 来设置该目录。

在 Windows 系统中,可以这样操作:新建系统环境变量,环境变量名为 VAGRANT_HOME,变量值为 E:\VirtualBox\.vagrant.d

16

下载虚机镜像

下面的例子以 CentOS 7 为例,使用其它版本操作系统的也可以参考。

在官方搜索CentOS系统,找下载量比较多的进行下载,我下载的为CentOS7.9的版本。

1718

添加镜像文件

我们需要将下载好的镜像文件添加到Vagrant中,首先我们通过命令行查看Vagrant列表中是否有对应的镜像,如果没有,我们需要通过命令将镜像文件添加进去。

19

提示现在还没有 box。如果这是第一次运行,此时 VAGRANT_HOME 目录下会自动生成若干的文件和文件夹,其中有一个 boxes 文件夹,这就是要存放 box 文件的地方。

执行 vagrant box add 命令添加 box,再通过vagrant box list查看添加的加载的虚拟机列表:

20

Vagrant 基本操作

新建虚拟机

创建一个目录,先执行 vagrant init

21

其中的 centos7 就是我们要使用的 box 名字。

这个命令只是为我们生成一个 Vagrantfile,所以,这里的名字没指定或者写错了都没关系,后面会介绍如何编辑这个 Vagrantfile 来修改。

启动虚拟机

通过vagrant up命令来启动虚拟机

22

在一分钟左右就可以正常启动。

查看虚机状态

执行下面的命令可以查看虚机的状态:

23

连接虚机

如果启动没问题,接下来执行 vagrant ssh 就能以 vagrant 用户直接登入虚机中。

root 用户没有默认密码,也不能直接登录。需要 root 权限的命令可以通过在命令前添加 sudo 来执行,也可以执行 sudo -i 直接切换到 root 用户。

25

这时候打开 VirtualBox 程序,可以看到自动创建的虚机:

24

我们也可以在 VirtualBox 的终端上登录系统,默认的登录用户名和密码都是 vagrant

在windows下使用自己的命令行操作不方便,操作LINUX系统还是习惯用一些SSH工具,例如FinalShell,SecureCRT ,XShell等。

停止虚机

执行下面的命令可以关闭虚机:

26

还有暂停虚机(vagrant suspend)、恢复虚机(vagrant resume)、重载虚机(vagrant reload)、删除虚机(vagrant destroy),这里不在演示。

注意: 在当前这个小例子中,上面所有的 vagrant 命令都需要在 Vagrantfile 所在的目录下执行。

初识 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://vagrantcloud.com/search.
config.vm.box = "centos7"

# 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"

# 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.

# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt 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

刨除注释,这个文件的实际生效内容实际只有 3 行:

Vagrant.configure("2") do |config|

config.vm.box = "centos7"

end

首尾两行组成一个代码块结构,不要去动它,除非你知道自己在干什么。我们平常只需要编辑这其中的配置项。

这里的 config.vm.box 对应的就是虚机的镜像,也就是 box 文件,这是唯一必填的配置项。

特别提醒,Vagrantfile 文件名是固定的写法,大小写也要完全一样,修改了就不认识了。

配置端口转发

网络配置一种是直接修改Vagrantfile文件,比如config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"配置就是把宿机的8080映射到虚机的80端口,实际上设置端口转发这个功能并不实用,一个很明显的问题就是如果启动多个虚机,很容易就出现宿主机上端口冲突的问题。即使没有端口冲突,使用起来也不方便,我个人不推荐使用的,可以把这部分配置直接删掉。直接使用下面的私有网络。

这个功能是虚拟机软件提供的,可以在虚机的网卡设置中展开高级选项,找到相关的配置:

27

配置私有网络

取消注释最下面一行,就可以为虚机设置指定的私有网络地址:

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

取消注册后,需要使用vagrant reload重载虚机的配置。

使用 SSH 客户端

vagrant ssh 命令虽然很方便,但是在 Windows 环境下,因为默认的命令行终端不太好用,所以往往还需要使用更专业的 SSH 客户端例如 XShell 或 SecureCRT等。

默认的镜像只支持 private_key 的方式登录,先使用 vagrant ssh-config 命令可以看到 SSH 的配置:

28

可以看到其中的 IdentityFile 就是私钥文件。

发现这个自定义 box 启动的虚机的密钥文件是固定在 VAGRANT_HOME 下的相关目录下。

以FinalShell为例:

29

至此,Vagrant常用的使用方法,已讲解完成。

举报/反馈

科技侃侃

303获赞 40粉丝
欢迎来到科技侃侃,10年程序员,除了会修BUG,还喜欢科技、数码、AI、软件等领域。
关注
0
0
收藏
分享