-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_swarm_cluster.sh
executable file
·76 lines (67 loc) · 2.03 KB
/
create_swarm_cluster.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
# get number of managers & workers from cli
num_managers=$1
num_workers=$2
# validate params
if [ -z "$num_managers" ] || [ -z "$num_workers" ]
then
echo "missing parameters"
exit 1
fi
if [ $num_managers == 0 ]
then
echo "cannot create a cluster when # of managers is less than 1. you need to have at least 1 manager"
exit 1
fi
# verify that docker-machine is installed
if [ ! -f /usr/local/bin/docker-machine ]
then
echo "could not locate docker-machine. quitting...."
exit 1
fi
# create managers
for manager in $(seq $num_managers);
do
echo "creating swarm manager-$manager"
echo "==============================="
docker-machine create -d "virtualbox" manager-$manager
done
# create workers
for worker in $(seq $num_workers)
do
echo "creating swarm worker-$worker"
echo "============================="
docker-machine create -d "virtualbox" worker-$worker
done
# create cluster
echo "creating cluster"
echo "================"
# joining managers
for manager in $(seq $num_managers);
do
if [ $manager == 1 ]
then
# 1st manager
docker-machine ssh manager-$manager docker swarm init --advertise-addr=eth1
manager_join_cmd=`docker-machine ssh manager-$manager docker swarm join-token manager | grep -i join -A2`
manager_join_cmd_one_line=`echo $manager_join_cmd`
worker_join_cmd=`docker-machine ssh manager-$manager docker swarm join-token worker | grep -i join -A2`
worker_join_cmd_one_line=`echo $worker_join_cmd`
else
# additional managers
echo "joining manager-$manager to the cluster"
echo "======================================="
docker-machine ssh manager-$manager eval ${manager_join_cmd_one_line}
fi
done
# joining workers
for worker in $(seq $num_workers);
do
echo "joining worker-$worker to the cluster"
echo "====================================="
docker-machine ssh worker-$worker eval ${worker_join_cmd_one_line}
echo ""
done
# display the cluster
echo "the cluster is ready: there are $num_managers managers and $num_workers workers:"
docker-machine ssh manager-1 docker node ls