Sync bootstrap scripts from Akraino IEC wiki 99/399/2
authorAlexandru Avadanii <Alexandru.Avadanii@enea.com>
Wed, 13 Feb 2019 14:58:41 +0000 (15:58 +0100)
committerAlexandru Avadanii <Alexandru.Avadanii@enea.com>
Wed, 13 Feb 2019 15:09:15 +0000 (16:09 +0100)
Bring in sh scripts from IEC Wiki page [1]:

The initial setup scripts to be executed on all cluster nodes:
- k8s_common:
  * prerequisites;
  * Kubernetes Install for Ubuntu, using Kubeadm;

The following scripts should be executed on K8s master node:
- k8s_master:
  * setup host as K8s master;
- calico:
  * setup etcd, RBAC roles, calico;
- nginx:
  * verify basic K8s functionality;
- helm:
  * bring in `tiller` and `helm` binaries;

There is one more step that is not covered by the current scripts:
- K8s slave nodes should execute `kubeadm join` after the k8s_master
  script has completed;

[1] https://wiki.akraino.org/display/AK/\
    IEC+Blueprints+Installation+Overview

Change-Id: I8d4f78b7482486e2c4a31db35cddb0c1570307a5
Signed-off-by: Alexandru Avadanii <Alexandru.Avadanii@enea.com>
scripts/calico.sh [new file with mode: 0755]
scripts/helm.sh [new file with mode: 0755]
scripts/k8s_common.sh [new file with mode: 0755]
scripts/k8s_master.sh [new file with mode: 0755]
scripts/nginx.sh [new file with mode: 0755]

