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

From OpenVZ Virtuozzo Containers Wiki
Jump to: navigation, search
 
(55 intermediate revisions by 22 users not shown)
Line 1: Line 1:
== Using NAT for providing access to/from VPS with private IPs ==
+
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 ==
  
== VPS to Internet ==
+
Make sure that below prerequisites are met, otherwise it won't work for you!
  
To enable the VPSs, 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:
+
=== IP conntracks ===
 +
 
 +
'''IP connection tracking should be enabled for CT0'''. For recent OpenVZ kernels (2.6.9 and later) connection tracking for CT0 is enabled by default, but it can be disabled by vzctl 4.7 and newer (because it has a negative impact on venet performance, see {{Bug|2755}}). So, make sure there is '''NO''' line like
 +
 
 +
options ip_conntrack ip_conntrack_disable_ve0=1
 +
or
 +
options nf_conntrack ip_conntrack_disable_ve0=1
 +
 
 +
in <code>/etc/modules.conf</code>, <code>/etc/modprobe.conf</code>, or any file under <code>/etc/modprobe.d/</code> (such as <code>/etc/modprobe.d/vz.conf</code>). '''If there is such a line, please'''
 +
#change <code>=1</code> to <code>=0</code>
 +
#reboot the node.
 +
 
 +
=== 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 :
 +
 
 +
[https://bugs.launchpad.net/ubuntu/+source/procps/+bug/84537 Launchpad]
 +
 
 +
The syntax of /etc/sysctl.conf has changed to:
 +
 
 +
<pre>net.ipv4.conf.default.forwarding=1
 +
net.ipv4.conf.all.forwarding=1</pre>
 +
 
 +
===Enable iptables in OpenVZ 7/Virtuozzo 7===
 +
 
 +
If you use OpenVZ 7/Virtuozzo 7 and want to manage iptables through iptables-services you must disable firewalld and enable iptables:
 +
 
 +
# systemctl stop firewalld
 +
# systemctl mask firewalld
 +
# yum install iptables-services
 +
# systemctl enable iptables
 +
 
 +
== How to create the container and attach network properties to it ==
 +
 
 +
Create the container:
 +
 
 +
# prlctl create 100700 --vmtype ct
 +
 
 +
Attach the internal IP address and DNS server:
 +
 
 +
# prlctl set 100700 --ipadd 192.168.0.101/24
 +
# prlctl set 100700 --nameserver 8.8.8.8
 +
 
 +
Start the container:
 +
 
 +
# prlctl start 100700
 +
 
 +
== How to provide access for container to the Internet ==
 +
 
 +
To enable the [[container]]s, 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 <tt>iptables</tt> utility.  
 +
 
 +
To perform a simple SNAT setup, execute the following command on the [[Hardware Node]]:
 
<pre>
 
<pre>
 
# iptables -t nat -A POSTROUTING -s src_net -o eth0 -j SNAT --to ip_address
 
# iptables -t nat -A POSTROUTING -s src_net -o eth0 -j SNAT --to ip_address
 
</pre>
 
</pre>
  
where src_net is a range of IP addresses of VPSs to be translated by SNAT, and ip_address is the external IP address of your Hardware Node. 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.
+
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:
 
 
Note: If you are using stable (2.6.8-based) kernel, then to enable SNAT for the VPSs on your local network you should also make sure that the following string is present in the /etc/modules.conf file:
 
 
<pre>
 
<pre>
options ip_conntrack ip_conntrack_enable_ve0=1
+
# iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o eth0 -j SNAT --to ip_address
 
</pre>
 
</pre>
  
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 VE0 is enabled by default in those kernels.
+
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 [[Hardware Node|Node]], you may need to specify a different interface for outgoing connections, e.g. <tt>-o eth2</tt>.
  
To make all IP addresses to be translated by SNAT (not only the ones of VPSs with private addresses), you should type the following string:
+
To make all IP addresses to be translated by SNAT (not only the ones of [[container]]s with private addresses), you should type the following string:
 
<pre>
 
<pre>
 
# iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to ip_address
 
# iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to ip_address
 
</pre>
 
</pre>
  
== Internet to VPS ==
+
Or you can just use:
 +
 
 +
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  
In addition, to make some services in VPS with internal 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:
+
=== Save new iptables rules ===
 +
 
 +
Do not forget to save your new iptables rules
 +
 
 +
# service iptables save
 +
# service iptables restart
 +
 
 +
=== Firewall ===
 +
 
 +
For Debian hardware node, you may need to allow a forward rule. The table still being the default table (filter) but the chain is FORWARD:
 +
 
 +
# iptables -A FORWARD -s 192.168.2.0/24 -j ACCEPT
 +
# iptables -A FORWARD -d 192.168.2.0/24 -j ACCEPT
 +
 
 +
For default RedHat/CentOS firewall, allow outgoing connections from your containers, for example:
 +
 
 +
# iptables -A RH-Firewall-1-INPUT -s 192.168.2.0/24 -j ACCEPT
 +
 
 +
=== Test ===
 +
 
 +
Now you should be able to reach internet from your container:
 +
 
 +
# prlctl enter 100700
 +
# ping openvz.org
 +
 
 +
== 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]]:
 
