开始

下载安装

https://www.elastic.co/guide/en/elasticsearch/reference/7.7/install-elasticsearch.html https://www.elastic.co/cn/start

历史版本下载

https://www.elastic.co/cn/downloads/past-releases#elasticsearch

运行

解压下载的 elasticsearch 包,到解压目录执行

bin/elasticsearch

这样 elasticsearch 就启动了

配置

官方配置说明(7.7版本)

https://www.elastic.co/guide/en/elasticsearch/reference/7.7/settings.html

elasticsearch的config文件夹里面有两个配置文件:elasticsearch.ymllogging.yml,第一个是es的基本配置文件,第二个是日志配置文件,es也是使用log4j来记录日志的,所以logging.yml里的设置按普通log4j配置文件来设置就行了。

这些文件位于config目录,目录的默认位置是 $ES_HOME/config/。通过Debian和RPM安装的配置文件的目录是 /etc/elasticsearch/

可通过以下的命令修改配置文件的位置:

旧版本

./bin/elasticsearch -Epath.conf=/path/to/my/config/

新版本通过修改ES_PATH_CONF环境变量

export ES_PATH_CONF=/path/to/my/config
./bin/elasticsearch

常用的配置说明如下(基于 7.7 版本)


cluster.name: elasticsearch # 集群名称
#
# ------------------------------------ Node ------------------------------------
#
node.name: node2  # 节点名称
node.master: true
# 设置为true(默认)的 节点,有资格被选为控制群集的主节点
#
node.data:true
# node.data设置为true(默认)的 节点,数据节点保存数据并执行与数据相关的操作,例如CRUD,搜索和聚合。
#
index.number_of_shards:5
# 设置默认索引分片个数,默认为5片。
#
index.number_of_replicas:1
# 设置默认索引副本个数,默认为1个副本。
#
node.max_local_storage_nodes: 3
# 这个配置限制了单节点上可以开启的ES存储实例的个数

# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: ~/Documents/elasticsearch/data02
#
# Path to log files:
#
path.logs: ~/Documents/elasticsearch/log02
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
network.bind_host: 0.0.0.0
#
# Set a custom port for HTTP:
#
http.port: 9201
# 设置对外服务的http端口,默认为9200。
transport.port: 9301
# 设置节点间交互的tcp端口,默认是9300。

