Difference between revisions of "Using NAT for container with private IPs"

From OpenVZ Virtuozzo Containers Wiki
Jump to: navigation, search
(fixed bad iptables -s example (thanks to laurento frittella, bug #1158), fixed a typo in IP address)
Line 42: Line 42:
 
</pre>
 
</pre>
  
where <tt>src_net</tt> is a range of IP addresses of containers to be translated by SNAT, and <tt>ip_address</tt> is the external IP address of your [[Hardware Node]]. The format of src_net is xx.xx.xx.xx/xx. For example to specify IP addresses from 192.168.2.12 through 192.168.2.25 use:
+
where <tt>src_net</tt> is a range of IP addresses of containers to be translated by SNAT, and <tt>ip_address</tt> is the external IP address of your [[Hardware Node]]. The format of src_net is xx.xx.xx.xx/xx ([[w:CIDR|CIDR notation]]). For example to specify IP addresses from 192.168.2.1 through 192.168.2.127 use:
 
<pre>
 
<pre>
# iptables -t nat -A POSTROUTING -s 192.168.2.12/25 -o eth0 -j SNAT --to ip_address
+
# iptables -t nat -A POSTROUTING -s 192.168.2.0/25 -o eth0 -j SNAT --to ip_address
 
</pre>
 
</pre>
  
Line 56: Line 56:
 
If you are using an iptables firewall (for example the default RedHat/CentOS firewall), don't forget to allow outgoing connections from your containers. For example if you are using the subnet 172.16.150.0/24 for your VMs, you should do the following:
 
If you are using an iptables firewall (for example the default RedHat/CentOS firewall), don't forget to allow outgoing connections from your containers. For example if you are using the subnet 172.16.150.0/24 for your VMs, you should do the following:
 
<pre>
 
<pre>
# /sbin/iptables -A RH-Firewall-1-INPUT -s 172.16.149.0/24 -j ACCEPT
+
# /sbin/iptables -A RH-Firewall-1-INPUT -s 172.16.150.0/24 -j ACCEPT
 
# /sbin/iptables-save > /etc/sysconfig/iptables
 
# /sbin/iptables-save > /etc/sysconfig/iptables
 
# /sbin/service iptables restart
 
# /sbin/service iptables restart

Revision as of 15:39, 27 January 2009

Usually you supply public IP addresses to your containers. Sometimes you don't want to do it (lack of IPs, etc.). This article describes how to use private IP addresses for containers.

Prerequisites

IP forwarding

IP forwarding should be turned on, on the hardware node in order for container networking to work. Make sure it is turned on:

$ cat /proc/sys/net/ipv4/ip_forward 
1

Output should be '1'. If it is '0', enable IP forwarding as it is described in Quick installation#sysctl.

NOTE: Ubuntu made some changes to the syntax for NAT. See this link if you are needing to enable NAT on an Ubuntu host :

Launchpad

The syntax of /etc/sysctl.conf has changed to :

net.ipv4.conf.default.forwarding=1
net.ipv4.conf.all.forwarding=1

IP conntracks

IP connection tracking should be enabled for CT0.

For OpenVZ kernels 2.6.8, put the following line into /etc/modprobe.conf:

modprobe ip_conntrack ip_conntrack_enable_ve0=1

and reboot.

For OpenVZ kernels later than 2.6.8, connection tracking for CT0 is enabled by default. However, make sure there is no line like

options ip_conntrack ip_conntrack_disable_ve0=1

in /etc/modules.conf or /etc/modprobe.conf. If there is such line, comment it out (or remove) and reboot.

How to provide access for container to Internet

To enable the containers, which have only internal IP addresses, to access the Internet, SNAT (Source Network Address Translation, also known as IP masquerading) should be configured on the Hardware Node. This is ensured by the standard Linux iptables utility. To perform a simple SNAT setup, execute the following command on the Hardware Node:

# iptables -t nat -A POSTROUTING -s src_net -o eth0 -j SNAT --to ip_address

where src_net is a range of IP addresses of containers to be translated by SNAT, and ip_address is the external IP address of your Hardware Node. The format of src_net is xx.xx.xx.xx/xx (CIDR notation). For example to specify IP addresses from 192.168.2.1 through 192.168.2.127 use:

# iptables -t nat -A POSTROUTING -s 192.168.2.0/25 -o eth0 -j SNAT --to ip_address

Multiple rules are allowed, for example, in case you wish to specify several ranges of IP addresses. If you are using a number of physical network interfaces on the Node, you may need to specify a different interface for outgoing connections, e.g. -o eth2.

To make all IP addresses to be translated by SNAT (not only the ones of containers with private addresses), you should type the following string:

# iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to ip_address

If you are using an iptables firewall (for example the default RedHat/CentOS firewall), don't forget to allow outgoing connections from your containers. For example if you are using the subnet 172.16.150.0/24 for your VMs, you should do the following:

# /sbin/iptables -A RH-Firewall-1-INPUT -s 172.16.150.0/24 -j ACCEPT
# /sbin/iptables-save > /etc/sysconfig/iptables
# /sbin/service iptables restart
Yellowpin.svg Note: If the above is not working then check if one of the following solutions does the trick.

1. If you are using stable (currently 2.6.8-based) kernel, then to enable SNAT for the containers on your local network you need to explicitly enable connection tracking in CT0. Make sure that the following string is present in the /etc/modprobe.conf file:

options ip_conntrack ip_conntrack_enable_ve0=1
Yellowpin.svg Note: in kernels later than 2.6.8, connection tracking is enabled by default

In case it is not, add this string to the file by means of any text editor (for example, vi). This setting is not needed for kernels more recent than 2.6.8, since connection tracking for CT0 is enabled by default in those kernels.

2. For unknown reasons the above didn't work on a Debian host. The solution is to do it in an init.d script as follows:

modprobe ip_conntrack ip_conntrack_enable_ve0=1

Make sure that this module is loaded before any of the other iptables-modules are loaded! Also remember that if this module is loaded without the option, unloading and reloading doesn't work! You need to reboot the computer.

Yellowpin.svg Note: in kernels later than 2.6.8, connection tracking is enabled by default
Yellowpin.svg Note: you may need to modify the /etc/vz/vz.conf file to include:
IPTABLES="iptable_filter iptable_mangle ipt_limit ipt_multiport ipt_tos ipt_TOS ipt_REJECT ipt_TCPMSS ipt_tcpmss ipt_ttl ipt_LOG ipt_length ip_conntrack ip_conntrack_ftp ip_conntrack_irc ipt_conntrack ipt_state ipt_helper iptable_nat ip_nat_ftp ip_nat_irc ipt_REDIRECT"

How to provide access from Internet to a container

In addition, to make some services in container with private IP address be accessible from the Internet, DNAT (Destination Network Address Translation) should be configured on the Hardware Node. To perform a simple DNAT setup, execute the following command on the Hardware Node:

# iptables -t nat -A PREROUTING -p tcp -d ip_address --dport port_num \
  -i eth0 -j DNAT --to-destination ve_address:dst_port_num 

where ve_address is an IP address of the container, dst_port_num is a tcp port which requires service use, ip_address is the external (public) IP address of your Hardware Node, and port_num is a tcp port of Hardware Node, which will be used for Internet connections to private container service. Note that this setup makes the service which is using port_num on the Hardware Node be unaccessible from the Internet. Also note that SNAT translation is required too.

For example, if you need a web server in a container to be accessible from outside and, at the same time, keep a web server on the Hardware Node be accessible, use the following config:

# iptables -t nat -A PREROUTING -p tcp -d ip_address --dport 8080 \
  -i eth0 -j DNAT --to-destination ve_address:80
# iptables -t nat -A POSTROUTING -s ve_address -o eth0 -j SNAT --to ip_address

After applying this, you'll see container' web server at http://ip_address:8080/.

Yellowpin.svg Note: this rule will only work for external clients, i.e. connections originating from a different host — so you can not test if it works locally.
Yellowpin.svg Note: If you get any errors relating to:

iptables: No chain/target/match by that name double check to see if you have all the iptables/netfilter modules loaded properly. I had to modprobe xt_tcpudp before getting it to work.


The iptables utility allows to set up more complex rules for Network Address Translation, involving various protocols and ports. If you wish to get more information on this, consult the numerous Internet sites (e.g. netfilter.org) and tutorials devoted to this issue.

External Links