c0f067a4c8bc5bfe51f20999238f13c455490ef6
[osm/devops.git] / installers / install_kubeadm_cluster.sh
1 #!/bin/bash
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15
16 set +eux
17
18 #installs kubernetes packages
19 function install_kube() {
20 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
21 K8S_VERSION=1.20.11-00
22 # To check other available versions, run the following command
23 # curl -s https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages | grep Version | awk '{print $2}'
24 sudo apt-get update && sudo apt-get install -y apt-transport-https
25 sudo apt-get update && sudo apt-get install -y apt-transport-https
26 curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
27 sudo add-apt-repository "deb https://apt.kubernetes.io/ kubernetes-xenial main"
28 sudo apt-get update
29 echo "Installing Kubernetes Packages ..."
30 sudo apt-get install -y kubelet=${K8S_VERSION} kubeadm=${K8S_VERSION} kubectl=${K8S_VERSION}
31 sudo apt-mark hold kubelet kubeadm kubectl
32 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
33 }
34
35 #initializes kubernetes control plane
36 function init_kubeadm() {
37 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
38 sudo swapoff -a
39 sudo sed -i.bak '/.*none.*swap/s/^\(.*\)$/#\1/g' /etc/fstab
40 sudo kubeadm init --config $1
41 sleep 5
42 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
43 }
44
45 function kube_config_dir() {
46 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
47 K8S_MANIFEST_DIR="/etc/kubernetes/manifests"
48 [ ! -d $K8S_MANIFEST_DIR ] && FATAL "Cannot Install Kubernetes"
49 mkdir -p $HOME/.kube
50 sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
51 sudo chown $(id -u):$(id -g) $HOME/.kube/config
52 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
53 }
54
55 #deploys flannel as daemonsets
56 function deploy_cni_provider() {
57 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
58 CNI_DIR="$(mktemp -d -q --tmpdir "flannel.XXXXXX")"
59 trap 'rm -rf "${CNI_DIR}"' EXIT
60 wget -q https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml -P $CNI_DIR
61 kubectl apply -f $CNI_DIR
62 [ $? -ne 0 ] && FATAL "Cannot Install Flannel"
63 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
64 }
65
66 #taints K8s master node
67 function taint_master_node() {
68 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
69 K8S_MASTER=$(kubectl get nodes | awk '$3~/master/'| awk '{print $1}')
70 kubectl taint node $K8S_MASTER node-role.kubernetes.io/master:NoSchedule-
71 sleep 5
72 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
73 }
74
75 #Install Helm v3
76 function install_helm() {
77 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
78 helm > /dev/null 2>&1
79 if [ $? != 0 ] ; then
80 # Helm is not installed. Install helm
81 echo "Helm is not installed, installing ..."
82 curl https://get.helm.sh/helm-v3.6.3-linux-amd64.tar.gz --output helm-v3.6.3.tar.gz
83 tar -zxvf helm-v3.6.3.tar.gz
84 sudo mv linux-amd64/helm /usr/local/bin/helm
85 rm -r linux-amd64
86 rm helm-v3.6.3.tar.gz
87 helm repo add stable https://charts.helm.sh/stable
88 helm repo update
89 fi
90 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
91 }
92
93 function install_k8s_storageclass() {
94 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
95 OPENEBS_VERSION="1.12.0"
96 echo "Installing OpenEBS"
97 helm repo add openebs https://openebs.github.io/charts
98 helm repo update
99 helm install --create-namespace --namespace openebs openebs openebs/openebs --version ${OPENEBS_VERSION}
100 helm ls -n openebs
101 local storageclass_timeout=400
102 local counter=0
103 local storageclass_ready=""
104 echo "Waiting for storageclass"
105 while (( counter < storageclass_timeout ))
106 do
107 kubectl get storageclass openebs-hostpath &> /dev/null
108
109 if [ $? -eq 0 ] ; then
110 echo "Storageclass available"
111 storageclass_ready="y"
112 break
113 else
114 counter=$((counter + 15))
115 sleep 15
116 fi
117 done
118 [ -n "$storageclass_ready" ] || FATAL "Storageclass not ready after $storageclass_timeout seconds. Cannot install openebs"
119 kubectl patch storageclass openebs-hostpath -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
120 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
121 }
122
123 #installs metallb from helm
124 function install_helm_metallb() {
125 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
126 echo "Installing MetalLB"
127 METALLB_VERSION="0.11.0"
128 METALLB_IP_RANGE="$DEFAULT_IP/32"
129 echo "configInline:
130 address-pools:
131 - name: default
132 protocol: layer2
133 addresses:
134 - $METALLB_IP_RANGE" | sudo tee -a ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
135 helm repo add metallb https://metallb.github.io/metallb
136 helm repo update
137 helm install --create-namespace --namespace metallb-system metallb metallb/metallb --version ${METALLB_VERSION} -f ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
138 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
139 }
140
141 #checks openebs and metallb readiness
142 function check_for_readiness() {
143 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
144 # Default input values
145 sampling_period=2 # seconds
146 time_for_readiness=20 # seconds ready
147 time_for_failure=200 # seconds broken
148 OPENEBS_NAMESPACE=openebs
149 METALLB_NAMESPACE=metallb-system
150 # STACK_NAME=osm # By default, "osm"
151
152 # Equivalent number of samples
153 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
154 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
155 failures_in_a_row=0
156 oks_in_a_row=0
157
158 ####################################################################################
159 # Loop to check system readiness
160 ####################################################################################
161 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
162 do
163 # State of OpenEBS
164 OPENEBS_STATE=$(kubectl get pod -n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
165 OPENEBS_READY=$(echo "${OPENEBS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
166 OPENEBS_NOT_READY=$(echo "${OPENEBS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
167 COUNT_OPENEBS_READY=$(echo "${OPENEBS_READY}"| grep -v -e '^$' | wc -l)
168 COUNT_OPENEBS_NOT_READY=$(echo "${OPENEBS_NOT_READY}" | grep -v -e '^$' | wc -l)
169
170 # State of MetalLB
171 METALLB_STATE=$(kubectl get pod -n ${METALLB_NAMESPACE} --no-headers 2>&1)
172 METALLB_READY=$(echo "${METALLB_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
173 METALLB_NOT_READY=$(echo "${METALLB_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
174 COUNT_METALLB_READY=$(echo "${METALLB_READY}" | grep -v -e '^$' | wc -l)
175 COUNT_METALLB_NOT_READY=$(echo "${METALLB_NOT_READY}" | grep -v -e '^$' | wc -l)
176
177 # OK sample
178 if [[ $((${COUNT_OPENEBS_NOT_READY}+${COUNT_METALLB_NOT_READY})) -eq 0 ]]
179 then
180 ((++oks_in_a_row))
181 failures_in_a_row=0
182 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
183 # NOK sample
184 else
185 ((++failures_in_a_row))
186 oks_in_a_row=0
187 echo
188 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
189
190 # Reports failed pods in OpenEBS
191 if [[ "${COUNT_OPENEBS_NOT_READY}" -ne 0 ]]
192 then
193 echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
194 echo "${OPENEBS_NOT_READY}"
195 echo
196 fi
197
198 # Reports failed statefulsets
199 if [[ "${COUNT_METALLB_NOT_READY}" -ne 0 ]]
200 then
201 echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
202 echo "${METALLB_NOT_READY}"
203 echo
204 fi
205 fi
206
207 #------------ NEXT SAMPLE
208 sleep ${sampling_period}
209 done
210
211 ####################################################################################
212 # OUTCOME
213 ####################################################################################
214 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
215 then
216 echo
217 FATAL "K8S CLUSTER IS BROKEN"
218 else
219 echo
220 echo "K8S CLUSTER IS READY"
221 fi
222 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
223 }
224
225 #removes osm deployments and services
226 function remove_k8s_namespace() {
227 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
228 kubectl delete ns $1
229 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
230 }
231
232 # main
233 while getopts ":D:d:i:-: " o; do
234 case "${o}" in
235 i)
236 DEFAULT_IP="${OPTARG}"
237 ;;
238 d)
239 OSM_DOCKER_WORK_DIR="${OPTARG}"
240 ;;
241 D)
242 OSM_DEVOPS="${OPTARG}"
243 ;;
244 -)
245 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
246 echo -e "Invalid option: '--$OPTARG'\n" >&2
247 exit 1
248 ;;
249 :)
250 echo "Option -$OPTARG requires an argument" >&2
251 exit 1
252 ;;
253 \?)
254 echo -e "Invalid option: '-$OPTARG'\n" >&2
255 exit 1
256 ;;
257 *)
258 exit 1
259 ;;
260 esac
261 done
262
263 source $OSM_DEVOPS/common/logging
264 source $OSM_DEVOPS/common/track
265
266 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
267 echo "DEFAULT_IP=$DEFAULT_IP"
268 echo "OSM_DEVOPS=$OSM_DEVOPS"
269 echo "OSM_DOCKER_WORK_DIR=$OSM_DOCKER_WORK_DIR"
270 echo "INSTALL_K8S_MONITOR=$INSTALL_K8S_MONITOR"
271 echo "HOME=$HOME"
272
273
274 install_kube
275 track k8scluster install_k8s_ok
276 init_kubeadm $OSM_DOCKER_WORK_DIR/cluster-config.yaml
277 kube_config_dir
278 track k8scluster init_k8s_ok
279 if [ -n "$INSTALL_K8S_MONITOR" ]; then
280 # uninstall OSM MONITORING
281 uninstall_k8s_monitoring
282 track k8scluster uninstall_k8s_monitoring_ok
283 fi
284 #remove old namespace
285 remove_k8s_namespace osm
286 deploy_cni_provider
287 taint_master_node
288 install_helm
289 track k8scluster install_helm_ok
290 install_k8s_storageclass
291 track k8scluster k8s_storageclass_ok
292 install_helm_metallb
293 track k8scluster k8s_metallb_ok
294 check_for_readiness
295 track k8scluster k8s_ready_ok
296