VirtualBox+Vagrant快速搭建跨平台开发环境

前言

Linux系统对于我们后端工程师来说在我们的日常工作和学习中是必不可少的,但是我们很少将自己的操作系统直接装成Linux,而是选择在Windows上使用VMware或者VirtualBox工具搭建Linux虚拟机,但是搭建起来相对比较复杂,Vagrant作为一款管理虚拟开发环境的工具,只需要简单的配置就能够轻松的帮助我们管理虚拟机,如VirtualBox、VMware等,并且能够为我们提供一个可配置、可移植和复用的软件环境,因此Vagrant也需要依赖这些虚拟机工具,本文使用VirtualBox作为虚拟机工具。

安装VirtualBox

VirtualBox相对于VMware而言,显得小巧玲珑,而且VMware是收费的,对于我们来说,开源免费才是我们最喜爱的~

下载地址:VirtualBox下载地址

安装步骤极其简单,直接下一步

安装Vagrant

下载地址:Vagrant下载地址

安装步骤也比较简单,直接下一步即可

安装完成之后,在命令行输入“vagrant -v”查看Vagrant安装版本,测试安装是否成功
image2020-3-1_12-47-2-69c1667def714649bf051eefb9d04540

下载Box镜像

Box是Vagrant的基础镜像,和docker的images比较像,Vagrant官方提供的镜像仓库中有各种版本的Box镜像,我们可以按需自行下载。

地址:镜像下载地址

我们可以直接使用"vagrant box add xxx"下载一个镜像到本地,并且添加到vagrant 的box列表中,但是因为镜像地址时国外的,下载比较缓慢,建议先将box镜像下载到本地,再使用上述命令将本地box镜像加载到vagrant的box列表中,添加镜像之后,使用"vagrant box list"列出本地有哪些镜像。类似于docker 的“docker images”
image2020-3-1_13-7-29-de24378fada94859b1b7f7efb6f3e85d

初始化开发环境

使用上一步添加的box镜像文件来初始化一个开发环境,在初始化之前,有一个小提示,VirtualBox默认将虚拟机文件放置在C盘,即系统盘,我们最好事先将这个路径改成其他盘防止占用系统资源,如下图
image2020-3-1_13-13-46-a97c5ab6606f48d68f137decedacf121

然后创建一个存放这个开发环境的目录,进入目录之后,执行vagrant命令“vagrant init centos7”,init后面跟的是本地box列表中box的名字,如下图
image2020-3-1_13-18-48-112d1904052d438eba6520acd4867d9f

出现如图中提示说明初始化成功,它会在我们的开发目录中为我们自动生成一个Vagrantfile配置文件
Vagrantfile是类似于docker的Dockerfile一样的东西,我们可以修改里面的配置参数去初始化我们的虚拟机环境,自动生成的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

Vagrantfile文件使用ruby语法,但是因为该配置文件说明及其详细,我们不用去了解ruby就能够轻松配置,下面对里面的几个重要配置信息进行说明:

  • config.vm.box = "centos7":指定初始化该虚拟机的box
  • config.vm.network "forwarded_port":端口映射,将虚拟机中的端口映射到本地宿主机,这样外部就能访问本地宿主机的端口去访问虚拟机服务了
  • config.vm.network "private_network":使用host-only的方式初始化虚拟机网络,并且为虚拟机分配一个静态ip,后续宿主机可以通过这个ip来访问虚拟机
  • ...

启动虚拟机

配置完成之后,在我们的开发目录下,使用”vagrant up“启动虚拟机,如下图
image2020-3-1_13-37-4-65104453a03646d0afcfc933d765efd3

此时,虚拟机已经启动起来了,我们可以使用"vagrant ssh"连接到虚拟机中去
image2020-3-1_13-39-33-433136a5ac7844129817756525861e7a

此时我们就可以在Linux的海洋快乐的进行玩耍了~
需要注意的是,初始化的Linux环境默认是不支持用户名密码进行登陆的,需要修改/etc/ssh/sshd_config配置文件,修改步骤:

  1. sudo vim /etc/ssh/sshd_config
  2. 找到passwordAuthentication项,修改成yes
  3. 重启ssh服务:sudo service sshd restart
  4. 退出重新登陆即可,root的默认密码为vagrant

跨平台

像docker一样,vagrant支持将自己配置好的虚拟机重新打包成新的box镜像,这样就能将我们自己的开发环境打包出来共享给其他小伙伴使用,别人就可以有和自己完全相同的开发环境了,不必再纠结于在我电脑上能运行,在别人电脑上就跑不起来的各种各样奇葩的环境问题了。

打包

首先使用"vagrant halt"命令将虚拟机正常关闭,然后在我们的开发目录下使用”vagrant package“进行打包
image2020-3-1_16-6-51-e63ae86ca5f844b18ebf18247779e9d9

如图所示,在我们的开发目录中已经生成了一个package.box文件

posted @ 2020-03-24 23:08  ThomasYue  阅读(419)  评论(0编辑  收藏  举报