客户机之间的流动通信

时间:2017-05-18 15:29:39

标签: vagrant virtualbox vagrantfile

我有多个像这样的Vagrant机器:

config.vm.define 'vagrant1' do |vagrant1|
    config.vm.provider :virtualbox do |vb|
        vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
    end
    vagrant1.vm.box = 'ubuntu/trusty64'
    vagrant1.vm.network 'forwarded_port', guest: 80, host: 8080 
    vagrant1.vm.network 'forwarded_port', guest: 443, host: 8443
    vagrant1.vm.network 'forwarded_port', guest: 27017, host: 27017
    # Create a private network, which allows host-only access to the machine
    # using a specific IP.
    config.vm.network 'private_network', ip: '192.168.56.11'

    ENV['LC_ALL']='en_US.UTF-8'

end

config.vm.define 'vagrant2' do |vagrant2|
    config.vm.provider :virtualbox do |vb|
        vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
    end
    vagrant2.vm.box = 'ubuntu/trusty64'
    vagrant2.vm.network 'forwarded_port', guest: 80, host: 8081
    vagrant2.vm.network 'forwarded_port', guest: 443, host: 8444
    vagrant2.vm.network 'forwarded_port', guest: 27017, host: 27018
    # Create a private network, which allows host-only access to the machine
    # using a specific IP.
    config.vm.network 'private_network', ip: '192.168.56.12'

    ENV['LC_ALL']='en_US.UTF-8'
end 

我希望vagrant1计算机能够与vagrant2计算机进行通信。到目前为止,我可以从我的主机连接,但客户机无法相互访问。

如何启用来宾计算机之间的通信?

2 个答案:

答案 0 :(得分:4)

好吧我发现了错误 - 您错误地使用了配置变量,您编写为config.vm.的所有内容对所有虚拟机都有效,即使它在一个块内也是如此,因此在您创建多个网络接口的情况下。 / p>

因此,Vagrantfile的简化版本可以写成

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

    config.vm.box = 'ubuntu/trusty64'
    config.vm.provider :virtualbox do |vb|
        vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
    end

    config.vm.define 'vagrant1' do |vagrant1|
        vagrant1.vm.network 'private_network', ip: '192.168.56.11'
        ENV['LC_ALL']='en_US.UTF-8'
    end

    config.vm.define 'vagrant2' do |vagrant2|
        vagrant2.vm.network 'private_network', ip: '192.168.56.12'
        ENV['LC_ALL']='en_US.UTF-8'
    end 

end

注意:当您使用静态IP时不需要转发端口,因为您可以直接从IP访问

答案 1 :(得分:0)

尝试一起定义框;

Vagrant.configure("2") do |config|
    config.vm.define 'vagrant1' do |vagrant1|
        config.vm.provider :virtualbox do |vb|
            vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
        end
        vagrant1.vm.box = 'ubuntu/trusty64'
        vagrant1.vm.network 'forwarded_port', guest: 80, host: 8080 
        vagrant1.vm.network 'forwarded_port', guest: 443, host: 8443
        vagrant1.vm.network 'forwarded_port', guest: 27017, host: 27017
        # Create a private network, which allows host-only access to the machine
        # using a specific IP.
        config.vm.network 'private_network', ip: '192.168.56.11'

        ENV['LC_ALL']='en_US.UTF-8'

    end

    config.vm.define 'vagrant2' do |vagrant2|
        config.vm.provider :virtualbox do |vb|
            vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
        end
        vagrant2.vm.box = 'ubuntu/trusty64'
        vagrant2.vm.network 'forwarded_port', guest: 80, host: 8081
        vagrant2.vm.network 'forwarded_port', guest: 443, host: 8444
        vagrant2.vm.network 'forwarded_port', guest: 27017, host: 27018
        # Create a private network, which allows host-only access to the machine
        # using a specific IP.
        config.vm.network 'private_network', ip: '192.168.56.12'

        ENV['LC_ALL']='en_US.UTF-8'
    end 
end