Deploying it all

As we did for our simple web server, we will begin by creating another Swarm cluster:

$ docker swarm init
Swarm initialized: current node (1y1h7rgpxbsfqryvrxa04rvcp) is now a manager.

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

docker swarm join
--token SWMTKN-1-36flmf9vnika6x5mbxx7vf9kldqaw6bq8lxtkeyaj4r5s461ln-aiqlw49iufv3s6po4z2fytos1
192.168.4.128:2377

Then, we need to create our overlay network for the service-discovery hostname resolution to work. You don't need to know much about this other than it creates an isolated network that we will add all the services to:

$ docker network create --driver overlay service_network
44cyg4vsitbx81p208vslp0rx

Finally, we will build and launch our containers:

$ cd ../database
$ docker build . -t local_database
$ docker service create -d --replicas 1
--name local-database
--network service_network
--mount type=volume,source=database_volume,destination=/data/db
local_database

<snip>
pilssv8du68rg0oztm6gdsqse

$ cd ../application_server

$ docker build -t application_server .
$ docker service create -d -e DB_HOST=local-database
--replicas 3
--network service_network
--name application-server
application_server
<snip>
pue2ant1lg2u8ejocbsovsxy3

$ cd ../web_server
$ docker build -t web_server .
$ docker service create -d --name web-server
--network service_network
--replicas 3
-e APP_NAME=application-server
-p 8080:8080
web_server
<snip>
swi95q7z38i2wepmdzoiuudv7

$ # Sanity checks

$ docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
pilssv8du68r local-database replicated 1/1 local_database
pue2ant1lg2u application-server replicated 3/3 application_server
swi95q7z38i2 web-server replicated 3/3 web_server *:8080->8080/tcp

$ docker ps --format 'table {{.ID}} {{.Image}} {{.Ports}}'
CONTAINER ID IMAGE PORTS
8cdbec233de7 application_server:latest 8000/tcp
372c0b3195cd application_server:latest 8000/tcp
6be2d6e9ce77 web_server:latest 80/tcp, 8080/tcp
7aca0c1564f0 web_server:latest 80/tcp, 8080/tcp
3d621c697ed0 web_server:latest 80/tcp, 8080/tcp
d3dad64c4837 application_server:latest 8000/tcp
aab4b2e62952 local_database:latest 27017/tcp
If you are having trouble with getting these services up and running, you can check the logs with docker service logs <service_name> in order to figure out what went wrong. You can also use docker logs <container_id> if a specific container is having trouble.

With these in place, we can now check whether our code works at http://127.0.0.1:8080 (username: user, password: test):


Looks like it is working! Once we put in our credentials, we should be redirected to the main application page:

Does the database work if we put in some words?

Indeed! We have really created a 1-node swarm-backed service, and it is scalable plus load balanced!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset