简体   繁体   中英

Configuration private network with Vagrant

I'm configuring 3 virtual machines on my desktop with Vagrant. And I wanna build cluster with these 3 VMs. And I wanna configure the IP of these 3 machines in private network and access each VM in my desktop only . The reason of this configuration is I'll use these three VMs for development only.

So could you answer the questions how I shall configure the IP of VM with Vagrant for this purpose? I cannot understand how I configure IP address of private network.

Create A Vagrant file with name Vagrantfile and add:

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

Vagrant.configure("2") do |config|
         config.vm.provider :libvirt do |libvirt|
         libvirt.driver = "kvm"
         libvirt.host = 'localhost'
         libvirt.uri = 'qemu:///system'
         end
#        
config.vm.define "test1" do |vm1|
         vm1.vm.box = "ubuntu/trusty64"
         vm1.vm.hostname="vm1.example.com"
          vm1.vm.provider :libvirt do |domain|
         domain.memory = 1024
         domain.cpus = 1
         end
        vm1.vm.network :private_network,
                        :ip => '192.168.1.10',
                        :libvirt__netmask => '255.255.255.0',
                        :libvirt__network_name => 'mynetwork',
                        :libvirt__forward_mode => 'none'

end

config.vm.define "test2" do |vm2|
         vm2.vm.box = "ubuntu/trusty64"
         vm2.vm.hostname="vm2.example.com"
         vm2.vm.provider :libvirt do |domain|
         domain.memory = 1024
         domain.cpus = 1
         end
         vm2.vm.network :private_network, :ip => '192.168.1.11', :libvirt__network_name => 'mynetwork'
end 
config.vm.define "test3" do |vm3|
         vm3.vm.box = "ubuntu/trusty64"
         vm3.vm.hostname="vm3.example.com"
          vm3.vm.provider :libvirt do |domain|
         domain.memory = 1024
         domain.cpus = 1
         end
         vm3.vm.network :private_network, :ip => '192.168.1.12', :libvirt__network_name => 'mynetwork'

end

end

According to your provider you can change. This is for libvirt provider. In first test1 block

vm1.vm.network :private_network,
                            :ip => '192.168.1.10',
                            :libvirt__netmask => '255.255.255.0',
                            :libvirt__network_name => 'mynetwork',
                            :libvirt__forward_mode => 'none' 

this will create a isolated private network 192.168.1.0/24 with name mynetwork . you can change it something else. In second test2 and third test3 simply using network mynetwork . if you don't give any network name then it will assign some other name so it will better add networkname for more understandable. use command to up:

vagrant up test1 
vagrant up test2
vagrant up test3

If you are using virtualbox then Vagrantfile will be like below but many syntax will be same.

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", "1024"]
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM