-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmesos_bootstrap.sh
executable file
·291 lines (219 loc) · 8.98 KB
/
mesos_bootstrap.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
284
285
286
287
288
289
290
#!/bin/bash
########################################################################################################################
##
##
## Mesos_bootstrap.sh relies on the following environment variables. The MAIN_IP and DOCKER0_IP are required and have
## no default. You should pass them into the Docker container using the -e flag.
##
## $MAIN_IP - the IP of the host running Docker to which Mesos master and slave can bind (required)
## $DOCKER0_IP - the IP assigned to the docker0 interface onthe CoreOS host
## $ETCD_PORT - the port on which ETCD runs on CoreOS (default: 4001)
## $ETCD_MESOS_PATH - the path in ETCD where we store Mesos related data (default: /mesos)
## $ETCD_TTL - the TTL used in retrying ETCD calls
##
## Usage:
##
## When no arguments are passed into this script, it will try to dynamically configure a Mesos cluster consisting of:
## - 1 node running a Master, Zookeeper, Marathon and a local Slave
## - x slave nodes, depending on the amount of nodes you spin up
##
## Discovery of the Master's IP is done using ETCD. For this to work, all nodes should be in the same ETCD cluster.
## If automagic setup doesn't work, you can also pass in arguments and flag to set up Mesos manually:
##
##
##
## For example, when you want to start a master
##
## $ ./mesos_bootstrap.sh master`
##
## When starting a slave you need to pass in the Master's Zookeeper address
##
## $ ./mesos_bootstrap.sh slave --master=zk://172.17.8.101:2181/mesos
##
## Starting a Marathon instance is the same as a slave
##
## $ ./mesos_bootstrap.sh marathon --master=zk://172.17.8.101:2181/mesos --etcd=false
##
## This script is partly based on the great work by deis:
## https://github.com/deis/
##
## @todo: replace flags with REAL flags that don't depend on the position in cmd line
##
########################################################################################################################
# set font types
bold="\e[1;36m"
normal="\e[0m"
red="\e[0;31m"
yellow="\e[0;33m"
green="\e[0;32m"
blue="\e[0;34m"
purple="\e[0;35 m"
normal="\e[0m"
export MAIN_IP=${MAIN_IP}
export MESOS_BOOTSTRAP_VERSION=1.0
echo -e "${bold}==> Starting Mesos/CoreOS Bootstrap on $MAIN_IP (version $MESOS_BOOTSTRAP_VERSION)${normal}"
# configure docker
export DOCKER0_IP=${DOCKER0_IP}
export DOCKER_PORT=${DOCKER_PORT:-2375}
export DOCKER="$DOCKER0_IP:$DOCKER_PORT"
# configure etcd
export ETCD_PORT=${ETCD_PORT:-4001}
export ETCD="$DOCKER0_IP:$ETCD_PORT"
export ETCD_PATH=${ETCD_PATH:-/mesos}
export ETCD_TTL=${ETCD_TTL:-10}
# configure Mesos
export MASTER_PORT=${MASTER_PORT:-5050}
export SLAVE_PORT=${SLAVE_PORT:-5051}
MAX_RETRIES_CONNECT=10
retry=0
# Set locale: this is required by the standard Mesos startup scripts
echo -e "${normal}==> info: Setting locale to en_US.UTF-8..."
locale-gen en_US.UTF-8 > /dev/null 2>&1
# Start syslog if not started....
echo -e "${normal}==> info: Starting syslog..."
service rsyslog start > /dev/null 2>&1
# All functions
function start_zookeeper {
echo -e "${normal}==> info: Starting Zookeeper..."
service zookeeper start
}
function start_slave {
set_deimos
MASTER=`echo $1 | cut -d '=' -f2`
# using ETCD or not?
USING_ETCD=`echo $2 | cut -d '=' -f2`
# set the slave parameters
echo ${MASTER} > /etc/mesos/zk
echo /var/lib/mesos > /etc/mesos-slave/work_dir
echo external > /etc/mesos-slave/isolation
echo /usr/local/bin/deimos > /etc/mesos-slave/containerizer_path
echo ${MAIN_IP} > /etc/mesos-slave/ip
echo host:${MAIN_IP} >/etc/mesos-slave/attributes
echo -e "${bold}==> info: Mesos slave will try to register with a master at ${MASTER}"
echo -e "${normal}==> info: Starting slave..."
/usr/bin/mesos-init-wrapper slave > /dev/null 2>&1 &
# wait for the slave to start
sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$SLAVE_PORT\" && \$1 ~ tcp") ]] ; do
echo -e "${normal}==> info: Waiting for Mesos slave to come online..."
sleep 3;
done
echo -e "${normal}==> info: Mesos slave started on port ${SLAVE_PORT}"
# When not using ETCD for Master discovery, this is the end of the script.
if [ ${USING_ETCD} = false ]; then
# while the Slave runs, keep the Docker container running
while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$SLAVE_PORT\" && \$1 ~ tcp") ]] ; do
echo -e "${normal}==> info: `date` - Mesos slave is running on port ${SLAVE_PORT}"
sleep 10
done
fi
}
function start_master {
# using ETCD or not?
USING_ETCD=`echo $1 | cut -d '=' -f2`
echo $MAIN_IP > /etc/mesos-master/ip
echo in_memory > /etc/mesos/registry
echo "zk://localhost:2181/mesos" > /etc/mesos/zk
echo -e "${normal}==> info: Starting Mesos master..."
/usr/bin/mesos-init-wrapper master > /dev/null 2>&1 &
# wait for the master to start
sleep 1 && while [[ -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$MASTER_PORT\" && \$1 ~ tcp") ]] ; do
echo -e "${normal}==> info: Waiting for Mesos master to come online..."
sleep 3;
done
echo -e "${normal}==> info: Mesos master started on port ${MASTER_PORT}"
# When not using ETCD for Master discovery, this is the end of the script.
if [ ${USING_ETCD} = false ]; then
# while the Master runs, keep the Docker container running
while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$MASTER_PORT\" && \$1 ~ tcp") ]] ; do
echo -e "${normal}==> info: `date` - Mesos master is running on port ${MASTER_PORT}"
sleep 10
done
fi
}
function start_marathon {
MASTER_MARATHON=`echo $1 | cut -d '=' -f2`
# using ETCD or not?
USING_ETCD=`echo $2 | cut -d '=' -f2`
echo $MASTER_MARATHON > /etc/mesos/master
echo $MASTER_MARATHON > /etc/mesos/zk
if [ ! -d /etc/marathon/conf ]; then
mkdir -p /etc/marathon/conf
fi
echo "http_callback" > /etc/marathon/conf/event_subscriber
service marathon start > /dev/null 2>&1 &
# while marathon runs, keep the Docker container running
while [[ ! -z $(ps -ef | grep marathon | grep -v grep) ]] ; do
echo -e "${normal}==> info: `date` - Marathon with master ${MASTER_MARATHON} is running"
sleep 10
done
}
function set_deimos {
# Set the Deimos configuration Dockerfile
echo "[docker]" > /etc/deimos.cfg
echo "[log]" >> /etc/deimos.cfg
echo "console: INFO" >> /etc/deimos.cfg
}
function print_usage {
echo "not implemented yet"
}
function print_auto_mode {
echo -e "${normal}==> info: No flags or parameters were given, starting auto discovery..."
}
# Catch the command line options.
case "$1" in
marathon)
start_marathon $2;;
master)
start_zookeeper && start_master --etcd=false;;
slave)
start_slave $2 --etcd=false;;
help)
print_usage;;
*)
print_auto_mode
esac
#
# ETCD POLLING
#
echo -e "${normal}==> info: Connecting to ETCD..."
# wait for etcd to be available
until curl -L http://${ETCD}/v2/keys/ > /dev/null 2>&1; do
echo -e "${normal}==> info: Waiting for etcd at $ETCD..."
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
if [[ "$retry" -gt ${MAX_RETRIES_CONNECT} ]]; then
echo -e "==> error: Exceed maximum of ${MAX_RETRIES_CONNECT}...exiting"
exit 1
fi
((retry++))
done
# wait until etcd has discarded potentially stale values
sleep $(($ETCD_TTL/2))
echo -e "${normal}==> info: Connected to ETCD at $ETCD"
# Try to determine if there already is a master. This should return a valid public IP number.
export MASTER_IP=`curl -Ls ${ETCD}/v2/keys/mesos/master | \
sed -n 's/^.*"value":"\([0-9]\)/1/p' | \
sed -n 's/[{}]//g;p' | \
cut -d '"' -f1`
# Here comes the decision tree...
# 1. If we find an active master through ETCD, connect to it using zookeeper
# 2. If no master is found, just start a slave. Not much use...but hey....
if [[ ! -z ${MASTER_IP} ]]; then
start_slave --master=zk://${MASTER_IP}:2181/mesos --etcd=true
# Create a master and slave and register the master's zookeeper ID in ETCD.
# Then start a Marathon instance.
else
echo -e "${bold}==> info: Found no master: will start a new master and slave"
start_zookeeper
start_master --etcd=true
start_slave --master=zk://localhost:2181/mesos --etcd=true
# start Marathon
echo -e "${bold}==> info: Starting Marathon in a separate container..."
docker run --rm --name marathon -p 8080:8080 tnolet/mesos-on-coreos:1.0 marathon --master=zk://${MAIN_IP}:2181/mesos &
# While the master is running, keep publishing its IP to ETCD
while [[ ! -z $(netstat -lnt | awk "\$6 == \"LISTEN\" && \$4 ~ \".$MASTER_PORT\" && \$1 ~ tcp") ]] ; do
curl -L http://${ETCD}/v2/keys${ETCD_PATH}/master -XPUT -d value=${MAIN_IP} -d ttl=${ETCD_TTL} >/dev/null 2>&1
sleep $(($ETCD_TTL/2)) # sleep for half the TTL
done
exit 1
fi
wait