c6072457f2c0396c6870d7b6ea7e76f2bbad8016
[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.30
19 K8S_PACKAGE_VERSION="$K8S_VERSION".1-1.1
20 K8S_METRICS_VERSION="v0.7.1"
21
22 # installs kubernetes packages
23 function install_kube() {
24 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
25 # Kubernetes releases can be found here: https://kubernetes.io/releases/
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}'
28 sudo apt-get -y update && sudo apt-get install -y apt-transport-https ca-certificates curl
29 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
31 sudo apt-get -y update
32 echo "Installing Kubernetes Packages ..."
33 sudo apt-get install -y kubelet=${K8S_PACKAGE_VERSION} kubeadm=${K8S_PACKAGE_VERSION} kubectl=${K8S_PACKAGE_VERSION}
34 sudo apt-mark hold kubelet kubeadm kubectl
35 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
36 }
37
38 # check and track kube packages installation
39 function 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."
47 track k8scluster install_k8s_ok none none none kubelet ${kubelet_version} none none kubeadm ${kubeadm_version} none none kubectl ${kubectl_version} none none
48 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
49 }
50
51 # initializes kubernetes control plane
52 function 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
56 sudo kubeadm init --config $1 --dry-run || FATAL_TRACK k8scluster "kubeadm init dry-run failed"
57 sudo kubeadm init --config $1
58 sleep 5
59 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
60 }
61
62 # Initializes kubeconfig file
63 function kube_config_dir() {
64 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
65 K8S_MANIFEST_DIR="/etc/kubernetes/manifests"
66 [ ! -d $K8S_MANIFEST_DIR ] && FATAL_TRACK k8scluster "Kubernetes folder $K8S_MANIFEST_DIR was not found"
67 mkdir -p $HOME/.kube
68 sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
69 sudo chown $(id -u):$(id -g) $HOME/.kube/config
70 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
71 }
72
73 # test kubernetes installation
74 function check_and_track_init_k8s() {
75 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
76 echo "Reading existing namespaces"
77 kubectl get ns || FATAL_TRACK k8scluster "Failed getting namespaces"
78 track k8scluster init_k8s_ok
79 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
80 }
81
82 # deploys flannel as daemonsets
83 function deploy_cni_provider() {
84 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
85 CNI_DIR="$(mktemp -d -q --tmpdir "flannel.XXXXXX")"
86 trap 'rm -rf "${CNI_DIR}"' EXIT
87 KUBE_FLANNEL_FILE_URL="https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
88 wget --retry-on-host-error --retry-on-http-error 404,429,503 --tries=5 "${KUBE_FLANNEL_FILE_URL}" -P $CNI_DIR
89 [ ! -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"
90 kubectl apply -f $CNI_DIR
91 [ $? -ne 0 ] && FATAL_TRACK k8scluster "Cannot Install Flannel"
92 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
93 }
94
95 # taints K8s master node
96 function taint_master_node() {
97 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
98 K8S_MASTER=$(kubectl get nodes | awk '$3~/control-plane/'| awk '{print $1; exit}')
99 kubectl taint node $K8S_MASTER node-role.kubernetes.io/control-plane:NoSchedule-
100 sleep 5
101 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
102 }
103
104 # check and track kube packages installation
105 function check_and_track_k8s_ready_before_helm() {
106 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
107 kubectl get events || FATAL_TRACK k8scluster "Failed getting events"
108 track k8scluster k8s_ready_before_helm
109 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
110 }
111
112 # removes osm deployments and services
113 function install_k8s_metrics() {
114 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
115 echo "Installing Kubernetes metrics"
116 kubectl apply -f "https://github.com/kubernetes-sigs/metrics-server/releases/download/${K8S_METRICS_VERSION}/components.yaml"
117 kubectl -n kube-system patch deployment metrics-server --type=json -p '[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--kubelet-insecure-tls"}]'
118 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
119 }
120
121 # removes osm deployments and services
122 function remove_k8s_namespace() {
123 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
124 echo "Deleting existing namespace $1: kubectl delete ns $1"
125 kubectl delete ns $1 2>/dev/null
126 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
127 }
128 # main
129 while getopts ":D:d:i:-: " o; do
130 case "${o}" in
131 i)
132 DEFAULT_IP="${OPTARG}"
133 ;;
134 d)
135 OSM_CLUSTER_WORK_DIR="${OPTARG}"
136 ;;
137 D)
138 OSM_DEVOPS="${OPTARG}"
139 ;;
140 -)
141 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
142 echo -e "Invalid option: '--$OPTARG'\n" >&2
143 exit 1
144 ;;
145 :)
146 echo "Option -$OPTARG requires an argument" >&2
147 exit 1
148 ;;
149 \?)
150 echo -e "Invalid option: '-$OPTARG'\n" >&2
151 exit 1
152 ;;
153 *)
154 exit 1
155 ;;
156 esac
157 done
158
159 source $OSM_DEVOPS/common/logging
160 source $OSM_DEVOPS/common/track
161
162 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
163 echo "OSM_DEVOPS=$OSM_DEVOPS"
164 echo "OSM_CLUSTER_WORK_DIR=$OSM_CLUSTER_WORK_DIR"
165 echo "HOME=$HOME"
166
167 echo "Creating folders for installation"
168 [ ! -d "$OSM_CLUSTER_WORK_DIR" ] && sudo mkdir -p $OSM_CLUSTER_WORK_DIR
169 echo "Copying kubeadm-config from $OSM_DEVOPS/installers/kubeadm-config.yaml to $OSM_CLUSTER_WORK_DIR/kubeadm-config.yaml"
170 sudo cp -b $OSM_DEVOPS/installers/kubeadm-config.yaml $OSM_CLUSTER_WORK_DIR/kubeadm-config.yaml
171
172 install_kube
173 check_and_track_kube_install
174
175 init_kubeadm $OSM_CLUSTER_WORK_DIR/kubeadm-config.yaml
176 kube_config_dir
177 check_and_track_init_k8s
178
179 deploy_cni_provider
180 taint_master_node
181 check_and_track_k8s_ready_before_helm
182
183 install_k8s_metrics
184
185 remove_k8s_namespace osm
186
187 # install_helm has been moved to install_helm_client.sh, run from full_install_osm.sh,
188 # but tracking is still here because the installation analytics still expects it
189 track k8scluster install_helm_ok
190
191 # Installation of storage class, metallb and cert-manager has been moved
192 # to install_cluster_addons.sh, run from full_install_osm.sh