Improving k8s cluster install scriipt 47/2547/3
authorwrider <lji@research.att.com>
Thu, 20 Feb 2020 17:19:16 +0000 (12:19 -0500)
committerLusheng Ji <lji@research.att.com>
Fri, 21 Feb 2020 21:46:07 +0000 (21:46 +0000)
Making the script more rebust for reinstalling k8s stack.

Change-Id: I68e6a225952b74532bca4b364a95056b9bcc34e4
Signed-off-by: wrider <lji@research.att.com>
tools/k8s/bin/gen-cloud-init.sh
tools/k8s/heat/scripts/k8s_vm_install.sh

index d02ec1d..c0fe160 100755 (executable)
@@ -144,8 +144,11 @@ sed -i -e "s/__stack_name__/\$(hostname)/g" "$filename"
 
 # because cloud init user data has a 16kB limit, remove all comment lines to save space.
 # except for the #! line
-sed -i -e '/^[ \t]*#[^!]/d' "$filename" 
+sed -i -e '/^[ \t]*#[^!]/d' "$filename"
 
 chmod +x "$filename"
 
-mv "$filename" ./k8s-1node-cloud-init.sh
+K8SV=$(echo ${INFRA_K8S_VERSION} | cut -f 1-2 -d '.' | sed -e 's/\./_/g')
+HV=$(echo ${INFRA_HELM_VERSION} | cut -f 1-2 -d '.' | sed -e 's/\./_/g')
+DV=$(echo ${INFRA_DOCKER_VERSION} | cut -f 1-2 -d '.' | sed -e 's/\./_/g')
+mv "$filename" ./k8s-1node-cloud-init-k_${K8SV:-cur}-h_${HV:-cur}-d_${DV:-cur}.sh
index 2653f38..77641fa 100644 (file)
@@ -148,7 +148,7 @@ echo "APT::Acquire::Retries \"3\";" > /etc/apt/apt.conf.d/80-retries
 
 # install low latency kernel, docker.io, and kubernetes
 apt-get update
-apt-get -y autoremove
+
 RES=$(apt-get install -y virt-what curl jq netcat 2>&1)
 if [[ $RES == */var/lib/dpkg/lock* ]]; then
   echo "Fail to get dpkg lock.  Wait for any other package installation"
@@ -162,14 +162,25 @@ if ! echo $(virt-what) | grep "virtualbox"; then
   apt-get install -y linux-image-4.15.0-45-lowlatency
 fi
 
-if kubeadm version; then
-  # remove existing Kubernetes installation
-  echo "Removing existing Kubernetes installation, version $(kubeadm version)"
-  kubeadm reset -f
-  rm -rf ~/.kube
-fi
-
 APTOPTS="--allow-downgrades --allow-change-held-packages --allow-unauthenticated --ignore-hold "
+
+# remove infrastructure stack if present
+# note the order of the packages being removed.
+for PKG in kubeadm docker.io; do
+  INSTALLED_VERSION=$(dpkg --list |grep ${PKG} |tr -s " " |cut -f3 -d ' ')
+  if [ ! -z ${INSTALLED_VERSION} ]; then
+    if [ "${PKG}" == "kubeadm" ]; then
+      kubeadm reset -f
+      rm -rf ~/.kube
+      apt-get -y $APTOPTS remove kubeadm kubelet kubectl kubernetes-cni
+    else
+      apt-get -y $APTOPTS remove "${PKG}"
+    fi
+  fi
+done
+apt-get -y autoremove
+
+# install docker
 if [ -z ${DOCKERVERSION} ]; then
   apt-get install -y $APTOPTS docker.io
 else
@@ -308,11 +319,7 @@ EOF
   kubectl get pods --all-namespaces
 
   # install flannel
-  if [[ ${KUBEV} == 1.16.* ]]; then
-    kubectl apply -f "https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
-  else
-    kubectl apply -f "https://raw.githubusercontent.com/coreos/flannel/bc79dd1505b0c8681ece4de4c0d86c5cd2643275/Documentation/kube-flannel.yml"
-  fi
+  kubectl apply -f "https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
 
   # waiting for all 8 kube-system pods to be in running state
   # (at this point, minions have not joined yet)
@@ -328,9 +335,11 @@ EOF
   # install Helm
   HELMV=$(cat /opt/config/helm_version.txt)
   HELMVERSION=${HELMV}
+  if [ ! -e helm-v${HELMVERSION}-linux-amd64.tar.gz ]; then
+    wget https://storage.googleapis.com/kubernetes-helm/helm-v${HELMVERSION}-linux-amd64.tar.gz
+  fi
   cd /root && rm -rf Helm && mkdir Helm && cd Helm
-  wget https://storage.googleapis.com/kubernetes-helm/helm-v${HELMVERSION}-linux-amd64.tar.gz
-  tar -xvf helm-v${HELMVERSION}-linux-amd64.tar.gz
+  tar -xvf ../helm-v${HELMVERSION}-linux-amd64.tar.gz
   mv linux-amd64/helm /usr/local/bin/helm
 
   rm -rf /root/.helm
@@ -347,7 +356,6 @@ EOF
   export HELM_HOME="/root/.helm"
 
   # waiting for tiller pod to be in running state
-  wait_for_pods_running 1 kube-system tiller-deploy
   while ! helm version; do
     echo "Waiting for Helm to be ready"
     sleep 15
@@ -395,4 +403,4 @@ EOF
   docker pull ${__RUNRICENV_DOCKER_HOST__}:${__RUNRICENV_DOCKER_PORT__}/whoami:0.0.1
 fi
 
-if [ "$(uname -r)" != "4.15.0-45-lowlatency" ]; then reboot; fi
+if [ "$(uname -r)" != "4.15.0-45-lowlatency" ]; then reboot; fi
\ No newline at end of file