5. Swarm
5.1. Enable Swarm
To start Swarm
, type in the following.
1docker swarm init
You should see a similar output as follows.
Swarm initialized: current node (891q5gj0y69y2s1kzk01xdjzy) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3mwknlzd6h75b0l8e9324l3tlhi1hhn1k9cc0vklwsf937v1dq-3ri0hmvm2390rho4irbwby0ru 192.168.0.73:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
To list the nodes in the swarm.
1docker node ls
Output.
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
16ui13y7ijoaqw6u8h05u2a2b * ryzen Ready Active Leader 19.03.3
5.2. Set up a Docker registry
To set up a local Docker registry, type in the following.
1docker service create --name registry --publish published=5000,target=5000 registry:2
mq4lk2beo4yz1hj8aqe2po3up
overall progress: 1 out of 1 tasks
1/1: running [==================================================>]
verify: Service converged
Check the status as follows.
1docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
mq4lk2beo4yz registry replicated 1/1 registry:2 *:5000->5000/tcp
Check that it’s working.
1curl http://localhost:5000/v2/
5.3. Publish stack
Create docker-compose.yml
as follows.
1version: "3.7"
2services:
3 db:
4 image: 127.0.0.1:5000/db
5 build: ./mysql
6 ports:
7 - "3306:3306"
8 volumes:
9 - type: bind
10 source: ./mysql/docker-entrypoint-initdb.d
11 target: /docker-entrypoint-initdb.d
12 consistency: consistent
13 environment:
14 MYSQL_ROOT_PASSWORD: "oneoffcoder"
15 flask:
16 image: 127.0.0.1:5000/rest
17 build: ./flask
18 ports:
19 - "5001:5000"
20 depends_on:
21 - db
22 ng:
23 image: 127.0.0.1:5000/ui
24 container_name: ui
25 build: ./ng
26 ports:
27 - "80:80"
28 depends_on:
29 - flask
30 - db
Build.
1docker-compose up
Publish.
1docker-compose push
5.4. Deploy to Swarm
Now deploy the stack to the swarm.
1docker stack deploy -c docker-compose.yml student
Output.
Creating network student_default
Creating service student_flask
Creating service student_ng
Creating service student_db
5.4.1. List services
1docker service ls
Output.
ID NAME MODE REPLICAS IMAGE PORTS
mq4lk2beo4yz registry replicated 1/1 registry:2 *:5000->5000/tcp
v898krzykppw student_db replicated 1/1 127.0.0.1:5000/db:latest *:3306->3306/tcp
bgkeb2k2skpt student_flask replicated 1/1 127.0.0.1:5000/rest:latest *:5001->5000/tcp
ypnuugzylzp7 student_ng replicated 1/1 127.0.0.1:5000/ui:latest *:80->80/tcp
5.4.2. Inspect services
1docker service inspect --pretty student_db
2docker service inspect --pretty student_flask
3docker service inspect --pretty student_ng
5.4.3. Scale services
1docker service scale student_ng=5
2docker service ps student_ng
5.5. Stop services
1docker stack rm student
2docker service rm registry
Output.
Removing service student_db
Removing service student_flask
Removing service student_ng
Removing network student_default
5.6. Leave swarm
To leave swarm.
1docker swarm leave --force