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 the installation and use of Vue-Router
>>: How Database SQL SELECT Queries Work
The performance of your website or service depend...
Docker queries or obtains images in a private reg...
1. Summary of location usage Location can locate ...
In order to extend the disk life for storing audi...
Table of contents Breaking down components Left P...
Preface: I reinstalled win10 and organized the fi...
MySQL escape Escape means the original semantics ...
Table of contents Block-level functions Directly ...
The establishment of MySQL index is very importan...
Let’s look at an example first Copy code The code ...
Docker has many log plug-ins. The default is to u...
Events can specify the execution of SQL code once...
Copy code The code is as follows: <html> &l...
This article uses the deep learning framework ker...
1. Introduction Earlier we talked about the front...