写点什么

es 单机安装及配置其系统服务

作者:林昱榕
  • 2021 年 12 月 08 日
  • 本文字数:6936 字

    阅读完需:约 23 分钟

安装过程


1、下载安装包及解压缩、创建数据及日志目录(配置文件中使用):

https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.7.0.tar.gztar -zxvf elasticsearch-6.7.0.tar.gzmv elasticsearch-6.7.0 /usr/share/elasticsearchmkdir -p /data/elasticsearch/data /data/elasticsearch/logs
复制代码


2、创建用户(因为 Elasticsearch 默认不支持 root 用户启动,因此我们需要创建一个其他用户)

groupadd elasticsearchuseradd -s /sbin/nologin -M -g elasticsearch elasticsearch
复制代码

使用上述命令,我们首先创建 elasticsearch 分组作为 elasticsearch 用户所在分组,然后再使用 useradd -s /sbin/nologin -M -g elasticsearch elasticsearch 命令创建一个免登录用户 elasticsearch,并指定分组为 elasticsearch,其中参数-s 表示当前用户命令行,-M 表示不创建用户目录,-g 表示分组。


3、用户授权(将 elasticsearch 相关的目录授权给新建的用户)

chown -R elasticsearch:elasticsearch /usr/share/elasticsearchchown -R elasticsearch:elasticsearch /data/elasticsearchchown -R root:elasticsearch /etc/elasticsearchchown -R elasticsearch:elasticsearch /var/run/elasticsearch
复制代码

本次实践是将 elasticsearch 目录下的 config 目录内容复制到/etc/elasticsearch 目录下,也即/etc/elasticsearch 充当配置信息的目录(非必要操作,直接使用 config 目录也可以)。反正注意若调整了配置目录,通过 $ES_PATH_CONF 环境变量来告知即可。


4、修改配置文件

vim /etc/elasticsearch/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: my-cluster## ------------------------------------ Node ------------------------------------## Use a descriptive name for the node:##node.name: node-1## 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/elasticsearch/data## Path to log files:# 配置日志目录path.logs: /data/elasticsearch/logs## ----------------------------------- Memory -----------------------------------## Lock the memory on startup:#bootstrap.memory_lock: true## 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):# 配置ipnetwork.host: your-ip## Set a custom port for HTTP:# 端口http.port: 9200## 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: ["host1", "host2"]## Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):##discovery.zen.minimum_master_nodes: ## 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
复制代码


 ## JVM配置:一般就改下-Xms和-Xmx vim /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
-Xms8g-Xmx8g-XX:NewRatio=4
################################################################## 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
## G1GC Configuration# NOTE: G1GC is only supported on JDK version 10 or later.# To use G1GC uncomment the lines below.# 10-:-XX:-UseConcMarkSweepGC# 10-:-XX:-UseCMSInitiatingOccupancyOnly# 10-:-XX:+UseG1GC# 10-:-XX:InitiatingHeapOccupancyPercent=75
## DNS cache policy# cache ttl in seconds for positive DNS lookups noting that this overrides the# JDK security property networkaddress.cache.ttl; set to -1 to cache forever-Des.networkaddress.cache.ttl=60# cache ttl in seconds for negative DNS lookups noting that this overrides the# JDK security property networkaddress.cache.negative ttl; set to -1 to cache# forever-Des.networkaddress.cache.negative.ttl=10
## 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=${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/elasticsearch
# specify an alternative path for JVM fatal error logs-XX:ErrorFile=/data/elasticsearch/logs/hs_err_pid%p.log
## JDK 8 GC logging
8:-XX:+PrintGCDetails8:-XX:+PrintGCDateStamps8:-XX:+PrintTenuringDistribution8:-XX:+PrintGCApplicationStoppedTime8:-Xloggc:logs/gc.log8:-XX:+UseGCLogFileRotation8:-XX:NumberOfGCLogFiles=328:-XX:GCLogFileSize=64m
# JDK 9+ GC logging9-:-Xlog:gc*,gc+age=trace,safepoint:file=/data/elasticsearch/logs/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 locals9-:-Djava.locale.providers=COMPAT
# temporary workaround for C2 bug with JDK 10 on hardware with AVX-51210-:-XX:UseAVX=2
复制代码


正常完成以上步骤,即可通过主目录下的 bin/elasticsearch 脚本启动 es 了。加-d 则会后台运行。

但通过命令行方式操作不太方便,因此需要配置 elasticsearch 的系统服务,通过 systemctl 来进行管理。


5、配置 elasticsearch 的系统服务

step 1:创建 es 服务系统配置文件

在 /etc/sysconfig/ 目录下创建 elasticsearch 文件

