es 单机安装及配置其系统服务
- 2021 年 12 月 08 日
本文字数:6936 字
阅读完需:约 23 分钟
安装过程
1、下载安装包及解压缩、创建数据及日志目录(配置文件中使用):
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.7.0.tar.gz
tar -zxvf elasticsearch-6.7.0.tar.gz
mv elasticsearch-6.7.0 /usr/share/elasticsearch
mkdir -p /data/elasticsearch/data /data/elasticsearch/logs
2、创建用户(因为 Elasticsearch 默认不支持 root 用户启动,因此我们需要创建一个其他用户)
groupadd elasticsearch
useradd -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/elasticsearch
chown -R elasticsearch:elasticsearch /data/elasticsearch
chown -R root:elasticsearch /etc/elasticsearch
chown -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):
# 配置ip
network.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:+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=/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 locals
9-:-Djava.locale.providers=COMPAT
# temporary workaround for C2 bug with JDK 10 on hardware with AVX-512
10-:-XX:UseAVX=2
正常完成以上步骤,即可通过主目录下的 bin/elasticsearch 脚本启动 es 了。加-d 则会后台运行。
但通过命令行方式操作不太方便,因此需要配置 elasticsearch 的系统服务,通过 systemctl 来进行管理。
5、配置 elasticsearch 的系统服务
step 1:创建 es 服务系统配置文件
在 /etc/sysconfig/ 目录下创建 elasticsearch 文件
################################
# Elasticsearch
################################
# Elasticsearch home directory
ES_HOME=/usr/share/elasticsearch
# Elasticsearch Java path 改:jdk路径
JAVA_HOME=/usr/java/jdk1.8.0_181
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOMR/jre/lib
# Elasticsearch configuration directory 配置目录
ES_PATH_CONF=/etc/elasticsearch
# Elasticsearch PID directory
PID_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 process
ES_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=Elasticsearch
Documentation=http://www.elastic.co
Wants=network-online.target
After=network-online.target
[Service]
RuntimeDirectory=elasticsearch
PrivateTmp=true
Environment=ES_HOME=/usr/share/elasticsearch
Environment=ES_PATH_CONF=/etc/elasticsearch
Environment=PID_DIR=/var/run/elasticsearch
EnvironmentFile=/etc/sysconfig/elasticsearch
WorkingDirectory=/usr/share/elasticsearch
User=elasticsearch
Group=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=journal
StandardError=inherit
# Specifies the maximum file descriptor number that can be opened by this process
LimitNOFILE=65535
# Specifies the maximum number of processes
LimitNPROC=4096
# Specifies the maximum size of virtual memory
LimitAS=infinity
# Specifies the maximum file size
LimitFSIZE=infinity
# Disable timeout logic and wait until process is stopped
TimeoutStopSec=0
# SIGTERM signal is used to stop the Java process
KillSignal=SIGTERM
# Send the signal only to the JVM rather than its control group
KillMode=process
# Java process is never killed
SendSIGKILL=no
# When a JVM receives a SIGTERM signal it exits with code 143
SuccessExitStatus=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 file
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited
elasticsearch - 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
版权声明: 本文为 InfoQ 作者【林昱榕】的原创文章。
原文链接:【http://xie.infoq.cn/article/f7e4b0cfbd4856bf1bf0ec47a】。
本文遵守【CC-BY 4.0】协议,转载请保留原文出处及本版权声明。
林昱榕
开心生活,努力工作。 2018.02.13 加入
还未添加个人简介
评论