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