<pre>
 
<pre>
# iptables -t nat -A PREROUTING -p tcp -d ip_address --port port_num -i eth0 -j DNAT --to-destination vps_address:dst_port_num  
+
# iptables -t nat -A PREROUTING -p tcp -d ip_address --dport port_num \
 +
  -i eth0 -j DNAT --to-destination ve_address:dst_port_num  
 
</pre>
 
</pre>
  
where vps_address is an IP address of VPS, dst_port_num is a tcp port, which required service use, ip_address is the external 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 VPS service. Note that this setup makes the service, which use port_num on the Hardware Node, be unaccessible from the Internet. Also note that SNAT translation is required too.
+
where <tt>ve_address</tt> is an IP address of the container, <tt>dst_port_num</tt> is a tcp port which requires service use, <tt>ip_address</tt> is the external (public) IP address of your [[Hardware Node]], and <tt>port_num</tt> 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 <tt>port_num</tt> 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 VPS to be accessible from outside, and, at the same time, keep a web server on the Hardware Node be accessible, use the following config:
+
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:
 
<pre>
 
<pre>
# iptables -t nat -A PREROUTING -p tcp -d ip_address -p 8080 -i eth0 -j DNAT --to-destination vps_address:80
+
# iptables -t nat -A PREROUTING -p tcp -d ip_address --dport 8080 \
# iptables -t nat -A POSTROUTING -s vps_address -o eth0 -j SNAT --to ip_address
+
  -i eth0 -j DNAT --to-destination ve_address:80
 +
# iptables -t nat -A POSTROUTING -s ve_address -o eth0 -j SNAT --to ip_address
 
</pre>
 
</pre>
  
After applying this, you'll see VPS' web server at http://ip_address:8080/
+
Is need add GATEWAY in /etc/sysconfig/network-script/interface (IP of host that provide access internet).
 +
 
 +
After applying this, you'll see container' web server at <code><nowiki>http://ip_address:8080/</nowiki></code>.
 +
 
 +
{{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.}}
 +
 
 +
{{Note|If you get any errors relating to:
 +
<code>iptables: No chain/target/match by that name</code>
 +
double check to see if you have all the iptables/netfilter modules loaded properly. I had to <code> modprobe xt_tcpudp </code> before getting it to work.}}
 +
 
 +
 
 +
The <tt>iptables</tt> 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. [http://www.netfilter.org netfilter.org]) and tutorials devoted to this issue.
  
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. [http://www.netfilter.org www.netfilter.org]) and tutorials devoted to this issue.
+
== External Links ==
 +
* [http://www.netfilter.org netfilter.org]
 +
* [[w:Private network]]
  
 
[[Category: HOWTO]]
 
[[Category: HOWTO]]
 
[[Category: Networking]]
 
[[Category: Networking]]

Latest revision as of 09:52, 17 August 2016

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[edit]

Make sure that below prerequisites are met, otherwise it won't work for you!

IP conntracks[edit]

IP connection tracking should be enabled for CT0. For recent OpenVZ kernels (2.6.9 and later) connection tracking for CT0 is enabled by default, but it can be disabled by vzctl 4.7 and newer (because it has a negative impact on venet performance, see OpenVZ Bug #2755). So, make sure there is NO line like

options ip_conntrack ip_conntrack_disable_ve0=1

or

options nf_conntrack ip_conntrack_disable_ve0=1

in /etc/modules.conf, /etc/modprobe.conf, or any file under /etc/modprobe.d/ (such as /etc/modprobe.d/vz.conf). If there is such a line, please

  1. change =1 to =0
  2. reboot the node.

IP forwarding[edit]

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

Enable iptables in OpenVZ 7/Virtuozzo 7[edit]

If you use OpenVZ 7/Virtuozzo 7 and want to manage iptables through iptables-services you must disable firewalld and enable iptables:

# systemctl stop firewalld
# systemctl mask firewalld
# yum install iptables-services
# systemctl enable iptables 

How to create the container and attach network properties to it[edit]

Create the container:

# prlctl create 100700 --vmtype ct

Attach the internal IP address and DNS server:

# prlctl set 100700 --ipadd 192.168.0.101/24
# prlctl set 100700 --nameserver 8.8.8.8

Start the container:

# prlctl start 100700

How to provide access for container to the Internet[edit]

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:

# iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -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

Or you can just use:

# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Save new iptables rules[edit]

Do not forget to save your new iptables rules

# service iptables save
# service iptables restart

Firewall[edit]

For Debian hardware node, you may need to allow a forward rule. The table still being the default table (filter) but the chain is FORWARD:

# iptables -A FORWARD -s 192.168.2.0/24 -j ACCEPT
# iptables -A FORWARD -d 192.168.2.0/24 -j ACCEPT

For default RedHat/CentOS firewall, allow outgoing connections from your containers, for example:

# iptables -A RH-Firewall-1-INPUT -s 192.168.2.0/24 -j ACCEPT

Test[edit]

Now you should be able to reach internet from your container:

# prlctl enter 100700 
# ping openvz.org

How to provide access from Internet to a container[edit]

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

Is need add GATEWAY in /etc/sysconfig/network-script/interface (IP of host that provide access internet).

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[edit]