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