如何使用 Vagrant 在多机环境中模拟 Internet [英] How to emulate Internet in multi machine environment with Vagrant

查看:19
本文介绍了如何使用 Vagrant 在多机环境中模拟 Internet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 vagrant 多机配置文件.这是一个块:

I'm creating a vagrant multi machine configuration file. Here is a chunk:

Vagrant.configure(2) do |config|

  config.vm.box = "chef/centos-7.0"

  config.vm.define "radius" do |radius|
    radius.vm.hostname = "radius-server"
  end

  config.vm.define "mysql" do |mysql|
    mysql.vm.hostname = "mysql-server"
  end
end

如何模拟以上两个虚拟机在不同网络中被Internet隔开的情况?我可以创建两个不同的私有网络,它们具有两个不同的私有 IP 地址,例如第一个虚拟机的 192.168.1.3 和第二个虚拟机的 192.168.2.3.在这种情况下,机器将位于不同的网络中.但是他们可以互相交谈吗?

how can I emulate the situation in which the above two virtual machines are in different networks separated by Internet? I could create two different private networks having two different private ip addresses like 192.168.1.3 for the first virtual machine and 192.168.2.3 for the second virtual machine. in this case the machines would be in different networks. But could they talk to each other?

推荐答案

是的,他们可以交谈.

您需要一个路由器(第三个节点)连接到这两个网络.

You need a router (the 3rd node) connected to both of these networks.

路由器充当(迷你)互联网:

The router acts as the (mini) Internet:

A=[192.168.1.3] <=> [*.*.1.1]=router=[*.*.2.1] <=> [192.168.2.3]=B

详情

最简单的方法是将路由器角色分配给物理主机,因为它在这两个网络上都有 IP 地址(我猜它们是 192.168.1.1192.168.2.1代码> 在您的示例中).否则,它们只是数字,如果 Vagrant 没有抱怨,网络也将使用来自公共范围的 IP 地址运行(请小心断开物理网络以进行干净测试).

Details

The easiest way is to assign the router role to the physical host because it has IP addresses on both of these networks (I guess they are 192.168.1.1 and 192.168.2.1 in your example). Otherwise, they are just numbers and, if Vagrant does not complain, network will function with IP addresses from public ranges as well (just be careful to disconnect physical network for clean testing).

  • 只需在物理主机上的操作系统中启用路由(转发) - Google 搜索的 Linux 示例.
  • 并确保通过此路由器在两个虚拟机上配置了彼此之间的路由:

  • Just enable routing (forwarding) in the OS on the physical host - googled example for Linux.
  • And make sure routes are configured on both of the virtual machines to each other via this router:

A's shell> ip route add default via 192.168.1.1
B's shell> ip route add default via 192.168.2.1

注意:从技术上讲,由于网络使用私有 IP 地址,因此它们无法在 Internet 上路由(AFAIK,真实的 Internet 路由器会丢弃带有这些私有 IP 地址的数据包,而家庭路由器则对它们进行 NAT).

NOTE: Technically, because the networks use private IP addresses, they are not routable on the Internet (AFAIK, real Internet routers drop packets with these private IP addresses, while home routers NAT them).

使用 libvirt 提供程序的测试示例.请注意,VM 连接到两个不同的网络.

Tested example using libvirt provider. Note that VMs are connected to two different networks.

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.provider "libvirt"

  config.vm.define "rhel7_minion" do |rhel7_minion|
    rhel7_minion.vm.box = "uvsmtid/centos-7.1-1503-gnome"
    rhel7_minion.vm.synced_folder '.', '/vagrant', disabled: true
    rhel7_minion.vm.network 'private_network',
        :libvirt__network_name => 'primary_vagrant_private_net',
        ip: '192.168.1.2',
        :libvirt__netmask => '255.255.255.0',
        :libvirt__forward_mode => 'route',
        :libvirt__dhcp_enabled => true
  end

  config.vm.define "rhel5_minion" do |rhel5_minion|
    rhel5_minion.vm.box = "uvsmtid/centos-5.5-minimal"
    rhel5_minion.vm.synced_folder '.', '/vagrant', disabled: true
    rhel5_minion.vm.network 'private_network',
        :libvirt__network_name => 'secondary_vagrant_private_net',
        ip: '192.168.2.3',
        :libvirt__netmask => '255.255.255.0',
        :libvirt__forward_mode => 'route',
        :libvirt__dhcp_enabled => true
  end

end

这篇关于如何使用 Vagrant 在多机环境中模拟 Internet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