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

前言

Linux系统对于咱们后端工程师来讲在咱们的平常工做和学习中是必不可少的,可是咱们不多将本身的操做系统直接装成Linux,而是选择在Windows上使用VMware或者VirtualBox工具搭建Linux虚拟机,可是搭建起来相对比较复杂,Vagrant做为一款管理虚拟开发环境的工具,只须要简单的配置就可以轻松的帮助咱们管理虚拟机,如VirtualBox、VMware等,而且可以为咱们提供一个可配置、可移植和复用的软件环境,所以Vagrant也须要依赖这些虚拟机工具,本文使用VirtualBox做为虚拟机工具。html

安装VirtualBox

VirtualBox相对于VMware而言,显得小巧玲珑,并且VMware是收费的,对于咱们来讲,开源免费才是咱们最喜好的~docker

下载地址:VirtualBox下载地址shell

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

安装Vagrant

下载地址:Vagrant下载地址vim

安装步骤也比较简单,直接下一步便可后端

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

下载Box镜像

Box是Vagrant的基础镜像,和docker的images比较像,Vagrant官方提供的镜像仓库中有各类版本的Box镜像,咱们能够按需自行下载。ruby

地址:镜像下载地址网络

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

初始化开发环境

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

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

出现如图中提示说明初始化成功,它会在咱们的开发目录中为咱们自动生成一个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

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

此时咱们就能够在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

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

相关文章
相关标签/搜索