es 索引自动删除

  1. 由于我们每天要存储大量的数据到elasticsearch中,但是存储空间经常有限,导致es崩掉了好多次,所以想到了2种解决方案
    1. 通过kibana 设置索引生命周期

    1. 编写自动删除索引脚本,并通过k8s的方式运行
      #删除第10天的索引
      #/bin/sh
      DATA=`date -d "-10 days" +%Y.%m.%d`
      time=`date`
       #for i in `seq -w 1 17`; # 通过循环过滤出1-17的索引
      echo "开始清理  $DATE 索引"
      curl -u elastic:R^rlv4E*J8g$   -s  10.1.128.86:9200/_cat/indices?v|grep ${DATA}
      if [ $? == 0 ];then
        curl --user  elastic:R^rlv4E*J8g$  -XDELETE "http://10.1.128.86:9200/*-${DATA}"
        echo "于 $time 清理 $DATA 索引!"
      fi
      #删除1 -20 天的索引
      #/bin/sh
      time=`date`
      for i in `seq -w 1 20`; # 通过循环过滤出1-17的索引
      do 
      DATE="2021.05.${i}"
      echo "开始清理  $DATE 索引"
      curl -u elastic:R^rlv4E*J8g$   -s  10.1.128.86:9200/_cat/indices?v|grep ${DATE}
      if [ $? == 0 ];then
        curl --user  elastic:R^rlv4E*J8g$  -XDELETE "http://10.1.128.86:9200/*-${DATE}"
        echo "于 $time 清理 $DATA 索引!"
      fi
      done

      编写dockerfile

      FROM alpine:3.13.1
      
      RUN set -x \
          && sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
          && apk add --update busybox-suid \
          && apk add --no-cache curl ca-certificates tzdata
      
      ENV LANG C.UTF-8
      ENV TZ Asia/Shanghai
          
      WORKDIR /var/spool/cron/crontabs/
      
      COPY cron root
      COPY es.sh /root/
      
      ENTRYPOINT ["crond"]
      
      CMD ["-f", "-l", "1", "-d", "8"]

      cron

      # do daily/weekly/monthly maintenance
      # min	hour	day	month	weekday	command
        00	8	  *     *        *    /bin/sh /root/es.sh

      k8s-es-delete-indexs.yaml

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        labels:
          app: es-delete
        name:  es-delete
      spec:
        replicas: 1   
        selector:
          matchLabels:
            app: es-delete
        template:
          metadata:
            labels:
              app: es-delete
          spec:
            containers:
            - image: zexubao/es-delete:v4
              name: es-delete
              command: ["crond"] 
              args: ["-f", "-l", "1", "-d", "8"]
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