forked from jlim0930/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-elastick8s.sh
More file actions
executable file
·5005 lines (4660 loc) · 148 KB
/
deploy-elastick8s.sh
File metadata and controls
executable file
·5005 lines (4660 loc) · 148 KB
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# justin lim <justin@isthecoolest.ninja>
#
# version 3.1
# curl -fsSL https://raw.githubusercontent.com/jlim0930/scripts/master/deploy-elastick8s.sh -o deploy-elastick8s.sh
#
# NOTES
# eck 1.2+
# start of elasticsearchRef
# ES 6.8+ & 7.1+
# Beats 7.0+
# entsearch 7.7+
# all-in-one operator
# eck 1.4+
# ES 7.17.3+
# eck 1.6+
# elastic-agent 7.10+
# elastic map server 7.11+
# eck 1.7+
# crds.yaml & operator.yaml
# fleet 7.14
# sidecar container stack monitoring started with ES 7.14
# eck 1.9+
# helm 3.2.0
# eck 2.1+
# kibana configuration becomre longer and more specific for fleet server
# eck 2.2
# ES 8.0+
# Starting 7.17 stack container image changed to ubuntu - some fixes are needed due to this
# helm only for 7.14.0-> 7.17.x - older versions not as clean
# items needed:
# jq
# openssl
# docker binary and service
# kubectl
# helm
# set WORKDIR
WORKDIR="${HOME}/elastick8s"
###############################################################################################################
# colors
red=`tput setaf 1`
green=`tput setaf 2`
blue=`tput setaf 14`
reset=`tput sgr0`
###############################################################################################################
# help
help()
{
echo ""
echo "${green}This script will allow you to stand up various elasticstack configurations on suing ECK operator, HELM without the operator, HELM with the operator${reset}"
echo "${green}You must have jq, helm, docker(installed and running) to use this script${reset}"
echo "${green}This script was tested on GKE but should work on AKS, EKS as well${reset}"
echo "${green}Please use ${blue}gke.sh script (https://www.gooksu.com/2022/05/google-cloud-scripts/)${green} to stand up your gke environment before running this script${reset}"
echo "${green}This script assumes that your kubectl has the proper context to access your k8s environment${reset}"
echo ""
echo "${green}For eck operator builds operator is ${red}limited to 1.4 or above${green} and stack is ${red}limited to 7.10.0+${green}. - other limitations per commands${reset}"
echo "${green}For HELM without operator stack version is ${red}limited to 7.14.0-7.17.x${reset}"
echo "${green}For NATIVE without operator stack version is ${red}limited to 7.0.0-8.x.x${reset}"
echo ""
echo "${green}USAGE:${reset} ./`basename $0` command ...."
echo ""
echo "${blue}COMMANDS:${reset}"
echo " ${green}operator${reset} - will just stand up the operator only and apply a trial license."
echo " Need to specify operator version. Example: ${green}./`basename $0` operator 2.3.0${reset}"
echo " ${green}stack|start|build|eck${reset} - will stand up the ECK Operator, elasticsearch, & kibana with CLUSTER name : ${blue}eck-lab${reset}."
echo " Need to specify operator and stack version. Example: ${green}./`basename $0` stack 8.4.1 2.3.0${reset}"
echo " ${green}eckldap${reset} - will stand up the ECK Operator, elasticsearch, & kibana with CLUSTER name : ${blue}eck-lab${reset} and an openldap server and configure elasticsearch to auth with openldap."
echo " ${green}dedicated${reset} - will stand up the ECK Operator, elasticsearch, & kibana with CLUSTER name : ${blue}eck-lab${reset} with 3 dedicated masters and 3 dedicated data nodes"
echo " SAME as stack|start|built but with ${green}dedicated${reset} command"
echo " ${green}beats${reset} - will stand up the basic stack + filebeat, metricbeat, packetbeat, & heartbeat"
echo " SAME as stack|start|built but with ${green}beats${reset} command"
echo " ${green}monitor1${reset} - will stand up the basic stack named ${blue}eck-lab${reset} and a monitoring stack named ${blue}eck-lab-monitor${reset}, filebeat, & metricbeat as PODS to report stack monitoring to ${blue}eck-lab-monitor${reset}"
echo " SAME as stack|start|built but with ${green}monitor1${reset} command"
echo " ${green}monitor2${reset} - will be the same as ${blue}monitor1${reset} however both filebeat & metricbeat will be a sidecar container inside of elasticsearch & kibana Pods. Limited to ECK ${blue}1.7.0+${reset} & STACK ${blue}7.14.0+${reset}"
echo " SAME as stack|start|built but with ${green}monitor2${reset} command"
echo " ${green}fleet${reset} - will stand up the basic stack + FLEET Server & elastic-agent as DaemonSet on each ECK node."
echo " SAME as stack|start|built but with ${green}fleet${reset} command"
echo ""
echo " ${green}helm${reset} - will stand up elasticsearch and kibana using helm charts without any operator. Currently limited to 7.14.0 - 7.17.x"
echo " Need to specify stack version. Example: ${green}./`basename $0` helm 7.17.4${reset}"
echo " ${green}helmldap${reset} - will stand up the helm stack + openldap and configure elasticsearch for ldap authentication"
echo " ${green}helmbeats${reset} - will stand up the helm stack + filebeat, & metricbeat"
echo " SAME as helm but with ${green}helmbeats${reset} command"
echo " ${green}helmblogstashbeats${reset} - will stand up the helm stack + logstash, filebeat, & metricbeat. beats will use logstash to send data to ES."
echo " SAME as helm but with ${green}helmlogstashbeats${reset} command"
echo " ${green}helmmonitor${reset} - will stand up the helm stack & metricbeat for stack monitoring"
echo " SAME as helm but with ${green}helmstackmonitor${reset} command"
echo ""
echo " ${green}native${reset} - will stand up elasticsearch and kibana without helm charts & without any operator. Currently limited to 7.0.0 - 8.x.x"
echo " Need to specify stack version. Example: ${green}./`basename $0` native 7.17.4${reset}"
echo " ${green}nativebeats${reset} - will stand up the helm stack + filebeat, & metricbeat"
echo " SAME as helm but with ${green}nativebeats${reset} command"
echo " ${green}nativemonitor${reset} - will stand up the helm stack & metricbeat for stack monitoring"
echo " SAME as helm but with ${green}nativestackmonitor${reset} command"
echo ""
echo " ${green}cleanup${reset} - will delete all the resources including the ECK operator"
echo ""
echo ""
echo "All yaml files will be stored in ${blue}${WORKDIR}${reset}"
echo " ${blue}${WORKDIR}/notes${reset} will contain all endpoint and password information"
echo " ${blue}${WORKDIR}/ca.crt${reset} will be the CA used to sign the public certificate"
echo ""
} # end of help
#############################################################
# FUNCTIONS - HELPER
#############################################################
# FUNCTION - spinner
spinner() {
local PROC="${1}"
tput civis
# Clear Line
CL="\e[2K"
# Spinner Character
SPINNER="⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
while [ $(ps -ef | grep -c ${PROC}) -ge 2 ]; do
for (( i=0; i<${#SPINNER}; i++ )); do
sleep 0.05
printf "${CL}${SPINNER:$i:1} ${2}\r"
done
done
tput cnorm
} # end spinner
# FUNCTION - checkjq
checkjq() {
if ! [ -x "$(command -v jq)" ]; then
echo "${red}[DEBUG]${reset} jq is not installed. Please install jq and try again."
exit
else
echo "${green}[DEBUG]${reset} jq found"
fi
} # end checkjq
# FUNCTION - checkopenssl
checkopenssl() {
if ! [ -x "$(command -v openssl)" ]; then
echo "${red}[DEBUG]${reset} openssl is not installed. Please install openssl and try again."
exit
else
echo "${green}[DEBUG]${reset} openssl found"
fi
} # end checkopenssl
# FUNCTION - checkdocker
checkdocker() {
# check to ensure docker is installed or exit
docker info >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "${red}[DEBUG]${reset} Docker is not running or you are not part of the docker group. Please install docker and ensure that you are part of the docker group and try again."
exit
else
echo "${green}[DEBUG]${reset} docker found & running"
fi
} # end checkdocker
# FUNCTION - kubectl
checkkubectl()
{
if [ `kubectl version 2>/dev/null | grep -c "Client Version"` -lt 1 ]; then
echo "${red}[DEBUG]${reset} kubectl is not installed. Please install kubectl and try again."
exit
else
echo "${green}[DEBUG]${reset} kubectl found"
fi
if [ `kubectl version 2>/dev/null | grep -c "Server Version"` -lt 1 ]; then
echo "${red}[DEBUG]${reset} kubectl is not connecting to any kubernetes environment"
echo "${red}[DEBUG]${reset} if you did not setup your k8s environment. Please configure your kubernetes context and try again"
exit
fi
} # end checkubectl
# FUNCTION - helm
checkhelm()
{
if ! [ -x "$(command -v helm)" ]; then
echo "${red}[DEBUG]${reset} helm is not installed. Please install helm and try again. https://helm.sh/docs/intro/install/"
exit
else
echo "${green}[DEBUG]${reset} helm found"
fi
} # end checkhelm
# FUNCTION - checkversion
checkversion()
{
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
} # end checkversion
# FUNCTION - checkdir
checkdir()
{
# check to see if the directory exists should not since this is the start
if [ -d ${WORKDIR} ]; then
echo "${red}[DEBUG]${reset} Looks like ${WORKDIR} already exists."
echo "${red}[DEBUG]${reset} Please run ${blue}`basename $0` cleanup${reset} before trying again"
echo ""
help
exit
fi # end of if to check if WORKDIR exist
# create directorys and files
mkdir -p ${WORKDIR}
cd ${WORKDIR}
mkdir temp
if [ ! -z ${VERSION} ]; then
echo ${VERSION} > VERSION
fi
if [ ! -z ${ECKVERSION} ]; then
echo ${ECKVERSION} > ECKVERSION
fi
} # end checkdir
# FUNCTION - checkcontainerimage
checkcontainerimage() {
# check to see if the container image exists or not
docker manifest inspect ${1} >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "${red}[DEBUG]${reset} Image ${blue}${1}${reset} does not exist. Please verify and try again"
exit
else
echo "${green}[DEBUG]${reset} container image ${blue}${1}${reset} is valid"
fi
} # end checkcontainerimage
# FUNCTION - checkrequiredversion
checkrequiredversion() {
# manually limiting eck version to 1.4 or greater
if [ $(checkversion $ECKVERSION) -lt $(checkversion "1.4.0") ]; then
echo "${red}[DEBUG]${reset} Script is limited to operator 1.4.0 and higher"
echo ""
help
exit
else
if [ $(checkversion $ECKVERSION) -lt $(checkversion "1.7.0") ]; then
if curl -sL --fail https://download.elastic.co/downloads/eck/${ECKVERSION}/all-in-one.yaml -o /dev/null; then
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} version validated."
else
echo "${red}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} version is invalid."
echo ""
help
exit
fi
elif [ $(checkversion $ECKVERSION) -ge $(checkversion "1.7.0") ]; then
if curl -sL --fail https://download.elastic.co/downloads/eck/${ECKVERSION}/crds.yaml -o /dev/null; then
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} version validated."
else
echo "${red}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} version is invalid."
echo ""
help
exit
fi
fi
fi
if [ $(checkversion $ECKVERSION) -lt $(checkversion "2.2.0") -a $(checkversion $VERSION) -ge $(checkversion "8.0.0") ]; then
echo "${red}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} Can not run 8.x. Please use operator 2.2.0+"
echo ""
help
exit
fi
# manually limiting elasticsearch version to 7.10.0 or greater
if [ $(checkversion $VERSION) -lt $(checkversion "7.10.0") ]; then
echo "${red}[DEBUG]${reset} This command is limited to stack version 7.10.0 and higher"
echo ""
help
exit
fi
echo "${green}[DEBUG]${reset} This might take a while. In another window you can ${blue}watch -n2 kubectl get all${reset} or ${blue}kubectl get events -w${reset} to watch the stack being stood up"
echo ""
} # end checkrequiredversion
# FUNCTION - checkrequiredversionhelm
checkrequiredversionhelm() {
# manually limiting version to 7.14.0+ to -8.0.0
if [ $(checkversion $VERSION) -lt $(checkversion "7.14.0") -o $(checkversion $VERSION) -ge $(checkversion "8.0.0") ]; then
echo "${red}[DEBUG]${reset} This command is limited to stack version 7.14.0+ & < 8.0.0"
echo ""
help
exit
fi
} # end checkrequiredversionhelm
# FUNCTION - checkrequiredversionnative
checkrequiredversionnative() {
# manually limiting version to value
if [ $(checkversion $VERSION) -lt $(checkversion "${1}") ]; then
echo "${red}[DEBUG]${reset} This command is limited to stack version ${1}"
echo ""
help
exit
fi
} # end checkrequiredversionnative
# FUNCTION - checkhealth
checkhealth() {
sleep 3
echo ""
until [ "$(kubectl get ${1} ${2} -ojson | jq -r '.status.'${3}'')" == "${4}" ]; do
sleep 2
done &
spinner $! "${blue}[DEBUG]${reset} Checking to ensure all ${3}(${4}) are ready for ${2}. IF this does not finish in ~5 minutes something is wrong"
echo ""
kubectl get ${1} ${2} | sed "s/^/ /"
echo ""
} # end checkhealth
############################################################
# FUNCTION - createsummary
createsummary()
{
# FOR ECK deployments
if [ -e ${WORKDIR}/ECK ]; then
unset PASSWORD
c=0
while [ "${PASSWORD}" = "" ]
do
PASSWORD=$(kubectl get secret ${1}-es-elastic-user -o go-template='{{.data.elastic | base64decode}}')
sleep 1
((c++))
if [ $c = 30 ]; then
echo "${red}[DEBUG]${reset} Unable to get elasticsearch password."
exit
fi
done
echo "${green}[DEBUG]${reset} Grabbed elastic password for ${1}: ${blue}${PASSWORD}${reset}"
echo "${1} elastic password: ${PASSWORD}" >> notes
unset ESIP
c=0
while [ "${ESIP}" = "" -o "${ESIP}" = "<pending>" ]
do
ESIP=`kubectl get service | grep ${1}-es-http | awk '{ print $4 }'`
sleep 1
((c++))
if [ $c = 30 ]; then
echo "${red}[DEBUG]${reset} Unable to get elasticsearch endpoint."
exit
fi
done
echo "${green}[DEBUG]${reset} Grabbed elasticsearch endpoint for ${1}: ${blue}https://${ESIP}:9200${reset}"
echo "${1} elasticsearch endpoint: https://${ESIP}:9200" >> notes
unset KIBANAIP
c=0
while [ "${KIBANAIP}" = "" -o "${KIBANAIP}" = "<pending>" ]
do
KIBANAIP=`kubectl get service | grep ${1}-kb-http | awk '{ print $4 }'`
sleep 1
((c++))
if [ $c = 30 ]; then
echo "${red}[DEBUG]${reset} Unable to get kibana endpoint."
exit
fi
done
echo "${green}[DEBUG]${reset} Grabbed kibana endpoint for ${1}: ${blue}https://${KIBANAIP}:5601${reset}"
echo "${1} kibana endpoint: https://${KIBANAIP}:5601" >> notes
kubectl get secrets ${1}-es-http-certs-public -o jsonpath="{.data.ca\.crt}" | base64 -d > ${WORKDIR}/ca.crt
# FOR HELM deployments
elif [ -e ${WORKDIR}/HELM ]; then
unset PASSWORD
c=0
while [ "${PASSWORD}" = "" ]
do
PASSWORD=$(kubectl get secrets elastic-credentials -o go-template='{{.data.password | base64decode}}')
sleep 1
((c++))
if [ $c = 30 ]; then
echo "${red}[DEBUG]${reset} Unable to get elasticsearch password."
exit
fi
done
echo "${green}[DEBUG]${reset} Grabbed elastic password for ${1}: ${blue}${PASSWORD}${reset}"
echo "${1} elastic password: ${PASSWORD}" >> notes
unset ESIP
c=0
while [ "${ESIP}" = "" -o "${ESIP}" = "<pending>" ]
do
ESIP=`kubectl get service | grep ${1}-es-default | grep -v headless | awk '{ print $4 }'`
sleep 1
((c++))
if [ $c = 30 ]; then
echo "${red}[DEBUG]${reset} Unable to get elasticsearch endpoint."
exit
fi
done
echo "${green}[DEBUG]${reset} Grabbed elasticsearch endpoint for ${1}: ${blue}https://${ESIP}:9200${reset}"
echo "${1} elasticsearch endpoint: https://${ESIP}:9200" >> notes
unset KIBANAIP
c=0
while [ "${KIBANAIP}" = "" -o "${KIBANAIP}" = "<pending>" ]
do
KIBANAIP=`kubectl get service | grep helm-lab-kb-kibana | awk '{ print $4 }'`
sleep 1
((c++))
if [ $c = 30 ]; then
echo "${red}[DEBUG]${reset} Unable to get kibana endpoint."
exit
fi
done
echo "${green}[DEBUG]${reset} Grabbed kibana endpoint for ${1}: ${blue}https://${KIBANAIP}:5601${reset}"
echo "${1} kibana endpoint: https://${KIBANAIP}:5601" >> notes
# FOR NATIVE deployments
elif [ -e ${WORKDIR}/NATIVE ]; then
unset PASSWORD
c=0
while [ "${PASSWORD}" = "" ]
do
PASSWORD=$(kubectl get secrets elastic-credentials -o go-template='{{.data.password | base64decode}}')
sleep 1
((c++))
if [ $c = 30 ]; then
echo "${red}[DEBUG]${reset} Unable to get elasticsearch password."
exit
fi
done
echo "${green}[DEBUG]${reset} Grabbed elastic password for ${1}: ${blue}${PASSWORD}${reset}"
echo "${1} elastic password: ${PASSWORD}" >> notes
unset ESIP
c=0
while [ "${ESIP}" = "" -o "${ESIP}" = "<pending>" ]
do
ESIP=`kubectl get service | grep ${1}-default | grep -v headless | awk '{ print $4 }'`
sleep 1
((c++))
if [ $c = 30 ]; then
echo "${red}[DEBUG]${reset} Unable to get elasticsearch endpoint."
exit
fi
done
echo "${green}[DEBUG]${reset} Grabbed elasticsearch endpoint for ${1}: ${blue}https://${ESIP}:9200${reset}"
echo "${1} elasticsearch endpoint: https://${ESIP}:9200" >> notes
unset KIBANAIP
c=0
while [ "${KIBANAIP}" = "" -o "${KIBANAIP}" = "<pending>" ]
do
KIBANAIP=`kubectl get service | grep native-lab-kibana | awk '{ print $4 }'`
sleep 1
((c++))
if [ $c = 30 ]; then
echo "${red}[DEBUG]${reset} Unable to get kibana endpoint."
exit
fi
done
echo "${green}[DEBUG]${reset} Grabbed kibana endpoint for ${1}: ${blue}https://${KIBANAIP}:5601${reset}"
echo "${1} kibana endpoint: https://${KIBANAIP}:5601" >> notes
fi
echo ""
} # end createsummary
# FUNCTION - summary
summary()
{
if [ -e ${WORKDIR}/ECK ]; then
echo ""
echo "${green}[SUMMARY]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset}"
echo ""
kubectl get all | sed "s/^/ /"
echo ""
echo "${green}[SUMMARY]${reset} STACK INFO:"
while read line
do
string1=`echo $line | awk -F": " '{ print $1 }'`
string2=`echo $line | awk -F": " '{ print $2 }'`
echo "${string1}: ${blue}${string2}${reset}"
done < ${WORKDIR}/notes
echo ""
echo "${green}[SUMMARY]${reset} ${blue}ca.crt${reset} is located in ${blue}${WORKDIR}/ca.crt${reset}"
echo ""
echo "${green}[NOTE]${reset} If you missed the summary its also in ${blue}${WORKDIR}/notes${reset}"
echo "${green}[NOTE]${reset} You can start logging into kibana but please give things few minutes for proper startup and letting components settle down."
echo ""
elif [ -e ${WORKDIR}/HELM ]; then
echo ""
echo "${green}[SUMMARY]${reset} HELM STACK ${blue}${VERSION}${reset}"
echo ""
kubectl get all | sed "s/^/ /"
echo ""
echo "${green}[SUMMARY]${reset} HELM STACK INFO:"
while read line
do
string1=`echo $line | awk -F": " '{ print $1 }'`
string2=`echo $line | awk -F": " '{ print $2 }'`
echo "${string1}: ${blue}${string2}${reset}"
done < ${WORKDIR}/notes
echo ""
echo "${green}[SUMMARY]${reset} ${blue}ca.crt${reset} is located in ${blue}${WORKDIR}/ca.crt${reset}"
elif [ -e ${WORKDIR}/NATIVE ]; then
echo ""
echo "${green}[SUMMARY]${reset} NATIVE STACK ${blue}${VERSION}${reset}"
echo ""
kubectl get all | sed "s/^/ /"
echo ""
echo "${green}[SUMMARY]${reset} NATIVE STACK INFO:"
while read line
do
string1=`echo $line | awk -F": " '{ print $1 }'`
string2=`echo $line | awk -F": " '{ print $2 }'`
echo "${string1}: ${blue}${string2}${reset}"
done < ${WORKDIR}/notes
echo ""
echo "${green}[SUMMARY]${reset} ${blue}ca.crt${reset} is located in ${blue}${WORKDIR}/ca.crt${reset}"
fi
} # end summary
# FUNCTION - beatsetup
beatsetup()
{
echo "${green}[DEBUG]${reset} Running setup for ${1}.."
kubectl delete pod beats-setup >/dev/null 2>&1
kubectl run -it beats-setup --image=docker.elastic.co/beats/${1}:${VERSION} -- sh -c "${1} setup -E output.elasticsearch.hosts=\"${ESIP}:9200\" -E output.elasticsearch.protocol=https -E output.elasticsearch.username=elastic -E output.elasticsearch.password=${PASSWORD} -E output.elasticsearch.ssl.verification_mode=none -E setup.kibana.host=\"${KIBANAIP}:5601\" -E setup.kibana.protocol=https -E setup.kibana.username=elastic -E setup.kibana.password=${PASSWORD} -E setup.kibana.ssl.verification_mode=none -E setup.ilm.overwrite=true" >/dev/null 2>&1
kubectl delete pod beats-setup >/dev/null 2>&1
} # end beatsetup
# FUNCTION - createcerts
createcerts() {
echo ""
echo "${green}[DEBUG]${reset} Creating ca.crt and certificate/key for ${1}"
docker rm -f ${1}-certs >/dev/null 2>&1
# hard coding for 7.17.5 container... 8.x containers changed
docker run --name ${1}-certs -i -w /app \
docker.elastic.co/elasticsearch/elasticsearch:7.17.5 \
/bin/sh -c " \
elasticsearch-certutil ca --silent --pem -out /app/ca.zip && \
unzip /app/ca.zip -d /app && \
elasticsearch-certutil cert --silent --pem -out /app/${1}.zip --name ${1} --dns ${1} --ca-cert /app/ca/ca.crt --ca-key /app/ca/ca.key && \
unzip /app/${1}.zip -d /app
" >/dev/null 2>&1
mkdir ${WORKDIR}/${1}-certs >/dev/null 2>&1
docker cp ${1}-certs:/app/ca/ca.crt ${WORKDIR}/${1}-certs/
docker cp ${1}-certs:/app/ca/ca.key ${WORKDIR}/${1}-certs
docker cp ${1}-certs:/app/${1}/${1}.crt ${WORKDIR}/${1}-certs
docker cp ${1}-certs:/app/${1}/${1}.key ${WORKDIR}/${1}-certs
docker rm -f ${1}-certs >/dev/null 2>&1
echo "${green}[DEBUG]${reset} Creating secret ${1}-certificates"
kubectl create secret generic ${2} --from-file=${WORKDIR}/${1}-certs/ca.crt --from-file=${WORKDIR}/${1}-certs/${1}.crt --from-file=${WORKDIR}/${1}-certs/${1}.key >/dev/null 2>&1
} # end createcerts
#############################################################
# FUNCTIONS - MAIN
#############################################################
# FUNCTION - cleanup
cleanup()
{
# make sure to name all yaml files as .yaml so that it can be picked up during cleanup
echo ""
echo "${green}********** Cleaning up **********${reset}"
echo ""
if [ -e ${WORKDIR}/ECK ]; then
for item in `ls -1t ${WORKDIR}/*.yaml 2>/dev/null`
do
echo "${green}[DEBUG]${reset} DELETING Resources for: ${blue}${item}${reset}"
kubectl delete -f ${item} > /dev/null 2>&1
done
kubectl delete secret fleet-server-agent-http-certs-public >/dev/null 2>&1
if [ -e ${WORKDIR}/openldap.yaml ]; then
kubectl delete -f ${WORKDIR}/openldap.yaml >/dev/null 2>&1
kubectl delete secret openldap openldap-certificates ldapbindpw-secret >/dev/null 2>&1
fi
elif [ -e ${WORKDIR}/HELM ]; then
echo "${green}[DEBUG]${reset} DELETING Resources for: ${blue}helm-lab elasticsearch${reset}"
helm uninstall elasticsearch > /dev/null 2>&1
kubectl delete pvc helm-lab-es-default-helm-lab-es-default-0 > /dev/null 2>&1
kubectl delete pvc helm-lab-es-default-helm-lab-es-default-1 > /dev/null 2>&1
kubectl delete pvc helm-lab-es-default-helm-lab-es-default-2 > /dev/null 2>&1
kubectl delete secrets elastic-certificates > /dev/null 2>&1
kubectl delete secrets elastic-credentials > /dev/null 2>&1
kubectl delete secrets elastic-endpoint > /dev/null 2>&1
echo "${green}[DEBUG]${reset} DELETING Resources for: ${blue}helm-lab kibana${reset}"
helm uninstall helm-lab-kb > /dev/null 2>&1
kubectl delete secrets kibana-credentials > /dev/null 2>&1
kubectl delete secrets kibana-encryptionkey > /dev/null 2>&1
kubectl delete secrets kibana-certificates > /dev/null 2>&1
if [ -e ${WORKDIR}/fb-values.yaml ]; then
echo "${green}[DEBUG]${reset} DELETING Resources for: ${blue}helm-lab filebeat${reset}"
helm uninstall helm-lab-fb > /dev/null 2>&1
kubectl delete secrets beats-credentials > /dev/null 2>&1
fi
if [ -e ${WORKDIR}/mb-values.yaml ]; then
echo "${green}[DEBUG]${reset} DELETING Resources for: ${blue}helm-lab metricbeat${reset}"
helm uninstall kube-state-metrics > /dev/null 2>&1
helm uninstall helm-lab-mb > /dev/null 2>&1
kubectl delete secrets beats-credentials > /dev/null 2>&1
fi
if [ -e ${WORKDIR}/ls-values.yaml ]; then
echo "${green}[DEBUG]${reset} DELETING Resources for: ${blue}helm-lab logstash${reset}"
helm uninstall helm-lab-ls > /dev/null 2>&1
kubectl delete pvc helm-lab-ls-logstash-helm-lab-ls-logstash-0 > /dev/null 2>&1
fi
if [ -e ${WORKDIR}/openldap.yaml ]; then
kubectl delete -f ${WORKDIR}/openldap.yaml >/dev/null 2>&1
kubectl delete secret openldap openldap-certificates ldapbindpw-secret >/dev/null 2>&1
fi
elif [ -e ${WORKDIR}/NATIVE ]; then
echo "${green}[DEBUG]${reset} DELETING Resources for: ${blue}native-lab kibana${reset}"
kubectl delete -f ${WORKDIR}/kibana.yaml > /dev/null 2>&1
kubectl delete secrets kibana-encryptionkey > /dev/null 2>&1
kubectl delete secrets kibana-certificates > /dev/null 2>&1
kubectl delete secrets kibana-credentials > /dev/null 2>&1
echo "${green}[DEBUG]${reset} DELETING Resources for: ${blue}native-lab elasticsearch${reset}"
kubectl delete -f ${WORKDIR}/elasticsearch.yaml > /dev/null 2>&1
kubectl delete pvc native-lab-default-native-lab-default-0 > /dev/null 2>&1
kubectl delete pvc native-lab-default-native-lab-default-1 > /dev/null 2>&1
kubectl delete pvc native-lab-default-native-lab-default-2 > /dev/null 2>&1
kubectl delete secrets elastic-certificates > /dev/null 2>&1
kubectl delete secrets elastic-credentials > /dev/null 2>&1
if [ -e ${WORKDIR}/filebeat-kubernetes.yaml ]; then
echo "${green}[DEBUG]${reset} DELETING Resources for: ${blue}native-lab filebeat${reset}"
kubectl delete -f ${WORKDIR}/filebeat-kubernetes.yaml > /dev/null 2>&1
kubectl delete secrets beats-credentials > /dev/null 2>&1
fi
if [ -e ${WORKDIR}/metricbeat-kubernetes.yaml ]; then
echo "${green}[DEBUG]${reset} DELETING Resources for: ${blue}native-lab metricbeat${reset}"
kubectl delete -f ${WORKDIR}/kube-state-metrics/examples/standard > /dev/null 2>&1
kubectl delete -f ${WORKDIR}/metricbeat-kubernetes.yaml > /dev/null 2>&1
kubectl delete secrets beats-credentials > /dev/null 2>&1
fi
if [ -e ${WORKDIR}/openldap.yaml ]; then
kubectl delete -f ${WORKDIR}/openldap.yaml >/dev/null 2>&1
kubectl delete secret openldap openldap-certificates ldapbindpw-secret >/dev/null 2>&1
fi
fi
rm -rf ${WORKDIR} > /dev/null 2>&1
echo ""
echo "${green}[DEBUG]${reset} All cleanedup"
echo ""
} # end cleanup
# FUNCTION - operator
operator()
{
echo "${green} ********** Deploying ECK ${blue}${ECKVERSION}${green} OPERATOR **************${reset}"
echo ""
touch ${WORKDIR}/ECK
# all version checks complete & directory structures created starting operator
if [ $(checkversion $ECKVERSION) -lt $(checkversion "1.7.0") ]; then # if version is less than 1.7.0
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} downloading operator: all-in-one.yaml"
if curl -sL --fail https://download.elastic.co/downloads/eck/${ECKVERSION}/all-in-one.yaml -o all-in-one.yaml; then # if curl is successful
kubectl apply -f ${WORKDIR}/all-in-one.yaml > /dev/null 2>&1
else
echo "${red}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} Failed to get all-in-one.yaml - check network/version?"
echo ""
help
exit
fi
else # if eckversion is not less than 1.7.0
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} downloading crds: crds.yaml"
if curl -fsSL https://download.elastic.co/downloads/eck/${ECKVERSION}/crds.yaml -o crds.yaml; then
kubectl create -f ${WORKDIR}/crds.yaml > /dev/null 2>&1
else
echo "${red}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} Failed to get crds.yaml - check network/version?"
echo ""
help
exit
fi
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} downloading operator: operator.yaml"
if curl -fsSL https://download.elastic.co/downloads/eck/${ECKVERSION}/operator.yaml -o operator.yaml; then
kubectl create -f ${WORKDIR}/operator.yaml > /dev/null 2>&1
else
echo "${red}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} Failed to get operator.yaml - check network/version?"
echo ""
help
exit
fi
fi
until [ "$(kubectl -n elastic-system get pod | grep elastic-operator | awk '{ print $3 }')" = "Running" ]; do
sleep 2
done &
spinner $! "${blue}[DEBUG]${reset} Checking on the operator to become ready. If this does not finish in ~5 minutes something is wrong"
echo ""
kubectl -n elastic-system get all | sed "s/^/ /"
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} Creating license.yaml"
# apply trial licence
cat >> ${WORKDIR}/license.yaml<<EOF
apiVersion: v1
kind: Secret
metadata:
name: eck-trial-license
namespace: elastic-system
labels:
license.k8s.elastic.co/type: enterprise_trial
annotations:
elastic.co/eula: accepted
EOF
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} Applying trial license"
kubectl apply -f ${WORKDIR}/license.yaml > /dev/null 2>&1
} # end operator
# FUNCTION - stackbuild
stackbuild() {
echo ""
echo "${green} ********** Deploying ECK ${blue}${ECKVERSION}${green} STACK ${BLUE}${VERSION}${green} CLUSTER ${blue}${1}${reset} **************${reset}"
echo ""
# create elasticsearch.yaml
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} CLUSTER ${blue}${1}${reset} Creating elasticsearch.yaml"
cat >> ${WORKDIR}/elasticsearch-${1}.yaml <<EOF
apiVersion: elasticsearch.k8s.elastic.co/v1
kind: Elasticsearch
metadata:
name: ${1}
spec:
version: ${VERSION}
nodeSets:
- name: default
config:
node.roles: ["master", "data", "ingest", "ml", "remote_cluster_client", "transform"]
xpack.security.authc.api_key.enabled: true
podTemplate:
metadata:
labels:
scrape: es
spec:
containers:
- name: elasticsearch
resources:
requests:
memory: 2Gi
cpu: 1
limits:
memory: 2Gi
cpu: 1
initContainers:
- name: sysctl
securityContext:
privileged: true
runAsUser: 0
command: ['sh', '-c', 'sysctl -w vm.max_map_count=262144']
count: 3
http:
service:
spec:
type: LoadBalancer
EOF
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} CLUSTER ${blue}${1}${reset} Starting elasticsearch cluster."
kubectl apply -f ${WORKDIR}/elasticsearch-${1}.yaml > /dev/null 2>&1
# checkeshealth
checkhealth "statefulset" "${1}-es-default" "readyReplicas" "3"
# create kibana.yaml
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} CLUSTER ${blue}${1}${reset} Creating kibana.yaml"
cat >> ${WORKDIR}/kibana-${1}.yaml <<EOF
apiVersion: kibana.k8s.elastic.co/v1
kind: Kibana
metadata:
name: ${1}
spec:
version: ${VERSION}
count: 1
elasticsearchRef:
name: "${1}"
http:
service:
spec:
type: LoadBalancer
podTemplate:
metadata:
labels:
scrape: kb
EOF
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} CLUSTER ${blue}${1}${reset} Starting kibana."
kubectl apply -f ${WORKDIR}/kibana-${1}.yaml > /dev/null 2>&1
#checkkbhealth
checkhealth "deployment" "${1}-kb" "readyReplicas" "1"
createsummary ${1}
} # end stackbuild
# FUNCTION - eckldap
eckldap() {
echo "${green}[DEBUG]${reset} Adding OPENLDAP server with TLS and patching ES deployment for ldap realm"
echo ""
sleep 5
echo "${green}[DEBUG]${reset} Creating patch for elasticsearch deployment(starting with this since it will take the longest)"
cat > ${WORKDIR}/elasticsearch-eck-lab-patch.yaml <<EOF
spec:
secureSettings:
- secretName: ldapbindpw-secret
nodeSets:
- config:
node.roles:
- master
- data
- ingest
- ml
- remote_cluster_client
- transform
xpack.security.authc.api_key.enabled: true
xpack.security.authc.realms.ldap.ldap1:
order: 0
url: "ldaps://openldap:1636"
bind_dn: "cn=admin, dc=example, dc=org"
user_search:
base_dn: "dc=example,dc=org"
filter: "(cn={0})"
group_search:
base_dn: "dc=example,dc=org"
ssl:
certificate_authorities: [ "config/openldap-certs/ca.crt" ]
verification_mode: none
count: 3
name: default
podTemplate:
spec:
containers:
- name: elasticsearch
volumeMounts:
- name: openldap-certs
mountPath: /usr/share/elasticsearch/config/openldap-certs
volumes:
- name: openldap-certs
secret:
secretName: openldap-certificates
defaultMode: 0640
EOF
echo "${green}[DEBUG]${reset} Applying the patch to eck-lab."
kubectl patch elasticsearch eck-lab --type merge --patch-file ${WORKDIR}/elasticsearch-eck-lab-patch.yaml >/dev/null 2>&1
sleep 2
echo "${green}[DEBUG]${reset} Creating ldapbindpw secret to make the keystore entry for bindpw"
cat > ${WORKDIR}/ldapbindpw-secret.yaml <<EOF
---
apiVersion: v1
kind: Secret
metadata:
name: ldapbindpw-secret
type: Opaque
data:
xpack.security.authc.realms.ldap.ldap1.secure_bind_password: YWRtaW5wYXNzd29yZA==
EOF
kubectl apply -f ${WORKDIR}/ldapbindpw-secret.yaml >/dev/null 2>&1
echo "${green}[DEBUG]${reset} Creating ca.crt and server certificate/key for openldap"
createcerts openldap openldap-certificates
echo "${green}[DEBUG]${reset} Creating secret for users and passwords"
kubectl create secret generic openldap --from-literal=adminpassword=adminpassword --from-literal=users=user01,user02 --from-literal=passwords=password01,password02 >/dev/null 2>&1
echo "${green}[DEBUG]${reset} Creating openldap.yaml"
cat > ${WORKDIR}/openldap.yaml<<EOF
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: openldap
labels:
app.kubernetes.io/name: openldap
spec:
selector:
matchLabels:
app.kubernetes.io/name: openldap
replicas: 1
template:
metadata:
labels:
app.kubernetes.io/name: openldap
spec:
containers:
- name: openldap
image: docker.io/bitnami/openldap:latest
imagePullPolicy: "Always"
env:
- name: LDAP_ADMIN_USERNAME
value: "admin"
- name: LDAP_ADMIN_PASSWORD
valueFrom:
secretKeyRef:
key: adminpassword
name: openldap
- name: LDAP_USERS
valueFrom:
secretKeyRef:
key: users
name: openldap
- name: LDAP_PASSWORDS
valueFrom:
secretKeyRef:
key: passwords
name: openldap
- name: LDAPTLS_REQCERT
value: "never"
- name: LDAP_ENABLE_TLS
value: "yes"
- name: LDAP_TLS_CA_FILE
value: "/opt/bitnami/openldap/certs/ca.crt"
- name: LDAP_TLS_CERT_FILE
value: "/opt/bitnami/openldap/certs/openldap.crt"
- name: LDAP_TLS_KEY_FILE
value: "/opt/bitnami/openldap/certs/openldap.key"
volumeMounts:
- name: openldap-certificates
mountPath: /opt/bitnami/openldap/certs
readOnly: true
ports:
- name: tcp-ldap
containerPort: 1636
volumes:
- name: openldap-certificates
secret:
secretName: openldap-certificates
defaultMode: 0660
---
apiVersion: v1
kind: Service
metadata:
name: openldap
labels:
app.kubernetes.io/name: openldap
spec:
type: ClusterIP
ports:
- name: tcp-ldap
port: 1636
targetPort: tcp-ldap
selector:
app.kubernetes.io/name: openldap
EOF
echo "${green}[DEBUG]${reset} Creating openldap server"
kubectl apply -f ${WORKDIR}/openldap.yaml >/dev/null 2>&1
sleep 5
echo "${green}[DEBUG]${reset} Creating role mappings"
until curl -s -k -u "elastic:${PASSWORD}" -XPUT "https://${ESIP}:9200/_security/role_mapping/ldap-admin" -H 'Content-Type: application/json' -d'
{"enabled":true,"roles":["superuser"],"rules":{"all":[{"field":{"realm.name":"ldap1"}}]},"metadata":{}}' >/dev/null 2>&1
do
sleep 2
done
echo "${green}[DEBUG]${red} Patching elasticsearch takes a while so please ensure that all ES pods have been recreated before trying to login with ldap${reset}"
} # end eckldap
# FUNCTION - dedicated
dedicated()
{
echo ""
echo "${green} ********** Deploying ECK ${blue}${ECKVERSION}${green} STACK ${BLUE}${VERSION}${green} CLUSTER ${blue}${1}${reset} with DEDICATED masters and data nodes **************${reset}"
echo ""
# create elasticsearch.yaml
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} CLUSTER ${blue}${1}${reset} Creating elasticsearch.yaml"