0649c711c6c6b2529a3e981425c8e7e4e3aecbb5
[csit.git] / resources / libraries / bash / k8s_setup.sh
1 #!/bin/bash
2 # Copyright (c) 2017 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 set -xo pipefail
16
17 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
18
19 K8S_CALICO="${SCRIPT_DIR}/../../templates/kubernetes/calico_v2.4.1.yaml"
20 K8S_CSIT="${SCRIPT_DIR}/../../templates/kubernetes/csit.yaml"
21
22 trap "sudo kubeadm reset && sudo rm -rf $HOME/.kube" ERR
23
24 # Revert any changes made to this host by 'kubeadm init' or 'kubeadm join'
25 sudo kubeadm reset && sudo rm -rf $HOME/.kube || \
26     { echo "Failed to reset kubeadm"; exit 1; }
27
28 # Ret up the Kubernetes master
29 sudo -E kubeadm init --token-ttl 0 --pod-network-cidr=192.168.0.0/16 || \
30     { echo "Failed to init kubeadm"; exit 1; }
31
32 # Make cgroup non-exclusive for CPU and MEM
33 sudo cgset -r cpuset.cpu_exclusive=0 /kubepods
34 sudo cgset -r cpuset.mem_exclusive=0 /kubepods
35
36 rm -rf $HOME/.kube
37 mkdir -p $HOME/.kube
38 sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
39 sudo chown $(id -u):$(id -g) $HOME/.kube/config
40
41 # Apply resources
42 kubectl apply -f ${K8S_CALICO}  || \
43     { echo "Failed to apply Calico resources"; exit 1; }
44 kubectl apply -f ${K8S_CSIT}  || \
45     { echo "Failed to apply CSIT resource"; exit 1; }
46
47 # Update the taints
48 kubectl taint nodes --all node-role.kubernetes.io/master- || \
49     { echo "Failed to taint nodes"; exit 1; }
50
51 # Dump Kubernetes objects ...
52 kubectl get all --all-namespaces
53
54 echo Kubernetes is ready