################################# Elasticsearch################################
# Elasticsearch home directoryES_HOME=/usr/share/elasticsearch
# Elasticsearch Java path 改:jdk路径JAVA_HOME=/usr/java/jdk1.8.0_181CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOMR/jre/lib
# Elasticsearch configuration directory 配置目录ES_PATH_CONF=/etc/elasticsearch
# Elasticsearch PID directoryPID_DIR=/var/run/elasticsearch
# Additional Java OPTS#ES_JAVA_OPTS=
# Configure restart on package upgrade (true, every other setting will lead to not restarting)#RESTART_ON_UPGRADE=true
################################# Elasticsearch service################################
# SysV init.d## The number of seconds to wait before checking if Elasticsearch started successfully as a daemon processES_STARTUP_SLEEP_TIME=5
################################# System properties################################
# Specifies the maximum file descriptor number that can be opened by this process# When using Systemd, this setting is ignored and the LimitNOFILE defined in# /usr/lib/systemd/system/elasticsearch.service takes precedence#MAX_OPEN_FILES=65535
# The maximum number of bytes of memory that may be locked into RAM# Set to "unlimited" if you use the 'bootstrap.memory_lock: true' option# in elasticsearch.yml.# When using systemd, LimitMEMLOCK must be set in a unit file such as# /etc/systemd/system/elasticsearch.service.d/override.conf.#MAX_LOCKED_MEMORY=unlimited
# Maximum number of VMA (Virtual Memory Areas) a process can own# When using Systemd, this setting is ignored and the 'vm.max_map_count'# property is set at boot time in /usr/lib/sysctl.d/elasticsearch.conf#MAX_MAP_COUNT=262144
复制代码

该文件用于配置 es 服务的系统变量,用于 systemd 调用。上面我们配置了 ES_HOME、ES_PATH_CONF、PID_DIR 等,其中 PID_DIR 用于存放 es 进程的 PID,用于 systemd 管理 es 进程的启动或停止。


step 2:创建 es 服务

在 /usr/lib/systemd/system/ 目录下创建 elasticsearch.service 文件:

[Unit]Description=ElasticsearchDocumentation=http://www.elastic.coWants=network-online.targetAfter=network-online.target
[Service]RuntimeDirectory=elasticsearchPrivateTmp=trueEnvironment=ES_HOME=/usr/share/elasticsearchEnvironment=ES_PATH_CONF=/etc/elasticsearchEnvironment=PID_DIR=/var/run/elasticsearchEnvironmentFile=/etc/sysconfig/elasticsearch
WorkingDirectory=/usr/share/elasticsearch
User=elasticsearchGroup=elasticsearch
ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid
# StandardOutput is configured to redirect to journalctl since# some error messages may be logged in standard output before# elasticsearch logging system is initialized. Elasticsearch# stores its logs in /var/log/elasticsearch and does not use# journalctl by default. If you also want to enable journalctl# logging, you can simply remove the "quiet" option from ExecStart.StandardOutput=journalStandardError=inherit
# Specifies the maximum file descriptor number that can be opened by this processLimitNOFILE=65535
# Specifies the maximum number of processesLimitNPROC=4096
# Specifies the maximum size of virtual memoryLimitAS=infinity
# Specifies the maximum file sizeLimitFSIZE=infinity
# Disable timeout logic and wait until process is stoppedTimeoutStopSec=0
# SIGTERM signal is used to stop the Java processKillSignal=SIGTERM
# Send the signal only to the JVM rather than its control groupKillMode=process
# Java process is never killedSendSIGKILL=no
# When a JVM receives a SIGTERM signal it exits with code 143SuccessExitStatus=143
# 这个是常见错误需要配置的一项LimitMEMLOCK=infinity
[Install]WantedBy=multi-user.target
# Built for packages-6.7.2 (packages)
复制代码

给脚本授权:

chmod +x /usr/lib/systemd/system/elasticsearch.service
复制代码

重新加载 systemd 的守护线程

sudo systemctl daemon-reload
复制代码

开机启动生效:

sudo systemctl enable elasticsearch.service
复制代码

启动 elasticsearch.service:

sudo systemctl start elasticsearch.service
复制代码

查看 elasticsearch.serivce 状态:

sudo systemctl status elasticsearch.service
复制代码

如果出现错误可以使用如下命令查看日志:

sudo journalctl -u elaticsearch.service
复制代码


常见错误

错误 1:

Caused by: java.lang.RuntimeException: can not run elasticsearch as root
复制代码

原因:出于对 root 用户的安全保护,需要用其他用户组用户来启动。解决方法:需要对 elasticsearch 安装目录进行用户授权(相关的路径都需授权)。

chown -R elasticsearch:elasticsearch /usr/share/elasticsearch/
复制代码

错误 2:

ERROR: [3] bootstrap checks failed[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536][2]: max number of threads [3802] for user [elsearch] is too low, increase to at least [4096][3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
复制代码

原因:系统限制用户的执行内存解决方法:

[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]

[2]: max number of threads [3802] for user [elsearch] is too low, increase to at least [4096]

上述两个错误,需要修改系统安全限制配置文件/etc/security/limits.conf,如下所示:

sudo vim /etc/security/limits.conf
复制代码

在其中添加如下内容:

# End of fileelasticsearch soft memlock unlimitedelasticsearch hard memlock unlimitedelasticsearch   -       nofile  65535
复制代码

[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]该错误需要修改系统配置文件/etc/sysctl.conf,如下所示:

vim /etc/sysctl.conf 添加:vm.max_map_count=262144
复制代码

使用如下命令使配置生效:

sudo sysctl -p
复制代码

错误 3:

memory locking requested for elasticsearch process but memory is not locked
复制代码

解决方案:

/usr/lib/systemd/system/elasticsearch.service 的 service 下添加:

LimitMEMLOCK=infinity
复制代码


发布于: 25 分钟前阅读数: 3
用户头像

林昱榕

关注

开心生活,努力工作。 2018.02.13 加入

还未添加个人简介

评论

发布
暂无评论
es单机安装及配置其系统服务