-
Notifications
You must be signed in to change notification settings - Fork 3
/
worker.sh
executable file
·280 lines (244 loc) · 7.17 KB
/
worker.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
#!/bin/bash
# Copyright 2015 The Kubernetes Authors All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# A script to the k8s worker in docker containers.
# Authors @wizard_cxy @resouer
set -e
START_MODE=$1
init() {
# Make sure docker daemon is running
if ( ! ps -ef | grep "/usr/bin/docker" | grep -v 'grep' &> /dev/null ); then
echo "Docker is not running on this machine!"
exit 1
fi
# Run as root
if [ "$(id -u)" != "0" ]; then
echo >&2 "Please run as root"
exit 1
fi
# Make sure master ip is properly set
if [ -z ${MASTER_IP} ]; then
echo "Please export MASTER_IP in your env"
exit 1
fi
# Make sure node ip is properly set
if [ -z ${NODE_IP} ]; then
NODE_IP=$(hostname -I | awk '{print $1}')
fi
# Set working mode
if [[ -n ${START_MODE} && ${START_MODE} == "install" ]]; then
echo "Installing k8s cluster as a service on host."
RUN_MODE="service"
RESTART_POLICY="always"
else
echo "Starting k8s cluster in run mode. Will not restart on reboot."
RUN_MODE="hyperkube"
RESTART_POLICY="no"
fi
# Make sure k8s images are properly set
HYPERKUBE_IMAGE=${HYPERKUBE_IMAGE:-fest/hyperkube-amd64:latest}
PAUSE_IMAGE=${PAUSE_IMAGE:-gcr.io/google_containers/pause:2.0}
FLANNEL_IMAGE=${FLANNEL_IMAGE:-quay.io/coreos/flannel:0.5.5}
# Add docker registry as prefix for k8s images.
if [[ -n ${DOCKER_REGISTRY_PREFIX} ]]; then
HYPERKUBE_IMAGE=${DOCKER_REGISTRY_PREFIX}/${HYPERKUBE_IMAGE}
PAUSE_IMAGE=${DOCKER_REGISTRY_PREFIX}/${PAUSE_IMAGE}
FLANNEL_IMAGE=${DOCKER_REGISTRY_PREFIX}/${FLANNEL_IMAGE}
fi
# Make sure k8s version env is properly set
DEFAULT_NET_INTERFACE=$(ip -o -4 route show to default | awk '{print $5}')
FLANNEL_IFACE=${NET_INTERFACE:-${DEFAULT_NET_INTERFACE}}
FLANNEL_IPMASQ=${FLANNEL_IPMASQ:-"true"}
ARCH=${ARCH:-"amd64"}
# Paths
FLANNEL_SUBNET_DIR=${FLANNEL_SUBNET_DIR:-/run/flannel}
# Constants
TIMEOUT_FOR_SERVICES=20
# Init variable
DOCKER_CONF=""
lsb_dist=""
detect_lsb
echo "FLANNEL_IFACE is set to: ${FLANNEL_IFACE}"
echo "FLANNEL_IPMASQ is set to: ${FLANNEL_IPMASQ}"
echo "MASTER_IP is set to: ${MASTER_IP}"
echo "NODE_IP is set to: ${NODE_IP}"
echo "ARCH is set to: ${ARCH}"
echo "OS distribution is set to: ${lsb_dist}"
}
# Check if a command is valid
command_exists() {
command -v "$@" > /dev/null 2>&1
}
# Detect the OS distro, we support ubuntu, debian, mint, centos, fedora dist
detect_lsb() {
case "$(uname -m)" in
*64)
;;
*)
echo "Error: We currently only support 64-bit platforms."
exit 1
;;
esac
if command_exists lsb_release; then
lsb_dist="$(lsb_release -si)"
fi
if [ -z ${lsb_dist} ] && [ -r /etc/lsb-release ]; then
lsb_dist="$(. /etc/lsb-release && echo "$DISTRIB_ID")"
fi
if [ -z ${lsb_dist} ] && [ -r /etc/debian_version ]; then
lsb_dist='debian'
fi
if [ -z ${lsb_dist} ] && [ -r /etc/fedora-release ]; then
lsb_dist='fedora'
fi
if [ -z ${lsb_dist} ] && [ -r /etc/os-release ]; then
lsb_dist="$(. /etc/os-release && echo "$ID")"
fi
lsb_dist="$(echo ${lsb_dist} | tr '[:upper:]' '[:lower:]')"
case "${lsb_dist}" in
amzn|centos|debian|ubuntu)
;;
*)
echo "Error: We currently only support ubuntu|debian|amzn|centos."
exit 1
;;
esac
}
configure_docker(){
# Configure docker settings, then restart it
case "${lsb_dist}" in
amzn)
DOCKER_CONF="/etc/sysconfig/docker"
sed -i.bak 's/^\(MountFlags=\).*/\1shared/' $DOCKER_CONF
service docker restart
;;
centos)
DOCKER_CONF="/usr/lib/systemd/system/docker.service"
sed -i.bak 's/^\(MountFlags=\).*/\1shared/' ${DOCKER_CONF}
# Add support for docker registry
if [[ -n ${DOCKER_REGISTRY_URL} ]]; then
sed -i "/^ExecStart=/ s~$~ --insecure-registry=${DOCKER_REGISTRY_URL}~" ${DOCKER_CONF}
fi
systemctl daemon-reload
systemctl restart docker
;;
ubuntu|debian)
if command_exists systemctl; then
DOCKER_CONF=$(systemctl cat docker | head -1 | awk '{print $2}')
sed -i.bak 's/^\(MountFlags=\).*/\1shared/' $DOCKER_CONF
systemctl daemon-reload
systemctl restart docker
else
DOCKER_CONF="/etc/default/docker"
sed -i.bak 's/^\(MountFlags=\).*/\1shared/' $DOCKER_CONF
service docker stop
while [ `ps aux | grep /usr/bin/docker | grep -v grep | wc -l` -gt 0 ]; do
echo "Waiting for docker to terminate"
sleep 1
done
service docker start
fi
;;
*)
echo "Unsupported operations system ${lsb_dist}"
exit 1
;;
esac
}
setup_cluster_connectivity(){
# Make sure there is no subnet.env from previous run.
if [[ -f ${FLANNEL_SUBNET_DIR}/subnet.env ]]; then
rm ${FLANNEL_SUBNET_DIR}/subnet.env
fi
# Start flannel
docker run \
-d \
--restart=${RESTART_POLICY} \
--net=host \
--privileged \
-v /dev/net:/dev/net \
-v ${FLANNEL_SUBNET_DIR}:${FLANNEL_SUBNET_DIR} \
${FLANNEL_IMAGE} \
/opt/bin/flanneld \
--ip-masq="${FLANNEL_IPMASQ}" \
--etcd-endpoints=http://${MASTER_IP}:4001 \
--iface="${FLANNEL_IFACE}"
# Wait for the flannel subnet.env file to be created instead of a timeout. This is faster and more reliable
local SECONDS=0
while [[ ! -f ${FLANNEL_SUBNET_DIR}/subnet.env ]]; do
if [[ ${SECONDS} == ${TIMEOUT_FOR_SERVICES} ]]; then
echo "flannel failed to start. Exiting..."
exit
fi
sleep 1
done
}
# Start k8s components in a hyperkube container.
start_k8s_as_hyperkube() {
mkdir -p /var/lib/kubelet
mount --bind /var/lib/kubelet /var/lib/kubelet
mount --make-shared /var/lib/kubelet
docker run \
--name=kubelet \
--volume=/:/rootfs:ro \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:rw \
--volume=/var/run:/var/run:rw \
--volume=/run:/run:rw \
--volume=/var/lib/kubelet:/var/lib/kubelet:shared \
--net=host \
--pid=host \
--privileged=true \
--restart=${RESTART_POLICY} \
-d \
${HYPERKUBE_IMAGE} \
/hyperkube kubelet \
--hostname-override=${NODE_IP} \
--address="0.0.0.0" \
--api-servers=http://${MASTER_IP}:8080 \
--cluster-dns=10.10.0.10 \
--cluster-domain=cluster.local \
--allow-privileged=true --v=2 \
--pod-infra-container-image=${PAUSE_IMAGE} \
--network-plugin=cni \
--network-plugin-dir=/etc/cni/net.d
}
# Install k8s as a systemd service directly on host.
start_k8s_as_service(){
echo "Installing k8s as a service ..."
docker run -d \
--volume=/:/hostfs:rw \
--net=host \
--pid=host \
--privileged \
${HYPERKUBE_IMAGE} \
/setup/install.sh minion ${MASTER_IP} ${NODE_IP} ${PAUSE_IMAGE}
sleep 3
echo "Running kubelet ..."
systemctl daemon-reload
systemctl start kubelet
systemctl enable kubelet
systemctl status kubelet
}
set -e
echo "Checking prerequisites & initialize variable ..."
init
echo "Configure docker ..."
configure_docker
sleep 5
echo "Setting up cluster connectivity ..."
setup_cluster_connectivity
echo "Starting k8s ..."
start_k8s_as_${RUN_MODE}
echo "Worker done!"