ES索引管理curator


1. Curator安裝

CuratorElasticsearch提供的一個可用來管理索引和快照的命令行工具,它是一個Python程序,可以直接通過pipy庫安裝:

安裝pippip install elasticsearch-curator

-U更新到最新版本: pip install -U elasticsearch-curator

安裝特定版本:pip install -U elastcisearch-curator==X.Y.Z

curator安裝到/home/user/.local/bin/curator

 

pip install --user elastcisearch-curator

注:安裝Curator需要先安裝PythonPython的匹配命令,10.45.157.130Curator已安裝,可以直接使用。

2. Curator運行

Curator是一個命令行工具,本節會主要介紹如何使用命令行使用Curator

可以通過curator --help 命令查看curator的用法,返回結果如下:

一般我們用到的命令是:

curator [--config CONFIG.YML] [--dry-run] ACTION_FILE.YML

--config CONFIG.YML是配置文件,如果不寫在curator路徑下會有一個默認的配置文件,主要的就是寫action_file.yml腳本

curator.yml 配置文件,主要是配一下host ip和端口號:

 

client:
  hosts:
    - 10.45.157.94
  port: 9200
  url_prefix:
  use_ssl: False
  certificate:
  client_cert:
  client_key:
  ssl_no_validate: False
  http_auth:
  timeout: 30
  master_only: False

logging:
  loglevel: INFO
  logfile:
  logformat: default
  blacklist: ['elasticsearch', 'urllib3']

 

 

運行時有兩種方法:

1、 只模擬,並不真的做操作,這樣方便我們調試:

curator --config /root/.curator/curator.yml  --dry-run

/root/.curator/action_file.yml

2、去掉--dry-run,真正的運行命令

curator --config /root/.curator/curator.yml  /root/.curator/action_file.yml

如果需要將結果打印到日志中,后面可跟日志文件的路徑和名稱:

Curator --config /root/.curator/curator.yml  /root/.curator/action_file.yml

>> /root/.curator/test.log  

同理,我們可以用同樣的方法查看並學習其它命令,如:

curator_cli --help  

curator_cli [OPTIONS] COMMAND [ARGS] 

 

列出所有的索引:
curator_cli --host "10.45.157.*" --port "9200" show_indices --filter_list '{"filtertype":"none"}'

 

curator_cli show_indices --help

3. Rollover的使用

Rollover即滾動索引,用於將滿足一定時間或數據的索引滾動到新索引。例如:創建一個logs-0001的索引,它有一個別名是logs_write

curl -XPUT 'http://host:9200/logs-test-0001/'?pretty -d'{

  "aliases":{

"logs_write":{}

  }

}'

然后我們給這個logs_write創建了一個rollover規則,即這個索引文檔不超過1000個或者最多保存7天的數據,超過會自動切換別名到logs-000002,你也可以設置索引的settingmapping等參數,剩下的es會自動幫你處理。

curl -XPOST 'http://host:9200/logs_write/_rollover'?pretty -d'{

"conditions":{

    "max_age":"1m",

"max_docs":1000

  },

  "settings":{

    "index.number_of_shards":2,

"refresh_interval":"1s"

  }

}'

4. 定時任務的創建

crontab命令用於設置周期性被執行的指令。該命令從標准輸入設備讀取指令,並將其存放於“crontab”文件中,以供之后讀取和執行。

基本格式 : 
*  *  *  *  *  command 
分 時 日 月 周 命令 
第1列表示分鍾1~59 每分鍾用*或者 */1表示 
第2列表示小時1~23(0表示0點) 
第3列表示日期1~31 
第4列表示月份1~12 
第5列標識號星期0~6(0表示星期天) 
第6列要運行的命令 

 “*”代表取值范圍內的數字,
  “/”代表”每”,
  “-”代表從某個數字到某個數字,
  “,”分開幾個離散的數字

用法:

cat /etc/crontab    查看/etc/crontab文件

crontab -e   或者以root用戶運行:crontab -u root -e

如果要每周一到周六的8點執行一次命令:

0  8  *  *  1-6   你要運行的命令 >> /你的路徑/create-Index.log 2>&1  

例如:每分鍾執行一次curator --config /root/.curator/curator.yml  /root/.curator/action_file.yml命令:

 

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
*/1 * * * * curator --config /root/.curator/curator.yml /root/.curator/action_file.yml>>/root/.curator/create-Index.log 2>&1
//將日志打印到/root/.curator/目錄下的create-Index.log中,其中2>&1 表示執行結果及錯誤信息

 

:wq存盤退出

列出某個用戶cron服務的詳細內容:crontab -l

重啟:service crond restart

 

刪除所有任務調度工作:crontab -r  

5. 示例

現在以按照規定時間自動建立新索引,並將數據寫入到新索引中,再進行查詢為例,具體流程如下:

1、首先先寫索引模板(自動匹配以logs-開頭的所有索引)

 

curl -XPOST 'host:9200/_template/logs_template?pretty' -d'{
  "template": "logs-*",
  "order": 0,
  "settings": {
    "number_of_shards": 2,
    "number_of_replicas": 1
  },
  "mappings":{
    "type1":{
      "_all": {
        "enabled": false
      },
      "_field_names": {
        "enabled": false
      },
      "properties":{
      "id":{
      "type":"integer",
      "index":"not_analyzed"
      },
      "name":{
      "type":"keyword",
      "index":"not_analyzed"
      },
      "gender":{
      "type":"integer",
      "index":"not_analyzed"
      },
      "age":{
      "type":"integer",
      "index":"not_analyzed"
      },
      "enter_time":{
      "type":"date",
      "index":"not_analyzed"
      }
      }
    }
  }
}'

