Detailed explanation of direct routing in cross-host communication of Docker containers

Detailed explanation of direct routing in cross-host communication of Docker containers

Overview

As for the current default network of Docker itself, different Docker containers on a single host can communicate directly with the help of the docker0 bridge, which is fine. However, Docker containers on different hosts can only communicate by mapping ports on the host. Sometimes this method is inconvenient and may not even meet our requirements. Therefore, it is necessary for Docker containers on different physical machines to communicate directly using their own IP addresses. Furthermore, if we start Docker containers on different physical hosts, we will inevitably encounter cross-host communication problems of Docker containers. Let’s try it in this article.

Analysis of the scheme principle

Since the container's IP is used for routing, it is necessary to avoid containers on different hosts using the same IP. To this end, we should assign different subnets to different hosts to ensure this. So we construct a routing solution for communication between two containers.

Environment Introduction:

The IP address of host 1 is: 192.168.145.128
The IP address of host 2 is: 192.168.145.129
Subnet assigned to Docker container on Host 1: 172.17.1.0/24
Subnet assigned to Docker containers on Host 2: 172.17.2.0/24
After this configuration, the Docker containers on the two hosts will definitely not use the same IP address, thus avoiding IP conflicts.

To summarize, the data packet transmission process between two containers is as follows:
The data packet sent from container1 to container2 is first sent to the "gateway" docker0 of container1, and then by looking up the route of host1, it is found that the data packet needs to be sent to host2. After the data packet arrives at host2, it is forwarded to docker0 of host2, and finally it is transferred to container2 by docker0. The reverse principle is the same and will not be repeated here.

1. Configure docker0 on host 1 and host 2 respectively

Edit the /etc/docker/daemon.json file on host 1 and add the following content: "bip" : "ip/netmask"
{ "bip", "172.17.1.252/24" }

Edit the /etc/docker/daemon.json file on host 2 and add the following content: "bip" : "ip/netmask"
{ "bip", "172.17.2.252/24" }

Restart the docker service. Execute the following command on both host 1 and host 2 to restart the docker service so that the modified docker0 network segment will take effect. systemctl restart docker

2. Add routing rules

Add routing rules on host 1 as follows:
route add -net 172.17.2.0 netmask 255.255.255.0 gw 192.168.145.129

Add routing rules on host 2 as follows:
route add -net 172.17.1.0 netmask 255.255.255.0 gw 192.168.145.128

3. Configure iptables rules

Add the following rules on host 1:
iptables -t nat -F POSTROUTING
iptables -t nat -A POSTROUTING -s 172.17.1.0/24 ! -d 172.17.0.0/16 -j MASQUERADE

Add the following rules on host 2:
iptables -t nat -F POSTROUTING
iptables -t nat -A POSTROUTING -s 172.17.2.0/24 ! -d 172.17.0.0/16 -j MASQUERADE

4. Start the container

Start the centos container on host 1:
docker run -it --name container1 centos /bin/bash

Start the centos container on host 2:
docker run -it --name container2 centos /bin/bash

OK, now the two containers can ping each other.

5. Route persistence (to prevent route loss when host restarts)

root@rancher:~# vi /etc/rc.local

Add routing information, remember to write it before exit! ! ! :
route add -net 172.17.2.0 netmask 255.255.255.0 gw 192.168.102.88

The above is all the knowledge about cross-host communication of Docker containers. Thank you for your learning and support for 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of how Docker containers communicate across hosts
  • Detailed explanation of overlay network in Docker
  • Implementation of Docker cross-host network (overlay)
  • Docker container cross-host communication--overlay network

<<:  Detailed explanation of the installation and use of Vue-Router

>>:  How Database SQL SELECT Queries Work

Recommend

MySQL learning to create and operate databases and table DDL for beginners

Table of contents 1. Operate the database 1.1 Cre...

Repair solution for inconsistent MySQL GTID master and slave

Table of contents Solution 1: Rebuild Replicas Pr...

Vue Router loads different components according to background data

Table of contents Requirements encountered in act...

Building .NET Core 2.0 + Nginx + Supervisor environment under Centos7 system

1. Introduction to Linux .NET Core Microsoft has ...

How does MySQL achieve multi-version concurrency?

Table of contents MySQL multi-version concurrency...

MySQL character set garbled characters and solutions

Preface A character set is a set of symbols and e...

A brief discussion on the issue of element dragging and sorting in table

Recently, when using element table, I often encou...

MySQL decimal unsigned update negative numbers converted to 0

Today, when verifying the concurrency problem of ...

jQuery implements navigation bar effect with expansion animation

I designed and customized a navigation bar with a...

How to obtain a permanent free SSL certificate from Let's Encrypt in Docker

1. Cause The official cerbot is too annoying. It ...

Develop a vue component that encapsulates iframe

Table of contents 1. Component Introduction 2. Co...