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