Elasticsearch简单入门--elasticsearch简单配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhen_6137/article/details/86062181

配置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来改变

[root@zzf 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)

猜你喜欢

转载自blog.csdn.net/zhen_6137/article/details/86062181