Kubernetes optimised for Centos and Ubuntu 73/973/7
authorSrinivasan <srinivasan.s.n@huawei.com>
Tue, 11 Jun 2019 12:30:49 +0000 (12:30 +0000)
committerSrinivasan <srinivasan.s.n@huawei.com>
Thu, 27 Jun 2019 08:36:06 +0000 (08:36 +0000)
Optimized to use as non-root user as well as root user

Signed-off-by: Srinivasan <srinivasan.s.n@huawei.com>
Change-Id: I0e25f9b47ae6d79f148333d6782f32562943f039

scripts/common_centos.sh [new file with mode: 0755]
scripts/k8smaster_centos.sh [new file with mode: 0755]
scripts/setup.sh

diff --git a/scripts/common_centos.sh b/scripts/common_centos.sh
new file mode 100755 (executable)
index 0000000..5532ac2
--- /dev/null
@@ -0,0 +1,58 @@
+#!/bin/bash -ex
+
+##############################################################################
+# Copyright (c) 2019 Huawei Tech and others.                                 #
+#                                                                            #
+# All rights reserved. This program and the accompanying materials           #
+# are made available under the terms of the Apache License, Version 2.0      #
+# which accompanies this distribution, and is available at                   #
+# http://www.apache.org/licenses/LICENSE-2.0                                 #
+##############################################################################
+
+# constants
+
+DOCKER_VERSION=18.09.6
+KUBE_VERSION=1.15.0-0
+MACHINE=$(uname -m)
+
+# start
+
+# This script will install docker, kubeadm on both Eliot Master and Edge nodes
+
+sudo sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' \
+/etc/sysconfig/selinux
+
+sudo modprobe br_netfilter
+_conf='/etc/sysctl.d/99-akraino-eliot.conf'
+echo 'net.bridge.bridge-nf-call-iptables = 1' |& sudo tee "${_conf}"
+sudo sysctl -q -p "${_conf}"
+
+#echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
+
+swapoff -a
+
+sudo yum install -y yum-utils device-mapper-persistent-data lvm2
+
+sudo yum-config-manager \
+--add-repo https://download.docker.com/linux/centos/docker-ce.repo
+
+sudo yum install docker-ce-${DOCKER_VERSION} docker-ce-cli-${DOCKER_VERSION} \
+containerd.io
+
+# Kubernetes repository set
+
+cat <<-EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
+[kubernetes]
+name=Kubernetes
+baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-${MACHINE}
+enabled=1
+gpgcheck=1
+repo_gpgcheck=1
+gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
+        https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
+EOF
+
+sudo yum install -y kubeadm-${KUBE_VERSION}
+sudo systemctl start docker && sudo systemctl enable docker
+
+sudo systemctl daemon-reload
diff --git a/scripts/k8smaster_centos.sh b/scripts/k8smaster_centos.sh
new file mode 100755 (executable)
index 0000000..2681b6e
--- /dev/null
@@ -0,0 +1,45 @@
+#!/bin/bash -ex
+##############################################################################
+# Copyright (c) 2019 Huawei Tech and others.                                 #
+#                                                                            #
+# All rights reserved. This program and the accompanying materials           #
+# are made available under the terms of the Apache License, Version 2.0      #
+# which accompanies this distribution, and is available at                   #
+# http://www.apache.org/licenses/LICENSE-2.0                                 #
+##############################################################################
+
+# constants
+
+POD_NETWORK_CIDR=192.168.0.0/16
+KUBE_VERSION=1.15.0-0
+KUBERNETES_CNI=0.7.5-0
+
+# start
+
+hostname -I > hostname.tmp
+MASTER_IP="$(cut -d ' ' -f 1 hostname.tmp)"
+rm hostname.tmp
+
+# kubernetes installation
+
+sudo yum install -y kubelet-${KUBE_VERSION} kubectl-${KUBE_VERSION} \
+kubernetes-cni-${KUBERNETES_CNI}
+
+sudo systemctl start kubelet #&& sudo systemctl enable kubelet
+
+sudo docker info | grep -i cgroup
+sudo sed -i 's/cgroup-driver=systemd/cgroup-driver=cgroupfs/g' \
+/etc/systemd/system/kubelet.service.d/10-kubeadm.conf
+
+sudo systemctl daemon-reload
+sudo systemctl restart kubelet
+
+# Initialize kubernetes on master
+
+sudo kubeadm init \
+       --apiserver-advertise-address="${MASTER_IP}" \
+       --pod-network-cidr="${POD_NETWORK_CIDR}"
+
+mkdir -p "${HOME}/.kube"
+sudo cp -i /etc/kubernetes/admin.conf "${HOME}/.kube/config"
+sudo chown "$(id -u)":"$(id -g)" "${HOME}/.kube/config"
index e1b04a4..15e5a84 100755 (executable)
 # sshpass needs to be installed before executing this script.                          #
 ########################################################################################
 
+# constants
+
+OSPLATFORM=$(awk -F= '/^NAME/{print $2}' /etc/os-release)
+
+
 show_help()
 {
   echo "The script helps in setting up the ELIOT Toplogy Infrastrucutre"
@@ -36,7 +41,7 @@ show_help()
 # Installing Docker, K8S and Initializing K8S Master
 setup_k8smaster()
 {
-  set -o xtrace
+  #set -o xtrace
   sudo rm -rf ~/.kube
   source common.sh | tee eliotcommon.log
   source k8smaster.sh | tee kubeadm.log
@@ -56,7 +61,7 @@ setup_k8sworkers()
   #SETUP_WORKER_COMMON="cd eliot/scripts && source common.sh"
   SETUP_WORKER="cd eliot/scripts/ && source k8sworker.sh"
 
-  KUBEADM_JOIN=$(grep "kubeadm join " ./kubeadm.log)
+  KUBEADM_JOIN=$(grep "kubeadm join" ./kubeadm.log)
   KUBEADM_JOIN="sudo ${KUBEADM_JOIN}"
 
  # Read all the Worker Node details from nodelist file.
@@ -73,6 +78,48 @@ setup_k8sworkers()
 
 }
 
+setup_k8smaster_centos()
+{
+  set -o xtrace
+  sudo rm -rf ~/.kube
+  source common_centos.sh | tee eliotcommon_centos.log
+  source k8smaster_centos.sh | tee kubeadm_centos.log
+
+  # Setup ELIOT Node
+  setup_k8sworkers_centos
+
+  cd cni/calico
+  kubectl apply -f rbac.yaml
+  kubectl apply -f calico.yaml
+}
+
+
+setup_k8sworkers_centos()
+{
+  set -o xtrace
+  # Install Docker on ELIOT Node
+
+  ELIOT_REPO="https://gerrit.akraino.org/r/eliot"
+  SETUP_WORKER_COMMON_CENTOS="sudo rm -rf ~/eliot &&\
+                              git clone ${ELIOT_REPO} &&\
+                              cd eliot/scripts && source common_centos.sh"
+
+  KUBEADM_TOKEN=$(sudo kubeadm token create --print-join-command)
+  KUBEADM_JOIN_CENTOS="sudo ${KUBEADM_TOKEN}"
+
+ # Read all the Worker Node details from nodelist file.
+ while read line
+ do
+     nodeinfo="${line}"
+     nodeusr=$(echo ${nodeinfo} | cut -d"|" -f1)
+     nodeip=$(echo ${nodeinfo} | cut -d"|" -f2)
+     nodepaswd=$(echo ${nodeinfo} | cut -d"|" -f3)
+     sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${SETUP_WORKER_COMMON_CENTOS} < /dev/null
+     #sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${SETUP_WORKER_CENTOS} < /dev/null
+     sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${KUBEADM_JOIN_CENTOS} < /dev/null
+ done < nodelist
+
+}
 
 #verify kubernetes setup by deploying nginx server.
 
@@ -108,13 +155,16 @@ then
   exit 0
 fi
 
+if [[ $OSPLATFORM = *CentOS* ]]; then
+   setup_k8smaster_centos
+else
+   setup_k8smaster
+fi
 
-setup_k8smaster
 sleep 20
 verify_k8s_status
-
 install_cadvisor_edge
 sleep 10
 install_prometheus
-sleep 5 
+sleep 5
 sudo docker ps | grep prometheus