diff --git a/scripts/calico.sh b/scripts/calico.sh
new file mode 100755 (executable)
index 0000000..922d226
--- /dev/null
@@ -0,0 +1,29 @@
+#!/bin/bash -ex
+
+CLUSTER_IP=${1:-172.16.1.136} # Align with the value in our K8s setup script
+CALICO_URI_ROOT=https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation
+
+# Install the Etcd Database
+if [ "$(uname -m)" == 'aarch64' ]; then
+  ETCD_YAML=https://raw.githubusercontent.com/Jingzhao123/arm64TemporaryCalico/temporay_arm64/v3.3/getting-started/kubernetes/installation/hosted/etcd-arm64.yaml
+else
+  ETCD_YAML=${CALICO_URI_ROOT}/hosted/etcd.yaml
+fi
+wget -O etcd.yaml "${ETCD_YAML}"
+sed -i "s/10.96.232.136/${CLUSTER_IP}/" etcd.yaml
+kubectl apply -f etcd.yaml
+
+# Install the RBAC Roles required for Calico
+kubectl apply -f "${CALICO_URI_ROOT}/rbac.yaml"
+
+# Install Calico to system
+wget -O calico.yaml "${CALICO_URI_ROOT}/hosted/calico.yaml"
+sed -i "s/10.96.232.136/${CLUSTER_IP}/" calico.yaml
+if [ "$(uname -m)" == 'aarch64' ]; then
+  sed -i "s/quay.io\/calico/calico/" calico.yaml
+fi
+# FIXME: IP_AUTODETECTION_METHOD?
+kubectl apply -f calico.yaml
+
+# Remove the taints on master node
+kubectl taint nodes --all node-role.kubernetes.io/master- || true
diff --git a/scripts/helm.sh b/scripts/helm.sh
new file mode 100755 (executable)
index 0000000..524a770
--- /dev/null
@@ -0,0 +1,18 @@
+#!/bin/bash -ex
+
+VERSION='v2.12.3'
+if [ "$(uname -m)" == 'aarch64' ]; then
+  FLAVOR='linux-arm64'
+else
+  FLAVOR='linux-amd64'
+fi
+
+URI_ROOT='https://storage.googleapis.com/kubernetes-helm'
+TGZ_NAME="helm-${VERSION}-${FLAVOR}.tar.gz"
+
+if [ ! -e /usr/bin/helm ] || [ ! -e /usr/bin/tiller ]; then
+  wget -O "/tmp/${TGZ_NAME}" "${URI_ROOT}/${TGZ_NAME}"
+  sudo tar xpPf "/tmp/${TGZ_NAME}" --overwrite \
+    --transform "s|${FLAVOR}|/usr/bin|" "${FLAVOR}/"{helm,tiller}
+  rm -f "/tmp/${TGZ_NAME}"
+fi
diff --git a/scripts/k8s_common.sh b/scripts/k8s_common.sh
new file mode 100755 (executable)
index 0000000..bf3216f
--- /dev/null
@@ -0,0 +1,34 @@
+#!/bin/bash -ex
+
+DOCKER_VERSION=18.06.1~ce~3-0~ubuntu
+KUBE_VERSION=1.13.0-00
+
+# Install Docker as Prerequisite
+curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
+sudo apt-key fingerprint 0EBFCD88
+sudo add-apt-repository \
+  "deb https://download.docker.com/linux/ubuntu \
+  $(lsb_release -cs) \
+  stable"
+sudo apt update
+sudo apt install -y docker-ce=${DOCKER_VERSION}
+
+# Disable swap on your machine
+sudo swapoff -a
+
+# Install Kubernetes with Kubeadm
+sudo apt update
+sudo apt install -y apt-transport-https curl
+curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
+
+cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
+deb https://apt.kubernetes.io/ kubernetes-xenial main
+EOF
+sudo apt update
+sudo apt install -y \
+  kubelet=${KUBE_VERSION} kubeadm=${KUBE_VERSION} kubectl=${KUBE_VERSION}
+apt-mark hold kubelet kubeadm kubectl
+
+_conf='/etc/sysctl.d/99-akraino-iec.conf'
+echo 'net.bridge.bridge-nf-call-iptables = 1' |& sudo tee "${_conf}"
+sudo sysctl -q -p "${_conf}"
diff --git a/scripts/k8s_master.sh b/scripts/k8s_master.sh
new file mode 100755 (executable)
index 0000000..1782769
--- /dev/null
@@ -0,0 +1,30 @@
+#!/bin/bash -ex
+
+# For host setup as Kubernetes master
+MGMT_IP=$1
+POD_NETWORK_CIDR=${2:-192.168.0.0/16}
+SERVICE_CIDR=${3:-172.16.1.0/24}
+
+if [ -z "${MGMT_IP}" ]; then
+  echo "Please specify a management IP!"
+  exit 1
+fi
+
+if ! kubectl get nodes; then
+  sudo kubeadm config images pull
+  sudo kubeadm init \
+    --pod-network-cidr="${POD_NETWORK_CIDR}" \
+    --apiserver-advertise-address="${MGMT_IP}" \
+    --service-cidr="${SERVICE_CIDR}"
+
+  if [ "$(id -u)" = 0 ]; then
+    echo "export KUBECONFIG=/etc/kubernetes/admin.conf" | \
+      tee -a "${HOME}/.profile"
+    # shellcheck disable=SC1090
+    source "${HOME}/.profile"
+  else
+    mkdir -p "${HOME}/.kube"
+    sudo cp -i /etc/kubernetes/admin.conf "${HOME}/.kube/config"
+    sudo chown "$(id -u)":"$(id -g)" "${HOME}/.kube/config"
+  fi
+fi
diff --git a/scripts/nginx.sh b/scripts/nginx.sh
new file mode 100755 (executable)
index 0000000..40a9377
--- /dev/null
@@ -0,0 +1,64 @@
+#!/bin/bash -ex
+
+NGINX_APP=~/nginx-app.yaml
+
+cat <<EOF > "${NGINX_APP}"
+apiVersion: v1
+kind: Service
+metadata:
+  name: nginx
+  labels:
+    app: nginx
+spec:
+  type: NodePort
+  ports:
+  - port: 80
+    protocol: TCP
+    name: http
+  selector:
+    app: nginx
+---
+apiVersion: v1
+kind: ReplicationController
+metadata:
+  name: nginx
+spec:
+  replicas: 2
+  template:
+    metadata:
+      labels:
+        app: nginx
+    spec:
+      containers:
+      - name: nginx
+        image: nginx
+        ports:
+        - containerPort: 80
+EOF
+
+if ! kubectl get services | grep -q nginx; then
+  kubectl create -f "${NGINX_APP}"
+fi
+kubectl get nodes
+kubectl get services
+kubectl get pods
+kubectl get rc
+
+attempts=60
+while [ $attempts -gt 0 ]
+do
+  if [ 3 == "$(kubectl get pods | grep -c -e STATUS -e Running)" ]; then
+    break
+  fi
+  ((attempts-=1))
+  sleep 10
+done
+[ $attempts -gt 0 ] || exit 1
+
+svcip=$(kubectl get services nginx  -o json | grep clusterIP | cut -f4 -d'"')
+sleep 10
+wget "http://$svcip"
+kubectl delete -f "${NGINX_APP}"
+kubectl get rc
+kubectl get pods
+kubectl get services