Navigate back to the homepage

learning docker - day 5

Christos Paschalidis
January 2nd, 2021 · 1 min read

swarm

install docker with user data

1#!/bin/bash
2curl -fsSL https://get.docker.com -o get-docker.sh
3sh get-docker.sh
4sudo usermod -aG docker <username eg root>

swarm lab - part 01 setup the swarm

  1. setup three droplets with docker installed as above.

  2. on node-01 initiate swarm on a public ip address

1docker swarm init --advertise--addr <IP_ADDRESS>
  1. add the node-02 as worker.

ssh on node-02 and ask it to join the swarm

1docker swarm join --token <token> <IP>

PS this command is printed for you after you do swarm init

  1. update the role of node-02 to be a manager

on node-01 execute

1docker node update --role manager node-02

now if you do docker node ls you will see node-02 being reachable.

  1. take the join token to add also the third node
1docker swarm join-token manager
  1. paste on node-03 the join command
1docker swarm join --token <token> <IP_ADDRESS>

image The manager status indicates that these nodes are managers.

swarm lab - part 02 install two services. a website and a database

  1. create a network overlay
1docker network create --driver overlay mydrupal
  1. create the database service and add it to the network we created
1docker service create --name psql --network mydrupal -e POSTGRES_PASSWORD=mypass postgres

image runs on node-01

  1. create the drupal service and add it to the same network
1docker service create --name drupal --network mydrupal -p 80:80 drupal

image =

  1. visit a droplet and see the service running

image

  1. drupal setup must be the same as the service name so that they can communicate. image

  2. drupal complete image

swarm lab - part 03 routing mesh

  1. creating one more service. elasticsearch
1docker service create --name search --replicas 3 -p 9200:9200 elasticsearch:2
  1. on node-01 use curl to see the loadbalancer working
1curl localhost:9200

swarm stacks

we will be using

1docker stack deploy

example

1version: "3"
2services:
3
4 redis:
5 image: redis:alpine
6 ports:
7 - "6379"
8 networks:
9 - frontend
10 deploy:
11 replicas: 1
12 update_config:
13 parallelism: 2
14 delay: 10s
15 restart_policy:
16 condition: on-failure
17 db:
18 image: postgres:9.4
19 volumes:
20 - db-data:/var/lib/postgresql/data
21 networks:
22 - backend
23 environment:
24 - POSTGRES_HOST_AUTH_METHOD=trust
25 deploy:
26 placement:
27 constraints: [node.role == manager]
28 vote:
29 image: bretfisher/examplevotingapp_vote
30 ports:
31 - 5000:80
32 networks:
33 - frontend
34 depends_on:
35 - redis
36 deploy:
37 replicas: 5
38 update_config:
39 parallelism: 2
40 restart_policy:
41 condition: on-failure
42 result:
43 image: bretfisher/examplevotingapp_result
44 ports:
45 - 5001:80
46 networks:
47 - backend
48 depends_on:
49 - db
50 deploy:
51 replicas: 1
52 update_config:
53 parallelism: 2
54 delay: 10s
55 restart_policy:
56 condition: on-failure
57
58 worker:
59 image: bretfisher/examplevotingapp_worker:java
60 networks:
61 - frontend
62 - backend
63 depends_on:
64 - db
65 - redis
66 deploy:
67 mode: replicated
68 replicas: 1
69 labels: [APP=VOTING]
70 restart_policy:
71 condition: on-failure
72 delay: 10s
73 max_attempts: 3
74 window: 120s
75 placement:
76 constraints: [node.role == manager]
77
78 visualizer:
79 image: dockersamples/visualizer
80 ports:
81 - "8080:8080"
82 stop_grace_period: 1m30s
83 volumes:
84 - "/var/run/docker.sock:/var/run/docker.sock"
85 deploy:
86 placement:
87 constraints: [node.role == manager]
88
89networks:
90 frontend:
91 backend:
92
93volumes:
94 db-data:

More articles from blog for tech

learning docker - day 4

Docker commands I learned today

December 17th, 2020 · 1 min read

learning docker - day 3

Docker commands I learned today

December 16th, 2020 · 1 min read
© 2020–2021 blog for tech
Link to $https://github.com/paschalidiLink to $https://www.linkedin.com/in/christos-paschalidis/