10

I have a multi-machine Vagrant setup. Using NFS and private_network I can get everything to work that I need (Drupal, PHP, etc) EXCEPT allowing someone down the hall to visit a web app running on my VM. I understand that private_network makes it impossible to connect to the VMs from the outside world.

Is there a way to create both a private AND public network so that all VMS EXCEPT the load balancer are private and the load balancer is accessible via the host's ip?

hosts = {
  "wloadlocal" => "192.168.33.10",
  "wweblocal1" => "192.168.33.11",
  "wweblocal2" => "192.168.33.12",
  "wwhlocal" => "192.168.33.13",
}

Vagrant.configure("2") do |config|
  hosts.each do |name, ip|
    config.vm.define name do |machine|
      machine.vm.box = "precise32"
      machine.vm.box_url = "http://files.vagrantup.com/precise32.box"
      machine.vm.hostname = "%s.example.org" % name
      machine.vm.network :private_network, ip: ip
      machine.vm.network "public_network"
      machine.vm.provider "virtualbox" do |v|
          v.name = name
          v.customize ["modifyvm", :id, "--memory", 200]
      end
      if name == "wloadlocal"
        machine.vm.network "forwarded_port", guest: 80, host: 8880
      end
    end
    #sync folders
    config.vm.synced_folder "~/data", "/data", type: "nfs"
  end
end

Edit #1

On Vagrant's NFS doc page it states that when using VirtualBox and NFS you must use private_network:

If you're using the VirtualBox provider, you'll also need to make sure you have a private network set up. This is due to a limitation of VirtualBox's built-in networking. With VMware, you do not need this.

I need to use NFS for performance reasons but I also would like to be able to share apps running in the VMs over a local network when needed. Is this possible?

eis
  • 51,991
  • 13
  • 150
  • 199
frodopwns
  • 942
  • 9
  • 15
  • 10
    P.S. To all the closers: Vagrant is a _dev_ tool, it's not a networking/sysadmin tool. Its purpose is to enable devs to set up VM recipes that work identically for all devs in your team. – C. K. Young Oct 21 '14 at 19:56
  • 5
    While Vagrant has an API, how to set up the internal network the way the OP wants could be a better fit on [su]. Once that configuration is known then putting it into code should be easy. Toward the OPs question, I don't think you can create two types of networks internally. – the Tin Man Oct 21 '14 at 20:34
  • 2
    I think this bouncing between sites is getting just silly. Let the question be. It's not in the scope of Super User to do Vagrant configurations. – eis Oct 22 '14 at 08:27
  • 1
    @frodopwns it should be fully possible to have multiple networks, like [explained in the manual](http://docs.vagrantup.com/v2/networking/basic_usage.html) ("Multiple Networks") as well as in [this answer](http://stackoverflow.com/questions/23497855/unable-to-connect-to-vagrant-private-network-from-host). Do you have a problem with those solutions? If so, can you detail what it is you're having problems with? – eis Oct 22 '14 at 08:39
  • @eis the poster in the linked question doesn't have the same issue I have. He was unable to access his VM when using a private network. I can access mine. I need a way to have both NFS (which requires a private network) and allow outside users to access a port on one of my VMs. – frodopwns Oct 22 '14 at 16:26
  • The best answer I have found so far is Vagrant Share but I'm open to other suggestions. – frodopwns Oct 22 '14 at 16:35
  • @ChrisJester-Young: But this question isn't about achieving a desired configuration using vagrant. He doesn't know what configuration he wants. See http://meta.stackoverflow.com/a/275041/103167 – Ben Voigt Oct 23 '14 at 03:36
  • @Ben I don't have a desired configuration I have a desired outcome. – frodopwns Oct 23 '14 at 15:40
  • @frodopwns: That's exactly why it is off-topic here. Finding a configuration that provides your outcome is a networking question. Automating that configuration, once you find it, using Vagrant will be an on-topic question for SO. – Ben Voigt Oct 23 '14 at 15:44
  • 1
    @Ben I don't think your differentiation makes any difference here. Have you worked with Vagrant? – frodopwns Oct 23 '14 at 22:32

1 Answers1

2

Simplest way I could find around this was to use multiple networks as mentioned in one of the comments. So my config ends up looking like this:

  config.vm.define :host01 do |host01|
    postgres01.vm.network :private_network, ip: '192.168.37.2'
    config.vm.network "public_network"
    host01.vm.hostname = 'host01'
    configure_vm(host01)
  end

Then I did vagrant ssh host01 to look up the public ip which ended up being 192.168.0.102 on my network. Now I can access that ip from my host. Note there are some caveats to using the public network feature.

lucrussell
  • 5,032
  • 2
  • 33
  • 39