I have two nodes in a Docker Swarm cluster. One of those nodes has an OpenVPN client connection to a VPN provider on interface tun0
. My goals are,
- Any services assigned to this node exclusively use the VPN connection
- No leaks (i.e., DNS or other traffic)
- If the VPN disconnects, all traffic gets dropped
- Allow service discovery and connections to other containers in the Swarm
For DNS, I have added a dns
entry to /etc/docker/daemon.json
that uses the VPN provider's DNS servers that are only accessible through the VPN.
Here are the iptable rules I have come up with:
iptables -I DOCKER-USER 1 -o tun0 -j ACCEPT
iptables -I DOCKER-USER 2 -i tun0 -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
iptables -I DOCKER-USER 3 -j DROP
The resulting DOCKER-USER
chain looks like this:
Chain DOCKER-USER (1 references)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- * tun0 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- tun0 * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0
From basic tests like running nslookup
and curl
with the VPN connection on and off these rules seem to work, but I have very little experience with iptables. Is this the correct way of doing this?