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.
20 # installs kubernetes packages
21 function install_kube
() {
22 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
23 # Kubernetes releases can be found here: https://kubernetes.io/releases/
24 # To check other available versions, run the following command
25 # curl -s https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages | grep Version | awk '{print $2}'
26 sudo apt-get update
&& sudo apt-get
install -y apt-transport-https
27 sudo apt-get update
&& sudo apt-get
install -y apt-transport-https
28 curl
-fsSL https
://packages.cloud.google.com
/apt
/doc
/apt-key.gpg | sudo apt-key add
-
29 sudo add-apt-repository
"deb https://apt.kubernetes.io/ kubernetes-xenial main"
31 echo "Installing Kubernetes Packages ..."
32 sudo apt-get
install -y kubelet
=${K8S_VERSION} kubeadm=${K8S_VERSION} kubectl=${K8S_VERSION}
33 cat << EOF | sudo tee -a /etc/default/kubelet
34 KUBELET_EXTRA_ARGS="--cgroup-driver=cgroupfs"
36 sudo apt-mark hold kubelet kubeadm kubectl
37 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
40 # check and track kube packages installation
41 function check_and_track_kube_install
() {
42 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
43 kubelet_version
=$
(dpkg
-s kubelet|
grep Version|
awk '{print $2}')
44 [ -n "${kubelet_version}" ] || FATAL_TRACK k8scluster
"Kubelet was not installed."
45 kubeadm_version
=$
(dpkg
-s kubeadm|
grep Version|
awk '{print $2}')
46 [ -n "${kubeadm_version}" ] || FATAL_TRACK k8scluster
"Kubeadm was not installed."
47 kubectl_version
=$
(dpkg
-s kubectl|
grep Version|
awk '{print $2}')
48 [ -n "${kubectl_version}" ] || FATAL_TRACK k8scluster
"Kubectl was not installed."
49 track k8scluster install_k8s_ok kubelet
${kubelet_version} none none kubeadm ${kubeadm_version} none none kubectl ${kubectl_version} none none
50 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
53 # initializes kubernetes control plane
54 function init_kubeadm
() {
55 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
57 sudo
sed -i.bak
'/.*none.*swap/s/^\(.*\)$/#\1/g' /etc
/fstab
58 sudo kubeadm init
--dry-run || FATAL_TRACK k8scluster
"kubeadm init dry-run failed"
59 sudo kubeadm init
--config $1
61 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
64 # Initializes kubeconfig file
65 function kube_config_dir
() {
66 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
67 K8S_MANIFEST_DIR
="/etc/kubernetes/manifests"
68 [ ! -d $K8S_MANIFEST_DIR ] && FATAL_TRACK k8scluster
"Kubernetes folder $K8S_MANIFEST_DIR was not found"
70 sudo
cp /etc
/kubernetes
/admin.conf
$HOME/.kube
/config
71 sudo chown $
(id
-u):$
(id
-g) $HOME/.kube
/config
72 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
75 # test kubernetes installation
76 function check_and_track_init_k8s
() {
77 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
78 kubectl get ns || FATAL_TRACK k8scluster
"Failed getting namespaces"
79 track k8scluster init_k8s_ok
80 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
83 # deploys flannel as daemonsets
84 function deploy_cni_provider
() {
85 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
86 CNI_DIR
="$(mktemp -d -q --tmpdir "flannel.XXXXXX
")"
87 trap 'rm -rf "${CNI_DIR}"' EXIT
88 KUBE_FLANNEL_FILE_URL
="https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
89 wget
--retry-on-host-error --retry-on-http-error 404,429,503 --tries=5 "${KUBE_FLANNEL_FILE_URL}" -P $CNI_DIR
90 [ ! -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"
91 kubectl apply
-f $CNI_DIR
92 [ $?
-ne 0 ] && FATAL_TRACK k8scluster
"Cannot Install Flannel"
93 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
96 # taints K8s master node
97 function taint_master_node
() {
98 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
99 K8S_MASTER
=$
(kubectl get nodes |
awk '$3~/master/'|
awk '{print $1}')
100 kubectl taint node
$K8S_MASTER node-role.kubernetes.io
/master
:NoSchedule-
102 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
105 # check and track kube packages installation
106 function check_and_track_k8s_ready_before_helm
() {
107 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
108 kubectl get events || FATAL_TRACK k8scluster
"Failed getting events"
109 track k8scluster k8s_ready_before_helm
110 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
114 #Helm releases can be found here: https://github.com/helm/helm/releases
115 function install_helm
() {
116 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
117 HELM_VERSION
="v3.7.2"
118 if ! [[ "$(helm version --short 2>/dev/null)" =~ ^v3.
* ]]; then
119 # Helm is not installed. Install helm
120 echo "Helm3 is not installed, installing ..."
121 curl https
://get.helm.sh
/helm-
${HELM_VERSION}-linux-amd64.
tar.gz
--output helm-
${HELM_VERSION}.
tar.gz
122 tar -zxvf helm-
${HELM_VERSION}.
tar.gz
123 sudo
mv linux-amd64
/helm
/usr
/local
/bin
/helm
125 rm helm-
${HELM_VERSION}.
tar.gz
127 echo "Helm3 is already installed. Skipping installation..."
129 helm version || FATAL_TRACK k8scluster
"Could not obtain helm version. Maybe helm client was not installed"
130 helm repo add stable https
://charts.helm.sh
/stable || FATAL_TRACK k8scluster
"Helm repo stable could not be added"
131 helm repo update || FATAL_TRACK k8scluster
"Helm repo stable could not be updated"
132 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
135 function install_k8s_storageclass
() {
136 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
137 echo "Installing open-iscsi"
139 sudo apt-get
install open-iscsi
140 sudo systemctl
enable --now iscsid
141 OPENEBS_VERSION
="3.1.0"
142 echo "Installing OpenEBS"
143 helm repo add openebs https
://openebs.github.io
/charts
145 helm
install --create-namespace --namespace openebs openebs openebs
/openebs
--version ${OPENEBS_VERSION}
147 local storageclass_timeout
=400
149 local storageclass_ready
=""
150 echo "Waiting for storageclass"
151 while (( counter
< storageclass_timeout
))
153 kubectl get storageclass openebs-hostpath
&> /dev
/null
155 if [ $?
-eq 0 ] ; then
156 echo "Storageclass available"
157 storageclass_ready
="y"
160 counter
=$
((counter
+ 15))
164 [ -n "$storageclass_ready" ] || FATAL_TRACK k8scluster
"Storageclass not ready after $storageclass_timeout seconds. Cannot install openebs"
165 kubectl
patch storageclass openebs-hostpath
-p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
166 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
169 #installs metallb from helm
170 function install_helm_metallb
() {
171 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
172 echo "Installing MetalLB"
173 METALLB_VERSION
="0.11.0"
174 METALLB_IP_RANGE
="$DEFAULT_IP/32"
180 - $METALLB_IP_RANGE" | sudo
tee -a ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
181 helm repo add metallb https
://metallb.github.io
/metallb
183 helm
install --create-namespace --namespace metallb-system metallb metallb
/metallb
--version ${METALLB_VERSION} -f ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
184 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
187 #checks openebs and metallb readiness
188 function check_for_readiness
() {
189 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
190 # Default input values
191 sampling_period
=2 # seconds
192 time_for_readiness
=20 # seconds ready
193 time_for_failure
=200 # seconds broken
194 OPENEBS_NAMESPACE
=openebs
195 METALLB_NAMESPACE
=metallb-system
196 # STACK_NAME=osm # By default, "osm"
198 # Equivalent number of samples
199 oks_threshold
=$
((time_for_readiness
/${sampling_period})) # No. ok samples to declare the system ready
200 failures_threshold
=$
((time_for_failure
/${sampling_period})) # No. nok samples to declare the system broken
204 ####################################################################################
205 # Loop to check system readiness
206 ####################################################################################
207 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
210 OPENEBS_STATE
=$
(kubectl get pod
-n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
211 OPENEBS_READY
=$
(echo "${OPENEBS_STATE}" |
awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
212 OPENEBS_NOT_READY
=$
(echo "${OPENEBS_STATE}" |
awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
213 COUNT_OPENEBS_READY
=$
(echo "${OPENEBS_READY}"|
grep -v -e '^$' |
wc -l)
214 COUNT_OPENEBS_NOT_READY
=$
(echo "${OPENEBS_NOT_READY}" |
grep -v -e '^$' |
wc -l)
217 METALLB_STATE
=$
(kubectl get pod
-n ${METALLB_NAMESPACE} --no-headers 2>&1)
218 METALLB_READY
=$
(echo "${METALLB_STATE}" |
awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
219 METALLB_NOT_READY
=$
(echo "${METALLB_STATE}" |
awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
220 COUNT_METALLB_READY
=$
(echo "${METALLB_READY}" |
grep -v -e '^$' |
wc -l)
221 COUNT_METALLB_NOT_READY
=$
(echo "${METALLB_NOT_READY}" |
grep -v -e '^$' |
wc -l)
224 if [[ $
((${COUNT_OPENEBS_NOT_READY}+${COUNT_METALLB_NOT_READY})) -eq 0 ]]
228 echo -ne ===\
> Successful checks
: "${oks_in_a_row}"/${oks_threshold}\\r
231 ((++failures_in_a_row
))
234 echo Bootstraping...
"${failures_in_a_row}" checks of
${failures_threshold}
236 # Reports failed pods in OpenEBS
237 if [[ "${COUNT_OPENEBS_NOT_READY}" -ne 0 ]]
239 echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
240 echo "${OPENEBS_NOT_READY}"
244 # Reports failed statefulsets
245 if [[ "${COUNT_METALLB_NOT_READY}" -ne 0 ]]
247 echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
248 echo "${METALLB_NOT_READY}"
253 #------------ NEXT SAMPLE
254 sleep ${sampling_period}
257 ####################################################################################
259 ####################################################################################
260 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
263 FATAL_TRACK k8scluster
"K8S CLUSTER IS BROKEN"
266 echo "K8S CLUSTER IS READY"
268 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
271 #removes osm deployments and services
272 function remove_k8s_namespace
() {
273 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
274 kubectl delete ns
$1 2>&1 >/dev
/null
275 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
279 while getopts ":D:d:i:-: " o
; do
282 DEFAULT_IP
="${OPTARG}"
285 OSM_DOCKER_WORK_DIR
="${OPTARG}"
288 OSM_DEVOPS
="${OPTARG}"
291 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL
="y" && continue
292 echo -e "Invalid option: '--$OPTARG'\n" >&2
296 echo "Option -$OPTARG requires an argument" >&2
300 echo -e "Invalid option: '-$OPTARG'\n" >&2
309 source $OSM_DEVOPS/common
/logging
310 source $OSM_DEVOPS/common
/track
312 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
313 echo "DEFAULT_IP=$DEFAULT_IP"
314 echo "OSM_DEVOPS=$OSM_DEVOPS"
315 echo "OSM_DOCKER_WORK_DIR=$OSM_DOCKER_WORK_DIR"
316 echo "INSTALL_K8S_MONITOR=$INSTALL_K8S_MONITOR"
321 check_and_track_kube_install
323 init_kubeadm
$OSM_DOCKER_WORK_DIR/cluster-config.yaml
325 check_and_track_init_k8s
327 if [ -n "$INSTALL_K8S_MONITOR" ]; then
328 # uninstall OSM MONITORING
329 uninstall_k8s_monitoring
330 track k8scluster uninstall_k8s_monitoring_ok
333 remove_k8s_namespace osm
336 check_and_track_k8s_ready_before_helm
339 track k8scluster install_helm_ok
341 install_k8s_storageclass
342 track k8scluster k8s_storageclass_ok
344 track k8scluster k8s_metallb_ok
346 track k8scluster k8s_ready_ok