⭐⭐⭐ Spring Boot 项目实战 ⭐⭐⭐ Spring Cloud 项目实战
《Dubbo 实现原理与源码解析 —— 精品合集》 《Netty 实现原理与源码解析 —— 精品合集》
《Spring 实现原理与源码解析 —— 精品合集》 《MyBatis 实现原理与源码解析 —— 精品合集》
《Spring MVC 实现原理与源码解析 —— 精品合集》 《数据库实体设计合集》
《Spring Boot 实现原理与源码解析 —— 精品合集》 《Java 面试题 + Java 学习指南》

摘要: 原创出处 blog.csdn.net/mr__bai/article/details/124472802 「mr__bai」欢迎转载,保留摘要,谢谢!


🙂🙂🙂关注**微信公众号:【芋道源码】**有福利:

  1. RocketMQ / MyCAT / Sharding-JDBC 所有源码分析文章列表
  2. RocketMQ / MyCAT / Sharding-JDBC 中文注释源码 GitHub 地址
  3. 您对于源码的疑问每条留言将得到认真回复。甚至不知道如何读源码也可以请教噢
  4. 新的源码解析文章实时收到通知。每周更新一篇左右
  5. 认真的源码交流微信群。

一、vagrant介绍

是一款用于管理虚拟机的命令行使用软件,用Ruby语言开发而成。换言说,可以省去你使用虚拟机创建操作系统的所有操作,比如创建虚拟机,挂在镜像文件,一步步点击安装等等,使用vagrant,这些都不需要做了,简简单单两行命令,快速创建属于你个人的系统。

在开发方面,Vagrant 是一款用来构建虚拟开发环境的工具,非常适合 php/python/ruby/java 这类语言开发 web 应用。我们可以通过 Vagrant 封装一个 Linux 的开发环境,分发给团队成员。成员可以在自己喜欢的桌面系统(Mac/Windows/Linux)上开发程序,代码却能统一在封装好的环境里运行,非常霸气。

二、安装用到的软件

1、安装virtualBox (此处不再讲解安装)

虽然 Vagrant 也支持 VMware,不过 VMware 是收费的,对应的 Vagrant 版本也是收费的

图片

2、安装Vagrant

(1)进入官网https://www.vagrantup.com/downloads,选择版本下载

图片

(2)傻瓜式安装

图片

(3)检测是否安装成功

命令行输入vagrant,如下图显示,说明安装成功。

图片

三、下载系统镜像。

在vagrant官网中,有一个findbox的按钮,这个是vagrant的镜像库,里边列出了都要哪些镜像可以用,并且提供了操作文档。

图片

但是这里默认下载往往会比较慢,所以下面我会介绍如何在其它地方下载到基础镜像,然后按照自己的需要重置。如果网速较好,下载顺利的朋友可以选择性地跳过部分内容。

下面我给出最常用的两个 Linux 操作系统镜像的下载地址:

CentOS

CentOS 的镜像下载网站是:

  • http://cloud.centos.org/centos/

在其中选择自己想要下载的版本,列表中有一个 vagrant 目录,里面是专门为 vagrant 构建的镜像。选择其中的 .box 后缀的文件下载即可。这里可以使用下载工具,以较快的速度下载下来。

Ubuntu

Ubuntu 的镜像下载网站是:

  • http://cloud-images.ubuntu.com/

同样先选择想要的版本,然后选择针对 vagrant 的 .box 文件即可。

如果这里官网的速度较慢,还可以从 清华大学的镜像站 下载。

例如:

图片

四、查询、添加、删除box

接下来我们需要将下载后的 .box 文件添加到 vagrant 中。Vagrant 没有 GUI,只能从命令行访问。

(1)查询vagrant 已经管理的 Box 有哪些:

vagrant box list

图片

(2)添加box命令

vagrant box add box的文件路径及文件名 --name centos8

  • vagrant box add 将 box 添加到vagrant 中,命令后面跟着的是box文件路径
  • --name centos8 可以为这个 box 指定一个名字

安装好以后如下图,可以在用查询命令查询一下,可看到安装好的box。

