Kubeedge code optimized
[eliot.git] / scripts / k8smaster.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 KUBE_VERSION=1.13.0-00
12 POD_NETWORK_CIDR=192.168.0.0/16
13 K8S_CNI_VERSION=0.6.0-00
14
15 # Install Kubernetes with Kubeadm
16
17 # Disable swap
18 sudo swapoff -a
19 sudo apt update
20 sudo apt install -y apt-transport-https curl
21 curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
22
23 cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
24 deb https://apt.kubernetes.io/ kubernetes-xenial main
25 EOF
26
27 sudo apt update
28 sudo apt install -y \
29   kubernetes-cni=${K8S_CNI_VERSION} kubelet=${KUBE_VERSION} \
30   kubeadm=${KUBE_VERSION} kubectl=${KUBE_VERSION}
31
32 sudo apt-mark hold kubelet kubeadm kubectl
33
34 if ! kubectl get nodes; then
35   hostname -I > hostname.tmp
36   MASTER_IP="$(cut -d ' ' -f 1 hostname.tmp)"
37   rm hostname.tmp
38   sudo kubeadm config images pull
39   sudo kubeadm init \
40          --apiserver-advertise-address="${MASTER_IP}" \
41          --pod-network-cidr="${POD_NETWORK_CIDR}"
42
43   if [ "$(id -u)" = 0 ]; then
44     echo "export KUBECONFIG=/etc/kubernetes/admin.conf" | \
45       tee -a "${HOME}/.profile"
46     source "${HOME}/.profile"
47   else
48     mkdir -p "${HOME}/.kube"
49     sudo cp -i /etc/kubernetes/admin.conf "${HOME}/.kube/config"
50     sudo chown "$(id -u)":"$(id -g)" "${HOME}/.kube/config"
51   fi
52   kubectl apply -f "cni/calico/rbac.yaml"
53   kubectl apply -f "cni/calico/calico.yaml"
54
55 fi