-
Notifications
You must be signed in to change notification settings - Fork 1
/
machine.sh
executable file
·283 lines (197 loc) · 6.68 KB
/
machine.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/bash -
#title :machine
#description :Script that uses docker-machine to create and manage docker-swarm cluster running elastic stack.
#author :Nick Sherron (@nsherron90)
#date :20190514
#version :0.1.0
#usage :./machine.sh
#notes :
#bash_version :3.2.57(1)-release
#license: MIT
#============================================================================
# TODO add swarm-restart function
# TODO add show config function
set -euf -o pipefail
# get variables from .env
. .env
# set defaults
machine=${MACHINE:-virtualbox}
manager=${MANAGER:-1}
worker=${WORKER:-2}
let nodes=manager+worker
usage() {
cat <<"EOF"
Script that uses docker-machine to create and manage docker-swarm cluster running elastic stack.
IMPORTANT: environment variables are read via .env file located within this directory.
Usage: ./machine.sh COMMAND
Commands:
dm-init Create docker machines named manager-1 , worker-1, worker-2 etc.
Machine driver (aws, virtualbox(default), gce) and quantity are
read from .env file or environment variables.
dm-destroy Destroys machines created by dm-init.
dm-restart Runs dm-destroy then dm-init
swarm-init Initializes swarm on machines created by dm-init.
Machines named manager-[n] and worker-[n] are created as swarm managers and workers.
swarm-destroy Destroys the swarm-created by swarm-init.
swarm-ls Runs docker node ls on the manager-1 node to list the swarm.
stack-init Runs `docker stack deploy -c elastic-stack.yml elastic`
as swarm manager, deploying elastic-stack.yml as a swarm stack.
stack-destroy Destroys stack created by stack-init
stack-restart Runs stack-destroy followed by stack-init
init Runs dm-init, swarm-init, and stack-init
destroy Runs swarm-destroy and dm-destroy
restart Runs init and destroy
./machine.sh -h,--help : show this message
EOF
}
dm-init() {
if [[ "$machine" == "amazon" ]]; then
echo "### Docker-machine type = aws"
echo "### Initializing docker-machine for managers"
for i in $(seq 1 ${manager}); do
docker-machine create \
--driver amazonec2 \
--amazonec2-region ${EC2_REGION:-us-east-2} \
manager-${i}
done
echo "### Initializing docker-machine for workers"
for i in $(seq 1 ${worker}); do
docker-machine create \
--driver amazonec2 \
--amazonec2-region ${EC2_REGION:-us-east-2} \
worker-${i}
done
elif [[ "${machine}" == "google" ]]; then
echo "### Docker-machine type = google"
echo "### Initializing docker-machine for managers"
export GOOGLE_PROJECT="${GOOGLE_PROJECT:-}"
for i in $(seq 1 ${manager}); do
docker-machine create \
--driver google \
manager-${i}
done
echo "### Initializing docker-machine for workers"
for i in $(seq 1 ${worker}); do
docker-machine create \
--driver google \
worker-${i}
done
else
echo "### Initializing docker-machine for managers"
echo "### Docker-machine type = virtualbox"
for i in $(seq 1 ${manager}); do
docker-machine create \
--driver virtualbox \
--virtualbox-no-vtx-check \
manager-${i}
done
echo "### Initializing docker-machine for workers"
for i in $(seq 1 ${worker}); do
docker-machine create \
--driver virtualbox \
--virtualbox-no-vtx-check \
worker-${i}
done
fi
# increase vm.max_map_count to 262144 for elasticsearch
# https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html
echo "### Increasing vm.max_map_count on managers"
for i in $(seq 1 ${manager}); do
docker-machine ssh manager-${i} sudo sysctl -w vm.max_map_count=262144
done
echo "### Increasing vm.max_map_count on workers "
for i in $(seq 1 ${worker}); do
docker-machine ssh worker-${i} sudo sysctl -w vm.max_map_count=262144
done
}
dm-destroy() {
echo "### Destroying docker-machine nodes ..."
for i in $(seq 1 ${manager}); do
docker-machine rm -y manager-${i}
done
for i in $(seq 1 ${worker}); do
docker-machine rm -y worker-${i}
done
}
dm-restart() {
dm-destroy 2> /dev/null
dm-init
}
swarm-init() {
echo "### Creating Docker Machine nodes ..."
leader_ip=$(docker-machine ip manager-1)
echo "### Initializing Swarm mode ..."
eval $(docker-machine env manager-1)
docker swarm init --advertise-addr ${leader_ip} || true
# Swarm tokens
manager_token=$(docker swarm join-token manager -q)
worker_token=$(docker swarm join-token worker -q)
echo "### Joining manager nodes ..."
for i in $(seq 1 ${manager}); do
eval $(docker-machine env manager-${i})
docker swarm join --token ${manager_token} ${leader_ip}:2377 2> /dev/null || true
done
echo "### Joining worker nodes ..."
for i in $(seq 1 ${worker}); do
eval $(docker-machine env worker-${i})
docker swarm join --token ${worker_token} ${leader_ip}:2377 || true
done
eval $(docker-machine env manager-1)
docker node ls
}
swarm-destroy() {
echo "### Destroying swarm ..."
for i in $(seq 1 ${manager}); do
eval $(docker-machine env manager-${i})
docker swarm leave -f
done
for i in $(seq 1 ${worker}); do
eval $(docker-machine env worker-${i})
docker swarm leave -f
done
}
swarm-ls() {
eval $(docker-machine env manager-1)
docker node ls
}
stack-init(){
eval $(docker-machine env manager-1)
# docker network create --driver overlay --attachable backend
docker stack deploy -c elastic-stack.yml elastic
}
stack-destroy(){
eval $(docker-machine env manager-1)
docker stack rm elastic
}
stack-ls(){
eval $(docker-machine env manager-1)
docker service ls
}
stack-restart(){
stack-destroy
sleep 5
stack-init
}
init() {
dm-init
swarm-init
stack-init
}
destroy() {
stack-destroy 2> /dev/null
dm-destroy 2> /dev/null
}
restart() {
destroy
sleep 5
init
}
main() {
if [[ "${1}" == '-h' || "${1}" == '--help' ]]; then
usage
exit 1
else
"${1}"
fi
}
main "${@:--h}"