图片

(3)删除box命令

vagrant box remove NAME      #根据名字删除指定的box

图片

五、vagrant基本操作

(1)新建虚拟机

我们在创建虚拟机的时候,会生产一些文件,所以我们为每个虚拟机最好都创建一个独立的文件夹。然后进入文件夹中。在文件夹路径下初始化

vagrant init [boxname]  #加上boxname 表示使用哪个box 创建虚拟机

图片

初始化后,会在文件夹下生成一个配置文件

图片

(2) 启动虚拟机

所有的 vagrant 命令都需要在 Vagrantfile 所在的目录下执行:

vagrant up

图片

如果没有报错,说明启动成功。(注意box的名字,写错了会报错)

(3)查看虚拟机状态

vagrant status

图片

如果是running 就说明我们的虚拟机,启动成功了

(4)链接虚拟机

如果启动没问题,此时在vbox的列表中自动生成一个虚拟机,这个虚拟机以文件夹名字命名,可以看到自动创建的虚机:

图片

执行 vagrant ssh 就能以 vagrant 用户直接登入虚机中。

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

图片

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

图片

(5)停止虚拟机

vagrant halt

图片

(6)暂停虚拟机

vagrant suspend

(7)恢复虚拟机

vagrant resume

注意:不管虚机是关闭还是暂停状态,甚至是 error 状态,都可以执行 vagrant up 来让虚机恢复运行

(8)删除虚拟机

vagrant destroy

六、Vagrantfilefile源文件

# -*- 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 = "centos8"

# 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

这是一个 Ruby 语法的文件,因为 Vagrant 就是用 Ruby 编写的。如果编辑器没有语法高亮可以手动设置文件类型为 Ruby。

这个缺省文件内容几乎都是注释,提示有哪些配置项可以修改,我们不需要去学 Ruby 编程也可以照葫芦画瓢的完成基本的配置。

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

Vagrant.configure("2") do |config|
config.vm.box = "centos8"
end

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

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

七、自定义配置Vagrantfile

1、配置端口转发

端口转发(Port forward)又叫端口映射,就是把虚机的某个端口,映射到宿主机的端口上。这样就能在宿主机上访问到虚拟机中的服务。

例如启动虚机时,默认的 22 (guest) => 2222 (host) (adapter 1) 就是把虚机的 SSH 服务端口(22)映射到宿主机的 2222 端口,这样直接在宿主机通过 ssh 客户端问 127.0.0.1:2222 端口就等价于访问虚拟机的 22 端口。

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

实际上设置端口转发这个功能并不实用,一个很明显的问题就是如果启动多个虚机,很容易就出现宿主机上端口冲突的问题。即使没有端口冲突,使用起来也不方便,我个人不推荐使用的,可以把这部分配置直接删掉。直接使用下面的私有网络。

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

图片

还有个地方需要注意,默认的 SSH 端口映射在这里没法直接修改。比如像我这样,2222 端口出现莫名问题,如果想要把 22 端口转发到其它端口如 22222,必须要先强制关闭掉默认的那条规则:

因为不关闭的话,只写第二行,会在原来的基础上新加一个端口转发规则,而不是替代原来的

config.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: "true"
config.vm.network "forwarded_port", guest: 22, host: 22222

2、配置私有网络

下面这段配置用来配置私有网络,实际上对应的是 VirtualBox 的主机网络,也就是 HostOnly 网络

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

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

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

修改完成后,执行 vagrant reload 命令重建虚机,就能看到多出来的网卡了。

文章目录
  1. 1. 一、vagrant介绍
  2. 2. 二、安装用到的软件
    1. 2.0.1. 1、安装virtualBox (此处不再讲解安装)
      1. 2.0.1.1. 2、安装Vagrant
  • 3. 三、下载系统镜像。
  • 4. 四、查询、添加、删除box
  • 5. 五、vagrant基本操作
  • 6. 六、Vagrantfilefile源文件
  • 7. 七、自定义配置Vagrantfile
    1. 7.0.1. 1、配置端口转发
    2. 7.0.2. 2、配置私有网络