Tutorials/Cloud/ONAP: create-kube-cluster.sh

File create-kube-cluster.sh, 3.3 KB (added by seskar, 2 years ago)

Kubernetes cluster creation script

Line 
1#!/bin/bash
2
3rm -f cluster.yml
4rm -f kube_config_cluster.yml
5rm -f rke
6rm -f config
7
8wget https://github.com/rancher/rke/releases/download/v0.2.1/rke_linux-amd64
9mv rke_linux-amd64 rke
10chmod 754 rke
11
12
13usage () {
14 echo "Usage:"
15 echo " ./$(basename $0) -c \"cNode1 cNode2 ... cnodeN\" -w \"wNode1 wnode2 ... wnodeN\""
16 echo "Note: controllers hostnames and workers hostnames are to be enclosed in \"\""
17 exit 0
18}
19
20if [[ ( $# == "--help") || $# == "-h" ]]
21 then
22 usage
23 exit 0
24fi
25
26if [ "$#" -lt 2 ]; then
27 echo "Missing Kubernetes control and worker nodes"
28 usage
29fi
30
31echo "# An example of an HA Kubernetes cluster for ONAP" >> cluster.yml
32echo "nodes:" >> cluster.yml
33
34while getopts c:w: option
35do
36case "${option}"
37in
38c) CONTROLLERS=${OPTARG};;
39w) WORKERS=${OPTARG};;
40esac
41done
42
43IFS=' ' read -ra C <<< "$CONTROLLERS"
44IFS=' ' read -ra W <<< "$WORKERS"
45
46echo "Testing node availability. This might take some time"
47for i in "${C[@]}"; do
48while ! ping -c 1 -n -w 1 $i &> /dev/null
49do
50 printf "%c" "."
51done
52
53echo "127.0.0.1 localhost" > hosts
54echo "`ping $i -c 1 | grep \"PING\" | grep '('|awk '{gsub(/[()]/,\"\"); print $3}'` ${i}" >> hosts
55
56scp hosts root@$i:/etc/hosts
57done
58
59for i in "${W[@]}"; do
60while ! ping -c 1 -n -w 1 $i &> /dev/null
61do
62 printf "%c" "."
63done
64echo "127.0.0.1 localhost" > hosts
65echo "`ping $i -c 1 | grep \"PING\" | grep '('|awk '{gsub(/[()]/,\"\"); print $3}'` ${i}" >> hosts
66scp hosts root@$i:/etc/hosts
67done
68echo "Availability check successful"
69
70for i in "${C[@]}"; do
71 echo "- address: `ping $i -c 1 | grep \"PING\" | grep '('|awk '{gsub(/[()]/,\"\"); print $3}'`" >> cluster.yml
72 echo ' port: "22"' >> cluster.yml
73 echo " role:" >> cluster.yml
74 echo " - controlplane" >> cluster.yml
75 echo " - etcd" >> cluster.yml
76 echo " hostname_override: `ping $i -c 1 | grep 'PING' | awk '{print $2}' | awk -F . '{print $1}'`" >> cluster.yml
77 echo " user: root" >> cluster.yml
78 echo " ssh_key_path: '~/.ssh/id_rsa'" >> cluster.yml
79done
80
81echo "# worker nodes start " >> cluster.yml
82
83for i in "${W[@]}"; do
84 echo "- address: `ping $i -c 1 | grep \"PING\" | grep '('|awk '{gsub(/[()]/,\"\"); print $3}'`" >> cluster.yml
85 echo ' port: "22"' >> cluster.yml
86 echo " role:" >> cluster.yml
87 echo " - worker" >> cluster.yml
88 echo " hostname_override: `ping $i -c 1 | grep 'PING' | awk '{print $2}' | awk -F . '{print $1}'`" >> cluster.yml
89 echo " user: root" >> cluster.yml
90 echo " ssh_key_path: '~/.ssh/id_rsa'" >> cluster.yml
91done
92
93echo 'services:
94 kube-api:
95 service_cluster_ip_range: 10.43.0.0/16
96 pod_security_policy: false
97 always_pull_images: false
98 kube-controller:
99 cluster_cidr: 10.42.0.0/16
100 service_cluster_ip_range: 10.43.0.0/16
101 kubelet:
102 cluster_domain: cluster.local
103 cluster_dns_server: 10.43.0.10
104 fail_swap_on: false
105network:
106 plugin: canal
107authentication:
108 strategy: x509
109ssh_key_path: "~/.ssh/id_rsa"
110ssh_agent_auth: false
111authorization:
112 mode: rbac
113ignore_docker_version: false
114kubernetes_version: "v1.13.5-rancher1-2"
115private_registries:
116- url: nexus3.onap.org:10001
117 user: docker
118 password: docker
119 is_default: true
120cluster_name: "onap"
121restore:
122 restore: false
123 snapshot_name: ""' >> cluster.yml
124
125./rke up
126
127for i in "${C[@]}"; do
128scp kube_config_cluster.yml root@$i:~/.kube/config
129done
130
131exit 0
132