Update - 1st March 2018
I just want to warn others that this post is quite old and I think there are probably much better ways of handling this now. A whole field has risen up around docker networking and tools are out there to make this much easier, such as docker swarm, Rancher and Kubernetes. I would recommend studying those areas instead. One of the easiest ways to get kubernetes up and running when you self-host may actually be through Rancher.
The default way of creating docker containers is to use a bridge with a host-only subnet provided by the docker0 or lxcbr0 bridges. However, this makes it incredibly difficult or impossible for containers on different hosts to communicate. This tutorial will show you how to deploy containers onto the same subnet as the host with DHCP or static IPs, so that you can deploy containers to any node, yet still have them communicate with each other.
DOCKER_OPTS="-e lxc --dns 8.8.8.8"
# The primary network interface auto eth0 iface eth0 inet dhcp auto br0 iface br0 inet dhcp bridge_ports eth0 bridge_stp off bridge_fd 0 bridge_maxwait 0
# bring up the bridge we just created sudo ifup br0 # set up routing (aws specific) sudo iptables -t nat -F sudo iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT sudo iptables --append FORWARD --in-interface br0 -j ACCEPT sudo iptables -t nat -A POSTROUTING -d 172.31.0.0/16 -j ACCEPT sudo iptables -t nat -A POSTROUTING -d 0.0.0.0/0 -j SNAT --to-source $HOST_PRIVATE_IP
If you one of your containers is acting as a reverse proxy, you will want to run append these commands as well.
echo 1 > /proc/sys/net/ipv4/ip_forward SEARCH="#net.ipv4.ip_forward=1" REPLACE="net.ipv4.ip_forward=1" FILEPATH="/etc/sysctl.conf" sed -i "s;$SEARCH;$REPLACE;" $FILEPATH sudo sysctl -p
sudo apt-get install dnsmasq -y sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.bak sudo vim /etc/dnsmasq.confReplace the contents of
interface=br0 dhcp-range=$STARTING_PRIVATE_IP,$ENDING_PRIVATE_IP,12h dhcp-option=3,$PRIVATE_IP_OF_HOST
No DHCP Configuration
If you don't want to use DHCP, then you simply need to start your containers similarly to below (but you will need to keep track of the IPs of every container)
docker run \ --net="none" \ --lxc-conf="lxc.network.type = veth" \ --lxc-conf="lxc.network.ipv4 = $IP_OF_CONTAINER/$CIDR" \ --lxc-conf="lxc.network.ipv4.gateway = $HOST_PRIVATE_IP" \ --lxc-conf="lxc.network.link = wan" \ --lxc-conf="lxc.network.name = eth123" \ --lxc-conf="lxc.network.flags = up" \ -d $IMAGE_ID
sudo service dnsmasq restart
RUN mv /sbin/dhclient /usr/sbin/dhclient
docker run \ -d \ --privileged \ --net="none" \ --lxc-conf="lxc.network.type = veth" \ --lxc-conf="lxc.network.link = br0" \ --lxc-conf="lxc.network.flags = up" \ $IMAGE
At the exact moment when I'm trying to reboot after making changes to interfaces, I lose connectivity. Any ideas? Please help :)
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteI could not see how the comment was related to this post so it was removed. It looked like it was just there to link to other content. However I am more than happy for people to link to other related content if it is relevant to the content of the post.
Delete