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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 K8S_PACKAGE_VERSION
="$K8S_VERSION".17-1.1
20 HELM_VERSION
="v3.10.3"
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 mkdir
/etc
/apt
/keyrings
29 sudo apt-get
-y update
&& sudo apt-get
install -y apt-transport-https ca-certificates curl
30 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
31 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
32 sudo apt-get
-y update
33 echo "Installing Kubernetes Packages ..."
34 sudo apt-get
install -y kubelet
=${K8S_PACKAGE_VERSION} kubeadm=${K8S_PACKAGE_VERSION} kubectl=${K8S_PACKAGE_VERSION}
35 sudo apt-mark hold kubelet kubeadm kubectl
37 sudo
rm /etc
/containerd
/config.toml
38 sudo systemctl restart containerd
40 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
43 #initializes kubernetes control plane
44 function init_kubeadm
() {
45 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
47 sudo
sed -i.bak
'/.*none.*swap/s/^\(.*\)$/#\1/g' /etc
/fstab
48 sudo kubeadm init
--config $1
50 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
53 function kube_config_dir
() {
54 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
55 K8S_MANIFEST_DIR
="/etc/kubernetes/manifests"
56 [ ! -d $K8S_MANIFEST_DIR ] && FATAL
"Cannot Install Kubernetes"
58 sudo
cp /etc
/kubernetes
/admin.conf
$HOME/.kube
/config
59 sudo chown $
(id
-u):$
(id
-g) $HOME/.kube
/config
60 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
63 #deploys flannel as daemonsets
64 function deploy_cni_provider
() {
65 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
66 CNI_DIR
="$(mktemp -d -q --tmpdir "flannel.XXXXXX
")"
67 trap 'rm -rf "${CNI_DIR}"' EXIT
68 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
69 [ ! -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"
70 kubectl apply
-f $CNI_DIR
71 [ $?
-ne 0 ] && FATAL
"Cannot Install Flannel"
72 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
75 #taints K8s master node
76 function taint_master_node
() {
77 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
78 K8S_MASTER
=$
(kubectl get nodes |
awk '$3~/control-plane/'|
awk '{print $1}')
79 kubectl taint node
$K8S_MASTER node-role.kubernetes.io
/master
:NoSchedule-
80 kubectl taint node
$K8S_MASTER node-role.kubernetes.io
/control-plane
:NoSchedule-
82 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
86 #Helm releases can be found here: https://github.com/helm/helm/releases
87 function install_helm
() {
88 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
89 if ! [[ "$(helm version --short 2>/dev/null)" =~ ^v3.
* ]]; then
90 # Helm is not installed. Install helm
91 echo "Helm3 is not installed, installing ..."
92 curl https
://get.helm.sh
/helm-
${HELM_VERSION}-linux-amd64.
tar.gz
--output helm-
${HELM_VERSION}.
tar.gz
93 tar -zxvf helm-
${HELM_VERSION}.
tar.gz
94 sudo
mv linux-amd64
/helm
/usr
/local
/bin
/helm
96 rm helm-
${HELM_VERSION}.
tar.gz
98 echo "Helm3 is already installed. Skipping installation..."
100 helm repo add stable https
://charts.helm.sh
/stable
102 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
105 function install_k8s_storageclass
() {
106 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
107 echo "Installing open-iscsi"
109 sudo apt-get
install open-iscsi
110 sudo systemctl
enable --now iscsid
111 OPENEBS_VERSION
="3.1.0"
112 echo "Installing OpenEBS"
113 helm repo add openebs https
://openebs.github.io
/charts
115 helm
install --create-namespace --namespace openebs openebs openebs
/openebs
--version ${OPENEBS_VERSION}
117 local storageclass_timeout
=400
119 local storageclass_ready
=""
120 echo "Waiting for storageclass"
121 while (( counter
< storageclass_timeout
))
123 kubectl get storageclass openebs-hostpath
&> /dev
/null
125 if [ $?
-eq 0 ] ; then
126 echo "Storageclass available"
127 storageclass_ready
="y"
130 counter
=$
((counter
+ 15))
134 [ -n "$storageclass_ready" ] || FATAL
"Storageclass not ready after $storageclass_timeout seconds. Cannot install openebs"
135 kubectl
patch storageclass openebs-hostpath
-p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
136 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
139 #installs metallb from helm
140 function install_helm_metallb
() {
141 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
142 echo "Installing MetalLB"
143 METALLB_VERSION
="0.11.0"
144 METALLB_IP_RANGE
="$DEFAULT_IP/32"
150 - $METALLB_IP_RANGE" | sudo
tee -a ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
151 helm repo add metallb https
://metallb.github.io
/metallb
153 helm
install --create-namespace --namespace metallb-system metallb metallb
/metallb
--version ${METALLB_VERSION} -f ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
154 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
157 #checks openebs and metallb readiness
158 function check_for_readiness
() {
159 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
160 # Default input values
161 sampling_period
=2 # seconds
162 time_for_readiness
=20 # seconds ready
163 time_for_failure
=200 # seconds broken
164 OPENEBS_NAMESPACE
=openebs
165 METALLB_NAMESPACE
=metallb-system
166 # STACK_NAME=osm # By default, "osm"
168 # Equivalent number of samples
169 oks_threshold
=$
((time_for_readiness
/${sampling_period})) # No. ok samples to declare the system ready
170 failures_threshold
=$
((time_for_failure
/${sampling_period})) # No. nok samples to declare the system broken
174 ####################################################################################
175 # Loop to check system readiness
176 ####################################################################################
177 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
180 OPENEBS_STATE
=$
(kubectl get pod
-n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
181 OPENEBS_READY
=$
(echo "${OPENEBS_STATE}" |
awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
182 OPENEBS_NOT_READY
=$
(echo "${OPENEBS_STATE}" |
awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
183 COUNT_OPENEBS_READY
=$
(echo "${OPENEBS_READY}"|
grep -v -e '^$' |
wc -l)
184 COUNT_OPENEBS_NOT_READY
=$
(echo "${OPENEBS_NOT_READY}" |
grep -v -e '^$' |
wc -l)
187 METALLB_STATE
=$
(kubectl get pod
-n ${METALLB_NAMESPACE} --no-headers 2>&1)
188 METALLB_READY
=$
(echo "${METALLB_STATE}" |
awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
189 METALLB_NOT_READY
=$
(echo "${METALLB_STATE}" |
awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
190 COUNT_METALLB_READY
=$
(echo "${METALLB_READY}" |
grep -v -e '^$' |
wc -l)
191 COUNT_METALLB_NOT_READY
=$
(echo "${METALLB_NOT_READY}" |
grep -v -e '^$' |
wc -l)
194 if [[ $
((${COUNT_OPENEBS_NOT_READY}+${COUNT_METALLB_NOT_READY})) -eq 0 ]]
198 echo -ne ===\
> Successful checks
: "${oks_in_a_row}"/${oks_threshold}\\r
201 ((++failures_in_a_row
))
204 echo Bootstraping...
"${failures_in_a_row}" checks of
${failures_threshold}
206 # Reports failed pods in OpenEBS
207 if [[ "${COUNT_OPENEBS_NOT_READY}" -ne 0 ]]
209 echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
210 echo "${OPENEBS_NOT_READY}"
214 # Reports failed statefulsets
215 if [[ "${COUNT_METALLB_NOT_READY}" -ne 0 ]]
217 echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
218 echo "${METALLB_NOT_READY}"
223 #------------ NEXT SAMPLE
224 sleep ${sampling_period}
227 ####################################################################################
229 ####################################################################################
230 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
233 FATAL
"K8S CLUSTER IS BROKEN"
236 echo "K8S CLUSTER IS READY"
238 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
241 #removes osm deployments and services
242 function remove_k8s_namespace
() {
243 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
244 kubectl delete ns
$1 2>&1 >/dev
/null
245 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
249 while getopts ":D:d:i:-: " o
; do
252 DEFAULT_IP
="${OPTARG}"
255 OSM_DOCKER_WORK_DIR
="${OPTARG}"
258 OSM_DEVOPS
="${OPTARG}"
261 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL
="y" && continue
262 echo -e "Invalid option: '--$OPTARG'\n" >&2
266 echo "Option -$OPTARG requires an argument" >&2
270 echo -e "Invalid option: '-$OPTARG'\n" >&2
279 source $OSM_DEVOPS/common
/logging
280 source $OSM_DEVOPS/common
/track
282 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
283 echo "DEFAULT_IP=$DEFAULT_IP"
284 echo "OSM_DEVOPS=$OSM_DEVOPS"
285 echo "OSM_DOCKER_WORK_DIR=$OSM_DOCKER_WORK_DIR"
286 echo "INSTALL_K8S_MONITOR=$INSTALL_K8S_MONITOR"
291 track k8scluster install_k8s_ok
292 init_kubeadm
$OSM_DOCKER_WORK_DIR/cluster-config.yaml
294 track k8scluster init_k8s_ok
295 if [ -n "$INSTALL_K8S_MONITOR" ]; then
296 # uninstall OSM MONITORING
297 uninstall_k8s_monitoring
298 track k8scluster uninstall_k8s_monitoring_ok
300 #remove old namespace
301 remove_k8s_namespace osm
305 track k8scluster install_helm_ok
306 install_k8s_storageclass
307 track k8scluster k8s_storageclass_ok
309 track k8scluster k8s_metallb_ok
311 track k8scluster k8s_ready_ok