一、前言
elasticsearch官方提供了多种安装包来进行安装的方式,选择不同的安装包进行安装,根据各自的特点,差异巨大
。也是由于社区针对不同的使用场景,对安装包进行了定制,顾名思义就是安装包中的目录结构、脚本文件、引用的模块等等都会有些许差别,这就导致了最终安装后能够满足安装包各自的使用场景和特点,本身这个设计没有任何问题。
二、背景
在一些特殊的使用场景下,或者有喜欢自己捣鼓的同学,偶尔也有自己的一些玩儿法,这样就会碰到一些看起来很奇怪的问题。
例如,我希望使用官方提供的TAR.GZ
包进行安装,同时参考RPM
包中提供的相关配置文件,自己生成systemd
配置文件,通过systemctl
进行elasticsearch.service
的启动。安装路径可以自己指定,这样便于管理文件。
正是因为这个想法,才引发了本文描述的内容。
elasticsearch版本:7.9.1
2.1 elasticsearch的TAR包和RPM包对比
使用官方提供的TAR.GZ
包进行安装,会把所有elasticsearch的文件都安装到同一个目录下,没有其他的文件,解压出来的就是全部,默认需要手动启动。
RPM
包使用通常使用rpm -ivh ***.rpm
进行安装,通过对官方rpm包的分析,粗浅了解到它会在系统的不同目录下,按照rpm包中的规划,对应安装不同的配置文件,可执行文件,日志目录,系统配置文件等等。例如在linux下,就会将上述文件对应安装到相应的目录中。
TAR.GZ
包安装的一些特点列举如下:
- 所有的文件都放在同一个目录下
- 不同文件之间的引用和依赖关系都是通过相对路径来生效的
- 默认需要通过执行
bin/elasticsearch
来启动程序,可以通过-d
将程序启动为daemon
模式等 - 不会安装其他的文件,解压即可使用
RPM
包的一些特点:
- 不同的文件各自拷贝到对应的目录中去
- 文件之间的引用和依赖关系,有的直接通过绝对路径进行指定(配合rpm安装对应的路径),可以安装以后进行手动修改
- 可以通过
systemctl start elasticsearch
进行启动 - rpm包中会携带系统相关的配置文件,例如
sysctl.d/
内核参数配置文件等
举例来说,使用tar包安装以后,安装目录中的文件变量引用关系都是通过相对路径来寻找的,例如,通过jvm.options
这个配置文件的内容可以清楚的看到差异。具体如下:
通过tar包解压出来的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
-Xms1g
-Xmx1g
################################################################
## 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
8-13:-XX:+UseConcMarkSweepGC
8-13:-XX:CMSInitiatingOccupancyFraction=75
8-13:-XX:+UseCMSInitiatingOccupancyOnly
## G1GC Configuration
# NOTE: G1 GC is only supported on JDK version 10 or later
# to use G1GC, uncomment the next two lines and update the version on the
# following three lines to your version of the JDK
# 10-13:-XX:-UseConcMarkSweepGC
# 10-13:-XX:-UseCMSInitiatingOccupancyOnly
14-:-XX:+UseG1GC
14-:-XX:G1ReservePercent=25
14-:-XX:InitiatingHeapOccupancyPercent=30
## JVM temporary directory
-Djava.io.tmpdir=${ES_TMPDIR}
## 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
# specify an alternative path for JVM fatal error logs
-XX:ErrorFile=logs/hs_err_pid%p.log
## JDK 8 GC logging
8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:logs/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=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m
通过rpm包安装,在/etc/elasticsearch/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
-Xms1g
-Xmx1g
################################################################
## 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
8-13:-XX:+UseConcMarkSweepGC
8-13:-XX:CMSInitiatingOccupancyFraction=75
8-13:-XX:+UseCMSInitiatingOccupancyOnly
## G1GC Configuration
# NOTE: G1 GC is only supported on JDK version 10 or later
# to use G1GC, uncomment the next two lines and update the version on the
# following three lines to your version of the JDK
# 10-13:-XX:-UseConcMarkSweepGC
# 10-13:-XX:-UseCMSInitiatingOccupancyOnly
14-:-XX:+UseG1GC
14-:-XX:G1ReservePercent=25
14-:-XX:InitiatingHeapOccupancyPercent=30
## JVM temporary directory
-Djava.io.tmpdir=${ES_TMPDIR}
## 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=/var/lib/elasticsearch
# specify an alternative path for JVM fatal error logs
-XX:ErrorFile=/var/log/elasticsearch/hs_err_pid%p.log
## JDK 8 GC logging
8:-XX:+PrintGCDetails
8:-XX:+PrintGCDateStamps
8:-XX:+PrintTenuringDistribution
8:-XX:+PrintGCApplicationStoppedTime
8:-Xloggc:/var/log/elasticsearch/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=/var/log/elasticsearch/gc.log:utctime,pid,tags:filecount=32,filesize=64m
不难发现,其中几处日志路径的地址都是不一样的,tar包中的直接采用了相对路径,rpm包中使用的则是绝对路径。(例如最后一行)
2.2 使用tar包安装后使用systemd启动遇到问题
在修复好各种环境变量问题和对应关系后,启动会一直卡住,例如:
systemctl start elasticsearch
执行后,就一直无法退出。
三、解决方案
3.1 手动添加systemd模块相关文件到modules目录
第一步:将systemd模块从rpm包解压出来的modules目录中取出,放到tar包对应的modules目录里。
第二步:修改文件和文件夹的权限(systemd默认elasticsearch用户启动)
3.2 修改bin目录下elasticsearch-env中ES_DISTRIBUTION_TYPE字段值
第一步:定位bin/elasticsearch-env
文件,找到ES_DISTRIBUTION_TYPE
关键字,可以看到,tar包中,该关键字的值为tar,将它修改为rpm
四、问题原理
4.1 elasticsearch使用systemd的启动机制
systemd启动的service文件是通过解压官方rpm包后,从里面取出来加以修改利用的。官方的