-
Notifications
You must be signed in to change notification settings - Fork 22
/
deploy-eck.sh.20220824
executable file
·1631 lines (1529 loc) · 46.6 KB
/
deploy-eck.sh.20220824
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 <[email protected]>
#
# version 1.0
# curl -fsSL https://raw.githubusercontent.com/jlim0930/scripts/master/deploy-eck.sh -o deploy-eck.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
# set WORKDIR
WORKDIR="${HOME}/eckstack"
###############################################################################################################
# colors
red=`tput setaf 1`
green=`tput setaf 2`
blue=`tput setaf 4`
reset=`tput sgr0`
###############################################################################################################
# help
help()
{
echo ""
echo "${green}This script is limited to ECK Operator 1.4.0+ & Stack 7.10.0+."
echo "${green} - various commands have additional limitations that will be listed below."
echo ""
echo "${green}USAGE:${reset} ./`basename $0` command STACKversion ECKversion"
echo ""
echo "${blue}COMMANDS:${reset}"
echo " ${green}operator${reset} - will just stand up the operator only and apply a trial license"
echo " ${green}stack|start|build${reset} - will stand up the ECK Operator, elasticsearch, & kibana with CLUSTER name : ${blue}eck-lab${reset}"
echo " ${green}beats${reset} - will stand up the basic stack + filebeat, metricbeat, packetbeat, & heartbeat"
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 " ${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 " ${green}fleet${reset} - will stand up the basic stack + FLEET Server & elastic-agent as DaemonSet on each ECK node."
echo ""
echo " ${green}cleanup${reset} - will delete all the resources including the ECK operator"
echo ""
echo "${green}EXAMPLE: ${reset}./`basename $0` fleet 8.2.0 2.2.0"
echo ""
echo "All yaml files will be stored in ${blue}~/eckstack${reset}"
echo " ${blue}~/eckstack/notes${reset} will contain all endpoint and password information"
echo " ${blue}~/eckstack/ca.crt${reset} will be the CA used to sign the public certificate"
echo ""
} # end of help
###############################################################################################################
# functions
# cleanup function
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 ""
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
rm -rf ${WORKDIR} > /dev/null 2>&1
echo ""
echo "${green}[DEBUG]${reset} All cleanedup"
echo ""
} # end of cleanup function
createsummary()
{
unset PASSWORD
while [ "${PASSWORD}" = "" ]
do
PASSWORD=$(kubectl get secret ${1}-es-elastic-user -o go-template='{{.data.elastic | base64decode}}')
echo "${green}[DEBUG]${reset} Grabbing elastic password for ${1}: ${blue}${PASSWORD}${reset}"
done
echo "${1} elastic password: ${PASSWORD}" >> notes
unset ESIP
while [ "${ESIP}" = "" ]
do
ESIP=`kubectl get service | grep ${1}-es-http | awk '{ print $4 }'`
echo "${green}[DEBUG]${reset} Grabbing elasticsearch endpoint for ${1}: ${blue}https://${ESIP}:9200${reset}"
done
echo "${1} elasticsearch endpoint: https://${ESIP}:9200" >> notes
unset KIBANAIP
while [ "${KIBANAIP}" = "" -o "${KIBANAIP}" = "<pending>" ]
do
KIBANAIP=`kubectl get service | grep ${1}-kb-http | awk '{ print $4 }'`
echo "${green}[DEBUG]${reset} Grabbing kibana endpoint for ${1}: ${blue}https://${KIBANAIP}:5601${reset}"
sleep 2
done
echo "${1} kibana endpoint: https://${KIBANAIP}:5601" >> notes
if [ "${1}" = "eck-lab" ]; then
kubectl get secrets ${1}-es-http-certs-public -o jsonpath="{.data.ca\.crt}" | base64 -d > ca.crt
fi
echo ""
}
summary()
{
echo ""
echo "${green}[SUMMARY]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset}"
echo ""
kubectl get all
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
# cat ${WORKDIR}/notes
#echo "${green}[SUMMARY]${reset} ${1} elastic user password: ${blue}`cat ${WORKDIR}/notes | grep 'ECK-LAB elastic password' | awk '{ print $NF }'`${reset}"
#echo "${green}[SUMMARY]${reset} ${1} elasticsearch endpoint: ${blue}`cat ${WORKDIR}/notes | grep 'ECK-LAB elasticsearch endpoint' | awk '{ print $NF }'`${reset}"
#echo "${green}[SUMMARY]${reset} ECK-LAB kibana endpoint: ${blue}`cat ${WORKDIR}/notes | grep 'ECK-LAB kibana endpoint' | awk '{ print $NF }'`${reset}"
echo ""
echo "${green}[SUMMARY]${reset} ${blue}ca.crt${reset} is located in ${blue}${WORKDIR}/ca.crt${reset}"
#echo "${green}[SUMMARY]${reset} EXAMPLE: ${blue}curl --cacert ${WORKDIR}/ca.crt -u \"elastic:${PASSWORD}\" https://${ESIP}:9200${reset}"
# curl --cacert ${WORKDIR}/ca.crt -u "elastic:${PASSWORD}" https://${ESIP}:9200
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 ""
}
# check jq
checkjq()
{
if ! [ -x "$(command -v jq)" ]; then
echo "${red}[DEBUG]${reset} jq is not installed. Please install jq and try again"
exit
fi
} # end of checkjq
# check 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
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 environment and try again"
exit
fi
} # end checkubectl
# function used for version checking and comparing
checkversion()
{
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }'
} # end of checkversion function
# check directory exist
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
echo ${VERSION} > VERSION
echo ${ECKVERSION} > ECKVERSION
} # checkdir
# check health of various things
checkhealth() {
sleep 3
while true
do
if [ "`kubectl get ${1} | grep "${2} " | awk '{ print $2 }'`" = "green" ]; then
sleep 2
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} is ${green}HEALTHY${reset}"
echo ""
kubectl get ${1}
echo ""
break
else
echo "${red}[DEBUG]${reset} ${1} is starting. Checking again in 20 seconds. If this does not finish in few minutes something is wrong. CTRL-C please"
#echo ""
#kubectl get ${1}
#echo ""
#kubectl get pods | grep "${2} "
#echo ""
sleep 20
fi
done
} # end checkhealth
###############################################################################################################
# operator
operator()
{
echo "${green} ********** Deploying ECK ${blue}${ECKVERSION}${green} OPERATOR **************${reset}"
echo ""
# 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 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 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 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
while true
do
if [ "`kubectl -n elastic-system get pod | grep elastic-operator | awk '{ print $3 }'`" = "Running" ]; then
sleep 2
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} OPERATOR is ${green}HEALTHY${reset}"
echo ""
kubectl -n elastic-system get all
echo ""
break
else
echo "${red}[DEBUG]${reset} ECK Operator is starting. Checking again in 20 seconds. If the operator does not goto Running status in few minutes something is wrong. CTRL-C please"
# kubectl -n elastic-system get pod
echo ""
sleep 20
fi
done
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} Creating license.yaml"
# apply trial licence
cat >>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 license.yaml > /dev/null 2>&1
# sleep 30
# kubectl -n elastic-system get configmap elastic-licensing -o json | jq -r '.data'
} # end of operator
###############################################################################################################
# stack
stack()
{
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 >> 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"]
xpack.security.authc.api_key.enabled: true
podTemplate:
metadata:
labels:
scrape: es
spec:
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 elasticsearch-${1}.yaml > /dev/null 2>&1
# checkeshealth
checkhealth "elasticsearch" "${1}"
# create kibana.yaml
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} CLUSTER ${blue}${1}${reset} Creating kibana.yaml"
cat >> 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 kibana-${1}.yaml > /dev/null 2>&1
#checkkbhealth
checkhealth "kibana" "${1}"
createsummary ${1}
} # end of stack
###############################################################################################################
# filebeat autodiscover & metricbeat hosts as daemonset onto k8s hosts
beats()
{
echo ""
echo "${green} ********** Deploying ECK ${blue}${ECKVERSION}${green} STACK ${BLUE}${VERSION}${green} with BEATS **************${reset}"
echo ""
# Create and apply metricbeat-rbac
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} Creating BEATS crds"
cat >> beats-crds.yaml<<EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: metricbeat
rules:
- apiGroups:
- ""
resources:
- nodes
- namespaces
- events
- pods
verbs:
- get
- list
- watch
- apiGroups:
- "extensions"
resources:
- replicasets
verbs:
- get
- list
- watch
- apiGroups:
- apps
resources:
- statefulsets
- deployments
- replicasets
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- nodes/stats
verbs:
- get
- nonResourceURLs:
- /metrics
verbs:
- get
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: metricbeat
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: metricbeat
subjects:
- kind: ServiceAccount
name: metricbeat
namespace: default
roleRef:
kind: ClusterRole
name: metricbeat
apiGroup: rbac.authorization.k8s.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: filebeat
rules:
- apiGroups: [""] # "" indicates the core API group
resources:
- namespaces
- pods
- nodes
verbs:
- get
- watch
- list
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: filebeat
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: filebeat
subjects:
- kind: ServiceAccount
name: filebeat
namespace: default
roleRef:
kind: ClusterRole
name: filebeat
apiGroup: rbac.authorization.k8s.io
EOF
kubectl apply -f beats-crds.yaml > /dev/null 2>&1
# Create and apply metricbeat-rbac
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} Creating BEATS"
cat >> beats.yaml<<EOF
apiVersion: beat.k8s.elastic.co/v1beta1
kind: Beat
metadata:
name: metricbeat
spec:
type: metricbeat
version: ${VERSION}
elasticsearchRef:
name: ${1}
config:
metricbeat:
autodiscover:
providers:
- hints:
default_config: {}
enabled: "true"
node: \${NODE_NAME}
type: kubernetes
modules:
- module: system
period: 10s
metricsets:
- cpu
- load
- memory
- network
- process
- process_summary
process:
include_top_n:
by_cpu: 5
by_memory: 5
processes:
- .*
- module: system
period: 1m
metricsets:
- filesystem
- fsstat
processors:
- drop_event:
when:
regexp:
system:
filesystem:
mount_point: ^/(sys|cgroup|proc|dev|etc|host|lib)($|/)
- module: kubernetes
period: 10s
node: \${NODE_NAME}
hosts:
- https://\${NODE_NAME}:10250
bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
ssl:
verification_mode: none
metricsets:
- node
- system
- pod
- container
- volume
processors:
- add_cloud_metadata: {}
- add_host_metadata: {}
daemonSet:
podTemplate:
spec:
serviceAccountName: metricbeat
automountServiceAccountToken: true # some older Beat versions are depending on this settings presence in k8s context
containers:
- args:
- -e
- -c
- /etc/beat.yml
- -system.hostfs=/hostfs
name: metricbeat
volumeMounts:
- mountPath: /hostfs/sys/fs/cgroup
name: cgroup
- mountPath: /var/run/docker.sock
name: dockersock
- mountPath: /hostfs/proc
name: proc
env:
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
dnsPolicy: ClusterFirstWithHostNet
hostNetwork: true # Allows to provide richer host metadata
securityContext:
runAsUser: 0
terminationGracePeriodSeconds: 30
volumes:
- hostPath:
path: /sys/fs/cgroup
name: cgroup
- hostPath:
path: /var/run/docker.sock
name: dockersock
- hostPath:
path: /proc
name: proc
---
apiVersion: beat.k8s.elastic.co/v1beta1
kind: Beat
metadata:
name: filebeat
spec:
type: filebeat
version: ${VERSION}
elasticsearchRef:
name: ${1}
kibanaRef:
name: ${1}
config:
filebeat:
autodiscover:
providers:
- type: kubernetes
node: \${NODE_NAME}
hints:
enabled: true
default_config:
type: container
paths:
- /var/log/containers/*\${data.kubernetes.container.id}.log
processors:
- add_cloud_metadata: {}
- add_host_metadata: {}
daemonSet:
podTemplate:
spec:
serviceAccountName: filebeat
automountServiceAccountToken: true
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirstWithHostNet
hostNetwork: true # Allows to provide richer host metadata
containers:
- name: filebeat
securityContext:
runAsUser: 0
# If using Red Hat OpenShift uncomment this:
#privileged: true
volumeMounts:
- name: varlogcontainers
mountPath: /var/log/containers
- name: varlogpods
mountPath: /var/log/pods
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
env:
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
volumes:
- name: varlogcontainers
hostPath:
path: /var/log/containers
- name: varlogpods
hostPath:
path: /var/log/pods
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
---
apiVersion: beat.k8s.elastic.co/v1beta1
kind: Beat
metadata:
name: packetbeat
spec:
type: packetbeat
version: ${VERSION}
elasticsearchRef:
name: ${1}
kibanaRef:
name: ${1}
config:
packetbeat.interfaces.device: any
packetbeat.protocols:
- type: dns
ports: [53]
include_authorities: true
include_additionals: true
- type: http
ports: [80, 8000, 8080, 9200, 5601]
packetbeat.flows:
timeout: 30s
period: 10s
processors:
- add_cloud_metadata: {}
- add_host_metadata: {}
daemonSet:
podTemplate:
spec:
terminationGracePeriodSeconds: 30
hostNetwork: true
automountServiceAccountToken: true # some older Beat versions are depending on this settings presence in k8s context
dnsPolicy: ClusterFirstWithHostNet
containers:
- name: packetbeat
securityContext:
runAsUser: 0
capabilities:
add:
- NET_ADMIN
---
apiVersion: beat.k8s.elastic.co/v1beta1
kind: Beat
metadata:
name: heartbeat
spec:
type: heartbeat
version: ${VERSION}
elasticsearchRef:
name: ${1}
config:
heartbeat.monitors:
- type: tcp
schedule: '@every 5s'
hosts: ["${1}-es-http.default.svc:9200"]
- type: tcp
schedule: '@every 5s'
hosts: ["${1}-kb-http.default.svc:5601"]
deployment:
replicas: 1
podTemplate:
spec:
securityContext:
runAsUser: 0
EOF
kubectl apply -f beats.yaml > /dev/null 2>&1
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} filebeat, metricbeat, packetbeat, & heartbeat deployed"
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} Please wait a few minutes for the beats to become healthy. (it will restart 3-4 times before it becomes healthy) & for the data to start showing"
#sleep 30
#echo ""
#kubectl get daemonset
echo ""
}
###############################################################################################################
# stack monitoring - beats in pods
monitor1()
{
echo ""
echo "${green} ********** Deploying ECK ${blue}${ECKVERSION}${green} STACK ${BLUE}${VERSION}${green} Stack Monitoring with BEATS in Pods **************${reset}"
echo ""
# remove labels from eck-lab-montor pods
# is this needed?
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} Removing scrape label from monitoring pods"
for item in `kubectl get pods --no-headers -o custom-columns=":metadata.name" | grep eck-lab-monitor`
do
kubectl label pod ${item} scrape- > /dev/null 2>&1
done
sleep 10
# Create and apply monitor1.yaml
cat >> monitor1.yaml<<EOF
apiVersion: beat.k8s.elastic.co/v1beta1
kind: Beat
metadata:
name: metricbeat
spec:
type: metricbeat
version: ${VERSION}
elasticsearchRef:
name: eck-lab-monitor
kibanaRef:
name: eck-lab-monitor
config:
metricbeat:
autodiscover:
providers:
- type: kubernetes
scope: cluster
hints.enabled: true
templates:
- condition:
contains:
kubernetes.labels.scrape: es
config:
- module: elasticsearch
metricsets:
- ccr
- cluster_stats
- enrich
- index
- index_recovery
- index_summary
- ml_job
- node_stats
- shard
period: 10s
hosts: "https://\${data.host}:\${data.ports.https}"
username: \${MONITORED_ES_USERNAME}
password: \${MONITORED_ES_PASSWORD}
# WARNING: disables TLS as the default certificate is not valid for the pod FQDN
# TODO: switch this to "certificate" when available: https://github.com/elastic/beats/issues/8164
ssl.verification_mode: "none"
xpack.enabled: true
- condition:
contains:
kubernetes.labels.scrape: kb
config:
- module: kibana
metricsets:
- stats
period: 10s
hosts: "https://\${data.host}:\${data.ports.https}"
username: \${MONITORED_ES_USERNAME}
password: \${MONITORED_ES_PASSWORD}
# WARNING: disables TLS as the default certificate is not valid for the pod FQDN
# TODO: switch this to "certificate" when available: https://github.com/elastic/beats/issues/8164
ssl.verification_mode: "none"
xpack.enabled: true
processors:
- add_cloud_metadata: {}
logging.json: true
deployment:
podTemplate:
spec:
serviceAccountName: metricbeat
automountServiceAccountToken: true
# required to read /etc/beat.yml
securityContext:
runAsUser: 0
containers:
- name: metricbeat
env:
- name: MONITORED_ES_USERNAME
value: elastic
- name: MONITORED_ES_PASSWORD
valueFrom:
secretKeyRef:
key: elastic
name: ${1}-es-elastic-user
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: metricbeat
rules:
- apiGroups: [""] # "" indicates the core API group
resources:
- namespaces
- pods
- nodes
verbs:
- get
- watch
- list
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: metricbeat
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: metricbeat
subjects:
- kind: ServiceAccount
name: metricbeat
namespace: default
roleRef:
kind: ClusterRole
name: metricbeat
apiGroup: rbac.authorization.k8s.io
---
# filebeat resources
apiVersion: beat.k8s.elastic.co/v1beta1
kind: Beat
metadata:
name: filebeat
spec:
type: filebeat
version: ${VERSION}
elasticsearchRef:
name: eck-lab-monitor
kibanaRef:
name: eck-lab-monitor
config:
filebeat:
autodiscover:
providers:
- type: kubernetes
node: \${NODE_NAME}
hints:
enabled: true
default_config:
type: container
paths:
- /var/log/containers/*\${data.kubernetes.container.id}.log
processors:
- add_cloud_metadata: {}
- add_host_metadata: {}
logging.json: true
daemonSet:
podTemplate:
spec:
serviceAccountName: filebeat
automountServiceAccountToken: true
terminationGracePeriodSeconds: 30
dnsPolicy: ClusterFirstWithHostNet
hostNetwork: true # Allows to provide richer host metadata
securityContext:
runAsUser: 0
# If using Red Hat OpenShift uncomment this:
#privileged: true
containers:
- name: filebeat
volumeMounts:
- name: varlogcontainers
mountPath: /var/log/containers
- name: varlogpods
mountPath: /var/log/pods
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
env:
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
volumes:
- name: varlogcontainers
hostPath:
path: /var/log/containers
- name: varlogpods
hostPath:
path: /var/log/pods
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: filebeat
rules:
- apiGroups: [""] # "" indicates the core API group
resources:
- namespaces
- pods
- nodes
verbs:
- get
- watch
- list
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: filebeat
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: filebeat
subjects:
- kind: ServiceAccount
name: filebeat
namespace: default
roleRef:
kind: ClusterRole
name: filebeat
apiGroup: rbac.authorization.k8s.io
EOF
kubectl apply -f monitor1.yaml > /dev/null 2>&1
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} Stack monitoring with BEATS in PODS deployed"
#echo ""
#kubectl get daemonset
echo ""
}
###############################################################################################################
# stack monitoring - side car
monitor2()
{
echo ""
echo "${green} ********** Deploying ECK ${blue}${ECKVERSION}${green} STACK ${BLUE}${VERSION}${green} Stack Monitoring with BEATS in sidecar containers **************${reset}"
echo ""
# create elasticsearch-eck-lab.yaml
echo "${green}[DEBUG]${reset} ECK ${blue}${ECKVERSION}${reset} STACK ${blue}${VERSION}${reset} CLUSTER ${blue}${1}${reset} Creating elasticsearch.yaml"
cat >> monitor2.yaml <<EOF
apiVersion: elasticsearch.k8s.elastic.co/v1
kind: Elasticsearch
metadata:
name: ${1}