SoFunction
Updated on 2025-03-09

Operational plan for connecting different host networks of Docker

This method uses the docker Swarm cluster to create an overlay network to connect

background

Because Java microservice uses Nacos as configuration center, in order to solve the problem of registering the Docker container intranet IP for Nacos service, use this solution

Prerequisites

1. Ports need to be opened between hosts

Management Ports:

2377/tcp: Used to manage Swarm mode clusters. This is the primary port for communication between Swarm Manager and Worker nodes, used for cluster management and task scheduling.

Inter-node communication port:

2375/tcp: Communication for Docker API. In Swarm mode, this port is usually only enabled on the Manager node for external clients to access.
2376/tcp: Similar to 2375, but uses TLS encryption. If TLS is enabled, port 2376 will be used for secure Docker API access.

Network port:

4789/udp: For VXLAN communication, this port is used when using overlay network drivers. This is one of the main ports Swarm uses for cross-node container communication.
7946/tcp and/or 7946/udp: Used for service discovery and heartbeat messages between nodes. These two ports are used for communication in the Raft protocol to maintain consistency in cluster state.

2. Docker version

Swarm inDocker 1.12The version was previously an independent project, inDocker 1.12After the release of the version, the project was merged into Docker and became a subcommand of Docker. at present,SwarmIt is the only native support provided by the Docker community.DockerA tool for cluster management. It can put multipleDockerThe system composed of hosts is converted into a single virtualDockerHost, allowing containers to form a subnet network across hosts. therefore,dockerThe version must be larger than1.12, I used the followingdockerVersion is27.1.1

Install docker

Because the docker source is blocked, installing docker requires installation from Alibaba source. If you need a detailed installation process, please refer to the document:https:///server/

In the early stage, you only need to replace the source. I won't describe it too much here. The system is using ubuntu 22

# Install GPG certificatecurl -fsSL /docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Installation sourcesudo sh -c 'echo "deb [arch=amd64] /docker-ce/linux/ubuntu $(lsb_release -cs) stable" > /etc/apt//'
# Verify that docker is installed successfullysudo systemctl status docker
docker --version

Install Swarm cluster

Master execution

docker swarm init --advertise-addr=192.168.0.1 # Please replace the IP here, the IP is the IP of the masterSwarm initialized: current node (maw28ll7mlxuwp47z5c5vo2v1) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token xxxxxxxxxxxxxxxxxxxxxxxxxxxxx 192.168.0.1:2377  # Pay attention to saving this line of commandTo add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

Work node execution

This is the command to add a node. Use it to delete a node.docker swarm leave

docker swarm join --token xxxxxxxxxxxxxxxxxxxxxxxxxxxxx 192.168.0.1:2377

On the manager node, check the node status of the current network cluster

highlighter- apache

root@ubuntu22:~# docker node ls
ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
7r4vvml8kd2jem850rqfl158h *   ubuntu22   Ready     Active         Leader           27.1.1
lrvsq6quwaxleqejf0w1nawvu     ubuntu22   Ready     Active                          27.1.1
u4v4os8zats4ro795a4l6lw3y     ubuntu22   Ready     Active                          27.1.1
root@ubuntu22:~#

On the manager node, create an overlay network

Note that it is emphasized here to use the --attachable parameter, otherwise docker-compose cannot use this network

docker network create -d overlay  --attachable test

Check whether the creation is successful in the master node

Under normal circumstances, if the network is not activated, the node will not have a test network, and the network will appear after execution.

root@ubuntu22:~# docker network ls
NETWORK ID     NAME                   DRIVER    SCOPE
28d3903acdb2   bridge                 bridge    local
c2147e916c72   docker_gwbridge        bridge    local
7jczo6vw7mig   test                   overlay   swarm
63fa0e285c02   host                   host      local
ypqnzuafqukz   ingress                overlay   swarm
b0e97299b587   none                   null      local

Activate the network to make the nodes exist in the overlay network

Create a mirror

The busybox image may not be able to be extracted. Everyone, please find a way to solve it yourself... Thank you again, Teacher Fang~

highlighter- dockerfile

FROM busybox
MAINTAINER  lanheader@
ENTRYPOINT  ["tail","-f","/etc/hosts"]

Packing mirror

docker build -t busybox-swarm . swarm

Activate the overlay network

bash

docker service create --replicas 3 --name  busybox-net  --network  test busybox-swarm

View Services

root@ubuntu22:~# docker service  ls
ID             NAME          MODE         REPLICAS   IMAGE                  PORTS
iicn2h7rw3af   busybox-net   replicated   3/3        busybox-swarm:latest

Check the working status of node containers

highlighter- sql

# View nodesroot@ubuntu22:~# docker  service  ps busybox-net
ID             NAME                IMAGE                  NODE       DESIRED STATE   CURRENT STATE               ERROR                         PORTS
s9reawp6seu5   busybox-net.1       busybox-swarm:latest   ubuntu22   Running         Running 41 minutes ago                                    
iw3fvcy3tu14   busybox-net.2       busybox-swarm:latest   ubuntu22   Running         Running about an hour ago                                 
vn16j18a2jzd   busybox-net.3       busybox-swarm:latest   ubuntu22   Running         Running about an hour ago

test

Use docker inspect xxx to view container IP address

In docker exec -it xxx sh enter the container for testing

Add network to Docker-compose configuration file

Just use the test network in the container

networks:
  test:
    external: true

complete! ! !

This is the end of this article about connecting Docker's different host networks. For more related content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!