The premise to prepare

Vm change1:192.168.1.20

Vm change2:192.168.1.21

Docker is installed on both VMS

Set up the cluster

Vm change2 Run the following command:

docker swarm init

To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-0p89ldytfq18roe0snxunylwq0q95um36qpnf896ogqil7uci5-0bw2g0fu2ug0ehf8vojo9s20t 192.1681.21.:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

Copy the code

Vm change1:192.168.1.20 Joins the cluster

docker swarm join --token SWMTKN-1-0p89ldytfq18roe0snxunylwq0q95um36qpnf896ogqil7uci5-0bw2g0fu2ug0ehf8vojo9s20t 192.168.1.21:2377

Creating a Cluster Network

Create a network only on the Change2 management node

Docker network create -d overlay --subnet=10.10.0.0/16 --gateway=10.10.0.1 --attachable=true my-network-overlay

View the network you just created

docker network ls

NETWORK ID     NAME                 DRIVER    SCOPE
1191d51d217f   bridge               bridge    local
6953e56e8ad1   docker_gwbridge      bridge    local
2fa9d09a1a36   host                 host      local
uiepae7dknqg   ingress              overlay   swarm
x0tjx8cjamm8   my-network-overlay   overlay   swarm
f5b89a7f54a8   none                 null      local

Copy the code

The ingress overlay network is used to transmit control or data messages from the cluster service. If the swarm service is created without specifying a user – defined overlay network, it will be added to the default Ingress network

Docker_gwbridge is a virtual bridge that connects the physical network and overlay network of docker processes. Docker_gwbridge is a virtual bridge that connects the physical network and overlay network of Docker processes. It exists in the kernel of docker. If we want to configure the network, we first need to ensure that the host is not in the cluster, and then proceed with the following steps:

  • Exiting the Cluster Service

  • Delete the docker_gwbridge network

  • Restart the Docker service

  • Create docker_gwbridge network, docker_gwbridge scope is local, so all cluster nodes need to regenerate

    Docker network create — subnet configures 172.21.0.0/20 – gateway 172.21.0.1 — opt com.docker.net work. Bridge. The name = docker_gwbridge –opt com.docker.network.bridge.enable_icc=false –opt com.docker.network.bridge.enable_ip_masquerade=true docker_gwbridge

If you want to connect to the my-network-Overlay network, make sure that the following ports are not served before the connection and that the server firewall allows the following ports to pass through:

  • TCP port 2377 is used to exchange cluster management information
  • TCP and UDP port 7946 is used to communicate with nodes in the cluster
  • UDP port 4789 is used for sending and receiving datagrams in overlay network

The network cannot be queried on the Work node. You need to upgrade the Work node to the management node

docker node promote change1

Network diagram across host containers

Overlay network in the figure is the My-network-overlay we created

The Physical network is the docker_gwbridge we created

So we’re almost done here

Test container communication

We run the Java service container in Change2

docker run -p 80:80 --env datasource.url=mysql --name myblog --restart always --network my-network-overlay myblog:latest

Run the mysql database container in Change1

docker run --name mysql --restart=always --network my-network-overlay -e MYSQL_ROOT_PASSWORD=123456 -d -p 3306:3306 mysql:latest

Then enter the test address http://192.168.1.21/ in your browser