-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeploy-virtual-machines.sh
executable file
·114 lines (95 loc) · 3.11 KB
/
deploy-virtual-machines.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
# When VMs are deleted, IPs remain allocated in dhcpdb
# IP reclaim: https://discourse.ubuntu.com/t/is-it-possible-to-either-specify-an-ip-address-on-launch-or-reset-the-next-ip-address-to-be-used/30316
ARG=$1
set -euo pipefail
RED="\e[1;31m"
YELLOW="\e[1;33m"
GREEN="\e[1;32m"
BLUE="\e[1;34m"
NC="\e[0m"
NUM_WORKER_NODES=2
MEM_GB=$(( $(sysctl hw.memsize | cut -d ' ' -f 2) / 1073741824 ))
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )/scripts
VM_MEM_GB=3G
if [ $MEM_GB -lt 8 ]
then
echo -e "${RED}System RAM is ${MEM_GB}GB. This is insufficient to deploy a working cluster.${NC}"
exit 1
fi
if [ $MEM_GB -lt 16 ]
then
echo -e "${YELLOW}System RAM is ${MEM_GB}GB. Deploying only one worker node.${NC}"
NUM_WORKER_NODES=1
VM_MEM_GB=2G
sleep 1
fi
workers=$(for n in $(seq 1 $NUM_WORKER_NODES) ; do echo -n "kubeworker0$n " ; done)
# workers=''
# If the nodes are running, reset them
if multipass list --format json | jq -r '.list[].name' | egrep 'kube(master|node01|node02)' > /dev/null
then
echo -n -e $RED
read -p "VMs are running. Delete and rebuild them (y/n)? " ans
echo -n -e $NC
[ "$ans" != 'y' ] && exit 1
fi
# Boot the nodes
for node in kubemaster $workers
do
if multipass list --format json | jq -r '.list[].name' | grep "$node"
then
echo -e "${YELLOW}Deleting $node${NC}"
multipass delete $node
multipass purge
fi
echo -e "${BLUE}Launching ${node}${NC}"
multipass launch --disk 5G --memory $VM_MEM_GB --cpus 2 --name $node jammy
echo -e "${GREEN}$node booted!${NC}"
done
# Create hostfile entries
echo -e "${BLUE}Setting hostnames${NC}"
hostentries=/tmp/hostentries
[ -f $hostentries ] && rm -f $hostentries
for node in kubemaster $workers
do
ip=$(multipass info $node --format json | jq -r 'first( .info[] | .ipv4[0] )')
echo "$ip $node" >> $hostentries
done
for node in kubemaster $workers
do
multipass transfer $hostentries $node:/tmp/
multipass transfer $SCRIPT_DIR/01-setup-hosts.sh $node:/tmp/
multipass exec $node -- /tmp/01-setup-hosts.sh
done
echo -e "${GREEN}Done!${NC}"
if [ "$ARG" = "-auto" ]
then
# Set up hosts
echo -e "${BLUE}Setting up common componenets${NC}"
join_command=/tmp/join-command.sh
for node in kubemaster $workers
do
echo -e "${BLUE}- ${node}${NC}"
multipass transfer $hostentries $node:/tmp/
multipass transfer $SCRIPT_DIR/*.sh $node:/tmp/
for script in 02-setup-kernel.sh 03-setup-cri.sh 04-kube-components.sh
do
multipass exec $node -- /tmp/$script
done
done
echo -e "${GREEN}Done!${NC}"
# Configure control plane
echo -e "${BLUE}Setting up control plane${NC}"
multipass exec kubemaster /tmp/05-deploy-controlplane.sh
multipass transfer kubemaster:/tmp/join-command.sh $join_command
echo -e "${GREEN}Done!${NC}"
# Configure workers
for n in $workers
do
echo -e "${BLUE}Setting up ${n}${NC}"
multipass transfer $join_command $n:/tmp
multipass exec $n -- sudo $join_command
echo -e "${GREEN}Done!${NC}"
done
fi