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.
18 #installs kubernetes packages
19 function install_kube
() {
20 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
21 K8S_VERSION
=1.20.14-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"
30 echo "Installing Kubernetes Packages ..."
31 sudo apt-get
install -y kubelet
=${K8S_VERSION} kubeadm=${K8S_VERSION} kubectl=${K8S_VERSION}
32 sudo apt-mark hold kubelet kubeadm kubectl
33 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
36 #initializes kubernetes control plane
37 function init_kubeadm
() {
38 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
40 sudo
sed -i.bak
'/.*none.*swap/s/^\(.*\)$/#\1/g' /etc
/fstab
41 sudo kubeadm init
--config $1
43 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
46 function kube_config_dir
() {
47 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
48 K8S_MANIFEST_DIR
="/etc/kubernetes/manifests"
49 [ ! -d $K8S_MANIFEST_DIR ] && FATAL
"Cannot Install Kubernetes"
51 sudo
cp /etc
/kubernetes
/admin.conf
$HOME/.kube
/config
52 sudo chown $
(id
-u):$
(id
-g) $HOME/.kube
/config
53 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
56 #deploys flannel as daemonsets
57 function deploy_cni_provider
() {
58 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
59 CNI_DIR
="$(mktemp -d -q --tmpdir "flannel.XXXXXX
")"
60 trap 'rm -rf "${CNI_DIR}"' EXIT
61 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
62 [ ! -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"
63 kubectl apply
-f $CNI_DIR
64 [ $?
-ne 0 ] && FATAL
"Cannot Install Flannel"
65 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
68 #taints K8s master node
69 function taint_master_node
() {
70 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
71 K8S_MASTER
=$
(kubectl get nodes |
awk '$3~/master/'|
awk '{print $1}')
72 kubectl taint node
$K8S_MASTER node-role.kubernetes.io
/master
:NoSchedule-
74 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
78 #Helm releases can be found here: https://github.com/helm/helm/releases
79 function install_helm
() {
80 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
82 if ! [[ "$(helm version --short 2>/dev/null)" =~ ^v3.
* ]]; then
83 # Helm is not installed. Install helm
84 echo "Helm3 is not installed, installing ..."
85 curl https
://get.helm.sh
/helm-
${HELM_VERSION}-linux-amd64.
tar.gz
--output helm-
${HELM_VERSION}.
tar.gz
86 tar -zxvf helm-
${HELM_VERSION}.
tar.gz
87 sudo
mv linux-amd64
/helm
/usr
/local
/bin
/helm
89 rm helm-
${HELM_VERSION}.
tar.gz
91 echo "Helm3 is already installed. Skipping installation..."
93 helm repo add stable https
://charts.helm.sh
/stable
95 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
98 function install_k8s_storageclass
() {
99 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
100 OPENEBS_VERSION
="1.12.0"
101 echo "Installing OpenEBS"
102 helm repo add openebs https
://openebs.github.io
/charts
104 helm
install --create-namespace --namespace openebs openebs openebs
/openebs
--version ${OPENEBS_VERSION}
106 local storageclass_timeout
=400
108 local storageclass_ready
=""
109 echo "Waiting for storageclass"
110 while (( counter
< storageclass_timeout
))
112 kubectl get storageclass openebs-hostpath
&> /dev
/null
114 if [ $?
-eq 0 ] ; then
115 echo "Storageclass available"
116 storageclass_ready
="y"
119 counter
=$
((counter
+ 15))
123 [ -n "$storageclass_ready" ] || FATAL
"Storageclass not ready after $storageclass_timeout seconds. Cannot install openebs"
124 kubectl
patch storageclass openebs-hostpath
-p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
125 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
128 #installs metallb from helm
129 function install_helm_metallb
() {
130 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
131 echo "Installing MetalLB"
132 METALLB_VERSION
="0.11.0"
133 METALLB_IP_RANGE
="$DEFAULT_IP/32"
139 - $METALLB_IP_RANGE" | sudo
tee -a ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
140 helm repo add metallb https
://metallb.github.io
/metallb
142 helm
install --create-namespace --namespace metallb-system metallb metallb
/metallb
--version ${METALLB_VERSION} -f ${OSM_DOCKER_WORK_DIR}/metallb-config.yaml
143 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
146 #checks openebs and metallb readiness
147 function check_for_readiness
() {
148 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
149 # Default input values
150 sampling_period
=2 # seconds
151 time_for_readiness
=20 # seconds ready
152 time_for_failure
=200 # seconds broken
153 OPENEBS_NAMESPACE
=openebs
154 METALLB_NAMESPACE
=metallb-system
155 # STACK_NAME=osm # By default, "osm"
157 # Equivalent number of samples
158 oks_threshold
=$
((time_for_readiness
/${sampling_period})) # No. ok samples to declare the system ready
159 failures_threshold
=$
((time_for_failure
/${sampling_period})) # No. nok samples to declare the system broken
163 ####################################################################################
164 # Loop to check system readiness
165 ####################################################################################
166 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
169 OPENEBS_STATE
=$
(kubectl get pod
-n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
170 OPENEBS_READY
=$
(echo "${OPENEBS_STATE}" |
awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
171 OPENEBS_NOT_READY
=$
(echo "${OPENEBS_STATE}" |
awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
172 COUNT_OPENEBS_READY
=$
(echo "${OPENEBS_READY}"|
grep -v -e '^$' |
wc -l)
173 COUNT_OPENEBS_NOT_READY
=$
(echo "${OPENEBS_NOT_READY}" |
grep -v -e '^$' |
wc -l)
176 METALLB_STATE
=$
(kubectl get pod
-n ${METALLB_NAMESPACE} --no-headers 2>&1)
177 METALLB_READY
=$
(echo "${METALLB_STATE}" |
awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
178 METALLB_NOT_READY
=$
(echo "${METALLB_STATE}" |
awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
179 COUNT_METALLB_READY
=$
(echo "${METALLB_READY}" |
grep -v -e '^$' |
wc -l)
180 COUNT_METALLB_NOT_READY
=$
(echo "${METALLB_NOT_READY}" |
grep -v -e '^$' |
wc -l)
183 if [[ $
((${COUNT_OPENEBS_NOT_READY}+${COUNT_METALLB_NOT_READY})) -eq 0 ]]
187 echo -ne ===\
> Successful checks
: "${oks_in_a_row}"/${oks_threshold}\\r
190 ((++failures_in_a_row
))
193 echo Bootstraping...
"${failures_in_a_row}" checks of
${failures_threshold}
195 # Reports failed pods in OpenEBS
196 if [[ "${COUNT_OPENEBS_NOT_READY}" -ne 0 ]]
198 echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
199 echo "${OPENEBS_NOT_READY}"
203 # Reports failed statefulsets
204 if [[ "${COUNT_METALLB_NOT_READY}" -ne 0 ]]
206 echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
207 echo "${METALLB_NOT_READY}"
212 #------------ NEXT SAMPLE
213 sleep ${sampling_period}
216 ####################################################################################
218 ####################################################################################
219 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
222 FATAL
"K8S CLUSTER IS BROKEN"
225 echo "K8S CLUSTER IS READY"
227 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
230 #removes osm deployments and services
231 function remove_k8s_namespace
() {
232 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
233 kubectl delete ns
$1 2>&1 >/dev
/null
234 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
238 while getopts ":D:d:i:-: " o
; do
241 DEFAULT_IP
="${OPTARG}"
244 OSM_DOCKER_WORK_DIR
="${OPTARG}"
247 OSM_DEVOPS
="${OPTARG}"
250 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL
="y" && continue
251 echo -e "Invalid option: '--$OPTARG'\n" >&2
255 echo "Option -$OPTARG requires an argument" >&2
259 echo -e "Invalid option: '-$OPTARG'\n" >&2
268 source $OSM_DEVOPS/common
/logging
269 source $OSM_DEVOPS/common
/track
271 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
272 echo "DEFAULT_IP=$DEFAULT_IP"
273 echo "OSM_DEVOPS=$OSM_DEVOPS"
274 echo "OSM_DOCKER_WORK_DIR=$OSM_DOCKER_WORK_DIR"
275 echo "INSTALL_K8S_MONITOR=$INSTALL_K8S_MONITOR"
280 track k8scluster install_k8s_ok
281 init_kubeadm
$OSM_DOCKER_WORK_DIR/cluster-config.yaml
283 track k8scluster init_k8s_ok
284 if [ -n "$INSTALL_K8S_MONITOR" ]; then
285 # uninstall OSM MONITORING
286 uninstall_k8s_monitoring
287 track k8scluster uninstall_k8s_monitoring_ok
289 #remove old namespace
290 remove_k8s_namespace osm
294 track k8scluster install_helm_ok
295 install_k8s_storageclass
296 track k8scluster k8s_storageclass_ok
298 track k8scluster k8s_metallb_ok
300 track k8scluster k8s_ready_ok