1. 程式人生 > >Elasticsearch簡單入門--elasticsearch簡單配置

Elasticsearch簡單入門--elasticsearch簡單配置

配置Elasticsearch

Elasticsearch具有良好的預設設定,並且只需要很少的配置. 可以使用群集更新設定API在執行的群集上更改大多數設定。

叢集更新設定API 參考:

Cluster Update Settings API (https://www.elastic.co/guide/en/elasticsearch/reference/5.6/cluster-update-settings.html

配置檔案應該包含特定於節點的設定(比如節點名和路徑),或者節點為了能夠加入叢集而需要的設定,比如cluster.name和network.host。

1. 配置檔案的位置

$ES_HOME : elasticsearch的安裝目錄簡寫

Elasticsearch有2個配置檔案:

  • elasticsearch.yml  配置Elasticsearch
  • log4j2.properties 配置Elasticsearch日誌

這2個檔案位於config目錄中,其位置預設在$ES_HOME/config/. 不同安裝方式,其配置檔案所在目錄不同,通過Debain和RPM包安裝時,配置檔案所在目錄為 /etc/elasticsearch/

配置檔案目錄所在位置可通過path.conf來改變

[[email protected]
elasticsearch-5.6.14]# ./bin/elasticsearch -Epath.conf=/path/to/my/config/

2. 配置檔案的格式

配置檔案的格式是YAML, 用過springboot的大佬們 應該都很熟悉,例如,改變資料和日誌的目錄

path:
    data: /var/lib/elasticsearch
    logs: /var/log/elasticsearch

設定也可以用平攤的格式,如下

path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch

3. 環境變數替換

在配置檔案中,通過${...} 符合引用的環境變數將被替換環境變數的值,例如

node.name:    ${HOSTNAME}
network.host: ${ES_NETWORK_HOST}

4. 提示的設定

對於你不想儲存在配置中的設定,你可以使用${prompt.text} 或者${prompt.secret}的值,並且在前端啟動elasticsearch時,${prompt.secret}禁止列印,因此你輸入的值不會在你的終端顯示,${prompt.text}允許你看到你鍵入的值,例如

node.name: ${prompt.text}

當你啟動elasticsearch時,你將被提示鍵入真實的值

Enter value for [node.name]:

注意:如果${prompt.text} 或 ${prompt.secret}在配置中被使用,並且程序作為服務或在後臺執行,Elasticsearch將不會啟動。

5. 日誌配置

Elasticsearch使用log4j2進行日誌記錄,使用log4j2.properties檔案可以配置log4j2, Elasticsearch暴露了3個屬性

${sys:es.logs.base_path}
${sys:es.logs.cluster_name}
${sys:es.logs.node_name}

${sys:es.logs.node_name} (如果節點名稱明確地通過node.name設定)在配置檔案中能被引用去確定日誌檔案的位置。

${sys:es.logs.base_path} 將解析為日誌目錄

${sys:es.logs.cluster_name} 將解析為叢集名稱(在預設配置中被用作日誌檔案的字首)

${sys:es.logs.node_name} 將被解析為節點名稱

例如: 如果你的日誌目錄(path.logs) 是/var/log/elasticsearch 並且你的叢集用production命名,那麼${sys:es.logs.base_path}將被解析為/var/log/elasticsearch,並且${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log將被解析為

/var/log/elasticsearch/production.log

appender.rolling.type = RollingFile    # 1
appender.rolling.name = rolling
appender.rolling.fileName = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}.log  # 2
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%d{ISO8601}][%-5p][%-25c] %.10000m%n
appender.rolling.filePattern = ${sys:es.logs.base_path}${sys:file.separator}${sys:es.logs.cluster_name}-%d{yyyy-MM-dd}.log # 3
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy # 4
appender.rolling.policies.time.interval = 1 # 5
appender.rolling.policies.time.modulate = true  # 6

Configure the RollingFile appender (配置RollingFile appender)

Log to /var/log/elasticsearch/production.log (記錄日誌到 /var/log/elasticsearch/production.log)

Roll logs to /var/log/elasticsearch/production-yyyy-MM-dd.log (滾動的日誌 /var/log/elasticsearch/production-yyy-MM-dd.log)

Using a time-based roll policy  (使用基於時間的滾動策略)

Roll logs on a daily basis  (每天滾動日誌)

Align rolls on the day boundary (as opposed to rolling every twenty-four hours)

注意: Log4j的配置解析會被任何無關的空白所混淆,如果您複製並貼上此頁上的任何Log4j設定,或輸入任何Log4j配置,請確保修剪任何開頭和結尾的空白。

如果你在appender.rolling.filePattern中新增.gz 或 .zip ,然後在滾動日誌在時對其進行壓縮。

如果希望在指定的一段時間內保留日誌檔案,可以使用帶有刪除操作的滾動策略。

appender.rolling.strategy.type = DefaultRolloverStrategy  # 1
appender.rolling.strategy.action.type = Delete # 2
appender.rolling.strategy.action.basepath = ${sys:es.logs.base_path}  # 3
appender.rolling.strategy.action.condition.type = IfLastModified  # 4
appender.rolling.strategy.action.condition.age = 7D  # 5
appender.rolling.strategy.action.PathConditions.type = IfFileName # 6
appender.rolling.strategy.action.PathConditions.glob = ${sys:es.logs.cluster_name}-* # 7

Configure the DefaultRolloverStrategy

Configure the Delete action for handling rollovers

The base path to the Elasticsearch logs

The condition to apply when handling rollovers

Retain logs for seven days

Only delete files older than seven days if they match the specified glob

Delete files from the base path matching the glob ${sys:es.logs.cluster_name}-*; this is the glob that log files are rolled to; this is needed to only delete the rolled Elasticsearch logs but not also delete the deprecation and slow logs

 只要命名為log4j2.properties,就可以載入多個配置檔案(在這種情況下,它們將被合併),並將Elasticsearch配置目錄作為祖先目錄.

有關如何定製日誌記錄和所有受支援的appender的詳細資訊,可以在Log4j文件中找到,http://logging.apache.org/log4j/2.x/manual/configuration.html

6. 配置日記級別

有4種方式配置日記的級別,每個都有適合使用它們的情況。

 

  1. 通過命令列:-E <name of logging hierarchy>=<level> (e.g., -Elogger.org.elasticsearch.transport=trace).當您在單個節點上臨時除錯問題時,這是最合適的。(例如,啟動或開發過程中的問題 )
  2. 通過elasticsearch.yml<name of logging hierarchy>: <level> (e.g.,logger.org.elasticsearch.transport: trace),  當你臨時除錯一個問題,但是不是通過命令列方式啟動Elasticsearch (e.g., via a serivce) 或者您希望在更持久的基礎上調整日誌級別,這就最合適。
  3. 通過叢集設定。                                                                                                                                                                         
    PUT /_cluster/settings
    {
      "transient": {
        "<name of logging hierarchy>": "<level>"
      }
    }
    
    #例如
    
    PUT /_cluster/settings
    {
      "transient": {
        "logger.org.elasticsearch.transport": "trace"
      }
    }

    當您需要動態地調整活動執行的叢集上的日誌級別時,這是最合適的(This is most appropriate when you need to dynamically need to adjust a logging level on an actively-running cluster.)

  4. 通過log4j.properties

    logger.<unique_identifier>.name = <name of logging hierarchy>
    logger.<unique_identifier>.level = <level>
    
    # 例如
    logger.transport.name = org.elasticsearch.transport
    logger.transport.level = trace

    當您需要對日誌記錄器進行細粒度控制時,這是最合適的(This is most appropriate when you need fine-grained control over the logger)