Location>code7788 >text

Docker's different hosts network connectivity

Popularity:588 ℃/2024-08-19 17:09:48

This approach uses docker Swarm clustering to create an overlay network to bridge the

contexts

Because java microservices use nacos as a configuration center, in order to solve the problem of Nacos service registration using Docker container intranet ip, use this solution

pre-conditions

1. Ports need to be opened between hosts

Management Ports.

2377/tcp: Used for managing Swarm mode clusters. This is the main port for communication between the Swarm Manager and Worker nodes for cluster management and task scheduling.

Inter-node communication ports.

2375/tcp: Used for Docker API communication. In Swarm mode, this port is usually only opened 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 Ports.

4789/udp: Used for VXLAN communication, this port is used when using the overlay network driver. This is one of the main ports used by Swarm 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 in order to maintain the consistency of the cluster state.

2. docker version

Swarm inDocker 1.12 version was previously part of a separate project in theDocker 1.12 After the release, the project was merged into Docker and became a subcommand of Docker. Currently.Swarm is the only native support provided by the Docker communityDocker tool for cluster management. It can combine multipleDocker A system consisting of hosts is converted to a single virtualDocker hosts, allowing containers to form subnet networks across hosts. As a result, thedockerThe version must be greater than1.12The following are the ones I usedockerVersions for27.1.1

Installing docker

Because docker source is blocked after the installation of docker need to be installed from the Ali source, if you need detailed installation process, please refer to the documentation:/lanheader/p/

The front only need to replace the source can be, here do not do too much description, the system I use is ubuntu 22

# mountingGPGcertificates
curl -fsSL /docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# mounting源
sudo sh -c 'echo "deb [arch=amd64] /docker-ce/linux/ubuntu $(lsb_release -cs) stable" > /etc/apt//'
# 验证是否成功mounting了docker
sudo systemctl status docker
docker --version

Installing a Swarm Cluster

master execution

docker swarm init --advertise-addr=192.168.0.1 # Note that replacing theIP,IPbecause ofmaster(used form a nominal expression)ip
Swarm 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 # Take care to save this line of command
 
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

The work node executes

This is the command for joining nodes, deleting nodes is done with thedocker swarm leave

docker swarm join --token xxxxxxxxxxxxxxxxxxxxxxxxxxxxx 192.168.0.1:2377

At the manager node, view the nodes of the current network cluster

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:~# 

At the manager node, create an overlay network

Note that the use of the --attachable parameter is emphasized here, otherwise docker-compose will not be able to use this network

docker network create -d overlay  --attachable test

Check for successful creation on the master node

Normally, if the network is not activated, the node will not have a test network and will need to be executed before the network will appear

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 so that nodes exist overlay network

Creating a mirror

busybox mirror may not be pulling up, you guys figure it out... busyboxThank you again, Mr. Fang.~

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

Packaging Mirror

docker build -t busybox-swarm . swarm 

Activate overlay network

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   

Viewing the Node Container Work Status

# View Node
root@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

beta (software)

Use docker inspect xxx to view the container IP address

Enter the container for testing in docker exec -it xxx sh

Adding a network to the Docker-compose configuration file

Just use the test network for execution in the container

networks:
  test:
    external: true

Over!!!