k8s的efk定义定期创建kibana索引和定期删除es索引,达到生命周期的效果
由于es索引偶然会添加字段导致有些字段报错,需要手动刷新,而kibana有接口可以调用,自动创建索引,这里只需要先删除kibana索引在添加一次就可达到相同效果,此处删除不会影响es数据 脚本功能,每月执行一次,删除前两个月之前的数据,不影响第3个月的...
·
由于es索引偶然会添加字段导致有些字段报错,需要手动刷新,而kibana有接口可以调用,自动创建索引,这里只需要先删除kibana索引在添加一次就可达到相同效果,此处删除不会影响es数据
apiVersion: batch/v1beta1 kind: CronJob metadata: name: cronjob-kibana-index namespace: pot spec: schedule: "*/30 * * * *" jobTemplate: spec: template: spec: containers: - name: busybox-container image: 自己的镜像 command: - "bin/bash" - "-c" - | #!/bin/bash INDEX='tapd|log' elk_ip=elasticsearch-master kibana_ip=kibana-kibana LOG=/tmp/index.log curl -s -XGET "http://${elk_ip}:9200/_cat/indices" > a.txt index2=$(cat a.txt | grep -E "$INDEX" | awk '{print $3}') for i in $index2 do curl -XDELETE -s -k "https://${kibana_ip}:5601/api/saved_objects/index-pattern/$i" -H 'kbn-xsrf: true' > $LOG curl -s -k -f -XPOST -H 'Content-Type: application/json' -H 'kbn-xsrf: anything' "https://${kibana_ip}:5601/api/saved_objects/index-pattern/$i" -d"{\"attributes\":{\"title\":\"$1\",\"timeFieldName\":\"@timestamp\"}}" >> $LOG cat $LOG done restartPolicy: OnFailure
注:7.11版本之后就可以弃用,因为自带索引字段自动添加
脚本功能,每月执行一次,删除前两个月之前的数据,不影响第3个月的
apiVersion: batch/v1beta1 kind: CronJob metadata: name: cronjob-es-index namespace: pot spec: schedule: "* * * */1 *" jobTemplate: spec: template: spec: containers: - name: busybox-container image: 自己的镜像 command: - "bin/bash" - "-c" - | #!/bin/bash format_day="%Y-%m" remove=() savedays=60 sevendayago=`date -d "-${savedays} day" +${format_day}` echo $sevendayago elk_ip=elasticsearch-master curl -s -XGET "http://${elk_ip}:9200/_cat/indices" > a.txt remove[${#remove[*]}]=$(cat a.txt | grep "tapd"| grep "log" |grep -E "$sevendayago"| awk '{print $3}'| awk -F- '{print $1"-"$2}'| uniq) function delIndex(){ local index_name=$remove local index=() for i in ${index_name[*]} do index[${#index[*]}]=${i}-${sevendayago} done for j in ${index[@]} do curl -XDELETE "http://${elk_ip}:9200/${j}" rm -rf a.txt done } delIndex restartPolicy: OnFailure
注:镜像需要安装该软件“coreutils”,否则时间格式无法使用-d选项
更多推荐
已为社区贡献9条内容
所有评论(0)