Docker With Spring Boot and MySQL: Docker Swarm Part 3
Docker With Spring Boot and MySQL: Docker Swarm Part 3
In this article, we look at how to using Docker Swarm with Spring Boot and MySQL. We then dive into relationships between Worker and Manager nodes.
Join the DZone community and get the full member experience.Join For Free
In my previous two articles, I wrote about docker basic and docker-compose where we deployed real applications to a Docker container with Docker CLI and docker-compose. In the first article, we used Dockerfile to set up our environment and deployed the application by running containers separately and then built a link between each of them.
In the second article, we ran multiple containers with the docker-compose tool. But for scaling and high-availability, we need to run our application on multiple host machines with cluster facilities. For orchestrating distributed systems at any scale to gain scalability and high availability, Docker has its own implementation called Swarm. Swarm uses the SwarmKit library for orchestration.
Overview of SwarmKit
SwarmKit is a node discovery, raft-based consensus, task scheduling, primitive-based orchestrating toolkit that can scale a distributed system. This toolkit uses Raft consensus algorithm to coordinate and decision making of a distributed system. SwarmKit is responsible for Orchestration, Scheduling and Cluster Management.
We have already learned some important terms about Docker, containers, images, etc. Now, we will try to deploy an application with scalability. In Docker terms, Swarm orchestrates a large number of hosts and maintains scalability and high availability.
Some key terms of Docker Swarm are listed below:
Node: In orchestration terms, a node is a host machine. A node can be multiple VMs in a single host machine.
Manager Node: This node is responsible for maintaining Swarm orchestration. It manages the cluster environment.
Worker Node: This node is responsible for executing tasks defined by the Manager Node. It will always notify its state to the Manager Node and provide services that is assigned to it.
Service: These are the tasks that execute on the Manager or Worker Node.
You may also like: Monitoring Docker Swarm.
Swarm Node Setup and Join With Example
We discussed Docker CLI and Docker-Compose in my previous two articles with real projects. Now, we will practice deploying an application in multiple hosts/VMs on a single machine using Docker Swarm. Before that, we will check Docker information with the command,
docker info. In my case, it returns the following snapshot:
Here, we can see that Docker Swarm is in an inactive state. So, we have to initialize a Swarm. One of the most important things to remember is to make sure your machine has a virtual machine installed. If you don't have a virtual machine installed, please install virtual machine first and then go to the further steps.
Now, we will create three Docker machines. One is for the Manager Node, and the two others are for Worker Nodes. To do this, we will use the following commands:
docker-machine create --driver virtualbox manager
By executing the previous command, we have created a Manager Node. Similarly, we will create two worker nodes/machines.
docker-machine env manager command will show the environment information for the Manager Node. In my case, it returns:
We should run the
eval $(docker-machine env manager) command to configure our shell. Then, we can get our two Worker Nodes with the following commands:
docker-machine create --driver virtualbox worker1
docker-machine create --driver virtualbox worker2
After executing this command, we should check the created machines by executing the
docker-machine ls command. In my case, the output from that command looks like this:
Now, if we run
docker node ls, it will return the following message:
Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.
So, now, we will
ssh to the manager machine using,
docker-machine ssh manager. Once in the manager machine, we will initiate Swarm using:
docker swarm init --advertise-addr 192.168.99.135
In your case, the IP address will be the Manager Node IP address. In my case the command returns:
Now, we should login to the other two worker machines using
ssh and run the
docker swarm join command that was returned as a
swarm init command response instruction.
In both workers, we will execute:
docker swarm join --token SWMTKN-1-5zsxl3dvxh4rneiq0b7j7zcb6fyd5i5e3l0fjzq8ec
Test Swarm Nodes
Now, we will again
ssh to the manager machine and execute the
docker node ls command. In my case, it returns:
Here, we can see that
MANAGER STATUS is the
Leader for the Manager Node. We can run and see any information on Docker from this node but not from our Worker Nodes.
We will only install MySQL in the Manager Node. We will skip database clustering. I think our shell, is using a manager environment. So, we can follow the Setup MySQL part of my first article and run the following command:
After we run the container, we will import our SQL script by running the following command. We can get the script file from here.
If we want to test our MySQL setup or details of setup, please check the Setup MySQL part of this article.
Deploy Our Application
To deploy the application, we need to clone the project from here. Now, we will move to the project's root directory (book_manager) and then checkout to the branch, docker-swarm. After that, we need to update the datasource at application.properties. We will update the following IP with our Manager Node's IP.
Now, we will run the
gradle clean build command to build this project.
Here, we can see that I used flopcoder/book_manager as the image name. We have to update our own Docker Hub id. After build completion, we will run
docker-compose build to build our Docker images.
I think if you had followed my all articles, you have an account on Docker Hub. After build completion, we will run
docker-compose push to send out build images to Docker Hub. As we are using Docker Swarm mode, we will deploy our project using Docker Stack. Now, we will run the following command:
docker stack deploy --compose-file docker-compose.yml book_manager
In my case, it returned:
It will take some time to deploy images to all Swarm nodes. You can check the log using the following command:
docker service logs book_manager_book-manager-app
We can also scale up our deployment with following command
docker service scale book_manager_book-manager-app=3
After that, we will check the running scaled containers on different nodes:
After waiting some time, you can hit the following URLs:
All three nodes will return the same author list.
We can configure a visualizer service using the following command:
After the visualizer service is created, we can check the status of our clustered services at 8080 port(visualizer). In my case, after hitting http://192.168.99.135:8080/ from my browser, it looks like this:
Some Important Commands
- docker-machine start node_name; #Use to start a stopped node
- docker-machine stop node_name; #Use to stop a running node
- docker-machine ls; #Use to check docker nodes
- docker-machine ip node_name; #Use to check ip of a node
- docker node ls; #Use to check node list
- docker ps; #Use to check running services;
- docker service scale web=6; #Use to scale a application
- docker service logs -f service_name; #Use to see the log of a service
- docker node inspect self; #Use to check node information
- docker node inspect node_name #Use to check node information
- docker node ps node_name; #Use to check running services in a node
- docker node inspect — pretty node_name; #Use to check node information
- docker container ls #Use to check container list
- docker container rm container_name; # Use to remove a container
- docker image ls; # Use to check image list
- docker image rm image_name; #Use to remove image
- docker-compose build #Use to build images
- docker-compose up #Use to deploy images
- docker-compose up --build #Use to deploy images after build
- docker-compose down #Use to shutdown container
- docker exec -it docker-mysql bash; #Use to access mysql container
- docker stack services book_manager; # Use to check services under book_manager stack
- docker stack rm service_name #Use to remove a service from stack
- docker stack ls # Use to check stack list
I think that's all you need to get started with Docker Swarm with Spring Boot and MySQL. Thanks so much for reading!
NB: In this article, I have used IP of my host machines' VM. You have to change it to yours.
Opinions expressed by DZone contributors are their own.