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

How to query or obtain images in a private registry

Docker queries or obtains images in a private reg...

Detailed explanation of location and rewrite usage in nginx

1. Summary of location usage Location can locate ...

Transplanting the mkfs.vfat command in busybox under Linux system

In order to extend the disk life for storing audi...

Vue3 draggable left and right panel split component implementation

Table of contents Breaking down components Left P...

mysql 5.7.17 winx64.zip installation and configuration method graphic tutorial

Preface: I reinstalled win10 and organized the fi...

Detailed use cases of MySql escape

MySQL escape Escape means the original semantics ...

Writing methods that should be prohibited in native JS

Table of contents Block-level functions Directly ...

MySQL index knowledge summary

The establishment of MySQL index is very importan...

Solutions to browser interpretation differences in size and width and height in CSS

Let’s look at an example first Copy code The code ...

Detailed explanation of log processing of Docker containers

Docker has many log plug-ins. The default is to u...

MySQL uses events to complete scheduled tasks

Events can specify the execution of SQL code once...

Using keras to judge SQL injection attacks (example explanation)

This article uses the deep learning framework ker...

JavaScript web form function communication full of practical information

1. Introduction Earlier we talked about the front...