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

Detailed explanation of MySQL database (based on Ubuntu 14.0.4 LTS 64 bit)

1. Composition and related concepts of MySQL data...

VMWare virtual machine 15.X LAN network configuration tutorial diagram

Recently, I have been working on several virtual ...

What are the drawbacks of deploying the database in a Docker container?

Preface Docker has been very popular in the past ...

How to use Vue3 asynchronous data loading component suspense

Table of contents Preface Creating Components Sum...

A brief analysis of mysql index

A database index is a data structure whose purpos...

JavaScript css3 to implement simple video barrage function

This article attempts to write a demo to simulate...

Vue project implements graphic verification code

This article example shares the specific code of ...

Detailed explanation of the use of filter properties in CSS3

Recently, when I was modifying the intranet porta...

How to use CSS to write different styles according to sub-elements

The effect we need to achieve: What is needed The...

Setting the engine MyISAM/InnoDB when creating a data table in MySQL

When I configured mysql, I set the default storag...

How to solve the problem that Seata cannot use MySQL 8 version

Possible reasons: The main reason why Seata does ...

Summary of 4 ways to add users to groups in Linux

Preface Linux groups are organizational units use...

Detailed steps for setting up a nexus server

1. The significance of building nexus service As ...

MySQL compression usage scenarios and solutions

Introduction Describes the use cases and solution...

How to quickly modify the root password under CentOS8

Start the centos8 virtual machine and press the u...