- Launch microservice using docker service
- Use overlay networks
- Discovering containers
- You need 2 ubuntu nodes i.e. one host will be swarm master and other node will act as a worker node.
- Make sure docker service is installed on both the hosts . Refer docker setup file docker service installation.
Follow step 1 and 2 on both the nodes i.e. master and worker.
-
Build microservice project
git clone https://github.com/getreddy/docswarm.git
cd microproj
// Below command will build required jar files under microproj/build/libs
gradlew clean && gradlew build
// Container will make use of this jar file by launching command as 'java -jar docMicroservice-0.1.0.jar'
cd ..
mkdir jars
cp microproj/build/libs/docMicroservice-0.1.0.jar jars/
-
Build docker image
Below command will make use of docker file
sudo docker build -t ubuntuswarmtest .
-
Create overlay network.
Docker introduced user-defined bridge networks to control which containers can communicate with each other, and also to enable automatic DNS resolution of container names to IP addresses.
By default when we create swarm cluster, it creates overlay network “ingress”
We will create a new overlay network for our docker to docker communication.
sudo docker network create --attachable -d overlay overnet
- create swarm cluster.
Run below command on master node :
sudo docker swarm init
copy and paste output of below command on worker node.
sudo docker swarm join-token worker
confirm the setup by running the below command on master node:
sudo docker node ls
You can launch a service as a docker service. A service will be the image for a microservice within the context of some larger application. Examples of services might include an HTTP server, a database, or any other type of executable program that you wish to run in a distributed environment. This service which we are launching is a simple microservice which just returns a "message" when you hit "curl http://servicename:8080. In below command, the service name is "systemservice".
You have 2 options of launching docker service.
- Using docker command line:
Run below command on master node:
sudo docker service create --network overnet --detach=true --replicas 2 --publish 8080:8080 --name systemservice ubuntuswarmtest java -jar /opt/jars/docMicroservice-0.1.0.jar
- Using docker-compose :
using checked in docker compose yml file, run below command to launch services:
sudo docker stack deploy --compose-file docker-compose.yml stackdemo
When you deploy the service to the swarm, the swarm manager accepts your service definition as the desired state for the service. Then it schedules the service on nodes in the swarm as one or more replica (in above command its 2) tasks. The tasks run independently of each other on nodes in the swarm.
A container can always discover other containers on the same stack using just the container name as hostname. This includes containers of the same service. Similarly, a container can always discover other services on the same stack using the service name.
Below is the example on how a new container launched under a same overlay network can discover above created service:
Launch a container:
sudo docker run -it --network="overnet" ubuntuswarmtest /bin/bash
On the container prompt run:
curl http://systemservice:8080
you should see below output from the service:
"Service...Root index API returned..need to work on build:-)"