-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube-stack.sh
executable file
·591 lines (529 loc) · 21 KB
/
kube-stack.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#!/bin/bash
#Kubernetes stack factory
#[email protected] - Ren
#automates to a great extent deployment of a Kubernetes cluster
#additional nodes can be added using the add node option after stack deployment
## turn off and disable swap on all nodes - not supported at this time
## Work in progress - still need to add some functionality
#Functions
# Organizes rows and processes to keep things clean and easy to read
twin_line() {
echo "**************************************************************************************************************************************"
echo "**************************************************************************************************************************************"
}
twin_column() {
echo "***** *****"
}
single_line() {
echo "**************************************************************************************************************************************"
}
# Main function generates main menu options
function show_menu(){
NORMAL=`echo "\033[m"`
MENU=`echo "\033[36m"` #Blue
NUMBER=`echo "\033[33m"` #yellow
FGRED=`echo "\033[41m"`
RED_TEXT=`echo "\033[31m"`
ENTER_LINE=`echo "\033[33m"`
clear
echo
echo
echo
echo
echo -e " ${MENU}************************************** Kubernetes Cluster Builder ********************************************${NORMAL}"
echo -e " ${MENU}********************************************************************************************************************${NORMAL}"
echo -e " ${MENU}****** ******${NORMAL}"
echo -e " ${MENU}****** ******${NORMAL}"
echo -e " ${MENU}****** This script aims to automate the install/config of a Kubernetes Cluster with a master and x amount of ******${NORMAL}"
echo -e " ${MENU}****** nodes. You will need to run this script with (option 2) on all hosts (master and nodes) ******${NORMAL}"
echo -e " ${MENU}****** ******${NORMAL}"
echo -e " ${MENU}****** You will be asked about your infrastructure including: server/node hostnames/ip addresses, ssh ports, ******${NORMAL}"
echo -e " ${MENU}****** in order to complete the cluster build. ******${NORMAL}"
echo -e " ${MENU}****** ******${NORMAL}"
echo -e " ${MENU}********************************************************************************************************************${NORMAL}"
echo -e " ${MENU}****** ******${NORMAL}"
echo -e " ${MENU}****** ******${NORMAL}"
echo -e " ${MENU}******${NUMBER} 1)${MENU} Check installed package dependency ******${NORMAL}"
echo -e " ${MENU}******${NUMBER} 2)${MENU} Install package binaries [golang, docker] (required on all hosts - Master + nodes) ******${NORMAL}"
echo -e " ${MENU}******${NUMBER} 3)${MENU} Install Kubeadm Toolbox (required on all hosts - Master + nodes) ******${NORMAL}"
echo -e " ${MENU}******${NUMBER} 4)${MENU} Destroy Kubernetes cluster ******${NORMAL}"
echo -e " ${MENU}******${NUMBER} 5)${MENU} Reset Kubernetes cluster (run to clean up old cluster and prep for new one) ******${NORMAL}"
echo -e " ${MENU}******${NUMBER} 6)${MENU} Deploy Kubernetes Cluster ******${NORMAL}"
echo -e " ${MENU}******${NUMBER} 7)${MENU} Add a Node to existing Cluster ******${NORMAL}"
echo -e " ${MENU}****** ******${NORMAL}"
echo -e " ${MENU}****** ******${NORMAL}"
echo -e " ${MENU}****** ******${NORMAL}"
echo -e " ${MENU}********************************************************************************************************************${NORMAL}"
echo -e " ${MENU}********************************************************************************************************************${NORMAL}"
echo -e " ${ENTER_LINE}Please enter a menu option number or ${RED_TEXT}Press enter to exit. ${NORMAL}"
read opt
}
function option_picked() {
COLOR='\033[01;31m' # bold red
RESET='\033[00;00m' # normal white
MESSAGE=${@:-"${RESET}Error: No message passed"}
echo -e "${COLOR}${MESSAGE}${RESET}"
}
function return_menu(){
echo
echo
echo "Press any key to go back to Menu..."
read -n 1
clear
}
# checks if dependencies are installed already
function check_packages(){
echo
echo -e "${NUMBER} Please standby ....${NORMAL}"
echo
echo
twin_line
echo
sudo docker version
echo
echo
git version
echo
echo
go version
echo
twin_line
echo
echo -e "${RED_TEXT} If command is not found for any packages (go, docker, git), ${NORMAL}"
echo -e "${RED_TEXT} you will need to run the packages install in main menu option 2 ${NORMAL}"
echo
return_menu
show_menu;
}
function install_package_dependency() {
## install and configure golang, docker 1.13+, git
##
single_line
echo -e "${RED_TEXT}***** Begin preliminary install of required dependency packages.... *****${NORMAL}"
single_line
echo
sudo apt-get update
single_line
echo -e "${RED_TEXT}***** Installing docker 17.03+ if not already installed.. *****${NORMAL}"
single_line
sudo apt-get install -y docker.io
sudo groupadd docker
sudo usermod -aG docker $USER
single_line
echo -e "${RED_TEXT}***** Installing latest git version if not already installed.. *****${NORMAL}"
single_line
sudo apt-get install -y git
single_line
echo -e "${RED_TEXT}***** Adding golang repo and installing latest binary *****${NORMAL}"
single_line
sudo add-apt-repository ppa:hnakamur/golang-1.10
sudo apt update
sudo apt-get install -y golang-go golang-1.10-doc
single_line
echo -e "${RED_TEXT}***** Adding go binary to your .profile so it is in your PATH.. *****${NORMAL}"
single_line
echo "export PATH=\$PATH:/usr/lib/go-1.10/bin" >> ~/.profile
source ~/.profile
clear
sleep 2
echo
echo
twin_line
echo -e "${RED_TEXT}***** Recap of installed components: *****${NORMAL}"
single_line
echo
echo
echo "Docker Version:"
echo
echo
\
sudo docker version
echo
echo
git version
echo
echo
go version
echo
echo
echo "Note: User that ran this configuration has been added to the docker group to remove the need to sudo any docker commands"
echo "You should log out and back in for the changes to take effect..."
return_menu
show_menu;
}
function install_kubeadm_toolbox(){
echo
echo
twin_line
echo
echo -e "${RED_TEXT} In order to build a Kubernetes cluster, the kubeadm toolbox (kubeadm, kubelet, and kubectl) will
need to be installed and swap will need to be disabled on this machine. ${NORMAL}"
echo
echo -e "${RED_TEXT} The next step will perform that process.. ${NORMAL}"
echo
twin_line
echo
read -r -p "Do you want to proceed with installing the Kubeadm Toolbox? [y/N]: " response
case "$response" in
[yY][eE][sS]|[yY])
echo
echo "Initiating Kubeadm toolbox install - Please Wait...."
echo
echo
single_line
echo -e "${RED_TEXT}***** updating & installing some dependencies (apt-transport, curl) *****${NORMAL}"
single_line
echo
sudo apt-get update && apt-get install -y apt-transport-https curl
sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
echo
echo
single_line
echo -e "${RED_TEXT}***** adding kubernetes packages to repo list...... *****${NORMAL}"
single_line
echo
echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" > /etc/apt/sources.list.d/kubernetes.list
echo
single_line
echo -e "${RED_TEXT}***** disabling swap on drive... *****${NORMAL}"
single_line
echo
swapoff -a
echo
single_line
echo -e "${RED_TEXT}***** updating package lists from repos & installing kubeadm toolkit.. *****${NORMAL}"
single_line
echo
echo
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
echo
echo
single_line
echo -e "${RED_TEXT}***** Kubeadm toolkit installed! *****${NORMAL}"
single_line
echo
return_menu
show_menu;
;;
*)
echo
echo
echo "Cancelling Kubeadm Toolbox install..."
return_menu
show_menu;
;;
esac
}
function destroy_cluster(){
echo
echo
twin_line
echo
echo -e "${RED_TEXT} WARNING!!! your cluster will be destroyed ${NORMAL}"
echo -e "${RED_TEXT} You will need to place this script on each node including master and run this option (4) ${NORMAL}"
echo -e "${RED_TEXT} Please confirm you want to do this! ${NORMAL}"
echo
twin_line
echo
read -r -p "Are you sure you want to continue with destroying your cluster? Again, this will remove all pods [y/N]: " response
case "$response" in
[yY][eE][sS]|[yY])
echo
echo
echo "Please enter name of your node - start with your nodes first then do master last"
echo
read nodename1
echo
single_line
echo -e "${RED_TEXT} Draining node and all resources... ${NORMAL}"
single_line
kubectl drain $nodename1 --delete-local-data --force --ignore-daemonsets
echo
echo
single_line
echo -e "${RED_TEXT} Node is deleting... ${NORMAL}"
single_line
echo
kubectl delete node $nodename1
echo
echo
echo
single_line
echo -e "${RED_TEXT} Node has been drained and deleted ${NORMAL}"
single_line
#source ~/.profile
return_menu
show_menu;
;;
*)
echo
echo
echo "Aborting..."
return_menu
show_menu;
;;
esac
}
function reset_cluster(){
echo
echo
twin_line
echo
echo -e "${RED_TEXT} This is the second step towards resetting your cluster, prior to deploying a new one. ${NORMAL}"
echo -e "${RED_TEXT} You will need to place this script on each node including master and run this option (5) for each. ${NORMAL}"
echo -e "${RED_TEXT} Please confirm you want to do this! ${NORMAL}"
echo
twin_line
echo
read -r -p "Are you sure you want to continue with resetting your cluster? Again, this will remove all current cluster
configurations [y/N]: " response
case "$response" in
[yY][eE][sS]|[yY])
echo
echo
echo "Please enter the name of your Node - start with your nodes first then do master last:"
echo
read nodename
echo
single_line
echo -e "${RED_TEXT} resetting ... ${NORMAL}"
single_line
echo
sudo kubeadm reset
echo
single_line
echo -e "${RED_TEXT} Node was reset... ${NORMAL}"
single_line
echo
echo
single_line
echo -e "${RED_TEXT} Now deleting .kube configuration folder in your home directory.. ${NORMAL}"
single_line
echo
cd ~
sudo rm -rv .kube
rm -rv ~/heapster
rm -v dashboard-admin.yaml
echo
single_line
echo -e "${RED_TEXT} Node is clean and reset - repeat with all other nodes and lastly your master node. ${NORMAL}"
single_line
echo
return_menu
show_menu;
;;
*)
echo
echo "Aborting..."
return_menu
show_menu;
;;
esac
}
function deploy_cluster(){
echo
echo
twin_line
echo
echo -e "${RED_TEXT} Welcome to the Kubernetes Cluster builder! if you reached this step, you completed the first 3 steps ${NORMAL}"
echo -e "${RED_TEXT} which includes installing the package binaries (on all your hosts, master node included) for golang, git, and docker.${NORMAL}"
echo -e "${RED_TEXT} And finally, installed the Kubeadm toolbox, which installs kubeadm, kubectl, and kubelet, as well on all your hosts. ${NORMAL}"
echo -e "${RED_TEXT} This step should only be run on the host that will serve as your Master Node - DO NOT RUN THIS option on any of your
other nodes - After completing this step on your Master, go the the next step menu option 7 to add your nodes to this master ${NORMAL}"
echo
twin_line
echo
echo
read -r -p "Please confirm that you want to continue with building your Kubernetes cluster? [y/N]: " response
case "$response" in
[yY][eE][sS]|[yY])
echo
echo
single_line
echo
echo -e "${RED_TEXT} Making sure swap is disabled - this is mandatory as swap is not supported ${NORMAL}"
echo
single_line
echo
echo
sudo cat /proc/swaps
echo
echo
sudo swapoff -a
echo
echo
twin_line
echo
echo -e "${RED_TEXT}Please enter the advertise address ip in the next prompt ${NORMAL}"
echo -e "${RED_TEXT}This can be your external ip if you plan to access your cluster from outside your network ${NORMAL}"
echo -e "${RED_TEXT}or the local host ip to your what will be your Node Master (This should be the machine you are running this script on) ${NORMAL}"
echo
single_line
echo
echo -e "${RED_TEXT}Please enter the advertise address ip: ${NORMAL}"
read masterIp
echo
sudo kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=$masterIp
echo
echo
single_line
echo
echo -e "${RED_TEXT} NOTE - you will not need to complete the steps above for creating the .kube configuration
directory as it will be created for you automatically in the next step - please standby... ${NORMAL}"
echo
echo
single_line
echo
echo -e "${RED_TEXT} Creating Kubernetes config directory in ~/.kube... ${NORMAL}"
echo
echo
## Creates hidden .kube directory in users home folder - contains all kubernetes cluster configurations
## for Master
mkdir -pv $HOME/.kube
sudo cp -rfv /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
echo
single_line
echo
echo -e "${RED_TEXT} Completing Master Node install and initializing... ${NORMAL}"
echo
echo
echo -e "${RED_TEXT} Please standby..... ${NORMAL}"
single_line
echo
echo -e "${RED_TEXT} Creating and initializing Pod network (This will deploy Calico Canal network) ${NORMAL}"
echo -e "${RED_TEXT} Please standby..... ${NORMAL}"
echo
echo
## Deploy Calico project Canal Pod network - can apply various others to work with Kubernetes cluster
## See documentation here - https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/#pod-network
kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/canal/rbac.yaml
kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/canal/canal.yaml
echo
echo
single_line
echo -e "${RED_TEXT} Cloning heapster to your home directory (~heapster)..... ${NORMAL}"
echo
cd ~
## Add Heapster packages for charts and graphs in Kubernetes Dashboard
git clone https://github.com/kubernetes/heapster
kubectl create -f ~/heapster/deploy/kube-config/influxdb/
kubectl create -f ~/heapster/deploy/kube-config/rbac/heapster-rbac.yaml
echo
echo
echo
single_line
echo -e "${RED_TEXT} Cloning Kubernetes Dashboard repo and deploying app to cluster..... ${NORMAL}"
echo
echo
## Add Dashboard packages/roles for viewing cluster infrastructure
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
cd ~
git clone https://gist.github.com/05ada26c02fd09f335580ba851252cfb.git
cd ~/05ada26c02fd09f335580ba851252cfb
cp dashboard-admin.yaml ~/
sudo rm -rv ~/05ada26c02fd09f335580ba851252cfb
cd ~
## Add dashboard package
kubectl apply -f dashboard-admin.yaml
kubectl create clusterrolebinding add-on-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:default
echo
single_line
echo -e "${RED_TEXT} Getting Token secret (used for accesing dashboard) and join command to add your nodes to this master ${NORMAL}"
echo
echo
single_line
echo -e "${RED_TEXT} Copy the command below, log in to each of your nodes(the ones you want to add to the cluster), switch to root (sudo su - ), ${NORMAL}"
echo -e "${RED_TEXT} and paste the command into the terminal for each node. ${NORMAL}"
echo -e "${RED_TEXT} This will add the nodes to your cluster... ${NORMAL}"
echo
## Prints the command that you use to join your nodes to master
kubeadm token create --print-join-command
echo
echo
echo
echo
return_menu
show_menu;
;;
*)
echo
echo "Aborting..."
return_menu
show_menu;
;;
esac
}
clear
show_menu
while [ opt != '' ]
do
if [[ $opt = "" ]]; then
clear;
echo
single_line
echo
echo -e "${RED_TEXT} Exit selected, quitting application...${NORMAL}"
echo
echo -e "${RED_TEXT} Goodbye! ${NORMAL}"
echo
single_line
clear;
exit;
else
case $opt in
1) clear;
option_picked "Option 1 Picked, Checking for installed required packages....";
echo
check_packages
;;
2) clear;
option_picked "Option 2 Picked, Begin Cluster Pre-requisite Installs/Configuration....";
echo
install_package_dependency
;;
3) clear;
option_picked "Option 3 picked, Install Kubeadm Toolbox....";
echo
echo "This option needs to be run on your master + all nodes... "
echo "Start with your master node first, then run on nodes"
install_kubeadm_toolbox
;;
4) clear;
option_picked "Option 4 picked, Destroy Kubernetes Stack and drain all pods...";
echo
echo "Stack uninstalling....."
destroy_cluster
;;
5) clear;
option_picked "Option 5 picked, Reset Kubernetes Cluster...";
echo
echo "Resetting Cluster...."
reset_cluster
;;
6) clear;
option_picked "Option 6 picked, Deploy Kubernetes Cluster...";
echo
echo "Building Master-Node and starting up pods... "
deploy_cluster
;;
7) clear;
option_picked "Option 7 picked, Add Node to existing Cluster...";
echo
echo "This option does not yet work.. you can join a node using the join command"
return_menu
show_menu;
;;
x)exit;
;;
\n)exit;
;;
*)clear;
option_picked "Pick an option from the menu";
show_menu;
;;
esac
fi
done