blob: f1873bcf3375eff75e9059aa2221e8f3c5530e3f [file] [log] [blame]
czichycd6dd982022-05-23 12:05:07 +03001#!/bin/bash -x
2#
3################################################################################
4# Copyright (c) 2019 AT&T Intellectual Property. #
5# Copyright (c) 2022 Nokia. #
6# #
7# Licensed under the Apache License, Version 2.0 (the "License"); #
8# you may not use this file except in compliance with the License. #
9# You may obtain a copy of the License at #
10# #
11# http://www.apache.org/licenses/LICENSE-2.0 #
12# #
13# Unless required by applicable law or agreed to in writing, software #
14# distributed under the License is distributed on an "AS IS" BASIS, #
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
16# See the License for the specific language governing permissions and #
17# limitations under the License. #
18################################################################################
19
20
21usage() {
22 echo "Usage: $0 [ -k <k8s version> -d <docker version> -e <helm version> -c <cni-version>" 1>&2;
23
24 echo "k: kubernetes version" 1>&2;
25 echo "c: kubernetes CNI version" 1>&2;
26 echo "d: docker version" 1>&2;
27 echo "e: helm version" 1>&2;
28 exit 1;
29}
30
31
32wait_for_pods_running () {
33 NS="$2"
34 CMD="kubectl get pods --all-namespaces "
35 if [ "$NS" != "all-namespaces" ]; then
36 CMD="kubectl get pods -n $2 "
37 fi
38 KEYWORD="Running"
39 if [ "$#" == "3" ]; then
40 KEYWORD="${3}.*Running"
41 fi
42
43 CMD2="$CMD | grep \"$KEYWORD\" | wc -l"
44 NUMPODS=$(eval "$CMD2")
45 echo "waiting for $NUMPODS/$1 pods running in namespace [$NS] with keyword [$KEYWORD]"
46 while [ $NUMPODS -lt $1 ]; do
47 sleep 5
48 NUMPODS=$(eval "$CMD2")
49 echo "> waiting for $NUMPODS/$1 pods running in namespace [$NS] with keyword [$KEYWORD]"
50 done
51}
52
53
54start_ipv6_if () {
55 IPv6IF="$1"
56 if ifconfig -a $IPv6IF; then
57 echo "" >> /etc/network/interfaces.d/50-cloud-init.cfg
58 echo "allow-hotplug ${IPv6IF}" >> /etc/network/interfaces.d/50-cloud-init.cfg
59 echo "iface ${IPv6IF} inet6 auto" >> /etc/network/interfaces.d/50-cloud-init.cfg
60 ifconfig ${IPv6IF} up
61 fi
62}
63
64KUBEV="1.16.0"
65KUBECNIV="0.7.5"
66HELMV="3.5.4"
czichyd44f1bc2023-03-13 12:24:09 +020067DOCKERV="20.10.21"
czichycd6dd982022-05-23 12:05:07 +030068
69echo running ${0}
70while getopts ":k:d:e:n:c" o; do
71 case "${o}" in
72 e)
73 HELMV=${OPTARG}
74 ;;
75 d)
76 DOCKERV=${OPTARG}
77 ;;
78 k)
79 KUBEV=${OPTARG}
80 ;;
81 c)
82 KUBECNIV=${OPTARG}
83 ;;
84 *)
85 usage
86 ;;
87 esac
88done
89
90if [[ ${HELMV} == 2.* ]]; then
91 echo "helm 2 ("${HELMV}")not supported anymore"
92 exit -1
93fi
94
95set -x
96export DEBIAN_FRONTEND=noninteractive
97echo "$(hostname -I) $(hostname)" >> /etc/hosts
98printenv
99
100IPV6IF=""
101
102rm -rf /opt/config
103mkdir -p /opt/config
104echo "" > /opt/config/docker_version.txt
105echo "1.16.0" > /opt/config/k8s_version.txt
106echo "0.7.5" > /opt/config/k8s_cni_version.txt
107echo "3.5.4" > /opt/config/helm_version.txt
108echo "$(hostname -I)" > /opt/config/host_private_ip_addr.txt
109echo "$(curl ifconfig.co)" > /opt/config/k8s_mst_floating_ip_addr.txt
110echo "$(hostname -I)" > /opt/config/k8s_mst_private_ip_addr.txt
111echo "__mtu__" > /opt/config/mtu.txt
112echo "__cinder_volume_id__" > /opt/config/cinder_volume_id.txt
113echo "$(hostname)" > /opt/config/stack_name.txt
114
115ISAUX='false'
116if [[ $(cat /opt/config/stack_name.txt) == *aux* ]]; then
117 ISAUX='true'
118fi
119
120modprobe -- ip_vs
121modprobe -- ip_vs_rr
122modprobe -- ip_vs_wrr
123modprobe -- ip_vs_sh
124modprobe -- nf_conntrack_ipv4
125modprobe -- nf_conntrack_ipv6
126modprobe -- nf_conntrack_proto_sctp
127
128if [ ! -z "$IPV6IF" ]; then
129 start_ipv6_if $IPV6IF
130fi
131
132SWAPFILES=$(grep swap /etc/fstab | sed '/^[ \t]*#/ d' | sed 's/[\t ]/ /g' | tr -s " " | cut -f1 -d' ')
133if [ ! -z $SWAPFILES ]; then
134 for SWAPFILE in $SWAPFILES
135 do
136 if [ ! -z $SWAPFILE ]; then
137 echo "disabling swap file $SWAPFILE"
138 if [[ $SWAPFILE == UUID* ]]; then
139 UUID=$(echo $SWAPFILE | cut -f2 -d'=')
140 swapoff -U $UUID
141 else
142 swapoff $SWAPFILE
143 fi
144 sed -i "\%$SWAPFILE%d" /etc/fstab
145 fi
146 done
147fi
148
149
150echo "### Docker version = "${DOCKERV}
151echo "### k8s version = "${KUBEV}
152echo "### helm version = "${HELMV}
153echo "### k8s cni version = "${KUBECNIV}
154
155KUBEVERSION="${KUBEV}-00"
156CNIVERSION="${KUBECNIV}-00"
157DOCKERVERSION="${DOCKERV}"
158
159UBUNTU_RELEASE=$(lsb_release -r | sed 's/^[a-zA-Z:\t ]\+//g')
160if [[ ${UBUNTU_RELEASE} == 16.* ]]; then
161 echo "Installing on Ubuntu $UBUNTU_RELEASE (Xenial Xerus) host"
162 if [ ! -z "${DOCKERV}" ]; then
163 DOCKERVERSION="${DOCKERV}-0ubuntu1~16.04.5"
164 fi
165elif [[ ${UBUNTU_RELEASE} == 18.* ]]; then
166 echo "Installing on Ubuntu $UBUNTU_RELEASE (Bionic Beaver)"
167 if [ ! -z "${DOCKERV}" ]; then
168 DOCKERVERSION="${DOCKERV}-0ubuntu1~18.04.4"
169 fi
170elif [[ ${UBUNTU_RELEASE} == 20.* ]]; then
171 echo "Installing on Ubuntu $UBUNTU_RELEASE (Focal Fossal)"
172 if [ ! -z "${DOCKERV}" ]; then
czichyde828542023-05-11 13:50:59 +0300173 DOCKERVERSION="${DOCKERV}-0ubuntu1~20.04.2" # 20.10.21-0ubuntu1~20.04.2
czichycd6dd982022-05-23 12:05:07 +0300174 fi
175else
176 echo "Unsupported Ubuntu release ($UBUNTU_RELEASE) detected. Exit."
177fi
178
179echo "docker version to use = "${DOCKERVERSION}
180
181curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
182echo 'deb http://apt.kubernetes.io/ kubernetes-xenial main' > /etc/apt/sources.list.d/kubernetes.list
183
184mkdir -p /etc/apt/apt.conf.d
185echo "APT::Acquire::Retries \"3\";" > /etc/apt/apt.conf.d/80-retries
186
187apt-get update
188RES=$(apt-get install -y curl jq netcat make ipset moreutils 2>&1)
189if [[ $RES == */var/lib/dpkg/lock* ]]; then
190 echo "Fail to get dpkg lock. Wait for any other package installation"
191 echo "process to finish, then rerun this script"
192 exit -1
193fi
194
195APTOPTS="--allow-downgrades --allow-change-held-packages --allow-unauthenticated --ignore-hold "
196
197for PKG in kubeadm docker.io; do
198 INSTALLED_VERSION=$(dpkg --list |grep ${PKG} |tr -s " " |cut -f3 -d ' ')
199 if [ ! -z ${INSTALLED_VERSION} ]; then
200 if [ "${PKG}" == "kubeadm" ]; then
201 kubeadm reset -f
202 rm -rf ~/.kube
203 apt-get -y $APTOPTS remove kubeadm kubelet kubectl kubernetes-cni
204 else
205 apt-get -y $APTOPTS remove "${PKG}"
206 fi
207 fi
208done
209apt-get -y autoremove
210
211if [ -z ${DOCKERVERSION} ]; then
212 apt-get install -y $APTOPTS docker.io
213else
214 apt-get install -y $APTOPTS docker.io=${DOCKERVERSION}
215fi
216cat > /etc/docker/daemon.json <<EOF
217{
218 "exec-opts": ["native.cgroupdriver=systemd"],
219 "log-driver": "json-file",
220 "log-opts": {
221 "max-size": "100m"
222 },
223 "storage-driver": "overlay2"
224}
225EOF
226mkdir -p /etc/systemd/system/docker.service.d
227systemctl enable docker.service
228systemctl daemon-reload
229systemctl restart docker
230
231if [ -z ${CNIVERSION} ]; then
232 apt-get install -y $APTOPTS kubernetes-cni
233else
234 apt-get install -y $APTOPTS kubernetes-cni=${CNIVERSION}
235fi
236
237if [ -z ${KUBEVERSION} ]; then
238 apt-get install -y $APTOPTS kubeadm kubelet kubectl
239else
240 apt-get install -y $APTOPTS kubeadm=${KUBEVERSION} kubelet=${KUBEVERSION} kubectl=${KUBEVERSION}
241fi
242
243apt-mark hold docker.io kubernetes-cni kubelet kubeadm kubectl
244
245
246kubeadm config images pull --kubernetes-version=${KUBEV}
247
248
249NODETYPE="master"
250if [ "$NODETYPE" == "master" ]; then
251
252 if [[ ${KUBEV} == 1.13.* ]]; then
253 cat <<EOF >/root/config.yaml
254apiVersion: kubeadm.k8s.io/v1alpha3
255kubernetesVersion: v${KUBEV}
256kind: ClusterConfiguration
257apiServerExtraArgs:
258 feature-gates: SCTPSupport=true
259networking:
260 dnsDomain: cluster.local
261 podSubnet: 10.244.0.0/16
262 serviceSubnet: 10.96.0.0/12
263---
264apiVersion: kubeproxy.config.k8s.io/v1alpha1
265kind: KubeProxyConfiguration
266mode: ipvs
267EOF
268
269 elif [[ ${KUBEV} == 1.14.* ]]; then
270 cat <<EOF >/root/config.yaml
271apiVersion: kubeadm.k8s.io/v1beta1
272kubernetesVersion: v${KUBEV}
273kind: ClusterConfiguration
274apiServerExtraArgs:
275 feature-gates: SCTPSupport=true
276networking:
277 dnsDomain: cluster.local
278 podSubnet: 10.244.0.0/16
279 serviceSubnet: 10.96.0.0/12
280---
281apiVersion: kubeproxy.config.k8s.io/v1alpha1
282kind: KubeProxyConfiguration
283mode: ipvs
284EOF
285 elif [[ ${KUBEV} == 1.15.* ]] || [[ ${KUBEV} == 1.16.* ]] || [[ ${KUBEV} == 1.18.* ]]; then
286 cat <<EOF >/root/config.yaml
287apiVersion: kubeadm.k8s.io/v1beta2
288kubernetesVersion: v${KUBEV}
289kind: ClusterConfiguration
290apiServer:
291 extraArgs:
292 feature-gates: SCTPSupport=true
293networking:
294 dnsDomain: cluster.local
295 podSubnet: 10.244.0.0/16
296 serviceSubnet: 10.96.0.0/12
297---
298apiVersion: kubeproxy.config.k8s.io/v1alpha1
299kind: KubeProxyConfiguration
300mode: ipvs
301EOF
302 else
303 echo "Unsupported Kubernetes version requested. Bail."
304 exit
305 fi
306
307 cat <<EOF > /root/rbac-config.yaml
308apiVersion: v1
309kind: ServiceAccount
310metadata:
311 name: tiller
312 namespace: kube-system
313---
314apiVersion: rbac.authorization.k8s.io/v1
315kind: ClusterRoleBinding
316metadata:
317 name: tiller
318roleRef:
319 apiGroup: rbac.authorization.k8s.io
320 kind: ClusterRole
321 name: cluster-admin
322subjects:
323 - kind: ServiceAccount
324 name: tiller
325 namespace: kube-system
326EOF
327
328
329 kubeadm init --config /root/config.yaml
330
331 cd /root
332 rm -rf .kube
333 mkdir -p .kube
334 cp -i /etc/kubernetes/admin.conf /root/.kube/config
335 chown root:root /root/.kube/config
336 export KUBECONFIG=/root/.kube/config
337 echo "KUBECONFIG=${KUBECONFIG}" >> /etc/environment
338
339 kubectl get pods --all-namespaces
340
czichy4e8ef192022-08-15 07:48:28 +0300341 # we refer to version 0.18.1 because later versions use namespace kube-flannel instead of kube-system TODO
342 kubectl apply -f "https://raw.githubusercontent.com/flannel-io/flannel/v0.18.1/Documentation/kube-flannel.yml"
czichycd6dd982022-05-23 12:05:07 +0300343
344 wait_for_pods_running 8 kube-system
345
346 kubectl taint nodes --all node-role.kubernetes.io/master-
347
348 HELMV=$(cat /opt/config/helm_version.txt)
349 HELMVERSION=${HELMV}
350 if [ ! -e helm-v${HELMVERSION}-linux-amd64.tar.gz ]; then
351 wget https://get.helm.sh/helm-v${HELMVERSION}-linux-amd64.tar.gz
352 fi
353 cd /root && rm -rf Helm && mkdir Helm && cd Helm
354 tar -xvf ../helm-v${HELMVERSION}-linux-amd64.tar.gz
355 mv linux-amd64/helm /usr/local/bin/helm
356
357 cd /root
358
359 rm -rf /root/.helm
360# if [[ ${KUBEV} == 1.16.* ]]; then
361# if [[ ${HELMVERSION} == 2.* ]]; then
362# helm init --service-account tiller --override spec.selector.matchLabels.'name'='tiller',spec.selector.matchLabels.'app'='helm' --output yaml > /tmp/helm-init.yaml
363# sed 's@apiVersion: extensions/v1beta1@apiVersion: apps/v1@' /tmp/helm-init.yaml > /tmp/helm-init-patched.yaml
364# kubectl apply -f /tmp/helm-init-patched.yaml
365# fi
366# else
367# if [[ ${HELMVERSION} == 2.* ]]; then
368# helm init --service-account tiller
369# fi
370# fi
371# if [[ ${HELMVERSION} == 2.* ]]; then
372# helm init -c
373# export HELM_HOME="$(pwd)/.helm"
374# echo "HELM_HOME=${HELM_HOME}" >> /etc/environment
375# fi
376
377 while ! helm version; do
378 echo "Waiting for Helm to be ready"
379 sleep 15
380 done
381
382 echo "Preparing a master node (lower ID) for using local FS for PV"
383 PV_NODE_NAME=$(kubectl get nodes |grep master | cut -f1 -d' ' | sort | head -1)
384 kubectl label --overwrite nodes $PV_NODE_NAME local-storage=enable
385 if [ "$PV_NODE_NAME" == "$(hostname)" ]; then
386 mkdir -p /opt/data/dashboard-data
387 fi
388
389 echo "Done with master node setup"
390fi
391
392
393if [[ ! -z "" && ! -z "" ]]; then
394 echo " " >> /etc/hosts
395fi
396if [[ ! -z "" && ! -z "" ]]; then
397 echo " " >> /etc/hosts
398fi
399if [[ ! -z "" && ! -z "helm.ricinfra.local" ]]; then
400 echo " helm.ricinfra.local" >> /etc/hosts
401fi
402
403if [[ "1" -gt "100" ]]; then
404 cat <<EOF >/etc/ca-certificates/update.d/helm.crt
405
406EOF
407fi
408
409if [[ "1" -gt "100" ]]; then
410 mkdir -p /etc/docker/certs.d/:
411 cat <<EOF >/etc/docker/ca.crt
412
413EOF
414 cp /etc/docker/ca.crt /etc/docker/certs.d/:/ca.crt
415
416 service docker restart
417 systemctl enable docker.service
418 docker login -u -p :
419 docker pull :/whoami:0.0.1
420fi