banner
云野阁

云野阁

闲云野鹤,八方逍遥

Implementing Docker Container Communication Across Hosts

[TOC]

Solution 1: Docker Swarm Cluster#

https://blog.csdn.net/AMCUL/article/details/132913280

Solution 2: Direct Routing Method#

The implementation of the method mentioned in the deployment guide consists of three parts: fixed subnet, route persistence, and IP forwarding.

Method 1:#

Modify and Fix the Default Subnet of docker0#
  1. Modify the docker0 subnet IP by adding the following content to the host's /etc/docker/daemon.json file:
# Open daemon.json file
vi /etc/docker/daemon.json

# Content added on Host 1
# 172.16.200.1 is the docker subnet IP of Host 1
{
 "bip": "172.16.200.1/24"
}

# Content added on Host 2
# 172.16.210.1 is the docker subnet IP of Host 2
{
 "bip": "172.16.210.1/24"
}

# Restart docker service
systemctl restart docker 
  1. Add routing rules, mutually add between hosts
# Add routing rules on Host 1 (add Host 2's IP and subnet)
ip route add 172.16.210.0/24 via 172.20.1.52

# Add routing rules on Host 2 (add Host 1's IP and subnet)
ip route add 172.16.200.0/24 via 172.20.1.51

# Method 1:
# Enable IP forwarding
iptables -P FORWARD ACCEPT

# Method 2:
# Configure iptables rules (local subnet)
# Host 1
iptables -t nat -F POSTROUTING
iptables -t nat -A POSTROUTING -s 172.16.200.0/24 ! -d 172.16.0.0/16 -j MASQUERADE

# Host 2
iptables -t nat -F POSTROUTING
iptables -t nat -A POSTROUTING -s 172.16.210.0/24 ! -d 172.16.0.0/16 -j MASQUERADE
  1. Start containers and test
# Start ubuntu container on Host 1
docker run -it --name node1 ubuntu:latest /bin/bash

# Start ubuntu container on Host 2
docker run -it --name node2 ubuntu:latest /bin/bash

# Install testing tools
apt-get update
# Install ping tool
apt-get install inetutils-ping -y
# Install IP viewing tool
apt-get install net-tools -y
Route Persistence (Prevent Route Loss After Host Reboot)#

Create a file named route-enp0s3 (enp0s3 is the name of the host's network card, adjust accordingly) in the /etc/sysconfig/network-scripts/ directory.

This method is recommended; note that the name after route- should be the actual name of the host's network card.

vi /etc/sysconfig/network-scripts/route-enp0s3

# Add the following format of content to this file
# Added on Host 1
172.16.230.0/24 via 172.20.1.52
# Added on Host 2
172.16.220.0/24 via 172.20.1.51

# Restart network to verify
service network restart

# Check if the newly added route is in the routing table
ip route show | column -t
Method 2:

Add the following content to the /etc/sysconfig/static-routes file (create static-routes manually if it does not exist).

# Added on Host 1
any net 172.16.230.0/24 via 172.20.1.52
# Added on Host 2
any net 172.16.220.0/24 via 172.20.1.51

# Restart network service
systemctl restart network

# Check if the newly added route is in the routing table
ip route show | column -t
Enable IP Forwarding#
# Enable permanently
vi /etc/sysctl.conf
# Modify
net.ipv4.ip_forward=1

Method 2: (Applicable for Containers Using Fixed IP)#

Create Docker Network#
  1. Create a docker bridge type network

Create a network named test with subnet ranges 172.16.220.0/24 and 172.16.230.0/24 on both servers.

# On Host 1
docker network create test --driver bridge --ipam-driver default --subnet 172.16.220.0/24

# On Host 2
docker network create test --driver bridge --ipam-driver default --subnet 172.16.230.0/24
# View networks
docker network ls
  1. Set static routes
# Add routing rules on Host 1 (add Host 2's IP and subnet)
ip route add 172.16.230.0/24 via 172.20.1.52

# Add routing rules on Host 2 (add Host 1's IP and subnet)
ip route add 172.16.220.0/24 via 172.20.1.51

# Enable IP forwarding
iptables -P FORWARD ACCEPT
  1. Start containers and test
# Start ubuntu container on Host 1 (with the created bridge network)
docker run -it --name node1 --net=test ubuntu:latest /bin/bash

# Start ubuntu container on Host 2 (with the created bridge network)
docker run -it --name node2 --net=test ubuntu:latest /bin/bash

# Install testing tools
apt-get update
# Install ping tool
apt-get install inetutils-ping -y
# Install IP viewing tool
apt-get install net-tools -y
Route Persistence (Prevent Route Loss After Host Reboot)#

Use nmtui graphical interface to add.

# Download nmtui
yum install net-tools -y
# Display nmtui graphical interface
nmtui

Configure the IP range and IP of Host 2's docker network in Host 1's network card, and configure the IP range and IP of Host 1's docker network in Host 2's network card.

# Added on Host 1
172.16.230.0/24  172.20.1.52
# Added on Host 2
172.16.220.0/24  172.20.1.51

After configuration, you will see it in the /etc/sysconfig/network-scripts folder.

image-20240323221412716-1711203257505-1

image-20240323221442754-1711203286475-3

Start the newly created network card on the corresponding host (it will be invalid after network restart or system reboot).

# On Host 1
nmcli c up br-aed92de88760
# On Host 2
nmcli c up br-0ab3914edle2

To keep the new network card enabled, change ONBOOT=no to ONBOOT=yes in the corresponding newly created network card file.

# On Host 1
vi ifcfg-br-aed92de88760
# Change ONBOOT=no to ONBOOT=yes
ONBOOT=yes
# On Host 2
vi ifcfg-br-0ab3914edle2
# Change ONBOOT=no to ONBOOT=yes
ONBOOT=yes

# Restart network service
systemctl restart network

image-20240323221537519-1711203339288-5

Enable IP Forwarding#
# Enable permanently
vi /etc/sysctl.conf
# Modify
net.ipv4.ip_forward=1

At this point, the containers are not affected by network restarts and can achieve cross-host communication.

However, after a system reboot, the created docker networks and containers will be invalid.

You need to recreate the docker networks and containers, after which the docker networks and containers will not be affected by system reboots and network restarts, successfully achieving cross-host communication for docker containers.

image-20240323221547069-1711203348713-7

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.