Feature 11041: Enable K3s as Kubernetes distro for OSM installation
[osm/devops.git] / installers / install_k3s_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 # K3s releases: https://github.com/k3s-io/k3s/releases/
19 K8S_VERSION="v1.29.3+k3s1"
20
21 # installs k3s
22 function install_k3s() {
23 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
24 export INSTALL_K3S_EXEC="--disable traefik"
25 curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=${K8S_VERSION} sh -s -
26 sudo chmod 644 /etc/rancher/k3s/k3s.yaml
27 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
28 }
29
30 # updates service nodeport range
31 function update_service_nodeport_range() {
32 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
33 sudo k3s server --kube-apiserver-arg=service-node-port-range=80-32767
34 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
35 }
36
37 # checks cluster readiness
38 function check_for_readiness() {
39 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
40 # Check for Ready node, takes ~30 seconds
41 echo "Waiting for K8s nodes to be ready"
42 local time_for_failure=60 # seconds broken
43 local sampling_period=5 # seconds
44 local counter=0
45 local cluster_ready=""
46 while (( counter < time_for_failure ))
47 do
48 kubectl get nodes |grep master |grep -v none | grep Ready
49 if [ $? -eq 0 ] ; then
50 echo "K8s cluster is ready"
51 cluster_ready="y"
52 break
53 else
54 echo "K8s cluster is not ready yet"
55 counter=$((counter + sampling_period))
56 sleep ${sampling_period}
57 fi
58 done
59 [ -n "$cluster_ready" ] || FATAL_TRACK k8scluster "K3s cluster nodes not ready after $time_for_failure seconds."
60
61 echo "Waiting for pods to be ready"
62 local time_for_readiness=20 # seconds ready
63 local time_for_failure=100 # seconds broken
64
65 # Equivalent number of samples
66 oks_threshold=$((time_for_readiness/${sampling_period})) # No. ok samples to declare the system ready
67 failures_threshold=$((time_for_failure/${sampling_period})) # No. nok samples to declare the system broken
68 failures_in_a_row=0
69 oks_in_a_row=0
70 ####################################################################################
71 # Loop to check system readiness
72 ####################################################################################
73 K3S_NAMESPACE=kube-system
74 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
75 do
76 # State of pods rather than completed jobs
77 K3S_PODS_STATE=$(kubectl get pod -n ${K3S_NAMESPACE} --no-headers |grep -v Completed 2>&1)
78 K3S_PODS_READY=$(echo "${K3S_PODS_STATE}" | awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
79 K3S_PODS_NOT_READY=$(echo "${K3S_PODS_STATE}" | awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
80 COUNT_K3S_PODS_READY=$(echo "${K3S_PODS_READY}"| grep -v -e '^$' | wc -l)
81 COUNT_K3S_PODS_NOT_READY=$(echo "${K3S_PODS_NOT_READY}" | grep -v -e '^$' | wc -l)
82
83 # OK sample
84 if [[ ${COUNT_K3S_PODS_NOT_READY} -eq 0 ]]
85 then
86 ((++oks_in_a_row))
87 failures_in_a_row=0
88 echo -ne ===\> Successful checks: "${oks_in_a_row}"/${oks_threshold}\\r
89 # NOK sample
90 else
91 ((++failures_in_a_row))
92 oks_in_a_row=0
93 echo
94 echo Bootstraping... "${failures_in_a_row}" checks of ${failures_threshold}
95
96 # Reports failed pods in K3S
97 if [[ "${COUNT_K3S_PODS_NOT_READY}" -ne 0 ]]
98 then
99 echo "K3S kube-system: Waiting for ${COUNT_K3S_PODS_NOT_READY} of $((${COUNT_K3S_PODS_NOT_READY}+${COUNT_K3S_PODS_READY})) pods to be ready:"
100 echo "${K3S_PODS_NOT_READY}"
101 echo
102 fi
103 fi
104
105 #------------ NEXT SAMPLE
106 sleep ${sampling_period}
107 done
108
109 ####################################################################################
110 # OUTCOME
111 ####################################################################################
112 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
113 then
114 echo
115 FATAL_TRACK k8scluster "K8S CLUSTER IS BROKEN"
116 else
117 echo
118 echo "K8S CLUSTER IS READY"
119 fi
120 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
121 }
122
123 # Retrieves and saves the credentials
124 function save_credentials() {
125 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of function
126 KUBEDIR="${HOME}/.kube"
127 KUBEFILE="$KUBEDIR/config"
128 mkdir -p "${KUBEDIR}"
129 sudo cp /etc/rancher/k3s/k3s.yaml "${KUBEFILE}"
130 sudo chown $(id -u):$(id -g) "${KUBEFILE}"
131 chmod 700 "${KUBEFILE}"
132 echo
133 echo "Credentials saved at ${KUBEFILE}"
134 echo
135 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of function
136 }
137
138 # main
139 while getopts ":D:-: " o; do
140 case "${o}" in
141 D)
142 OSM_DEVOPS="${OPTARG}"
143 ;;
144 -)
145 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL="y" && continue
146 echo -e "Invalid option: '--$OPTARG'\n" >&2
147 exit 1
148 ;;
149 :)
150 echo "Option -$OPTARG requires an argument" >&2
151 exit 1
152 ;;
153 \?)
154 echo -e "Invalid option: '-$OPTARG'\n" >&2
155 exit 1
156 ;;
157 *)
158 exit 1
159 ;;
160 esac
161 done
162
163 source $OSM_DEVOPS/common/logging
164 source $OSM_DEVOPS/common/track
165
166 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
167 echo "OSM_DEVOPS=$OSM_DEVOPS"
168 echo "HOME=$HOME"
169
170 install_k3s
171 track k8scluster k3s_install_ok
172 check_for_readiness
173 track k8scluster k3s_node_ready_ok
174 # update_service_nodeport_range
175 # check_for_readiness
176 # track k8scluster k3s_update_nodeport_range_ok
177 save_credentials
178 track k8scluster k3s_creds_ok