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".2-1.1
21 # installs kubernetes packages
22 function install_kube
() {
23 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
24 # Kubernetes releases can be found here: https://kubernetes.io/releases/
25 # To check other available versions, run the following command
26 # curl -s https://packages.cloud.google.com/apt/dists/kubernetes-xenial/main/binary-amd64/Packages | grep Version | awk '{print $2}'
27 sudo apt-get
-y update
&& sudo apt-get
install -y apt-transport-https ca-certificates curl
28 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
29 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
30 sudo apt-get
-y update
31 echo "Installing Kubernetes Packages ..."
32 sudo apt-get
install -y kubelet
=${K8S_PACKAGE_VERSION} kubeadm=${K8S_PACKAGE_VERSION} kubectl=${K8S_PACKAGE_VERSION}
33 sudo apt-mark hold kubelet kubeadm kubectl
34 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
37 # check and track kube packages installation
38 function check_and_track_kube_install
() {
39 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
40 kubelet_version
=$
(dpkg
-s kubelet|
grep Version|
awk '{print $2}')
41 [ -n "${kubelet_version}" ] || FATAL_TRACK k8scluster
"Kubelet was not installed."
42 kubeadm_version
=$
(dpkg
-s kubeadm|
grep Version|
awk '{print $2}')
43 [ -n "${kubeadm_version}" ] || FATAL_TRACK k8scluster
"Kubeadm was not installed."
44 kubectl_version
=$
(dpkg
-s kubectl|
grep Version|
awk '{print $2}')
45 [ -n "${kubectl_version}" ] || FATAL_TRACK k8scluster
"Kubectl was not installed."
46 track k8scluster install_k8s_ok none none none kubelet
${kubelet_version} none none kubeadm ${kubeadm_version} none none kubectl ${kubectl_version} none none
47 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
50 # initializes kubernetes control plane
51 function init_kubeadm
() {
52 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
54 sudo
sed -i.bak
'/.*none.*swap/s/^\(.*\)$/#\1/g' /etc
/fstab
55 sudo kubeadm init
--config $1 --dry-run || FATAL_TRACK k8scluster
"kubeadm init dry-run failed"
56 sudo kubeadm init
--config $1
58 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
61 # Initializes kubeconfig file
62 function kube_config_dir
() {
63 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
64 K8S_MANIFEST_DIR
="/etc/kubernetes/manifests"
65 [ ! -d $K8S_MANIFEST_DIR ] && FATAL_TRACK k8scluster
"Kubernetes folder $K8S_MANIFEST_DIR was not found"
67 sudo
cp /etc
/kubernetes
/admin.conf
$HOME/.kube
/config
68 sudo chown $
(id
-u):$
(id
-g) $HOME/.kube
/config
69 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
72 # test kubernetes installation
73 function check_and_track_init_k8s
() {
74 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
75 echo "Reading existing namespaces"
76 kubectl get ns || FATAL_TRACK k8scluster
"Failed getting namespaces"
77 track k8scluster init_k8s_ok
78 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
81 # deploys flannel as daemonsets
82 function deploy_cni_provider
() {
83 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
84 CNI_DIR
="$(mktemp -d -q --tmpdir "flannel.XXXXXX
")"
85 trap 'rm -rf "${CNI_DIR}"' EXIT
86 KUBE_FLANNEL_FILE_URL
="https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
87 wget
--retry-on-host-error --retry-on-http-error 404,429,503 --tries=5 "${KUBE_FLANNEL_FILE_URL}" -P $CNI_DIR
88 [ ! -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"
89 kubectl apply
-f $CNI_DIR
90 [ $?
-ne 0 ] && FATAL_TRACK k8scluster
"Cannot Install Flannel"
91 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
94 # taints K8s master node
95 function taint_master_node
() {
96 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
97 K8S_MASTER
=$
(kubectl get nodes |
awk '$3~/control-plane/'|
awk '{print $1; exit}')
98 kubectl taint node
$K8S_MASTER node-role.kubernetes.io
/control-plane
:NoSchedule-
100 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
103 # check and track kube packages installation
104 function check_and_track_k8s_ready_before_helm
() {
105 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
106 kubectl get events || FATAL_TRACK k8scluster
"Failed getting events"
107 track k8scluster k8s_ready_before_helm
108 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
112 #Helm releases can be found here: https://github.com/helm/helm/releases
113 function install_helm
() {
114 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
115 HELM_VERSION
="v3.11.3"
116 if ! [[ "$(helm version --short 2>/dev/null)" =~ ^v3.
* ]]; then
117 # Helm is not installed. Install helm
118 echo "Helm3 is not installed, installing ..."
119 curl https
://get.helm.sh
/helm-
${HELM_VERSION}-linux-amd64.
tar.gz
--output helm-
${HELM_VERSION}.
tar.gz
120 tar -zxvf helm-
${HELM_VERSION}.
tar.gz
121 sudo
mv linux-amd64
/helm
/usr
/local
/bin
/helm
123 rm helm-
${HELM_VERSION}.
tar.gz
125 echo "Helm3 is already installed. Skipping installation..."
127 helm version || FATAL_TRACK k8scluster
"Could not obtain helm version. Maybe helm client was not installed"
128 helm repo add stable https
://charts.helm.sh
/stable || FATAL_TRACK k8scluster
"Helm repo stable could not be added"
129 helm repo update || FATAL_TRACK k8scluster
"Helm repo stable could not be updated"
130 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
133 function install_k8s_storageclass
() {
134 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
135 # Openebs versions can be found here: https://github.com/openebs/openebs/releases
136 OPENEBS_VERSION
="3.7.0"
137 echo "Installing OpenEBS"
138 helm repo add openebs https
://openebs.github.io
/charts
140 helm
install --create-namespace --namespace openebs openebs openebs
/openebs
--version ${OPENEBS_VERSION}
142 local storageclass_timeout
=400
144 local storageclass_ready
=""
145 echo "Waiting for storageclass"
146 while (( counter
< storageclass_timeout
))
148 kubectl get storageclass openebs-hostpath
&> /dev
/null
150 if [ $?
-eq 0 ] ; then
151 echo "Storageclass available"
152 storageclass_ready
="y"
155 counter
=$
((counter
+ 15))
159 [ -n "$storageclass_ready" ] || FATAL_TRACK k8scluster
"Storageclass not ready after $storageclass_timeout seconds. Cannot install openebs"
160 kubectl
patch storageclass openebs-hostpath
-p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
161 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
164 #installs metallb from helm
165 function install_helm_metallb
() {
166 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
167 echo "Installing MetalLB"
168 METALLB_VERSION
="0.13.10"
169 helm repo add metallb https
://metallb.github.io
/metallb
171 helm
install --create-namespace --namespace metallb-system metallb metallb
/metallb
--version ${METALLB_VERSION}
172 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
175 function configure_ipaddresspool_metallb
() {
176 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
177 echo "Creating IP address pool manifest: ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml"
178 METALLB_IP_RANGE
="$DEFAULT_IP/32"
179 echo "apiVersion: metallb.io/v1beta1
183 namespace: metallb-system
186 - ${METALLB_IP_RANGE}" | sudo
tee -a ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml
187 echo "Applying IP address pool manifest: kubectl apply -f ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml"
188 kubectl apply
-f ${OSM_CLUSTER_WORK_DIR}/metallb-ipaddrpool.yaml || FATAL_TRACK k8scluster
"Cannot create IP address Pool in MetalLB"
189 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
192 #installs cert-manager
193 function install_helm_certmanager
() {
194 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
195 echo "Installing cert-manager"
196 CERTMANAGER_VERSION
="v1.9.1"
197 helm repo add jetstack https
://charts.jetstack.io
199 helm
install cert-manager
--create-namespace --namespace cert-manager jetstack
/cert-manager \
200 --version ${CERTMANAGER_VERSION} --set installCRDs
=true
--set prometheus.enabled
=false \
201 --set clusterResourceNamespace
=osm \
202 --set extraArgs
="{--enable-certificate-owner-ref=true}"
203 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
206 #checks openebs and metallb readiness
207 function check_for_readiness
() {
208 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
209 # Default input values
210 sampling_period
=2 # seconds
211 time_for_readiness
=20 # seconds ready
212 time_for_failure
=200 # seconds broken
213 OPENEBS_NAMESPACE
=openebs
214 METALLB_NAMESPACE
=metallb-system
215 CERTMANAGER_NAMESPACE
=cert-manager
216 # STACK_NAME=osm # By default, "osm"
218 # Equivalent number of samples
219 oks_threshold
=$
((time_for_readiness
/${sampling_period})) # No. ok samples to declare the system ready
220 failures_threshold
=$
((time_for_failure
/${sampling_period})) # No. nok samples to declare the system broken
224 ####################################################################################
225 # Loop to check system readiness
226 ####################################################################################
227 while [[ (${failures_in_a_row} -lt ${failures_threshold}) && (${oks_in_a_row} -lt ${oks_threshold}) ]]
230 OPENEBS_STATE
=$
(kubectl get pod
-n ${OPENEBS_NAMESPACE} --no-headers 2>&1)
231 OPENEBS_READY
=$
(echo "${OPENEBS_STATE}" |
awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
232 OPENEBS_NOT_READY
=$
(echo "${OPENEBS_STATE}" |
awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
233 COUNT_OPENEBS_READY
=$
(echo "${OPENEBS_READY}"|
grep -v -e '^$' |
wc -l)
234 COUNT_OPENEBS_NOT_READY
=$
(echo "${OPENEBS_NOT_READY}" |
grep -v -e '^$' |
wc -l)
237 METALLB_STATE
=$
(kubectl get pod
-n ${METALLB_NAMESPACE} --no-headers 2>&1)
238 METALLB_READY
=$
(echo "${METALLB_STATE}" |
awk '$2=="1/1" || $2=="4/4" {printf ("%s\t%s\t\n", $1, $2)}')
239 METALLB_NOT_READY
=$
(echo "${METALLB_STATE}" |
awk '$2!="1/1" && $2!="4/4" {printf ("%s\t%s\t\n", $1, $2)}')
240 COUNT_METALLB_READY
=$
(echo "${METALLB_READY}" |
grep -v -e '^$' |
wc -l)
241 COUNT_METALLB_NOT_READY
=$
(echo "${METALLB_NOT_READY}" |
grep -v -e '^$' |
wc -l)
243 # State of CertManager
244 CERTMANAGER_STATE
=$
(kubectl get pod
-n ${CERTMANAGER_NAMESPACE} --no-headers 2>&1)
245 CERTMANAGER_READY
=$
(echo "${CERTMANAGER_STATE}" |
awk '$2=="1/1" || $2=="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
246 CERTMANAGER_NOT_READY
=$
(echo "${CERTMANAGER_STATE}" |
awk '$2!="1/1" && $2!="2/2" {printf ("%s\t%s\t\n", $1, $2)}')
247 COUNT_CERTMANAGER_READY
=$
(echo "${CERTMANAGER_READY}" |
grep -v -e '^$' |
wc -l)
248 COUNT_CERTMANAGER_NOT_READY
=$
(echo "${CERTMANAGER_NOT_READY}" |
grep -v -e '^$' |
wc -l)
251 if [[ $
((${COUNT_OPENEBS_NOT_READY}+${COUNT_METALLB_NOT_READY})) -eq 0 ]]
255 echo -ne ===\
> Successful checks
: "${oks_in_a_row}"/${oks_threshold}\\r
258 ((++failures_in_a_row
))
261 echo Bootstraping...
"${failures_in_a_row}" checks of
${failures_threshold}
263 # Reports failed pods in OpenEBS
264 if [[ "${COUNT_OPENEBS_NOT_READY}" -ne 0 ]]
266 echo "OpenEBS: Waiting for ${COUNT_OPENEBS_NOT_READY} of $((${COUNT_OPENEBS_NOT_READY}+${COUNT_OPENEBS_READY})) pods to be ready:"
267 echo "${OPENEBS_NOT_READY}"
271 # Reports failed pods in MetalLB
272 if [[ "${COUNT_METALLB_NOT_READY}" -ne 0 ]]
274 echo "MetalLB: Waiting for ${COUNT_METALLB_NOT_READY} of $((${COUNT_METALLB_NOT_READY}+${COUNT_METALLB_READY})) pods to be ready:"
275 echo "${METALLB_NOT_READY}"
279 # Reports failed pods in CertManager
280 if [[ "${COUNT_CERTMANAGER_NOT_READY}" -ne 0 ]]
282 echo "CertManager: Waiting for ${COUNT_CERTMANAGER_NOT_READY} of $((${COUNT_CERTMANAGER_NOT_READY}+${COUNT_CERTMANAGER_READY})) pods to be ready:"
283 echo "${CERTMANAGER_NOT_READY}"
288 #------------ NEXT SAMPLE
289 sleep ${sampling_period}
292 ####################################################################################
294 ####################################################################################
295 if [[ (${failures_in_a_row} -ge ${failures_threshold}) ]]
298 FATAL_TRACK k8scluster
"K8S CLUSTER IS BROKEN"
301 echo "K8S CLUSTER IS READY"
303 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
306 #removes osm deployments and services
307 function remove_k8s_namespace
() {
308 [ -z "${DEBUG_INSTALL}" ] || DEBUG beginning of
function
309 echo "Deleting existing namespace $1: kubectl delete ns $1"
310 kubectl delete ns
$1 2>/dev
/null
311 [ -z "${DEBUG_INSTALL}" ] || DEBUG end of
function
315 while getopts ":D:d:i:-: " o
; do
318 DEFAULT_IP
="${OPTARG}"
321 OSM_CLUSTER_WORK_DIR
="${OPTARG}"
324 OSM_DEVOPS
="${OPTARG}"
327 [ "${OPTARG}" == "debug" ] && DEBUG_INSTALL
="y" && continue
328 echo -e "Invalid option: '--$OPTARG'\n" >&2
332 echo "Option -$OPTARG requires an argument" >&2
336 echo -e "Invalid option: '-$OPTARG'\n" >&2
345 source $OSM_DEVOPS/common
/logging
346 source $OSM_DEVOPS/common
/track
348 echo "DEBUG_INSTALL=$DEBUG_INSTALL"
349 echo "DEFAULT_IP=$DEFAULT_IP"
350 echo "OSM_DEVOPS=$OSM_DEVOPS"
351 echo "OSM_CLUSTER_WORK_DIR=$OSM_CLUSTER_WORK_DIR"
352 echo "INSTALL_K8S_MONITOR=$INSTALL_K8S_MONITOR"
355 echo "Creating folders for installation"
356 [ ! -d "$OSM_CLUSTER_WORK_DIR" ] && sudo mkdir
-p $OSM_CLUSTER_WORK_DIR
357 echo "Copying kubeadm-config from $OSM_DEVOPS/installers/kubeadm-config.yaml to $OSM_CLUSTER_WORK_DIR/kubeadm-config.yaml"
358 sudo
cp -b $OSM_DEVOPS/installers
/kubeadm-config.yaml
$OSM_CLUSTER_WORK_DIR/kubeadm-config.yaml
361 check_and_track_kube_install
363 init_kubeadm
$OSM_CLUSTER_WORK_DIR/kubeadm-config.yaml
365 check_and_track_init_k8s
369 check_and_track_k8s_ready_before_helm
371 remove_k8s_namespace osm
373 # install_helm has been moved to install_helm_client.sh, run from full_install_osm.sh,
374 # but tracking is still here because the installation analytics still expects it
375 track k8scluster install_helm_ok
377 install_k8s_storageclass
378 track k8scluster k8s_storageclass_ok
380 track k8scluster k8s_metallb_ok
381 install_helm_certmanager
382 track k8scluster k8s_certmanager_ok
384 track k8scluster k8s_ready_ok
385 configure_ipaddresspool_metallb