Scaling and Clustering with Docker Swarm


Viktor Farcic


@vfarcic

TechnologyConversations.com

CloudBees.com

Viktor Farcic

Docker 1.12+

  • Service
  • Service Discovery
  • Failover
  • Networking
  • Rolling Updates

Demo Time

VMs

for i in 1 2 3; do
  docker-machine create -d virtualbox swarm-$i
done

Visualizer


eval $(docker-machine env swarm-1)

docker run --name visualizer -d \
  -p 8083:8083 \
  -e HOST=$(docker-machine ip swarm-1) \
  -e PORT=8083 \
  -v /var/run/docker.sock:/var/run/docker.sock \
  dockersamples/visualizer

open http://$(docker-machine ip swarm-1):8083

Cluster

eval $(docker-machine env swarm-1)

docker swarm init --advertise-addr $(docker-machine ip swarm-1)

TOKEN=$(docker swarm join-token -q worker)

for i in 2 3; do
  eval $(docker-machine env swarm-$i)

  docker swarm join --token $TOKEN --advertise-addr $(docker-machine ip swarm-$i) $(docker-machine ip swarm-1):2377
done

Cluster Info

eval $(docker-machine env swarm-1)

docker node ls

Cluster Info

Networking

docker network create --driver overlay proxy

docker network create --driver overlay go-demo

docker network ls

Networking

Service

docker service create --name go-demo-db \
    --network go-demo --reserve-memory 100m mongo

docker service ls

docker service create --name go-demo -e DB=go-demo-db \
    --network go-demo --network proxy \
    --reserve-memory 50m vfarcic/go-demo

docker service ps go-demo

docker service inspect go-demo --pretty

Service

Global Service

docker service create --name util \
    --network go-demo --network proxy \
    --mode global alpine sleep 1000000000

docker service ps util

ID=$(docker ps -q --filter label=com.docker.swarm.service.name=util)

docker exec -it $ID apk add --update drill

docker exec -it $ID drill go-demo

docker exec -it $ID drill go-demo-db

Scaling

docker service scale go-demo=5

docker service ps go-demo

Scaling

Failover

docker-machine rm -f swarm-3

docker service ps go-demo

Adding Nodes

docker-machine create -d virtualbox swarm-4

eval $(docker-machine env swarm-4)

docker swarm join --token $TOKEN --advertise-addr $(docker-machine ip swarm-4) $(docker-machine ip swarm-1):2377

eval $(docker-machine env swarm-1)

docker service ps util

Reverse Proxy

wget https://raw.githubusercontent.com/vfarcic/docker-flow-proxy/master/docker-compose.yml

export DOCKER_IP=$(docker-machine ip swarm-1)

docker-compose up -d consul-server

docker service create --name proxy -p 80:80 -p 443:443 \
    -p 8080:8080 --network proxy -e MODE=swarm --replicas 3 \
    -e CONSUL_ADDRESS=$(docker-machine ip swarm-1):8500 \
    vfarcic/docker-flow-proxy

docker service ps proxy

Reverse Proxy

Reverse Proxy

curl "$(docker-machine ip swarm-1):8080/v1/docker-flow-proxy/reconfigure?serviceName=go-demo&servicePath=/demo&port=8080&distribute=true"

for i in {1..10}; do
    curl -i $(docker-machine ip swarm-1)/demo/hello
done

Networking

Networking

Networking

Networking

Networking

Service Updates

docker service update --image vfarcic/go-demo:1.1 --update-delay 5s go-demo

docker service ps go-demo

Cleanup

docker-machine rm -f swarm-1 swarm-2 swarm-4

Viktor Farcic


@vfarcic


TechnologyConversations.com

Amazon
LeanPub

 
LeanPub