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. How can the Docker containers on the two hosts communicate directly through IP addresses at this time? One solution that comes to mind is to enable direct communication between two centos containers by adding routes in their respective hosts. 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, as shown in the following figure. The configurations are as follows:
After this configuration, the Docker containers on the two hosts will definitely not use the same IP address, thus avoiding IP conflicts. Next, we define two routing rules:
To summarize, the data packet transmission process between two containers is as follows:
This is what we have in mind. Let's put it into practice to see if it is feasible. Actual test • 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":"192.168.100.252/24" } Edit the /etc/docker/daemon.json file on host 2 and add the following content: "bip": "ip/netmask" { "bip":"192.168.200.252/24" } • 2. Restart the docker service Run the following command on both host 1 and host 2 to restart the docker service to make the modified docker0 network segment take effect systemctl restart docker • 3. Add routing rules Add routing rules on host 1 as follows: route add -net 192.168.200.0 netmask 255.255.255.0 gw 192.168.18.141 Add routing rules on host 2 as follows: route add -net 192.168.100.0 netmask 255.255.255.0 gw 192.168.18.162 • 4. Configure iptables rules Add the following rules on host 1: iptables -t nat -F POSTROUTING iptables -t nat -A POSTROUTING -s 192.168.100.0/24 ! -d 192.168.0.0/16 -j MASQUERADE Add the following rules on host 2: iptables -t nat -F POSTROUTING iptables -t nat -A POSTROUTING -s 192.168.200.0/24 ! -d 192.168.0.0/16 -j MASQUERADE • 5. 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 • Install ifconfig on both machines and check the container's IP address using the command: [root@695ba390d221 /]# yum search ifconfig [root@695ba390d221 /]# yum install net-tools.x86_64 Container ip address on host 1: Container ip on host 2: • 6. Direct communication between containers OK, now the two containers can ping each other. Ping on host 1: Ping on host 2: Summarize The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links You may also be interested in:
|
<<: 5 ways to determine whether an object is an empty object in JS
>>: How to create a my.ini file in the MySQL 5.7.19 installation directory
I'm currently working on my own small program...
Win10 installs mysql5.7 decompressed version, for...
<br />When uploading on some websites, a [Se...
After MySQL 5.7.18 is successfully installed, sin...
I encountered a problem today. When entering the ...
There are three pages A, B, and C. Page A contains...
Table of contents Preface 1. for loop 2. while lo...
Table of contents 1. props 2..sync 3.v-model 4.re...
Forwarding between two different servers Enable p...
Table of contents Easy to use Create a project vu...
Table of contents Preface: System Requirements: I...
Apache Tika is a library for file type detection ...
Use apk add ansible to add the ansible service to...
1. Install components yum install epel-rpm-macros...
For example, if your current path is /var/log and...