|
| 1 | +A demo of docker networking between containers with ability to isolate |
| 2 | +communication between the nodes while not granting excessive priviliges. |
| 3 | + |
| 4 | +Uses user defined locally scoped bridge network. |
| 5 | + |
| 6 | +## Run the demo |
| 7 | + |
| 8 | +Run with |
| 9 | + |
| 10 | + docker-compose up |
| 11 | + |
| 12 | +You'll see chatter between the nodes on stderr. Now you can mess with their |
| 13 | +networking externally using the methods below. |
| 14 | + |
| 15 | +## Detach and attach containers |
| 16 | + |
| 17 | +To break connectivity: |
| 18 | + |
| 19 | + docker disconnect unstable network_partition_client_1 |
| 20 | + |
| 21 | +to resume connectivity |
| 22 | + |
| 23 | + docker connect unstable network_partition_client_1 |
| 24 | + |
| 25 | +This will make the containers nonroutable. If there are existing TCP |
| 26 | +connections they won't break straight away, they'll keep retrying until their |
| 27 | +timeouts are hit, and they'll buffer sent data for retry. |
| 28 | + |
| 29 | +## Messing with connectivity using iptables |
| 30 | + |
| 31 | +You can use iptables within each container. Because each container has its own |
| 32 | +network namespace, this won't mess up the host's iptables rules. |
| 33 | + |
| 34 | +Within either container, it's possible to do things like: |
| 35 | + |
| 36 | + # blackhole from other container |
| 37 | + iptables -A INPUT -s client -j DROP |
| 38 | + |
| 39 | + # delete blackhole |
| 40 | + iptables -D INPUT 1 |
| 41 | + |
| 42 | + # send TCP RST for other container |
| 43 | + iptables -A INPUT -s client -j DROP |
| 44 | + |
| 45 | + # restore connectivity |
| 46 | + iptables -D INPUT 1 |
| 47 | + |
| 48 | +You can run this with `docker exec` e.g. |
| 49 | + |
| 50 | + docker exec network_partition_client_1 iptables -A INPUT -s server -j DROP |
| 51 | + |
| 52 | +## Docker-compose not required |
| 53 | + |
| 54 | +Example uses docker compose, but it's simple to do with plain docker using the |
| 55 | +`docker network create` command and the `--network` and `--cap-add NET_ADMIN` |
| 56 | +flags to `docker run`. |
| 57 | + |
| 58 | +e.g. |
| 59 | + |
| 60 | + docker build -t net-demo . |
| 61 | + |
| 62 | + docker network create unstable |
| 63 | + |
| 64 | + docker run \ |
| 65 | + --detach -n network_partition_client_1 \ |
| 66 | + --entrypoint /usr/local/bin/client \ |
| 67 | + --network unstable \ |
| 68 | + net-demo |
| 69 | + |
| 70 | + docker run \ |
| 71 | + --detach -n network_partition_server_1 \ |
| 72 | + --entrypoint /usr/local/bin/server \ |
| 73 | + --network unstable \ |
| 74 | + net-demo |
| 75 | + |
| 76 | +## Note on docker and network namespaces |
| 77 | + |
| 78 | +Docker uses network namespaces, but `ip netns` won't see them. Docker tracks it |
| 79 | +in `/var/run/docker/netns` but `ip netns` tracks them in `/var/run/netns`. |
| 80 | + |
| 81 | +You can hack around this with |
| 82 | + |
| 83 | + sudo rmdir /var/run/netns |
| 84 | + sudo ln -s /var/run/docker/netns /var/run/netns |
| 85 | + |
0 commit comments