ElasticSearch升级指南

背景

旧版本ElasticSearch为6.4.0,升级到官网最新版本7.3.1。

升级步骤

  1. ElasticSearch官网下载最新7.3.1版本,下载地址https://www.elastic.co/cn/downloads/elasticsearch;

  2. 解压tar.gz文件,进入config目录,设置elasticsearch.yml文件,具体设置如下:

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: zhangfang
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-3
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /data/lib/elastic
#
# Path to log files:
#
path.logs: /data/log/elastic
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 172.28.25.52
#
# Set a custom port for HTTP:
#
http.port: 9200
transport.tcp.port: 9300
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.zen.ping.unicast.hosts: ["172.28.25.54:9300", "172.28.25.55:9300","172.28.25.52:9300"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
discovery.zen.minimum_master_nodes: 2
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: "*"

其中,node.name,path.data,path.logs,network.host,discovery.zen.ping.unicast.hosts根据实际情况配置;

  1. 配置jvm.options信息,具体如下:
## JVM configuration

################################################################
## IMPORTANT: JVM heap size
################################################################
##
## You should always set the min and max JVM heap
## size to the same value. For example, to set
## the heap to 4 GB, set:
##
## -Xms4g
## -Xmx4g
##
## See https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## for more information
##
################################################################

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms2g
-Xmx2g

################################################################
## Expert settings
################################################################
##
## All settings below this section are considered
## expert settings. Don't tamper with them unless
## you understand what you are doing
##
################################################################

## GC configuration
-XX:+UseConcMarkSweepGC
-XX:CMSInitiatingOccupancyFraction=75
-XX:+UseCMSInitiatingOccupancyOnly

## optimizations

# pre-touch memory pages used by the JVM during initialization
-XX:+AlwaysPreTouch

## basic

# explicitly set the stack size
-Xss1m

# set to headless, just in case
-Djava.awt.headless=true

# ensure UTF-8 encoding by default (e.g. filenames)
-Dfile.encoding=UTF-8

# use our provided JNA always versus the system one
-Djna.nosys=true

# turn off a JDK optimization that throws away stack traces for common
# exceptions because stack traces are important for debugging
-XX:-OmitStackTraceInFastThrow

# flags to configure Netty
-Dio.netty.noUnsafe=true
-Dio.netty.noKeySetOptimization=true
-Dio.netty.recycler.maxCapacityPerThread=0

# log4j 2
-Dlog4j.shutdownHookEnabled=false
-Dlog4j2.disable.jmx=true

-Djava.io.tmpdir=/data/tmp/elastic

## heap dumps

# generate a heap dump when an allocation from the Java heap fails
# heap dumps are created in the working directory of the JVM
-XX:+HeapDumpOnOutOfMemoryError

# specify an alternative path for heap dumps
# ensure the directory exists and has sufficient space
-XX:HeapDumpPath=/data/lib/elastic

## JDK 8 GC logging

8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:/data/log/elastic/gc.log
8:-XX:+UseGCLogFileRotation
8:-XX:NumberOfGCLogFiles=32
8:-XX:GCLogFileSize=64m

# JDK 9+ GC logging
9-:-Xlog:gc*,gc+age=trace,safepoint:file=/data/log/elastic/gc.log:utctime,pid,tags:filecount=32,filesize=64m
# due to internationalization enhancements in JDK 9 Elasticsearch need to set the provider to COMPAT otherwise
# time/date parsing will break in an incompatible way for some date patterns and locals
9-:-Djava.locale.providers=COMPAT

其中 -Xms -Xmx视机器实际情况而定;

  1. 禁用分片自动分配,执行以下命令:
curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
  "persistent": {
    "cluster.routing.allocation.enable": "primaries"
  }
}
'
  1. 停止索引并执行同步刷新,执行以下命令:
curl -X POST "localhost:9200/_flush/synced?pretty"

执行同步刷新时,请检查响应以确保没有失败。失败时虽然请求本身仍返回200 OK状态,但响应正文中会列出由于挂起的索引操作而导致的失败条目。如果有失败,需要重新发出请求。

  1. 停止原集群所有节点的ElasticSearch进程,使用kill的方式执行;

  2. 启动新版的ElasticSearch

bin/elasticsearch -Ecluster.initial_master_nodes=node-1,node-2,node-3 -d

注意到启动命令中有-Ecluster.initial_master_nodes=node-1,node-2,node-3,是因为新版本首次启动,需要制定备选master列表。另外,也可以在elasticsearch.yml文件中添加以下配置:

cluster.initial_master_nodes:
  - node-1
  - node-2
  - node-3

如果是非首次启动,则无需添加-Ecluster.initial_master_nodes=node-1,node-2,node-3参数。

  1. 启动后,通过
curl -X GET "localhost:9200/_cat/health?pretty"
curl -X GET "localhost:9200/_cat/nodes?pretty"

命令查看集群健康度和节点情况,如果health已为yellow说明集群已启动。

  1. 恢复分片分配
curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
  "persistent": {
    "cluster.routing.allocation.enable": null
  }
}
'

此时,查看集群健康度会发现健康百分比会慢慢升高,直至100%,status会变为green,至此升级完成。

  1. 本升级因为变动较大,根据官方文档要求是全量升级,如果是小幅升级,可参考官方文档进行滚动升级。滚动升级时无需集群所有节点都停止服务,逐台升级即可。另外,本次升级未考虑ElasticSearch plugin的升级,如有需求,请参考官方文档。

参考文档

  • https://www.elastic.co/guide/en/elasticsearch/reference/7.3/restart-upgrade.html
  • https://www.elastic.co/guide/en/elasticsearch/reference/7.3/rolling-upgrades.html
  • https://www.elastic.co/guide/en/elasticsearch/reference/7.3/modules-discovery-bootstrap-cluster.html

你可能感兴趣的:(ElasticSearch升级指南)