CentOS 7.x 安装Elasticsearch-6.5.4与设置开机启动

最近在学习Elasticsearch,网上有很多关于安装ES的教程,但是很多都很片面,特别是关于设置开机启动的方面,本文使用systemd添加service作为开机启动服务,闲话不多说下面是具体步骤。

系统环境

Java 8或者以上版本,并配置好环境变量,关于如何在CentOS 7下安装jdk及配置环境变量这里不做赘述。

下载安装包

安装Elasticsearch可以选择rpm包这样很方便,但是本人比较喜欢使用源码安装,因此这里我们下载es的源码包,下载地址如下:

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.tar.gz

解压文件

下载完ES的源码包后,使用tar命令解压安装包,并复制到/opt/elasticsearch目录下,重命名为elasticsearch,如下所示:

tar -zxvf elasticsearch-6.5.4.tar.gz
sudo mv elasticsearch-6.5.4  /opt/elasticsearch
# 在es下新建data目录
sudo mkdir -p /opt/elasticsearch/data

创建用户

因为Elasticsearch默认不支持root用户启动,因此我们需要创建一个其他用户,如下所示:

groupadd es
useradd -s /sbin/nologin -M -g es es

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

用户授权

上文中我们将elasticsearch源码复制到了/opt/elasticsearch目录下,并且我们也创建了新用户来用于安装启动es,现在我们需要将/opt/elasticsearch目录授权给我们新建的用户,如下所示:

chown -R es:es /opt/elasticsearch

修改配置文件

接下来我们需要修改elasticsearch的配置文件,主要修改绑定IP与端口号,使用vim打开 /opt/elasticsearch/config/elasticsearch.yml,修改如下内容:

#---------------------Paths-----------------------------
path.data: /opt/elasticsearch/data
path.logs: /opt/elasticsearch/logs
#---------------------Networkd--------------------------
# 绑定本机IP地址或直接设置为0.0.0.0
network.host: 0.0.0.0
# 默认端口号为9200
http.port: 9200

开启es对应的端口

可能我们的虚拟机或者服务器并没有直接开启9200端口,这时我们是无法在外部访问es服务的,因此我们要设置防火墙开启9200端口号,如下所示:

# 添加开放端口9200
sudo firewall-cmd --zone=public --add-port=9200/tcp --permanent
# 重启防火墙
sudo firewall-cmd --reload

启动elasticsearch

安装上述步骤完成后,我们开始启动elasticsearch,如下所示:

cd /opt/elasticsearch/bin
./elasticsearch
# ./elasticsearch -d表示后台运行

这里我们不在后台启动es,这样就可以直接看到启动日志,便于我们查看是否启动成功。如果启动成功,我们可以在浏览器中访问 http://192.168.0.124:9200/, 返回如下结果表名启动成功:

{
  "name" : "VVeW0sU",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "oYOtouzuThGeyYSvfniJUw",
  "version" : {
    "number" : "6.5.4",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "d2ef93d",
    "build_date" : "2018-12-17T21:17:40.758843Z",
    "build_snapshot" : false,
    "lucene_version" : "7.5.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

常见错误

按照上述命令安装时,可能会出现一些错误,这里总结一下常见的错误及解决办法:
错误 1:

Caused by: java.lang.RuntimeException: can not run elasticsearch as root

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

chown -R es:es /opt/elasticsearch/

错误2:

Exception in thread "main" 2017-11-12 12:17:55,776 main ERROR No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property ‘log4j2.debug‘ to show Log4j2 internal initialization logging.
2017-11-12 12:17:56,319 main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")

原因:elasticsearch.yml中的配置项的格式有问题
解决方案:请尽量保持冒号前面没空格,后面一个空格,不要用tab键

错误3:

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
es   hard   nofile   65536
es   soft   nofile   65536
*    hard   nproc    4096
*    soft   nproc    4096

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

sudo vim /etc/sysctl.conf

添加如下内容:

vm.max_map_count=655360

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

sudo sysctl -p

上述错误修改完配置文件以后最好重启一下系统,然后再尝试启动elasticsearch。

配置开机启动

每次启动都要使用命令行不是很方便,所以我们需要配置elasticsearch的系统服务,让其可以随着系统启动自动启动,这里我们使用systemd来为es安装Linux的service,具体步骤如下:
step 1:创建es服务系统配置文件
/etc/sysconfig/ 目录下创建 elasticsearch 文件,内容如下所示:

#######################
#    Elasticsearch    #
#######################

# Elasticsearch home directory
ES_HOME=/opt/elasticsearch

# Elasticsearch Java path
JAVA_HOME=/usr/local/jdk
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOMR/jre/lib

# Elasticsearch configuration directory
ES_PATH_CONF=/opt/elasticsearch/config

# Elasticsearch PID directory
PID_DIR=/var/run/elasticsearch

#############################
#   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

################################
#   Elasticsearch 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=65536

# 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]
Environment=ES_HOME=/opt/elasticsearch
Environment=ES_PATH_CONF=/opt/elasticsearch/config
Environment=PID_DIR=/var/run/elasticsearch
EnvironmentFile=/etc/sysconfig/elasticsearch
WorkingDirectory=/opt/elasticsearch
User=es
Group=es
ExecStart=/opt/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=65536

# Specifies the maximum number of process
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
 
[Install]
WantedBy=multi-user.target

注意: 上面的脚本中关于es的pid文件路径,本人的/var分区是单独划分的所以可以直接将pid文件设置在该分区下,如果你的虚拟机或服务器的/var是默认的tmps,则不能将pid设置在该分区下,因为重启服务器后会被自动删除,建议将pid文件路径设置为es的config目录下并授权为es用户即可。

给脚本赋权限:

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
ps aux|grep java

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

sudo journalctl -u elaticsearch.service
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值