I had the privilege of testing a dedicated server from OVH while they’re going through their new datacenter beta, and although it started up with Debian 6.0, I saw that I had the option of installing Proxmox 2.1. I love Proxmox, and have been using it since version 0.5.
Anyway, as excited as I was to have it up, I ran into the battle of running all of my VMs and the host node under a single public IP address. Luckily, the host is just pure Debian, and ships with iptables.
What needs to be done is essentially to run all the VMs on a private internal network. Outbound internet access is done via NAT. Inbound access is via port forwarding.
Here’s how it’s done:
Create a virtual interface that serves as the gateway for your VMs:
My public interface (the one with the public IP assigned) is vmbr0. I will then create an alias interface called vmbr0:0 and give it a private IP address in /etc/network/interfaces. Note that this is needed for KVM and OpenVZ bridged interfaces; venet interfaces automagically work.
|
|
auto vmbr0:0 iface vmbr0:0 inet static address 192.168.4.1 netmask 255.255.255.0 network 192.168.4.0 broadcast 192.168.4.255 |
Create an iptables rule to allow outbound traffic:
There are a few ways to specify this, but the most straightforward is:
|
|
iptables -A POSTROUTING -s 192.168.4.0/24 -o vmbr0 -j MASQUERADE |
In one of your VMs, set the interface IP to something in 192.168.4.2-254, and set the default gateway to 192.168.4.1, with the subnet mask of 255.255.255.0. Feel free to adjust this as you see fit. Test pinging your public IP address, and perhaps even an external address (like 4.2.2.2). If this works, you’re on the right track.
At this point, you have internet access from your VMs, but how do you get to them? For your OpenVZ containers, sure, you could SSH into the host node and ‘vzctl enter’ into a CTID, but that’s probably not what you want. We will need to set iptables rules to dictate which ports point to which servers.
Assuming you want VM 100 to have SSH on port 10022, and let RDP of VM 101 ‘live’ on port 10189, we can do the following:
|
|
iptables -A PREROUTING -i vmbr0 -p tcp -m tcp --dport 10022 -j DNAT --to-destination 192.168.4.100:22 iptables -A PREROUTING -i vmbr0 -p tcp -m tcp --dport 10189 -j DNAT --to-destination 192.168.4.101:3389 |
You can add as many of these as you’d like.
Once you have your configuration set up as you please, we will need to make it persistent. If you reboot at this point, all of your iptables rules will be cleared. To prevent this, we simply do:
|
|
iptables-save > /etc/iptables.rules |
This step saves the rules to an iptables-readable file. In order to apply them upon boot, you have several options. One of the easier ones is to modify /etc/network/interfaces as such (notice the third line):
|
|
auto vmbr0 iface vmbr0 inet static pre-up iptables-restore < /etc/iptables.rules address pu.bl.ic.ip netmask 255.255.255.0 ... |
At this point, you now have a functioning inbound/outbound setup on your own private LAN.