I am managing multiple VMs with Vagrant. Networks are configured as private, ip addresses have been set and hostnames are assigned. As shown in the Vagrantfile below.
The VMs can communicate with each other via the IP address, but I would like to know how to allow VMs to communicate using their assigned hostname. I.e. How to make ping comtest2 work from comtest1?
Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.define "comtest1" do |comtest1|
comtest1.vm.box = "precise32"
comtest1.vm.hostname = "comtest1"
comtest1.vm.network "private_network", ip: "192.168.10.21"
end
config.vm.define "comtest2" do |comtest2|
comtest2.vm.box = "precise32"
comtest2.vm.hostname = "comtest2"
comtest2.vm.network "private_network", ip: "192.168.10.22"
end
end
Cheat the dns resolution with https://github.com/adrienthebo/vagrant-hosts ?
You can use Zeroconf. It broadcasts the host name in network and makes it available to the other hosts on the local network. That way you can access your hosts using test1.local, test2.local, etc.
Just install avahi-daemon and libnss-mdns!
Example
Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.define "vm1" do |machine|
machine.vm.hostname = "vm1"
machine.vm.network "private_network", type: "dhcp"
end
config.vm.define "vm2" do |machine|
machine.vm.hostname = "vm2"
machine.vm.network "private_network", type: "dhcp"
end
# allow guests to reach each other by hostname
config.vm.provision "allow_guest_host_resolution",
type: "shell",
inline: <<-SHELL
apt update
apt install -y avahi-daemon libnss-mdns
SHELL
end
Test
$ vagrant up
...
$ vagrant ssh vm1 -- ping -c 1 vm2.local
PING vm2.local (172.28.128.8) 56(84) bytes of data.
64 bytes from 172.28.128.8 (172.28.128.8): icmp_seq=1 ttl=64 time=0.333 ms
$ vagrant ssh vm2 -- ping -c 1 vm1.local
PING vm1.local (172.28.128.7) 56(84) bytes of data.
64 bytes from 172.28.128.7 (172.28.128.7): icmp_seq=1 ttl=64 time=0.254 ms
It isn't the most elegant solution in the world but it is very simple, how about something like:
Vagrant.configure("2") do |config|
config.vm.define "comtest1" do |comtest1|
comtest1.vm.box = "precise32"
comtest1.vm.hostname = "comtest1"
comtest1.vm.network "private_network", ip: "192.168.10.21"
comtest1.vm.provision "shell", inline: <<-SHELL
sed -i '$ a 192.168.10.22 comtest2' /etc/hosts
SHELL
end
config.vm.define "comtest2" do |comtest2|
comtest2.vm.box = "precise32"
comtest2.vm.hostname = "comtest2"
comtest2.vm.network "private_network", ip: "192.168.10.22"
end
end
If the host resolves DNS correctly, then you can configure Virtualbox to use the host as the DNS resolver.
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
See https://serverfault.com/a/506206/250071
We use a local Ansible task to automatically add the provisioned box to the host /etc/hosts file. It is a little awkward, but has been very robust.
- setup:
gather_subset: [network]
- name: Add host mapping to local /etc/hosts
delegate_to: 127.0.0.1
lineinfile: dest=/etc/hosts regexp=".+{{ vm.hostname }}$" line="{{ ansible_all_ipv4_addresses|sort|last }} {{ vm.hostname }}"
Check out landrush on Github.
It will setup a DNS for your vagrant private network.
Related
I am working on setting up a multi-node, multi hardware server Kubernetes Cluster.
I am using Calico and Kubeadm.
So I am trying to use a bridge adapter within VMS to allow visibility over the network to a remote server using promiscuous mode and a static IP address.
The issue is when the VM is created, I cannot ping into it.
I have used to initialize kubeadm:
kubeadm init --apiserver-advertise-address="192.168.2.50" --apiserver-cert-extra-sans="192.168.2.50" --node-name master --pod-network-cidr=192.168.0.0/16
I am asking if there's an additional networking config to enable ping the Vms successfully.
This is the code I've used into the vagrantfile:
s.vm.provider "virtualbox" do |v|
v.name = vM_NAME
v.customize ['modifyvm', :id, '--nictype3', 'Am79C973']
v.customize ['modifyvm', :id, '--nicpromisc3', 'allow-all']
v.memory = 2048
v.gui = false
end
s.vm.network "private_network", ip: "192.168.2.#{i + m - 1}",# netmask: "255.255.255.0",
auto_config: true,
virtualbox__intnet: "k8s-net"
s.vm.network "public_network", bridge: "Intel(R) Ethernet Connection I217-LM", ip: "192.168.2.#{i + m -1}",# netmask: "255.255.255.0",
auto_config: true
Thank you.
While doing kubeadm init add the PUBLIC IP and PORT as part of --control-plane-endpoint parameter.
sudo kubeadm init --apiserver-advertise-address=x.x.x.x --apiserver-cert-extra-sans=x.x.x.x **--control-plane-endpoint=y.y.y.y** --node-name master --pod-network-cidr=z.z.z.z/16
Worker nodes over the network can join the master node using the new generated join-command.
I have tried doing it like this in my vagrantfile
config.vm.network "private_network", ip : "192.168.33.15"
so when I start my box, I could access nginx through my browser 'locally'.
what I want is to access it using public ip. I have tried this, (found in documentation)
config.vm.network "public_network", ip: "202.137.x.x", netmask: "255.255.x.x"
config.vm.provision "shell",
run: "always",
inline: "route add default gw 202.137.x.x"
config.vm.provision "shell",
run: "always",
inline: "eval `route -n | awk '{ if ($8 ==\"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"
this still doesn't work. any ideas?
*edit
I've also tried port forwarding,
config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "202.137.x.x"
I have to do this: 202.137.x.x:8080, I wanted to access without the port. Also, host port cannot be < 1024, so I cannot put port 80..
I've tried doing port forwarding in windows (host), so this is how I solved it..
netsh interface portproxy add v4tov4 listenport=80 listenaddress=202.137.x.x connectport=8080 connectaddress=202.137.x.x
This forwards any requests to the host on port 80 to port 8080 on the host.
So, the port forwarding flow becomes:
host:80 => host:8080 => guest:80
source here
I cannot find any information in the docs about this error message:
NFS requires a host-only network to be created.
Please add a host-only network to the machine (with either DHCP or a
static IP) for NFS to work.
Here is my Vagrant config:
Vagrant.configure(2) do |config|
config.vm.box = "localbox"
config.vm.network "public_network", hostonly: "192.168.33.10"
config.vm.synced_folder ".", "/var/www",
:nfs => true,
:mount_options =>['noacl,nolock,vers=3,udp,noatime,nodiratime,rsize=32768,wsize=32768']
When asked, I pick my Airport connection for the bridge (Wi-Fi (AirPort)).
I cannot find a single usage of hostonly in the Vagrant docs.
Using Vagrant 1.7.4
My goal is simply to be able to access the VM running on one computer in my house, from other computers (and my phone) in my house.
If you want to have NFS and Public/bridge network try this:
Vagrantfile
config.vm.network "private_network", ip: "192.168.10.100"
config.vm.network "public_network", ip: "192.168.20.200"
or
config.vm.network "private_network", ip: "192.168.10.100"
config.vm.network "public_network", ip: "192.168.20.200", bridge: "en1: Wi-Fi (AirPort)"
Replace hostonly by ip in your Vagrantfile
config.vm.network "public_network", ip: "192.168.33.10"
I am using vagrant with virtualbox as provider. Within my guest system I have nginx installed and configured.
nginx is serving some static files from a folder and exposing them on port 80. That works fine. If I call curl localhost within the guest machine I get the answer I was supposed to receive.
I have a very simple vagrantfile, which you can see below. I forward port 80 to port 8080, but from the host machine I cant access that page via localhost:8080.
I already disabled the firewall in the guest machine without any success.
Vagrant.configure("2") do |config|
# VirtualBox Settings: Give it a little bit more memory
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "768"]
end
# Base Image: CentOS 7.0 x86_64
config.vm.box = "jayunit100/centos7"
# Use Vagrant's default insecure key (~/.vagrant.d/insecure_private_key)
config.ssh.insert_key = false
# Add port forwarding for node-inspector
config.vm.network :forwarded_port, guest: 80, host: 8080 # node-inspector
# Map project directory
config.vm.synced_folder ".", "/server/"
# Provisioning Shell Script
config.vm.provision :shell, :path => "vagrant-setup/base.sh"
end
If I call curl -v 'http://localhost:8080' from the host system I get told that the connection got refused. Any idea what I could do?
I had to disable my firewall on the host machine with iptables -F
For the purpose of this post, I am using Vagrant to launch NGINX (through Docker, but that is not important I don't think).
My Vagrant looks like the following:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
#Assign Box and VM Properties
config.vm.box = "ubuntu/trusty64"
config.vm.provider "virtualbox" do |v|
v.memory = 1024
v.cpus = 2
end
# Network
config.vm.network "forwarded_port", guest:80, host: 80 #--> DOESN'T WORK localhost
config.vm.network "forwarded_port", guest:80, host:8391 #--> WORKS localhost:8391
# Provision
config.vm.provision :shell, inline: "sudo apt-get update"
config.vm.provision :docker
end
The goal is to be able to hist NGINX on localhost and not localhost:8391
I KNOW that NGINX is listening on 80 because of the mapping, and from running CURL within Vagrant.
You can use setcap to enable to use ports under 1024 for non-root users for specific binaries.
This only works under Linux and must be applied to the Vagrant box, to use Port 80 inside the box, and your host, to use Port 80 on your host.
You need the package libcap2-bin, e.g. with apt:
sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /path/to/nginx-binary
Afterwards NGINX is allowed to use Port 80 inside the box as user vagrant. Now enable setup for Vagrant on your host.
sudo setcap cap_net_bind_service=+ep /path/to/vagrant-binary
In general you can't bind to ports 1024 or under on the host when using Vagrant, unless you run it as root. (As with other apps, it's obviously not recommended to run Vagrant as root.)
As an alternative, if you don't need to connect to "localhost" specifically you could try setting up a private network so your Vagrant box has a separate IP address. See http://docs.vagrantup.com/v2/networking/private_network.html for more info. That should let you connect to port 80 on that IP fine.