Update K8s version in install_kubeadm_cluster.sh to 1.28
[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 K8S_VERSION=1.28.2-00
19
20 # installs kubernetes packages
21 function install_kube() {
22 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
23 # Kubernetes releases can be found here: https://kubernetes.io/releases/
24 # To check other available versions, run the following command
25 # curl -s https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages | grep Version | awk '{print $2}'
26 sudo apt-get -y update && sudo apt-get install -y apt-transport-https ca-certificates curl
27 curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg
28 echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
29 sudo apt-get -y 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 # check and track kube packages installation
37 function check_and_track_kube_install() {
38 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
39 kubelet_version=$(dpkg -s kubelet|grep Version|awk '{print $2}')
40 [ -n "${kubelet_version}" ] || FATAL_TRACK k8scluster "Kubelet was not installed."
41 kubeadm_version=$(dpkg -s kubeadm|grep Version|awk '{print $2}')
42 [ -n "${kubeadm_version}" ] || FATAL_TRACK k8scluster "Kubeadm was not installed."
43 kubectl_version=$(dpkg -s kubectl|grep Version|awk '{print $2}')
44 [ -n "${kubectl_version}" ] || FATAL_TRACK k8scluster "Kubectl was not installed."
45 track k8scluster install_k8s_ok none none none kubelet ${kubelet_version} none none kubeadm ${kubeadm_version} none none kubectl ${kubectl_version} none none
46 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
47 }
48
49 # initializes kubernetes control plane
50 function init_kubeadm() {
51 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
52 sudo swapoff -a
53 sudo sed -i.bak '/.*none.*swap/s/^\(.*\)$/#\1/g' /etc/fstab
54 sudo kubeadm init --config $1 --dry-run || FATAL_TRACK k8scluster "kubeadm init dry-run failed"
55 sudo kubeadm init --config $1
56 sleep 5
57 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
58 }
59
60 # Initializes kubeconfig file
61 function kube_config_dir() {
62 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
63 K8S_MANIFEST_DIR="/etc/kubernetes/manifests"
64 [ ! -d $K8S_MANIFEST_DIR ] && FATAL_TRACK k8scluster "Kubernetes folder $K8S_MANIFEST_DIR was not found"
65 mkdir -p $HOME/.kube
66 sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
67 sudo chown $(id -u):$(id -g) $HOME/.kube/config
68 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
69 }
70
71 # test kubernetes installation
72 function check_and_track_init_k8s() {
73 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
74 echo "Reading existing namespaces"
75 kubectl get ns || FATAL_TRACK k8scluster "Failed getting namespaces"
76 track k8scluster init_k8s_ok
77 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
78 }
79
80 # deploys flannel as daemonsets
81 function deploy_cni_provider() {
82 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
83 CNI_DIR="$(mktemp -d -q --tmpdir "flannel.XXXXXX")"
84 trap 'rm -rf "${CNI_DIR}"' EXIT
85 KUBE_FLANNEL_FILE_URL="https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
86 wget --retry-on-host-error --retry-on-http-error 404,429,503 --tries=5 "${KUBE_FLANNEL_FILE_URL}" -P $CNI_DIR
87 [ ! -f $CNI_DIR/kube-flannel.yml ] && FATAL_TRACK k8scluster "Cannot Install Flannel because $CNI_DIR/kube-flannel.yml was not found. Maybe the file ${KUBE_FLANNEL_FILE_URL} is temporarily not accessible"
88 kubectl apply -f $CNI_DIR
89 [ $? -ne 0 ] && FATAL_TRACK k8scluster "Cannot Install Flannel"
90 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
91 }
92
93 # taints K8s master node
94 function taint_master_node() {
95 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
96 K8S_MASTER=$(kubectl get nodes | awk '$3~/control-plane/'| awk '{print $1; exit}')
97 kubectl taint node $K8S_MASTER node-role.kubernetes.io/control-plane:NoSchedule-
98 sleep 5
99 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
100 }
101
102 # check and track kube packages installation
103 function check_and_track_k8s_ready_before_helm() {
104 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
105 kubectl get events || FATAL_TRACK k8scluster "Failed getting events"
106 track k8scluster k8s_ready_before_helm
107 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
108 }
109
110 #Install Helm v3
111 #Helm releases can be found here: https://github.com/helm/helm/releases
112 function install_helm() {
113 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
114 HELM_VERSION="v3.11.3"
115 if ! [[ "$(helm version --short 2>/dev/null)" =~ ^v3.* ]]; then
116 # Helm is not installed. Install helm
117 echo "Helm3 is not installed, installing ..."
118 curl https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz --output helm-${HELM_VERSION}.tar.gz
119 tar -zxvf helm-${HELM_VERSION}.tar.gz
120 sudo mv linux-amd64/helm /usr/local/bin/helm
121 rm -r linux-amd64
122 rm helm-${HELM_VERSION}.tar.gz
123 else
124 echo "Helm3 is already installed. Skipping installation..."
125 fi
126 helm version || FATAL_TRACK k8scluster "Could not obtain helm version. Maybe helm client was not installed"
127 helm repo add stable https://charts.helm.sh/stable || FATAL_TRACK k8scluster "Helm repo stable could not be added"
128 helm repo update || FATAL_TRACK k8scluster "Helm repo stable could not be updated"
129 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
130 }
131
132 function install_k8s_storageclass() {
133 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
134 # Openebs versions can be found here: https://github.com/openebs/openebs/releases
135 OPENEBS_VERSION="3.7.0"
136 echo "Installing OpenEBS"
137 helm repo add openebs https://openebs.github.io/charts
138 helm repo update
139 helm install --create-namespace --namespace openebs openebs openebs/openebs --version ${OPENEBS_VERSION}
140 helm ls -n openebs
141 local storageclass_timeout=400
142 local counter=0
143 local storageclass_ready=""
144 echo "Waiting for storageclass"
145 while (( counter < storageclass_timeout ))
146 do
147 kubectl get storageclass openebs-hostpath &> /dev/null
148
149 if [ $? -eq 0 ] ; then
150 echo "Storageclass available"
151 storageclass_ready="y"
152 break
153 else
154 counter=$((counter + 15))
155 sleep 15
156 fi
157 done
158 [ -n "$storageclass_ready" ] || FATAL_TRACK k8scluster "Storageclass not ready after $storageclass_timeout seconds. Cannot install openebs"
159 kubectl patch storageclass openebs-hostpath -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
160 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
161 }
162
163 #installs metallb from helm
164 function install_helm_metallb() {
165 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
166 echo "Installing MetalLB"
167 METALLB_VERSION="0.13.10"
168 helm repo add metallb https://metallb.github.io/metallb
169 helm repo update
170 helm install --create-namespace --namespace metallb-system metallb metallb/metallb --version ${METALLB_VERSION}
171 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
172 }
173
174 function configure_ipaddresspool_metallb() {
175 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
176 echo "Creating IP address pool manifest: ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml"
177 METALLB_IP_RANGE="$DEFAULT_IP/32"
178 echo "apiVersion: metallb.io/v1beta1
179 kind: IPAddressPool
180 metadata:
181 name: first-pool
182 namespace: metallb-system
183 spec:
184 addresses:
185 - ${METALLB_IP_RANGE}" | sudo tee -a ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml
186 echo "Applying IP address pool manifest: kubectl apply -f ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml"
187 kubectl apply -f ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml || FATAL_TRACK k8scluster "Cannot create IP address Pool in MetalLB"
188 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
189 }
190
191 #installs cert-manager
192 function install_helm_certmanager() {
193 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
194 echo "Installing cert-manager"
195 CERTMANAGER_VERSION="v1.9.1"
196 helm repo add jetstack https://charts.jetstack.io
197 helm repo update
198 helm install cert-manager --create-namespace --namespace cert-manager jetstack/cert-manager \
199 --version ${CERTMANAGER_VERSION} --set installCRDs=true --set prometheus.enabled=false \
200 --set clusterResourceNamespace=osm \
201 --set extraArgs="{--enable-certificate-owner-ref=true}"
202 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
203 }
204
205 #checks openebs and metallb readiness
206 function check_for_readiness() {
207 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
208 # Default input values
209 sampling_period=2 # seconds
210 time_for_readiness=20 # seconds ready
211 time_for_failure=200 # seconds broken
212 OPENEBS_NAMESPACE=openebs
213 METALLB_NAMESPACE=metallb-system
214 # STACK_NAME=osm # By default, "osm"
215
216 # Equivalent number of samples
217 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
218 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
219 failures_in_a_row=0
220 oks_in_a_row=0
221
222 ####################################################################################
223 # Loop to check system readiness
224 ####################################################################################
225 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
226 do
227 # State of OpenEBS
228 OPENEBS_STATE=$(kubectl get pod -n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
229 OPENEBS_READY=$(echo "${OPENEBS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
230 OPENEBS_NOT_READY=$(echo "${OPENEBS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
231 COUNT_OPENEBS_READY=$(echo "${OPENEBS_READY}"| grep -v -e '^$' | wc -l)
232 COUNT_OPENEBS_NOT_READY=$(echo "${OPENEBS_NOT_READY}" | grep -v -e '^$' | wc -l)
233
234 # State of MetalLB
235 METALLB_STATE=$(kubectl get pod -n ${METALLB_NAMESPACE} --no-headers 2>&1)
236 METALLB_READY=$(echo "${METALLB_STATE}" | awk '$2=="1/1" || $2=="4/4" {printf ("%s\t%s\t\n", $1, $2)}')
237 METALLB_NOT_READY=$(echo "${METALLB_STATE}" | awk '$2!="1/1" && $2!="4/4" {printf ("%s\t%s\t\n", $1, $2)}')
238 COUNT_METALLB_READY=$(echo "${METALLB_READY}" | grep -v -e '^$' | wc -l)
239 COUNT_METALLB_NOT_READY=$(echo "${METALLB_NOT_READY}" | grep -v -e '^$' | wc -l)
240
241 # State of CertManager
242 CERTMANAGER_STATE=$(kubectl get pod -n ${CERTMANAGER_NAMESPACE} --no-headers 2>&1)
243 CERTMANAGER_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
244 CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
245 COUNT_CERTMANAGER_READY=$(echo "${CERTMANAGER_READY}" | grep -v -e '^$' | wc -l)
246 COUNT_CERTMANAGER_NOT_READY=$(echo "${CERTMANAGER_NOT_READY}" | grep -v -e '^$' | wc -l)
247
248 # OK sample
249 if [[ $((${COUNT_OPENEBS_NOT_READY}+${COUNT_METALLB_NOT_READY})) -eq 0 ]]
250 then
251 ((++oks_in_a_row))
252 failures_in_a_row=0
253 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
254 # NOK sample
255 else
256 ((++failures_in_a_row))
257 oks_in_a_row=0
258 echo
259 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
260
261 # Reports failed pods in OpenEBS
262 if [[ "${COUNT_OPENEBS_NOT_READY}" -ne 0 ]]
263 then
264 echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
265 echo "${OPENEBS_NOT_READY}"
266 echo
267 fi
268
269 # Reports failed pods in MetalLB
270 if [[ "${COUNT_METALLB_NOT_READY}" -ne 0 ]]
271 then
272 echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
273 echo "${METALLB_NOT_READY}"
274 echo
275 fi
276
277 # Reports failed pods in CertManager
278 if [[ "${COUNT_CERTMANAGER_NOT_READY}" -ne 0 ]]
279 then
280 echo "CertManager: Waiting for ${COUNT_CERTMANAGER_NOT_READY} of $((${COUNT_CERTMANAGER_NOT_READY}+${COUNT_CERTMANAGER_READY})) pods to be ready:"
281 echo "${CERTMANAGER_NOT_READY}"
282 echo
283 fi
284 fi
285
286 #------------ NEXT SAMPLE
287 sleep ${sampling_period}
288 done
289
290 ####################################################################################
291 # OUTCOME
292 ####################################################################################
293 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
294 then
295 echo
296 FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN"
297 else
298 echo
299 echo "K8S CLUSTER IS READY"
300 fi
301 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
302 }
303
304 #removes osm deployments and services
305 function remove_k8s_namespace() {
306 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
307 echo "Deleting existing namespace $1: kubectl delete ns $1"
308 kubectl delete ns $1 2>/dev/null
309 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
310 }
311
312 # main
313 while getopts ":D:d:i:-: " o; do
314 case "${o}" in
315 i)
316 DEFAULT_IP="${OPTARG}"
317 ;;
318 d)
319 OSM_CLUSTER_WORK_DIR="${OPTARG}"
320 ;;
321 D)
322 OSM_DEVOPS="${OPTARG}"
323 ;;
324 -)
325 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
326 echo -e "Invalid option: '--$OPTARG'\n" >&2
327 exit 1
328 ;;
329 :)
330 echo "Option -$OPTARG requires an argument" >&2
331 exit 1
332 ;;
333 \?)
334 echo -e "Invalid option: '-$OPTARG'\n" >&2
335 exit 1
336 ;;
337 *)
338 exit 1
339 ;;
340 esac
341 done
342
343 source $OSM_DEVOPS/common/logging
344 source $OSM_DEVOPS/common/track
345
346 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
347 echo "DEFAULT_IP=$DEFAULT_IP"
348 echo "OSM_DEVOPS=$OSM_DEVOPS"
349 echo "OSM_CLUSTER_WORK_DIR=$OSM_CLUSTER_WORK_DIR"
350 echo "INSTALL_K8S_MONITOR=$INSTALL_K8S_MONITOR"
351 echo "HOME=$HOME"
352
353 echo "Creating folders for installation"
354 [ ! -d "$OSM_CLUSTER_WORK_DIR" ] && sudo mkdir -p $OSM_CLUSTER_WORK_DIR
355 echo "Copying kubeadm-config from $OSM_DEVOPS/installers/kubeadm-config.yaml to $OSM_CLUSTER_WORK_DIR/kubeadm-config.yaml"
356 sudo cp -b $OSM_DEVOPS/installers/kubeadm-config.yaml $OSM_CLUSTER_WORK_DIR/kubeadm-config.yaml
357
358 install_kube
359 check_and_track_kube_install
360
361 init_kubeadm $OSM_CLUSTER_WORK_DIR/kubeadm-config.yaml
362 kube_config_dir
363 check_and_track_init_k8s
364
365 deploy_cni_provider
366 taint_master_node
367 check_and_track_k8s_ready_before_helm
368
369 remove_k8s_namespace osm
370
371 install_helm
372 track k8scluster install_helm_ok
373
374 install_k8s_storageclass
375 track k8scluster k8s_storageclass_ok
376 install_helm_metallb
377 track k8scluster k8s_metallb_ok
378 install_helm_certmanager
379 track k8scluster k8s_certmanager_ok
380 check_for_readiness
381 track k8scluster k8s_ready_ok
382 configure_ipaddresspool_metallb