-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap
executable file
·148 lines (131 loc) · 4.25 KB
/
bootstrap
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/sh
# This script is intended to be run from an alpine container, which uses a
# mostly bash-compatible shell (ash) implemented by BusyBox (both ash and
# sh are symlinked to /bin/busybox).
#
# This script starts containers that will run a registry
# and a number of Docker engines to participate as swarm nodes.
#
# Usage: MANAGERS=3 WORKERS=2 bootstrap -p=8000:8000
#
# Using Docker, this looks like:
# docker run -it --rm \
# -v /var/run/docker.sock:/var/run/docker.sock \
# -v $PWD:/usr/local/sbin \
# --network=hostnet \
# -e MANAGERS=3 \
# -e WORKERS=2 \
# docker bootstrap -p=8000:8000
#
# Note: the port publishing option (-p=8000:8000) is passed to the bootstrap command.
# Any options will be supplied as `docker run` options for the container that gets
# created to be the swarm manager node. In this case, a service in the swarm listening
# on port 8000 will also be available from the host (at 127.0.0.1:8000).
set -e
trap 'onexit $LINENO' EXIT
# Supplied by environment
MANAGERS=${MANAGERS:-3}
WORKERS=${WORKERS:-2}
# Command args will be passed on as `docker run` options when creating the swarm manager
# (mainly for exposing extra ports)
# Use normal `docker run [OPTIONS]` syntax. Ex: `bootstrap -p=3000:3000 -p=8000:8000`
SWARMOPTS="$@"
MANAGERS=${MANAGERS:-3}
WORKERS=${WORKERS:-2}
REGISTRY=registry
HOSTNET=hostnet
SWARMIP=""
MTOKEN=""
WTOKEN=""
SWARMNET=swarmnet
main() {
create_host_bridge_network
start_registry
create_swarm
}
create_host_bridge_network() {
(docker network ls | grep ${HOSTNET} &> /dev/null) || ( \
echo "Create host bridge network: ${HOSTNET}" && \
docker network create ${HOSTNET} )
}
start_registry() {
(docker ps --format '{{ .Names }}' | grep "^${REGISTRY}$" &> /dev/null ) || ( \
echo start registry service: ${REGISTRY} && \
docker run \
--name=${REGISTRY} \
--detach \
--network=${HOSTNET} \
-p=5000:5000 \
--restart=unless-stopped \
--volume=registry:/var/lib/registry \
--label="host.role=cluster" \
registry:2
sleep 5 )
echo "Registry is running on port 5000"
}
create_swarm() {
create_managers
create_workers
}
node_exists() {
name=$1
docker ps -aq --filter="name=${name}" | grep "${name}"
}
remove_node() {
name=$1
(docker rm -f $(docker ps -aq --filter=name="${name}")) &> /dev/null || true
}
start_node() {
node=$1
opts="${@:2}"
remove_node "${node}"
docker run --privileged --detach --name="${node}" --hostname="${node}" \
--network=${HOSTNET} \
--label="host.role=cluster" \
--label="node.role=swarm" \
${opts} \
subfuzion/dind --registry-mirror=http://${REGISTRY}:5000
}
create_managers() {
name=m1
echo starting manager: ${name}
# publish common default ports plus whatever is passed to this script as extra options
start_node "${name}" "-p=3000:3000" "-p=80:80" "-p=8080:8080" "-p=50101:50101" "${SWARMOPTS}"
SWARMIP=$(docker inspect "${name}" --format '{{ .NetworkSettings.Networks.'${HOSTNET}'.IPAddress }}')
echo "swarm manager ip (m1): ${SWARMIP}"
# a bit of extra time seems to be needed before trying to initalize the node as a swarm manager
sleep 5
(docker --host=${name} swarm init --advertise-addr="${SWARMIP}:2377") &> /dev/null
MTOKEN=$(docker --host="${name}" swarm join-token -q manager)
WTOKEN=$(docker --host="${name}" swarm join-token -q worker)
docker --host=${name} network create -d overlay --attachable ${SWARMNET}
count=$((MANAGERS-1))
for m in $(seq ${count}); do
i=$((m+1))
name="m$i"
echo "Starting manager $i: ${name}"
start_node ${name}
echo "Joining manager ${name} to swarm at: m1:2377"
docker --host="${name}" swarm join --token "${MTOKEN}" m1:2377
done
}
create_workers() {
for w in $(seq "${WORKERS}"); do
name="w$w"
echo "Starting worker $w: ${name}"
start_node "${name}"
echo "Joining worker ${name} to swarm at: m1:2377"
docker --host="${name}" swarm join --token "${WTOKEN}" m1:2377
done
}
onexit() {
if [[ "$?" -eq 0 ]]; then
echo "---------------------------------------------------------------------------"
echo "Swarm running at m1:2377"
echo "---------------------------------------------------------------------------"
docker --host=m1 node ls
else
echo "Bootstrap failed, line: $1"
fi
}
main