Setting up Swarm Micro-services

Note – if you don’t need to split your app into multiple instances, just forget about Docker Compose

MWE

Prepare the Docker Compose file, here’s a basic example

mkdir -p ~/compose/weblab/
cd ~/compose/weblab/
vi docker-compose.yml

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: pbraun9/ubuntu:latest
    deploy:
      replicas: 3
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "8080:8080"
    networks:
      - webnet
networks:
  webnet:

and here’s how to create some depedencies – docker host constraints – among the instances

...

Shared volume

bind/map a folder from the docker host

make sure the folder exists on all the docker hosts

mkdir /datafs/weblab/
echo ok > /datafs/weblab/test

then e.g. in the web service stanza

    volumes:
      - /datafs/weblab:/weblab

run the thing

docker stack deploy -c docker-compose.yml APPLICATION

and check that the volume is available everywhere

docker ps
docker exec -ti INSTANCE /bin/bash
ls -alhF /weblab/

Ready to go

from some manager

run the lab application (webnet and web instances) and check on all the nodes at once

docker stack deploy -c docker-compose.yml lab1
docker stack ps lab1

review the services associated with an application

docker stack services lab1

Operations

from some manager

eventually scale the app and increase the number of instance replicas

vi docker-compose.yml
docker stack deploy -c docker-compose.yml lab1

remove an application and all its instances

docker stack rm lab1

Resources

https://docs.docker.com/get-started/part3/#your-first-docker-composeyml-file

https://docs.docker.com/compose/swarm/#multiple-dependencies

https://docs.docker.com/compose/compose-file/#external


GUIDES | LECTURES | BENCHMARKS | SMTP HEALTH