How to emulate Internet in multi machine environment with Vagrant

非 Y 不嫁゛ 提交于 2019-12-24 04:31:36

问题


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

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?


回答1:


Yes, they can talk.

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

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).

  • 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
    

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).

Working Vagrant file

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


来源:https://stackoverflow.com/questions/28094827/how-to-emulate-internet-in-multi-machine-environment-with-vagrant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!