Vagrant (三) - 网络配置

网络配置

Vagrant可以指定三种网络:端口转发(Forwarded Ports),私有网络(Private Network),公有网络(Public Network)。他们和多数虚拟机提供的网络是对应的。

Vagrant 网络模型
端口转发:
config.vm.network :forwarded_port, guest: 80, host: 8080

将虚拟机(被称作guest)的端口80映射为宿主机的端口8080

端口转发隐含着一个provider的NAT网络将被首先创建。
所以,如果你单独定义一条端口转发的配置语句的话,VM将会自动建立NAT网络环境。

私有网络:
config.vm.network :private_network, ip: "192.168.1.104"

你可以从宿主机自由访问虚拟机,但LAN网络中的其他人不需要也无法访问虚拟机。

值得注意的是,ip地址“192.168.1.104”不是随便指定的。
首先你可以不指定,这表示虚机启动时会DHCP到一个可用的IP地址(例如:192.168.33.101),这是vagrant通过virtualbox私有网络的DHCP机制获得的。
如果你要自行指定明确的IP地址,要保证该地址是在恰当的网段中,例如192.168.33.71。

多台虚拟机在私有网络模式下也可以互相访问,只要设置为相同的网段就可以。

本质上说,这是使用provider的HostOnly模式。

公有网络:

公有网络实际上是表示将虚拟机暴露为LAN(例如你的宿主机所在的办公室网络)中的一台主机。

例如使用LAN的DHCP自动获得IP地址:

config.vm.network :public_network

也可以指定LAN网段中的一个可用的地址,但需要注意不要和LAN中已有的主机或者保留的IP地址相冲突。

本质上说,这是使用provider的桥接网络模式。

Provider的网络模式

对于vagrant的provider,例如VirtualBox来说,网络模式区分的更细,但vagrant并不能使用全部vbox网络模型。

VirtualBox 的典型网络模型:NAT,Hostonly,Bridge以及Internal。

这些模式的细节我们不再列举。

借用一张表格来归纳:

NATBridgedInternalHostonly
vm -> host××
vm -> vm×××
vm -> others hosts××
others hosts => vm×××
vm <-> vm×same subnet×

这张表格描述了virtualbox的网络模型。

实用的网络配置

一般来说,端口转发足以满足开发需要了。

但对于特殊的需要来说,你可能需要一台完全“真实”的虚机,这台虚机可以被稳定地从宿主机访问,并且可以访问LAN中的其他资源。这样的需求实际上可以通过配置多块网卡来解决问题,例如一块配置为私有网络模式,一块配置为公有网络模式。

vagrant通过配置文件能够支持virtualbox的NAT,Bridge以及Hostonly网络模型。

默认情况

默认情况下,我们已经知道一个最简的流程来启动vagrant:

mkdir /dev
cd /dev
vagrant box add ubuntu/trusty64
vagrant init ubuntu/trusty64
vagrant up
vagrant ssh
vagrant halt

这样的步骤,可以得到一台ubuntu 14.04的虚拟机,采用Provider的NAT网络模式,在虚拟机中可以访问宿主机,也可以使用宿主机的外网路由上网。

观察它生成的默认的Vagranfile,其网络配置是未指定的。

此时,vagrant建立的vm具有一个NAT网卡。

桥接网络

当采用如下配置语句时,vagrant建立的vm具有一个Bridged网络:

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

此时,vm在宿主机所在的LAN中等价于一台物理机器。假如你具有LAN Router的管理权的话,这是很简单的一种开发模型:通过路由器的mac绑定为vm保留一个固定的dhcp地址,这样vm无论何时启动都会获取到相同的IP地址,你的开发和调试将会很顺利很简单。

私有网络

当采用如下配置语句时,vagrant建立的vm具有两个hostonly网络:

config.vm.network "private_network", ip: "192.168.9.10"
config.vm.network "private_network", ip: "192.168.33.10"

标识符“private_network”总是被映射为virtualbox的hostonly模型。

私有网络模型是允许宿主机访问的,为了便于宿主机访问,我们也可以明确指定一个适当网段的地址。要知道适当的网段是多少,可以这样查证:

通过进入虚机显示网卡信息和IP地址来了解网段。
打开 VirtualBox 的网络配置,检查HostOnly网络的DHCP网段。

混合网络

当采用如下配置语句时,vagrant建立的vm具有一个NAT和一个hostonly网络:

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

标识符“private_network”总是被映射为virtualbox的hostonly模型。

注意 NAT 网络将被隐含地创建。

vagrant在创建网卡时,如果配置文件仅配置了一个private_network,则vagrant自动创建NAT网卡,然后在创建配置文件所描述的网络;而如果配置文件指定了两个以上的private_network的话,vagrant不再自动创建

NAT 网卡了。 混合网络非常适合开发和测试环境,你可以通过NAT和Internet相通,然后多个vm之间也能相互通信。

内外网络

内外网络 只是我随便命名的,也就是从vm的角度出发既有内网(VM的私有网络),又有外网(宿主机所在的局域网)。

当采用如下配置语句时,vagrant建立的vm具有一个bridged和一个hostonly网络:

config.vm.network “public_network”
config.vm.network “private_network”, ip: “192.168.33.10”
这是比较通用的配置模式,vm既有host主机所在局域网的ip,又有一个私有网络的ip地址,因此这些vm之间具有全连通性。

不过,一般来说开发和测试使用较为封闭的网络模型是比较好的方式,通常不建议vm配置有 public_network 的网卡。

小结

任何

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

语句都可以改为:

config.vm.network "private_network", type: "dhcp"

这时virtualbox的网关负责dhcp应答和分配IP。

实例

nginx服务
  1. 建立一个新的工作目录 sample-nginx
  2. 使用 vagrant init ubuntu/xenial64 进行初始化
  3. 修改 Vagrantfile 加入必要的声明 (见后)
  4. 使用 vagrant up 启动该虚拟机,然后可以SSH进入或者 curl -i http://localhost:8080/
    来尝试访问它

完整的 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 = "ubuntu/xenial64"
  
  # 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
  # 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 nginx
  SHELL
end

这是一个相当简单的实例。更复杂的初始化,可以考虑使用一个独立的脚本文件来完成,而不是直接放在 Vagrantfile 中。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值