| 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 | |
| garciadeblas | 2886e57 | 2024-04-01 17:15:01 +0200 | [diff] [blame] | 18 | K8S_VERSION=1.29 |
| 19 | K8S_PACKAGE_VERSION="$K8S_VERSION".3-1.1 |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 20 | |
| 21 | # installs kubernetes packages |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 22 | function install_kube() { |
| 23 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| garciadeblas | c1ae239 | 2021-12-14 18:02:30 +0100 | [diff] [blame] | 24 | # Kubernetes releases can be found here: https://kubernetes.io/releases/ |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 25 | # To check other available versions, run the following command |
| 26 | # curl -s https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages | grep Version | awk '{print $2}' |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 27 | sudo apt-get -y update && sudo apt-get install -y apt-transport-https ca-certificates curl |
| Pedro Escaleira | 0265b5f | 2024-03-04 17:07:11 +0000 | [diff] [blame] | 28 | 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 |
| 29 | 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 |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 30 | sudo apt-get -y update |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 31 | echo "Installing Kubernetes Packages ..." |
| Pedro Escaleira | 0265b5f | 2024-03-04 17:07:11 +0000 | [diff] [blame] | 32 | 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] | 33 | sudo apt-mark hold kubelet kubeadm kubectl |
| 34 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 35 | } |
| 36 | |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 37 | # check and track kube packages installation |
| 38 | function check_and_track_kube_install() { |
| 39 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 40 | kubelet_version=$(dpkg -s kubelet|grep Version|awk '{print $2}') |
| 41 | [ -n "${kubelet_version}" ] || FATAL_TRACK k8scluster "Kubelet was not installed." |
| 42 | kubeadm_version=$(dpkg -s kubeadm|grep Version|awk '{print $2}') |
| 43 | [ -n "${kubeadm_version}" ] || FATAL_TRACK k8scluster "Kubeadm was not installed." |
| 44 | kubectl_version=$(dpkg -s kubectl|grep Version|awk '{print $2}') |
| 45 | [ -n "${kubectl_version}" ] || FATAL_TRACK k8scluster "Kubectl was not installed." |
| garciadeblas | b17abf7 | 2023-06-06 18:56:32 +0200 | [diff] [blame] | 46 | track k8scluster install_k8s_ok none none none kubelet ${kubelet_version} none none kubeadm ${kubeadm_version} none none kubectl ${kubectl_version} none none |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 47 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 48 | } |
| 49 | |
| 50 | # initializes kubernetes control plane |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 51 | function init_kubeadm() { |
| 52 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 53 | sudo swapoff -a |
| 54 | sudo sed -i.bak '/.*none.*swap/s/^\(.*\)$/#\1/g' /etc/fstab |
| garciadeblas | 2e1c9f8 | 2023-06-01 14:20:03 +0200 | [diff] [blame] | 55 | sudo kubeadm init --config $1 --dry-run || FATAL_TRACK k8scluster "kubeadm init dry-run failed" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 56 | sudo kubeadm init --config $1 |
| 57 | sleep 5 |
| 58 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 59 | } |
| 60 | |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 61 | # Initializes kubeconfig file |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 62 | function kube_config_dir() { |
| 63 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 64 | K8S_MANIFEST_DIR="/etc/kubernetes/manifests" |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 65 | [ ! -d $K8S_MANIFEST_DIR ] && FATAL_TRACK k8scluster "Kubernetes folder $K8S_MANIFEST_DIR was not found" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 66 | mkdir -p $HOME/.kube |
| 67 | sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config |
| 68 | sudo chown $(id -u):$(id -g) $HOME/.kube/config |
| 69 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 70 | } |
| 71 | |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 72 | # test kubernetes installation |
| 73 | function check_and_track_init_k8s() { |
| 74 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 75 | echo "Reading existing namespaces" |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 76 | kubectl get ns || FATAL_TRACK k8scluster "Failed getting namespaces" |
| 77 | track k8scluster init_k8s_ok |
| 78 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 79 | } |
| 80 | |
| 81 | # deploys flannel as daemonsets |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 82 | function deploy_cni_provider() { |
| 83 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 84 | CNI_DIR="$(mktemp -d -q --tmpdir "flannel.XXXXXX")" |
| 85 | trap 'rm -rf "${CNI_DIR}"' EXIT |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 86 | KUBE_FLANNEL_FILE_URL="https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml" |
| 87 | wget --retry-on-host-error --retry-on-http-error 404,429,503 --tries=5 "${KUBE_FLANNEL_FILE_URL}" -P $CNI_DIR |
| 88 | [ ! -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" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 89 | kubectl apply -f $CNI_DIR |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 90 | [ $? -ne 0 ] && FATAL_TRACK k8scluster "Cannot Install Flannel" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 91 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 92 | } |
| 93 | |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 94 | # taints K8s master node |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 95 | function taint_master_node() { |
| 96 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 97 | K8S_MASTER=$(kubectl get nodes | awk '$3~/control-plane/'| awk '{print $1; exit}') |
| 98 | kubectl taint node $K8S_MASTER node-role.kubernetes.io/control-plane:NoSchedule- |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 99 | sleep 5 |
| 100 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 101 | } |
| 102 | |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 103 | # check and track kube packages installation |
| 104 | function check_and_track_k8s_ready_before_helm() { |
| 105 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| 106 | kubectl get events || FATAL_TRACK k8scluster "Failed getting events" |
| 107 | track k8scluster k8s_ready_before_helm |
| 108 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 109 | } |
| 110 | |
| garciadeblas | a9e34f6 | 2024-04-02 14:29:12 +0200 | [diff] [blame] | 111 | # removes osm deployments and services |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 112 | function remove_k8s_namespace() { |
| 113 | [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 114 | echo "Deleting existing namespace $1: kubectl delete ns $1" |
| 115 | kubectl delete ns $1 2>/dev/null |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 116 | [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function |
| 117 | } |
| 118 | |
| 119 | # main |
| 120 | while getopts ":D:d:i:-: " o; do |
| 121 | case "${o}" in |
| 122 | i) |
| 123 | DEFAULT_IP="${OPTARG}" |
| 124 | ;; |
| 125 | d) |
| garciadeblas | 2e1c9f8 | 2023-06-01 14:20:03 +0200 | [diff] [blame] | 126 | OSM_CLUSTER_WORK_DIR="${OPTARG}" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 127 | ;; |
| 128 | D) |
| 129 | OSM_DEVOPS="${OPTARG}" |
| 130 | ;; |
| 131 | -) |
| 132 | [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue |
| 133 | echo -e "Invalid option: '--$OPTARG'\n" >&2 |
| 134 | exit 1 |
| 135 | ;; |
| 136 | :) |
| 137 | echo "Option -$OPTARG requires an argument" >&2 |
| 138 | exit 1 |
| 139 | ;; |
| 140 | \?) |
| 141 | echo -e "Invalid option: '-$OPTARG'\n" >&2 |
| 142 | exit 1 |
| 143 | ;; |
| 144 | *) |
| 145 | exit 1 |
| 146 | ;; |
| 147 | esac |
| 148 | done |
| 149 | |
| 150 | source $OSM_DEVOPS/common/logging |
| 151 | source $OSM_DEVOPS/common/track |
| 152 | |
| 153 | echo "DEBUG_INSTALL=$DEBUG_INSTALL" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 154 | echo "OSM_DEVOPS=$OSM_DEVOPS" |
| garciadeblas | 2e1c9f8 | 2023-06-01 14:20:03 +0200 | [diff] [blame] | 155 | echo "OSM_CLUSTER_WORK_DIR=$OSM_CLUSTER_WORK_DIR" |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 156 | echo "HOME=$HOME" |
| 157 | |
| garciadeblas | 5b3f6b6 | 2024-01-22 13:15:31 +0100 | [diff] [blame] | 158 | echo "Creating folders for installation" |
| 159 | [ ! -d "$OSM_CLUSTER_WORK_DIR" ] && sudo mkdir -p $OSM_CLUSTER_WORK_DIR |
| 160 | echo "Copying kubeadm-config from $OSM_DEVOPS/installers/kubeadm-config.yaml to $OSM_CLUSTER_WORK_DIR/kubeadm-config.yaml" |
| 161 | sudo cp -b $OSM_DEVOPS/installers/kubeadm-config.yaml $OSM_CLUSTER_WORK_DIR/kubeadm-config.yaml |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 162 | |
| 163 | install_kube |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 164 | check_and_track_kube_install |
| 165 | |
| garciadeblas | 2e1c9f8 | 2023-06-01 14:20:03 +0200 | [diff] [blame] | 166 | init_kubeadm $OSM_CLUSTER_WORK_DIR/kubeadm-config.yaml |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 167 | kube_config_dir |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 168 | check_and_track_init_k8s |
| 169 | |
| garciadeblas | 0bc8752 | 2021-10-20 22:16:17 +0200 | [diff] [blame] | 170 | deploy_cni_provider |
| 171 | taint_master_node |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 172 | check_and_track_k8s_ready_before_helm |
| 173 | |
| garciadeblas | 80b2e17 | 2023-06-01 18:38:13 +0200 | [diff] [blame] | 174 | remove_k8s_namespace osm |
| 175 | |
| garciadeblas | 9c62488 | 2024-04-01 15:01:27 +0200 | [diff] [blame] | 176 | # install_helm has been moved to install_helm_client.sh, run from full_install_osm.sh, |
| 177 | # but tracking is still here because the installation analytics still expects it |
| garciadeblas | 4d89c37 | 2021-11-25 11:57:18 +0100 | [diff] [blame] | 178 | track k8scluster install_helm_ok |
| garciadeblas | 8fed108 | 2022-08-29 11:25:02 +0200 | [diff] [blame] | 179 | |
| garciadeblas | 41f5ce5 | 2024-04-01 17:46:09 +0200 | [diff] [blame] | 180 | # Installation of storage class, metallb and cert-manager has been moved |
| 181 | # to install_cluster_addons.sh, run from full_install_osm.sh |