Changes

Jump to: navigation, search

Using NAT for container with private IPs

818 bytes removed, 09:52, 17 August 2016
no edit summary
== Prerequisites ==
 
Make sure that below prerequisites are met, otherwise it won't work for you!
 
=== 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
[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>
=== IP conntracks Enable iptables in OpenVZ 7/Virtuozzo 7===IP connection tracking should be enabled for CT0.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:
'''For OpenVZ kernels 2.6.8''', put the following line into /etc/modprobe.conf: # prlctl create 100700 --vmtype ct
modprobe ip_conntrack ip_conntrack_enable_ve0=1Attach the internal IP address and DNS server:
and reboot # prlctl set 100700 --ipadd 192.168.0.101/24 # prlctl set 100700 --nameserver 8.8.8.8
'''For OpenVZ kernels later than 2.6.8''', connection tracking for CT0 is enabled by default. '''However''', make sure there is '''no''' line likeStart the container:
options ip_conntrack ip_conntrack_disable_ve0=1# prlctl start 100700
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 the Internet ==
== How To enable the [[container]]s, which have only internal IP addresses, to provide access for container to 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 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>
# iptables -t nat -A POSTROUTING -s src_net -o eth0 -j SNAT --to ip_address
</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 ([[w:CIDR|CIDR notation]]). For example to specify IP addresses from 192.168.2.1 through 192.168.2.127 use:
<pre>
# iptables -t nat -A POSTROUTING -s 192.168.2.0/25 24 -o eth0 -j SNAT --to ip_address
</pre>
</pre>
{{Note|For a Debian O.S v4 or v5 Hardware Node, Or you may need to allow a forward rule. The table still being the default table (filter) but the chain is FORWARD can just use<pre># /sbin/iptables -A FORWARD -s 172.16.150.0/24 -j ACCEPT# /sbin/iptables -A FORWARD -d 172.16.150.0/24 -j ACCEPT</pre>}}
# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
If you are using an === Save new 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># /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</pre>rules ===
Do not forget to save your new iptables rules
{{Note|If the above is not working then check if one of the following solutions does the trick.}} # service iptables save1. 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 <tt>/etc/modprobe.conf</tt> file:<pre>options ip_conntrack ip_conntrack_enable_ve0=1</pre> # service iptables restart
{{Note|in kernels later than 2.6.8, connection tracking is enabled by default}}=== Firewall ===
In case it is notFor Debian hardware node, add this string you may need to allow a forward rule. The table still being the file by means of any text editor default table (for example, vifilter). This setting is not needed for kernels more recent than 2.6.8, since connection tracking for [[CT0]] but the chain is enabled by default in those kernels.FORWARD:
2 # iptables -A FORWARD -s 192. For unknown reasons the above didn't work on a Debian host168. The solution is to do it in an init2.d script as follows:<pre>modprobe ip_conntrack ip_conntrack_enable_ve0=1<0/pre>24 -j ACCEPTMake 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 computerA FORWARD -d 192.168.2.0/24 -j ACCEPT
{{Note|in kernels later than 2.6.8For default RedHat/CentOS firewall, connection tracking is enabled by default}}allow outgoing connections from your containers, for example:
{{Note| you may need to modify the /etc/vz/vz # iptables -A RH-Firewall-1-INPUT -s 192.168.2.conf file to include:}} <pre>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"<0/pre>24 -j ACCEPT
Don't forget to flush [[Hardware Node]] iptables to apply changes:=== Test ===
<pre>iptables -F</pre>Now you should be able to reach internet from your container:
Now you should be able to reach internet from yours VE: # prlctl enter 100700  <pre>vzctl exec <VE ID> # ping www.openvz.org</pre>
== How to provide access from Internet to a container ==
# iptables -t nat -A POSTROUTING -s ve_address -o eth0 -j SNAT --to ip_address
</pre>
 
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>.
Anonymous user

Navigation menu