1. Docker’s springboot+docker swam realizes load balancing

First prepare three machines, here choose 216, 248, 249

Select one of the machines as the master node and as the Manager. Select 216 here.

1. Initialize the swam cluster on 216 and execute it in the terminal

docker swarm init --advertise-addr xxx.xxx.xxx.216

After execution, the token will be returned in the terminal. The token here needs to be recorded:

docker swarm join --token SWMTKN-1-2u0oa7fphoae5rgwbws4fsd975qe xxx.xxx.xxx.216:2377

2. Execute the results returned by 216 on machines 248 and 249 respectively, and add 248 and 249 to the swam cluster respectively.

docker swarm join --token SWMTKN-1-2u0oa7fphoae5rgwbws4fsd975qe xxx.xxx.xxx.216:2377

3. Create a grid, and the docker containers started later can communicate with each other as long as they are in the grid.

docker network create -d overlay eg1

4. Since the currently used springboot is started based on docker-compose, here we write a docker-compose.xml file

version: "3.7" #Version
 
services:
  server1:
    image: xxx:0.0.1
    volumes:
      - "/xxx/spring:/home/xxx"
    restart: always
    network_mode: "eg1" #Use the grid you created
    deploy:
      mode: replicated
      replicas: 3 #swam How many instances of server1 should be started in the swam cluster? Setting 3 will start one instance on each of the three machines.
    ports:
      - "8081:8081" #Port mapping

5. Start the service by specifying docker-compose.xml through docker stack

docker stack deploy --compose-file docker-compose.yml myapp1

6. Check the service status started by docker stack

docker stack ls

You can see that a myapp1 service has been started in the swam cluster.

7. Check the running status of the three instances under the myapp1 service

docker stack ps myapp1

You can see that three instances are started on machines 248 249 216 and are running.

8. Check the IP addresses of each of the three containers and execute

docker inspect d63ad023f61b (container id)

It can be found that three different IPs are assigned to the three machines in the grid. To access the services in the container, you need to access them through the Manager.

9. Then call the springboot service on 216 multiple times, and the response can be initiated on 248 249 216. Each call will poll three machines, and the Manager selects a machine to initiate the request. At this point, docker swam realizes the load balancing of springboot.

Precautions:

1. In docker-compose.xml, the network_mode network mode cannot be set to host, and you must use the grid you created yourself.
2. Be sure to configure port mapping, otherwise the host cannot access the services in the container.
3. Do not directly use the docker compose command to start the docker container on the three machines. This will not allow you to register it into the docker swam service. You need to use docker stack to register the service in docker swam.
4. The version of docker compose must be >= 2. I have tried version 1.x but it does not support it.

Related Posts

Docker, which has 54k stars on Github, what exactly is it?

Docker learning: start the container and mount the directory

Install docker on Mac (easy installation solution)

Ethereum 2.0 client Prysm builds and runs node records

Developers, what exactly are Docker images, containers and repositories?

Mobaihe CM311-1a does not require disassembly, flash firmware and + brush armbian to install docker and run Qinglong panel

SpringBoot unit testing-JUnit5

Detailed process and problem solving of installing Docker under Windows

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*