# 增加参数,使head插件可以访问es
# http.cors.enabled: true
# http.cors.allow-origin: "*"
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.seed_hosts: ["127.0.0.1:9300", "127.0.0.1:9301", "127.0.0.1:9302"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
cluster.initial_master_nodes: ["node1"]

# 磁盘分配策略,下面会进行详细说明
cluster.routing.allocation.disk.threshold_enabled: true
cluster.routing.allocation.disk.watermark.flood_stage: 200mb
cluster.routing.allocation.disk.watermark.low: 1000mb
cluster.routing.allocation.disk.watermark.high: 500mb

常用请求

查看主从分配

http://localhost:9200/_cat/nodes?v

查看集群状态

http://127.0.0.1:9200/_cluster/health?pretty

查看分片

http://localhost:9200/_cat/shards?h=index,shard,prirep,state,unassigned.reason

磁盘分配策略

集群unassign shards 故障

[2019-08-07T10:24:26,053][INFO ][o.e.c.r.a.DiskThresholdMonitor] [skye-1] low disk watermark [85%] exceeded on [sCr8-8UdSI2Di06LAdnpbg][skye-3][/data/elasticsearch-6.2.0/data/nodes/0] free: 134.7gb[13.6%], replicas will not be assigned to this node
[2019-08-07T10:24:56,058][INFO ][o.e.c.r.a.DiskThresholdMonitor] [skye-1] low disk watermark [85%] exceeded on [5uby7FwmQ2uk41lirnyiNw][skye-1][/data/elasticsearch-6.2.0/data/nodes/0] free: 132.2gb[13.4%], replicas will not be assigned to this node
[2019-08-07T10:24:56,058][INFO ][o.e.c.r.a.DiskThresholdMonitor] [skye-1] low disk watermark [85%] exceeded on [sCr8-8UdSI2Di06LAdnpbg][skye-3][/data/elasticsearch-6.2.0/data/nodes/0] free: 134.7gb[13.6%], replicas will not be assigned to this node

出现故障的原因是ES的分片默认情况下受控于ES集群节点的数据盘磁盘空间,有4个参数进行控制。默认情况下,当数据盘使用超过85%,Elasticsearch不会将分片分配给使用磁盘超过85%的节点。

可以通过cluster.routing.allocation.disk.threshold_enabled=false 来关闭磁盘容量的限制,简单粗暴。但当系统磁盘真的满了的时候,这个是不安全的。elasticsearch 为了保障数据的安全可用才做了基于磁盘容量的分片策略。

我们来看下这四个控制参数

cluster.routing.allocation.disk.threshold_enabled
# 默认为true。设置为false禁用磁盘分配决策程序。

cluster.routing.allocation.disk.watermark.low
# 控制磁盘使用的低水位线。它默认为85%,这意味着Elasticsearch不会将分片分配给使用磁盘超过85%的节点。它也可以设置为绝对字节值(如500mb),以防止Elasticsearch在小于指定的可用空间量时分配分片。此设置不会影响新创建的索引的主分片,或者特别是之前从未分配过的任何分片。

cluster.routing.allocation.disk.watermark.high
# 控制高水印。它默认为90%,意味着Elasticsearch将尝试从磁盘使用率超过90%的节点重新定位分片。它也可以设置为绝对字节值(类似于低水印),以便在节点小于指定的可用空间量时将其从节点重新定位。此设置会影响所有分片的分配,无论先前是否分配。

cluster.routing.allocation.disk.watermark.flood_stage
# 控制洪水阶段水印。它默认为95%,这意味着Elasticsearch index.blocks.read_only_allow_delete对每个索引强制执行只读索引块(),该索引在至少有一个磁盘超过泛洪阶段的节点上分配了一个或多个分片。这是防止节点耗尽磁盘空间的最后手段。一旦有足够的可用磁盘空间允许索引操作继续,就必须手动释放索引块

简单来讲,ES 有4个参数:是否启用磁盘分片策略,高水位,低水位,最后的防线洪水水位。 高低水位洪水水位默认情况下的值为85%,90%95%。 当ES数据节点所在磁盘使用率超过85%,ES进去低水位线,Elasticsearch不会将分片分配给该节点。 当ES数据节点所在磁盘使用率超过90%,ES进去高水位线,Elasticsearch将分片重新定位分配到其他节点。 当ES数据节点所在磁盘使用率超过95%,ES进去洪水水位线,Elasticsearch将进入只读状态。

ES的磁盘分片配额策略在很多情况下可能不符合需求,比如我们的数据盘又1T,当磁盘使用率在85%是可用磁盘还有150G,在某种场景下该磁盘还可以存储”大量”数据,我们需要手动修改磁盘配额值。

修改磁盘分片策略

1)API 在线修改

curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
  "transient": {
    "cluster.routing.allocation.disk.watermark.low": "90%",
    "cluster.routing.allocation.disk.watermark.high": "95%",
    "cluster.routing.allocation.disk.watermark.flood_stage": "98%",
  }
}
'

此方案可以临时修改集群的磁盘配额值,但重启后失效。集群环境下,我们可以在业务低峰期手动修改每台的值,依次重启服务。但在单节点或压力较大的情况下,可以使用临时修改的方式,修改集群状态,然后修改ES的配置文件,再下次重启后自动生效。 2)通过配置文件修改

# vim elasticsearch.yml
  cluster.routing.allocation.disk.threshold_enabled: true
  cluster.routing.allocation.disk.watermark.flood_stage: 200mb
  cluster.routing.allocation.disk.watermark.low: 500mb
  cluster.routing.allocation.disk.watermark.high: 300mb

注意事项

ES的三个水位控制可以配置为磁盘使用量百分比,也设置为具体的数值(比如300G),但设置要统一,要么都是百分比,要么都是具体的值 ES的三个水位控制的值要遵循下面的原则,否则不生效: 低水位 < 高水位 < 水平水位 ES的三个水位控制 均存在默认值,若不设置则采用默认值。但若配置,则需要注意四个值都要配置,经常被人忽略的就是cluster.routing.allocation.disk.watermark.flood_stage,该值不设置将不生效。

参考

https://blog.csdn.net/qq_28364999/article/details/82945543
https://blog.csdn.net/weixin_43423965/article/details/100528812
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-cluster.html#disk-based-shard-allocation
https://blog.csdn.net/laoyang360/article/details/78443006
https://blog.csdn.net/lp2388163/article/details/80611138