swarm
install docker with user data
1#!/bin/bash2curl -fsSL https://get.docker.com -o get-docker.sh3sh get-docker.sh4sudo usermod -aG docker <username eg root>
swarm lab - part 01 setup the swarm
setup three droplets with docker installed as above.
on node-01 initiate swarm on a public ip address
1docker swarm init --advertise--addr <IP_ADDRESS>
- 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
- 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.
- take the join token to add also the third node
1docker swarm join-token manager
- paste on node-03 the join command
1docker swarm join --token <token> <IP_ADDRESS>
swarm lab - part 02 install two services. a website and a database
- create a network overlay
1docker network create --driver overlay mydrupal
- create the database service and add it to the network we created
1docker service create --name psql --network mydrupal -e POSTGRES_PASSWORD=mypass postgres
- create the drupal service and add it to the same network
1docker service create --name drupal --network mydrupal -p 80:80 drupal
- visit a droplet and see the service running
drupal setup must be the same as the service name so that they can communicate.
drupal complete
swarm lab - part 03 routing mesh
- creating one more service. elasticsearch
1docker service create --name search --replicas 3 -p 9200:9200 elasticsearch:2
- 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:34 redis:5 image: redis:alpine6 ports:7 - "6379"8 networks:9 - frontend10 deploy:11 replicas: 112 update_config:13 parallelism: 214 delay: 10s15 restart_policy:16 condition: on-failure17 db:18 image: postgres:9.419 volumes:20 - db-data:/var/lib/postgresql/data21 networks:22 - backend23 environment:24 - POSTGRES_HOST_AUTH_METHOD=trust25 deploy:26 placement:27 constraints: [node.role == manager]28 vote:29 image: bretfisher/examplevotingapp_vote30 ports:31 - 5000:8032 networks:33 - frontend34 depends_on:35 - redis36 deploy:37 replicas: 538 update_config:39 parallelism: 240 restart_policy:41 condition: on-failure42 result:43 image: bretfisher/examplevotingapp_result44 ports:45 - 5001:8046 networks:47 - backend48 depends_on:49 - db50 deploy:51 replicas: 152 update_config:53 parallelism: 254 delay: 10s55 restart_policy:56 condition: on-failure5758 worker:59 image: bretfisher/examplevotingapp_worker:java60 networks:61 - frontend62 - backend63 depends_on:64 - db65 - redis66 deploy:67 mode: replicated68 replicas: 169 labels: [APP=VOTING]70 restart_policy:71 condition: on-failure72 delay: 10s73 max_attempts: 374 window: 120s75 placement:76 constraints: [node.role == manager]7778 visualizer:79 image: dockersamples/visualizer80 ports:81 - "8080:8080"82 stop_grace_period: 1m30s83 volumes:84 - "/var/run/docker.sock:/var/run/docker.sock"85 deploy:86 placement:87 constraints: [node.role == manager]8889networks:90 frontend:91 backend:9293volumes:94 db-data: