Kubernetes optimised for Centos and Ubuntu
[eliot.git] / scripts / setup.sh
1 #!/bin/bash -ex
2 ##############################################################################
3 # Copyright (c) 2019 Huawei Tech and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 ########################################################################################
12 #                                                                                      #
13 # The script is to setup the ELIOT Manager and ELIOT nodes.                            #
14 # It installs Docker in both ELIOT Manager and ELIOT node.                             #
15 # It installs Kubernetes. In the ELIOT Manager kubeadm, kubelet, kubectl is installed. #
16 # In ELIOT Edge Node it will install kubeadn, kubelet.                                 #
17 # Script is tested in Ubuntu 16.04 version.                                            #
18 # sshpass needs to be installed before executing this script.                          #
19 ########################################################################################
20
21 # constants
22
23 OSPLATFORM=$(awk -F= '/^NAME/{print $2}' /etc/os-release)
24
25
26 show_help()
27 {
28   echo "The script helps in setting up the ELIOT Toplogy Infrastrucutre"
29   echo "The setup installs Docker, K8S Master and K8S worker nodes in  "
30   echo "ELIOT Manager and ELIOT Workder Nodes respectively "
31   echo "After completion of script execution execute command: "
32   echo "kubectl get nodes to check whether the connection between "
33   echo "ELIOT Manager and ELIOT Nodes are established"
34   echo ""
35   echo "Nodelist file should have the details of Worker Nodes in the format of:"
36   echo "EliotNodeUserName|EliotNodeIP|EliotNodePasswor"
37   echo "Each line should have detail of one ELIOT Node only"
38 }
39
40 # Setting up ELIOT Manager Node.
41 # Installing Docker, K8S and Initializing K8S Master
42 setup_k8smaster()
43 {
44   #set -o xtrace
45   sudo rm -rf ~/.kube
46   source common.sh | tee eliotcommon.log
47   source k8smaster.sh | tee kubeadm.log
48   # Setup ELIOT Node
49   setup_k8sworkers
50 }
51
52 setup_k8sworkers()
53 {
54   set -o xtrace
55
56   # Install Docker on ELIOT Node
57   ELIOT_REPO="https://gerrit.akraino.org/r/eliot"
58   SETUP_WORKER_COMMON="sudo rm -rf ~/eliot &&\
59                        git clone ${ELIOT_REPO} &&\
60                        cd eliot/scripts && source common.sh"
61   #SETUP_WORKER_COMMON="cd eliot/scripts && source common.sh"
62   SETUP_WORKER="cd eliot/scripts/ && source k8sworker.sh"
63
64   KUBEADM_JOIN=$(grep "kubeadm join" ./kubeadm.log)
65   KUBEADM_JOIN="sudo ${KUBEADM_JOIN}"
66
67  # Read all the Worker Node details from nodelist file.
68  while read line
69  do
70      nodeinfo="${line}"
71      nodeusr=$(echo ${nodeinfo} | cut -d"|" -f1)
72      nodeip=$(echo ${nodeinfo} | cut -d"|" -f2)
73      nodepaswd=$(echo ${nodeinfo} | cut -d"|" -f3)
74      sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${SETUP_WORKER_COMMON} < /dev/null
75      sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${SETUP_WORKER} < /dev/null
76      sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${KUBEADM_JOIN} < /dev/null
77  done < nodelist > /dev/null 2>&1
78
79 }
80
81 setup_k8smaster_centos()
82 {
83   set -o xtrace
84   sudo rm -rf ~/.kube
85   source common_centos.sh | tee eliotcommon_centos.log
86   source k8smaster_centos.sh | tee kubeadm_centos.log
87
88   # Setup ELIOT Node
89   setup_k8sworkers_centos
90
91   cd cni/calico
92   kubectl apply -f rbac.yaml
93   kubectl apply -f calico.yaml
94 }
95
96
97 setup_k8sworkers_centos()
98 {
99   set -o xtrace
100   # Install Docker on ELIOT Node
101
102   ELIOT_REPO="https://gerrit.akraino.org/r/eliot"
103   SETUP_WORKER_COMMON_CENTOS="sudo rm -rf ~/eliot &&\
104                               git clone ${ELIOT_REPO} &&\
105                               cd eliot/scripts && source common_centos.sh"
106
107   KUBEADM_TOKEN=$(sudo kubeadm token create --print-join-command)
108   KUBEADM_JOIN_CENTOS="sudo ${KUBEADM_TOKEN}"
109
110  # Read all the Worker Node details from nodelist file.
111  while read line
112  do
113      nodeinfo="${line}"
114      nodeusr=$(echo ${nodeinfo} | cut -d"|" -f1)
115      nodeip=$(echo ${nodeinfo} | cut -d"|" -f2)
116      nodepaswd=$(echo ${nodeinfo} | cut -d"|" -f3)
117      sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${SETUP_WORKER_COMMON_CENTOS} < /dev/null
118      #sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${SETUP_WORKER_CENTOS} < /dev/null
119      sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${KUBEADM_JOIN_CENTOS} < /dev/null
120  done < nodelist
121
122 }
123
124 #verify kubernetes setup by deploying nginx server.
125
126 verify_k8s_status(){
127   set -o xtrace
128   source verifyk8s.sh | tee verifyk8s.log
129 }
130
131 install_cadvisor_edge(){
132  set -o xtrace
133  SETUP_CADVISOR_ATEDGE="cd eliot/scripts/ && source cadvisorsetup.sh" 
134  while read line
135  do
136      nodeinfo="${line}"
137      nodeusr=$(echo ${nodeinfo} | cut -d"|" -f1)
138      nodeip=$(echo ${nodeinfo} | cut -d"|" -f2)
139      nodepaswd=$(echo ${nodeinfo} | cut -d"|" -f3)
140      sshpass -p ${nodepaswd} ssh ${nodeusr}@${nodeip} ${SETUP_CADVISOR_ATEDGE} < /dev/null
141  done < nodelist > /dev/null 2>&1
142 }
143
144 install_prometheus(){
145 set -o xtrace
146 source prometheus.sh | tee install_prometheus.log
147 }
148
149 # Start
150 #
151
152 if [ $1 == "--help" ] || [ $1 == "-h" ];
153 then
154   show_help
155   exit 0
156 fi
157
158 if [[ $OSPLATFORM = *CentOS* ]]; then
159    setup_k8smaster_centos
160 else
161    setup_k8smaster
162 fi
163
164 sleep 20
165 verify_k8s_status
166 install_cadvisor_edge
167 sleep 10
168 install_prometheus
169 sleep 5
170 sudo docker ps | grep prometheus