2、logs-當前日期建立一個索引,如今天為2017.7.20,會建立一個"logs-2017.07.20-1"的索引指定別名logs_write

curl -XPUT 'http://host:9200/%3Clogs-%7Bnow%2Fd%7D-1%3E/'?pretty -d'{
"aliases":{
"logs_write":{}
 }
}'

3、action_file.yml文件,使用rollover讓其每滿1鍾便可新建一個索引

 

actions:
  1:
    action: rollover
    description: >- 
        Rollover the index associated with index 'name', which should be in the form of prefix-000001 (or similar), or prefix-YYYY.MM.DD-1.
    options:
      name: logs_write
      conditions:
        max_age: 10s
        max_docs: 10000
      extra_settings:
        index.number_of_shards: 2
        index.number_of_replicas: 1
      timeout_override:
      continue_if_exception: False
      disable_action: False

 

4、運行curator,如果滿1分鍾,下次運行時會創建"logs-2017.07.20-000002"索引

5、crontab建立定時任務,讓其每分鍾運行一次curator

 

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
*/1 * * * * curator --config /root/.curator/curator.yml /root/.curator/action_file.yml>>/root/.curator/create-Index.log 2>&1
//將日志打印到/root/.curator/目錄下的create-Index.log中,其中2>&1 表示執行結果及錯誤信息

有時需要重啟一下crontab:service crond restart

 

 

6、將批量寫數據的程序打包成jar包,放到你的目錄下,再次目錄下運行java -jar 你的jar包名稱,即可往索引中批量寫入數據

 

7、數據查詢,可將自動創建的索引放入一個別名中

 

 actions:
  1:
    action: alias
    description: >-
      Add/Remove selected indices to or from the specified alias,
      with a prefix of logs- to 'logs_all'
    options:
      name: logs_all
      warn_if_no_indices: False
      disable_action: False
    add:
      filters:
      - filtertype: pattern
        kind: prefix
        value: logs-
        exclude:

直接用別名進行查詢即可。

6. 定期刪除索引

利用curator刪除索引的執行文件如下:

主要的配置是filtertype部分,即過濾出你要刪除的索引。上例中,有兩個filtertype,一個是以logs-為開頭的前綴,另一個是創建時間為5分鍾之前,所以,此執行文件會刪除5分鍾之前創建的,並且名字以logs-為前綴的所有索引。

filtertypeage時,時間計算單位unit可以是:secondsminuteshoursdaysweeksmouthsyears

寫好執行文件,利用crontab創建定時任務的過程與上面相同。

7.補充

命令刪除索引:

curator delete indices --index .marvel- --older-than 3 --time-unit days --timestring %Y.%m.%d --dry-run  //索引名是.marvel-開頭的,時間格式是%Y.%m.%d,刪除三天前的索引。並使用調試模式(--dry-run),只模擬刪除,並不真的做操作,這樣方便我們調試,如果換成正式的語句,去掉--dry-run即可

 創建索引的action_file.yml 執行文件:

actions:
  1:
    action: create_index
    description: Create the index as named, with the specified extra settings.
    options:
      name: '<test10-{now/d}>' //索引名字
      extra_settings:
        settings:
          number_of_shards: 2
          number_of_replicas: 1
        mappings:
          type1:            //索引類型
             _all:
                enabled: false
             properties: //前面的空格要對其,不然會報錯
                field1:
                   type: string   //如果寫成type:string就會報錯
                   index: not_analyzed 
      disable_action: False

注意:冒號后面需要加一個空格再寫屬性,不然會出現異常,無法解析map,會當成一個字符串

 如果有兩個action操作,依次往下寫即可:

actions:
  1:
    action: create_index
    description: Create the index as named, with the specified extra settings.
    options:
      name: '<logstash-{now/d}>'
      extra_settings:
        settings:
          number_of_shards: 2
          number_of_replicas: 1
      disable_action: True
  2:
    action: rollover
    description: >- 
        Rollover the index associated with index 'name', which should be in the form of prefix-000001 (or similar), or prefix-YYYY.MM.DD-1.
    options:
      name: logs_write
      conditions:
        max_age: 1m
        max_docs: 100000
      extra_settings:
        index.number_of_shards: 3
        index.number_of_replicas: 1
      timeout_override:
      continue_if_exception: False
      disable_action: False

別名操作:如果今日是7.13,現有三個索引logstash-2017.07.05、logstash-2017.07.12、logstash-2017.07.13,第一個是屬於上周的,第二個和第三個是屬於本周的,下面的例子會將上周的索引放入別名last_week中,如果last_week不存在會創建這個別名;如果last_week存在,並且logstash-2017.07.12索引也在里面,它會從last_week別命中刪除logstash-2017.07.12,只放時間是上周的索引。add和remove可以都有或者有一個(但如果兩個都有時,就要有對應的索引,不然會報錯,比如如果沒有remove中的range_from是-2,而不存在兩周前的索引,就會報錯)

actions:
  1:
    action: alias
    description: >-
      Alias indices from last week, with a prefix of logstash- to 'last_week',
      remove indices from the previous week.
    options:
      name: last_week
      warn_if_no_indices: False
      disable_action: False
    add:
      filters:
      - filtertype: pattern
        kind: prefix
        value: logstash-
        exclude:
      - filtertype: period
        source: name
        range_from: -1
        range_to: -1
        timestring: '%Y.%m.%d'
        unit: weeks
        week_starts_on: sunday
    remove:
      filters:
      - filtertype: pattern
        kind: prefix
        value: logstash-
      - filtertype: period
        source: name
        range_from: 0
        range_to: 0
        timestring: '%Y.%m.%d'
        unit: weeks
        week_starts_on: sunday

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM