elasticsearch通过tarball(tar.gz)方式安装,结合systemd(自定义elasticsearch.service)使用需要修改的内容

本文介绍了当使用tar.gz包安装Elasticsearch后,如何结合systemd启动并解决启动卡住的问题。通过手动添加systemd模块、修改elasticsearch-env中的ES_DISTRIBUTION_TYPE字段,确保notify机制正常工作,从而实现Elasticsearch的顺利启动。
摘要由CSDN通过智能技术生成

一、前言

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包安装的一些特点列举如下:
  1. 所有的文件都放在同一个目录下
  2. 不同文件之间的引用和依赖关系都是通过相对路径来生效的
  3. 默认需要通过执行bin/elasticsearch来启动程序,可以通过-d将程序启动为daemon 模式等
  4. 不会安装其他的文件,解压即可使用
  • RPM包的一些特点:
  1. 不同的文件各自拷贝到对应的目录中去
  2. 文件之间的引用和依赖关系,有的直接通过绝对路径进行指定(配合rpm安装对应的路径),可以安装以后进行手动修改
  3. 可以通过systemctl start elasticsearch进行启动
  4. 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包后,从里面取出来加以修改利用的。官方的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值