forked from xakraz/kubeadm-installer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
entrypoint.sh
executable file
·310 lines (262 loc) · 8.55 KB
/
entrypoint.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/env bash
# Params
# --
distro=${1}
action=${2:-install}
# Variables
# --
#
# These values can be overridden by ENV Vars
#
K8S_REPO="kubernetes/kubernetes"
# Example
#K8S_VERSION=${K8S_VERSION:-v1.6.1}
K8S_VERSION=${K8S_VERSION:-}
# Example
#KUBEADM_RELEASE=${KUBEADM_RELEASE:-v1.6.0-alpha.0.2074+a092d8e0f95f52}
KUBEADM_RELEASE=${KUBEADM_RELEASE:-}
# Example
#CNI_RELEASE=${CNI_RELEASE:-07a8a28637e97b22eb8dfe710eeae1344f69d16e}
CNI_RELEASE=${CNI_RELEASE:-}
CNI_REPO="containernetworking/cni"
CNI_PLUGINS_REPO="containernetworking/plugins"
# Installer variables
ARCH=${ARCH:-amd64}
CNI_BIN_DIR=${CNI_BIN_DIR:-/opt/cni}
ROOTFS=${ROOTFS:-/rootfs}
# Helpers
# --
fail()
{
echo "$*"
exit 1
}
usage()
{
cat <<-EOF
Hi, you should run this container like this:
docker run -it \
-v /etc/:/rootfs/etc \
-v /usr:/rootfs/usr \
-v /opt:/rootfs/opt \
xakra/kubeadm-installer your_os_here
your_os_here can be coreos, ubuntu, debian, fedora or centos
You can also revert this action with running:
docker run -it \
-v /etc/:/rootfs/etc \
-v /usr:/rootfs/usr \
-v /opt:/rootfs/opt \
xakra/kubeadm-installer your_os_here uninstall
EOF
}
get_git_latest_release() {
local url="https://api.github.com/repos/${1}/releases/latest"
curl --silent ${url} | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/' || \
fail "Failed to get latest release from ${url}"
}
print_systemd() {
echo $FUNCNAME $@
local action=$1
[[ install == "$action" ]] \
&& cat <<- EOF
Done! Now run this in your terminal to enable docker and kubelet:
systemctl daemon-reload
systemctl enable docker kubelet
systemctl restart docker kubelet
EOF
[[ uninstall == "$action" ]] \
&& cat <<- EOF
Done! Now run this in your terminal to complete the uninstall:
systemctl daemon-reload
systemctl disable kubelet
EOF
exit 0
}
# Handle no values for Versions
# --
# If no values are provided, get the latest release of k8s
if [[ -z "${K8S_VERSION}" ]]; then
echo "[ENV] 'K8S_VERSION' is empty -> Getting latest release version"
K8S_VERSION=$(get_git_latest_release $K8S_REPO)
fi
echo "[ENV] 'K8S_VERSION' = ${K8S_VERSION}"
# If no values are provided, use the same than k8s
if [[ -z "${KUBEADM_RELEASE}" ]]; then
echo "[ENV] 'KUBEADM_RELEASE' is empty -> Getting same version as k8s"
KUBEADM_RELEASE=${K8S_VERSION}
fi
echo "[ENV] 'KUBEADM_RELEASE' = ${KUBEADM_RELEASE}"
# If no values are provided, get the latest release of CNI
if [[ -z "${CNI_RELEASE}" ]]; then
echo "[ENV] 'CNI_RELEASE' is empty -> Getting latest release version"
CNI_RELEASE=$(get_git_latest_release $CNI_REPO)
fi
echo "[ENV] 'CNI_RELEASE' = ${CNI_RELEASE}"
# Kubernetes Google Cloud Storage
# --
#
# Handling usage of Dev / Final release buckets
#
K8S_URL=${K8S_URL:-https://storage.googleapis.com/kubernetes-release/release}
echo "[Kubeadm] Selected version: ${KUBEADM_RELEASE}"
if [[ -z "${KUBEADM_URL}" ]]; then
if [[ -n "${KUBEADM_VERSION/v[0-9].[0-9].[0-9]/}" ]]; then
echo "[Kubeadm] KUBEADM_RELEASE ${KUBEADM_RELEASE} is a Pre-release version"
KUBEADM_URL=https://storage.googleapis.com/kubernetes-release-dev/ci-cross
else
echo "[Kubeadm] KUBEADM_RELEASE ${KUBEADM_RELEASE} is a Release version"
KUBEADM_URL=https://storage.googleapis.com/kubernetes-release/release
fi
fi
echo "[Kubeadm] Download url: ${KUBEADM_URL}"
# To find out if the CNI_RELEASE is available on Google Cloud Storage:
# `$ docker run --rm -ti google/cloud-sdk gsutil ls gs://kubernetes-release/network-plugins`
#
echo "[CNI] Selected version: ${CNI_RELEASE}"
if [[ -z "${CNI_URL}" ]]; then
if [[ -n "${CNI_RELEASE/v[0-9].[0-9].[0-9]/}" ]]; then
echo "[CNI] CNI_RELEASE ${CNI_RELEASE} is a Pre-release version"
CNI_URL=https://storage.googleapis.com/kubernetes-release/network-plugins/cni-${ARCH}-${CNI_RELEASE}.tar.gz
else
echo "[CNI] CNI_RELEASE ${CNI_RELEASE} is a Release version"
CNI_URL=https://github.com/${CNI_REPO}/releases/download/${CNI_RELEASE}/cni-${ARCH}-${CNI_RELEASE}.tgz
if [[ -n "${CNI_RELEASE/v0.[0-5].[0-9]/}" ]]; then
CNI_PLUGINS_URL=https://github.com/${CNI_PLUGINS_REPO}/releases/download/${CNI_RELEASE}/cni-plugins-${ARCH}-${CNI_RELEASE}.tgz
fi
fi
fi
echo "[CNI] Download url: ${CNI_URL}"
if [[ -n "${CNI_PLUGINS_URL}" ]]; then
echo "[CNI] Plugins download url: ${CNI_PLUGINS_URL}"
fi
# Linux Distro
# --
#
# Environment settings according to Distro
#
case ${distro} in
coreos)
BIN_DIR=${BIN_DIR:-/opt/bin}
KUBELET_EXEC=${KUBELET_EXEC:-/usr/lib/coreos/kubelet-wrapper}
EXTRA_ENVIRONMENT=${EXTRA_ENVIRONMENT:-"RKT_RUN_ARGS=\
--volume opt-cni,kind=host,source=/opt/cni \
--mount volume=opt-cni,target=/opt/cni \
--volume etc-cni,kind=host,source=/etc/cni \
--mount volume=etc-cni,target=/etc/cni"}
INSTALL_KUBELET=0
;;
ubuntu|debian|fedora|centos)
BIN_DIR=${BIN_DIR:-/usr/bin}
KUBELET_EXEC=${KUBELET_EXEC:-${BIN_DIR}/kubelet}
EXTRA_ENVIRONMENT=${EXTRA_ENVIRONMENT:-"NOOP=true"}
INSTALL_KUBELET=1
;;
*)
usage
exit 1
;;
esac
# Main
# --
set -o errexit
set -o nounset
if [[ ${action} == "uninstall" ]]; then
rm -rfv ${ROOTFS}/etc/cni \
${ROOTFS}/${BIN_DIR}/kubectl \
${ROOTFS}/${BIN_DIR}/kubelet \
${ROOTFS}/${BIN_DIR}/kubeadm \
${ROOTFS}/${CNI_BIN_DIR} \
${ROOTFS}/etc/systemd/system/kubelet.service
print_systemd uninstall
fi
# Install
#
mkdir -p ${ROOTFS}/etc/cni \
${ROOTFS}/${BIN_DIR}
if [[ ! -f ${ROOTFS}/${BIN_DIR}/kubectl ]]; then
echo "Downloading kubectl to ${BIN_DIR}/kubectl"
if curl -sSL ${K8S_URL}/${K8S_VERSION}/bin/linux/${ARCH}/kubectl > ${ROOTFS}/${BIN_DIR}/kubectl; then
chmod +x ${ROOTFS}/${BIN_DIR}/kubectl
echo "Installed kubectl in ${BIN_DIR}/kubectl"
else
rm -f ${ROOTFS}/${BIN_DIR}/kubectl
fail "Failed to download kubectl"
fi
else
echo "Ignoring ${BIN_DIR}/kubectl, since it seems to exist already"
fi
if [[ ${INSTALL_KUBELET} == 0 ]]; then
true;
elif [[ ! -f ${ROOTFS}/${BIN_DIR}/kubelet ]]; then
echo "Downloading kubelet to ${BIN_DIR}/kubelet"
if curl -sSL ${K8S_URL}/${K8S_VERSION}/bin/linux/${ARCH}/kubelet > ${ROOTFS}/${BIN_DIR}/kubelet; then
chmod +x ${ROOTFS}/${BIN_DIR}/kubelet
echo "Installed kubelet in ${BIN_DIR}/kubelet"
else
rm -f ${ROOTFS}/${BIN_DIR}/kubelet
fail "Failed to download kubelet"
fi
else
echo "Ignoring ${BIN_DIR}/kubelet, since it seems to exist already"
fi
if [[ ! -f ${ROOTFS}/${BIN_DIR}/kubeadm ]]; then
echo "Downloading kubeadm to ${BIN_DIR}/kubeadm"
if curl -sSL ${KUBEADM_URL}/${KUBEADM_RELEASE}/bin/linux/${ARCH}/kubeadm > ${ROOTFS}/${BIN_DIR}/kubeadm; then
chmod +x ${ROOTFS}/${BIN_DIR}/kubeadm
echo "Installed kubeadm in ${BIN_DIR}/kubeadm"
else
rm -f ${ROOTFS}/${BIN_DIR}/kubeadm
fail "Failed to download kubeadm"
fi
else
echo "Ignoring ${BIN_DIR}/kubeadm, since it seems to exist already"
fi
if [[ ! -d ${ROOTFS}/${CNI_BIN_DIR} ]]; then
mkdir -p ${ROOTFS}/${CNI_BIN_DIR}
echo "Downloading CNI binaries to ${CNI_BIN_DIR}"
if curl -sSL ${CNI_URL} | tar -xz -C ${ROOTFS}/${CNI_BIN_DIR}; then
echo "Installed CNI binaries in ${CNI_BIN_DIR}"
if [[ -n "${CNI_PLUGINS_URL}" ]]; then
echo "Downloading CNI plugins to ${CNI_BIN_DIR}"
if curl -sSL ${CNI_PLUGINS_URL} | tar -xz -C ${ROOTFS}/${CNI_BIN_DIR}; then
echo "Installed CNI plugins in ${CNI_BIN_DIR}"
else
rm -rf ${ROOTFS}/${CNI_BIN_DIR}
fail "Failed to download CNI plugins"
fi
fi
else
rm -rf ${ROOTFS}/${CNI_BIN_DIR}
fail "Failed to download CNI binaries"
fi
else
echo "Ignoring ${CNI_BIN_DIR}, since it seems to exist already"
fi
if [[ ! -f ${ROOTFS}/etc/systemd/system/kubelet.service ]]; then
mkdir -p -v ${ROOTFS}/etc/systemd/system/
cat > ${ROOTFS}/etc/systemd/system/kubelet.service <<-EOF
[Unit]
Description=kubelet: The Kubernetes Node Agent
Documentation=http://kubernetes.io/docs/
[Service]
Environment="KUBELET_VERSION=${K8S_VERSION}_coreos.0"
Environment="KUBELET_IMAGE_TAG=${K8S_VERSION}_coreos.0"
Environment="${EXTRA_ENVIRONMENT}"
ExecStart=${KUBELET_EXEC} --kubeconfig=/etc/kubernetes/kubelet.conf \
--require-kubeconfig=true \
--pod-manifest-path=/etc/kubernetes/manifests \
--allow-privileged=true \
--network-plugin=cni \
--cni-conf-dir=/etc/cni/net.d --cni-bin-dir=${CNI_BIN_DIR} \
--cluster-dns=10.96.0.10 --cluster-domain=cluster.local
Restart=always
StartLimitInterval=0
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF
echo "Installed the kubelet.service in /etc/systemd/system/kubelet.service"
else
echo "Ignoring /etc/systemd/system/kubelet.service, since it seems to exist already"
fi
print_systemd install