Instantly share code, notes, and snippets.

@jamiejackson /create_swarm.sh Secret
Last active Jul 5, 2017

Embed
What would you like to do?
#!/bin/bash
# https://codefresh.io/blog/deploy-docker-compose-v3-swarm-mode-cluster/
# vars
[ -z "$NUM_WORKERS" ] && NUM_WORKERS=3
# init swarm (need for service command); if not created
docker node ls 2> /dev/null | grep "Leader"
if [ $? -ne 0 ]; then
if [ "$(hostname -i)" = "127.0.0.1" ]; then
# vagrant environment
docker swarm init --advertise-addr=192.168.56.10
else
docker swarm init
fi
fi
# get join token
SWARM_TOKEN=$(docker swarm join-token -q worker)
# get Swarm master IP (Docker for Mac xhyve VM IP)
SWARM_MASTER=$(docker info --format "{{.Swarm.NodeAddr}}")
echo "Swarm master IP: ${SWARM_MASTER}"
sleep 10
# start Docker registry mirror
docker run -d --restart=always -p 4000:5000 --name v2_mirror \
-v $PWD/rdata:/var/lib/registry \
-e REGISTRY_PROXY_REMOTEURL=https://registry-1.docker.io \
registry:2.5
# run NUM_WORKERS workers with SWARM_TOKEN
for i in $(seq "${NUM_WORKERS}"); do
# remove node from cluster if exists
docker node rm --force $(docker node ls --filter "name=worker-${i}" -q) > /dev/null 2>&1
# remove worker contianer with same name if exists
docker rm --force $(docker ps -q --filter "name=worker-${i}") > /dev/null 2>&1
# run new worker container
docker run -d --privileged --name worker-${i} --hostname=worker-${i} \
-p ${i}2375:2375 \
-p ${i}5000:5000 \
-p ${i}5001:5001 \
-p ${i}5601:5601 \
docker:stable-dind --registry-mirror http://${SWARM_MASTER}:4000
sleep 1
# add worker container to the cluster
docker --host=localhost:${i}2375 swarm join --token ${SWARM_TOKEN} ${SWARM_MASTER}:2377
done
# show swarm cluster
printf "\nLocal Swarm Cluster\n===================\n"
docker node ls
# echo swarm visualizer
printf "\nLocal Swarm Visualizer\n===================\n"
docker run --rm -it -d --name swarm_visualizer \
-p 8081:8080 -e HOST=localhost \
-v /var/run/docker.sock:/var/run/docker.sock \
dockersamples/visualizer
version: "3"
services:
redis:
image: redis:3.2-alpine
ports:
- "6379"
networks:
- voteapp
deploy:
placement:
constraints: [node.role == manager]
db:
image: postgres:9.4
volumes:
- db-data:/var/lib/postgresql/data
networks:
- voteapp
deploy:
placement:
constraints: [node.role == manager]
voting-app:
image: dockersamples/examplevotingapp_vote:latest
ports:
- 5000:80
networks:
- voteapp
depends_on:
- redis
deploy:
mode: replicated
replicas: 2
labels: [APP=VOTING]
placement:
constraints: [node.role == worker]
result-app:
image: dockersamples/examplevotingapp_result:latest
ports:
- 5001:80
networks:
- voteapp
depends_on:
- db
worker:
image: dockersamples/examplevotingapp_worker:latest
networks:
voteapp:
aliases:
- workers
depends_on:
- db
- redis
# service deployment
deploy:
mode: replicated
replicas: 2
labels: [APP=VOTING]
# service resource management
resources:
# Hard limit - Docker does not allow to allocate more
limits:
cpus: '0.25'
memory: 512M
# Soft limit - Docker makes best effort to return to it
reservations:
cpus: '0.25'
memory: 256M
# service restart policy
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
window: 120s
# service update configuration
update_config:
parallelism: 1
delay: 10s
failure_action: continue
monitor: 60s
max_failure_ratio: 0.3
# placement constraint - in this case on 'worker' nodes only
placement:
constraints: [node.role == worker]
networks:
voteapp:
volumes:
db-data:
docker stack deploy --compose-file=docker-compose.yml codefresh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment