blob: ecca8dcc116bf574ee262d7572412798c1dd699b [file] [log] [blame]
garciadeblas0bc87522021-10-20 22:16:17 +02001#!/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
16set +eux
17
garciadeblas4c3b3fb2024-05-30 14:49:01 +020018K8S_VERSION=1.30
19K8S_PACKAGE_VERSION="$K8S_VERSION".1-1.1
garciadeblas44cb8512024-05-31 17:52:09 +020020K8S_METRICS_VERSION="v0.7.1"
garciadeblas8fed1082022-08-29 11:25:02 +020021
22# installs kubernetes packages
garciadeblas0bc87522021-10-20 22:16:17 +020023function install_kube() {
24 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblasc1ae2392021-12-14 18:02:30 +010025 # Kubernetes releases can be found here: https://kubernetes.io/releases/
garciadeblas0bc87522021-10-20 22:16:17 +020026 # 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}'
garciadeblas80b2e172023-06-01 18:38:13 +020028 sudo apt-get -y update && sudo apt-get install -y apt-transport-https ca-certificates curl
Pedro Escaleira0265b5f2024-03-04 17:07:11 +000029 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
30 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
garciadeblas80b2e172023-06-01 18:38:13 +020031 sudo apt-get -y update
garciadeblas0bc87522021-10-20 22:16:17 +020032 echo "Installing Kubernetes Packages ..."
Pedro Escaleira0265b5f2024-03-04 17:07:11 +000033 sudo apt-get install -y kubelet=${K8S_PACKAGE_VERSION} kubeadm=${K8S_PACKAGE_VERSION} kubectl=${K8S_PACKAGE_VERSION}
garciadeblas0bc87522021-10-20 22:16:17 +020034 sudo apt-mark hold kubelet kubeadm kubectl
35 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
36}
37
garciadeblas8fed1082022-08-29 11:25:02 +020038# check and track kube packages installation
39function check_and_track_kube_install() {
40 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
41 kubelet_version=$(dpkg -s kubelet|grep Version|awk '{print $2}')
42 [ -n "${kubelet_version}" ] || FATAL_TRACK k8scluster "Kubelet was not installed."
43 kubeadm_version=$(dpkg -s kubeadm|grep Version|awk '{print $2}')
44 [ -n "${kubeadm_version}" ] || FATAL_TRACK k8scluster "Kubeadm was not installed."
45 kubectl_version=$(dpkg -s kubectl|grep Version|awk '{print $2}')
46 [ -n "${kubectl_version}" ] || FATAL_TRACK k8scluster "Kubectl was not installed."
garciadeblasb17abf72023-06-06 18:56:32 +020047 track k8scluster install_k8s_ok none none none kubelet ${kubelet_version} none none kubeadm ${kubeadm_version} none none kubectl ${kubectl_version} none none
garciadeblas8fed1082022-08-29 11:25:02 +020048 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
49}
50
51# initializes kubernetes control plane
garciadeblas0bc87522021-10-20 22:16:17 +020052function init_kubeadm() {
53 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
54 sudo swapoff -a
55 sudo sed -i.bak '/.*none.*swap/s/^\(.*\)$/#\1/g' /etc/fstab
garciadeblas2e1c9f82023-06-01 14:20:03 +020056 sudo kubeadm init --config $1 --dry-run || FATAL_TRACK k8scluster "kubeadm init dry-run failed"
garciadeblas0bc87522021-10-20 22:16:17 +020057 sudo kubeadm init --config $1
58 sleep 5
59 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
60}
61
garciadeblas8fed1082022-08-29 11:25:02 +020062# Initializes kubeconfig file
garciadeblas1f338482024-07-04 19:26:54 +020063function save_kubeconfig() {
garciadeblas0bc87522021-10-20 22:16:17 +020064 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
65 K8S_MANIFEST_DIR="/etc/kubernetes/manifests"
garciadeblas8fed1082022-08-29 11:25:02 +020066 [ ! -d $K8S_MANIFEST_DIR ] && FATAL_TRACK k8scluster "Kubernetes folder $K8S_MANIFEST_DIR was not found"
garciadeblas1f338482024-07-04 19:26:54 +020067 KUBEDIR="${HOME}/.kube"
68 KUBEFILE="$KUBEDIR/config"
69 mkdir -p "${KUBEDIR}"
70 KUBEADM_KUBECONFIG="/etc/kubernetes/admin.conf"
71 sudo cp "${KUBEADM_KUBECONFIG}" "${KUBEFILE}"
72 sudo chown $(id -u):$(id -g) "${KUBEFILE}"
73 echo
74 echo "Credentials saved at ${KUBEFILE}"
75 echo
garciadeblas0bc87522021-10-20 22:16:17 +020076 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
77}
78
garciadeblas8fed1082022-08-29 11:25:02 +020079# test kubernetes installation
80function check_and_track_init_k8s() {
81 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblas80b2e172023-06-01 18:38:13 +020082 echo "Reading existing namespaces"
garciadeblas8fed1082022-08-29 11:25:02 +020083 kubectl get ns || FATAL_TRACK k8scluster "Failed getting namespaces"
84 track k8scluster init_k8s_ok
85 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
86}
87
88# deploys flannel as daemonsets
garciadeblas0bc87522021-10-20 22:16:17 +020089function deploy_cni_provider() {
90 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
91 CNI_DIR="$(mktemp -d -q --tmpdir "flannel.XXXXXX")"
92 trap 'rm -rf "${CNI_DIR}"' EXIT
garciadeblas8fed1082022-08-29 11:25:02 +020093 KUBE_FLANNEL_FILE_URL="https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
garciadeblascf603f52025-06-04 11:57:28 +020094 curl -f --retry 5 --retry-delay 2 --retry-connrefused "${KUBE_FLANNEL_FILE_URL}" -o "$CNI_DIR/$(basename ${KUBE_FLANNEL_FILE_URL})"
garciadeblas8fed1082022-08-29 11:25:02 +020095 [ ! -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"
garciadeblas0bc87522021-10-20 22:16:17 +020096 kubectl apply -f $CNI_DIR
garciadeblas8fed1082022-08-29 11:25:02 +020097 [ $? -ne 0 ] && FATAL_TRACK k8scluster "Cannot Install Flannel"
garciadeblas0bc87522021-10-20 22:16:17 +020098 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
99}
100
garciadeblas8fed1082022-08-29 11:25:02 +0200101# taints K8s master node
garciadeblas0bc87522021-10-20 22:16:17 +0200102function taint_master_node() {
103 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblas80b2e172023-06-01 18:38:13 +0200104 K8S_MASTER=$(kubectl get nodes | awk '$3~/control-plane/'| awk '{print $1; exit}')
105 kubectl taint node $K8S_MASTER node-role.kubernetes.io/control-plane:NoSchedule-
garciadeblas0bc87522021-10-20 22:16:17 +0200106 sleep 5
107 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
108}
109
garciadeblas8fed1082022-08-29 11:25:02 +0200110# check and track kube packages installation
111function check_and_track_k8s_ready_before_helm() {
112 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
113 kubectl get events || FATAL_TRACK k8scluster "Failed getting events"
114 track k8scluster k8s_ready_before_helm
115 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
116}
117
garciadeblasa9e34f62024-04-02 14:29:12 +0200118# removes osm deployments and services
garciadeblas44cb8512024-05-31 17:52:09 +0200119function install_k8s_metrics() {
120 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
121 echo "Installing Kubernetes metrics"
garciadeblas9c253bd2024-06-12 17:41:22 +0200122 kubectl apply -f "https://github.com/kubernetes-sigs/metrics-server/releases/download/${K8S_METRICS_VERSION}/components.yaml"
garciadeblas44cb8512024-05-31 17:52:09 +0200123 kubectl -n kube-system patch deployment metrics-server --type=json -p '[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--kubelet-insecure-tls"}]'
124 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
125}
126
127# removes osm deployments and services
garciadeblas0bc87522021-10-20 22:16:17 +0200128function remove_k8s_namespace() {
129 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
garciadeblas80b2e172023-06-01 18:38:13 +0200130 echo "Deleting existing namespace $1: kubectl delete ns $1"
131 kubectl delete ns $1 2>/dev/null
garciadeblas0bc87522021-10-20 22:16:17 +0200132 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
133}
garciadeblas1f338482024-07-04 19:26:54 +0200134
garciadeblas82981162024-07-23 15:24:00 +0200135# main
garciadeblascf603f52025-06-04 11:57:28 +0200136while getopts ":-: " o; do
garciadeblas0bc87522021-10-20 22:16:17 +0200137 case "${o}" in
garciadeblas0bc87522021-10-20 22:16:17 +0200138 -)
139 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
140 echo -e "Invalid option: '--$OPTARG'\n" >&2
141 exit 1
142 ;;
143 :)
144 echo "Option -$OPTARG requires an argument" >&2
145 exit 1
146 ;;
147 \?)
148 echo -e "Invalid option: '-$OPTARG'\n" >&2
149 exit 1
150 ;;
151 *)
152 exit 1
153 ;;
154 esac
155done
156
garciadeblas82981162024-07-23 15:24:00 +0200157DEBUG_INSTALL=${DEBUG_INSTALL:-}
garciadeblascf603f52025-06-04 11:57:28 +0200158K8SCLUSTER_CONFIG_FOLDER=${K8SCLUSTER_CONFIG_FOLDER:-"/etc/osm"}
garciadeblas0bc87522021-10-20 22:16:17 +0200159echo "DEBUG_INSTALL=$DEBUG_INSTALL"
garciadeblascf603f52025-06-04 11:57:28 +0200160echo "K8SCLUSTER_CONFIG_FOLDER=$K8SCLUSTER_CONFIG_FOLDER"
garciadeblas0bc87522021-10-20 22:16:17 +0200161echo "HOME=$HOME"
162
garciadeblascf603f52025-06-04 11:57:28 +0200163export HERE=$(dirname "$(readlink --canonicalize "$BASH_SOURCE")")
164source "${HERE}/../../../library/logging"
165source "${HERE}/../../../library/track"
garciadeblas82981162024-07-23 15:24:00 +0200166
garciadeblas5b3f6b62024-01-22 13:15:31 +0100167echo "Creating folders for installation"
garciadeblascf603f52025-06-04 11:57:28 +0200168[ ! -d "$K8SCLUSTER_CONFIG_FOLDER" ] && sudo mkdir -p $K8SCLUSTER_CONFIG_FOLDER
169echo "Copying kubeadm-config from ${HERE}/installers/kubeadm-config.yaml to $K8SCLUSTER_CONFIG_FOLDER/kubeadm-config.yaml"
170sudo cp -b "${HERE}/kubeadm-config.yaml" "$K8SCLUSTER_CONFIG_FOLDER/kubeadm-config.yaml"
garciadeblas0bc87522021-10-20 22:16:17 +0200171
172install_kube
garciadeblas8fed1082022-08-29 11:25:02 +0200173check_and_track_kube_install
174
garciadeblascf603f52025-06-04 11:57:28 +0200175init_kubeadm "${K8SCLUSTER_CONFIG_FOLDER}/kubeadm-config.yaml"
garciadeblas1f338482024-07-04 19:26:54 +0200176save_kubeconfig
garciadeblas8fed1082022-08-29 11:25:02 +0200177check_and_track_init_k8s
178
garciadeblas0bc87522021-10-20 22:16:17 +0200179deploy_cni_provider
180taint_master_node
garciadeblas8fed1082022-08-29 11:25:02 +0200181check_and_track_k8s_ready_before_helm
182
garciadeblas44cb8512024-05-31 17:52:09 +0200183install_k8s_metrics
184
garciadeblas8d8cd992024-05-21 16:04:14 +0200185# Clean existing namespace (idempotent installation)
garciadeblas80b2e172023-06-01 18:38:13 +0200186remove_k8s_namespace osm
187
garciadeblascf603f52025-06-04 11:57:28 +0200188# Installation of storage class, metallb and cert-manager
189# is done outside this script, by install_cluster_addons.sh