软件配置

  • Ubuntu版本:16.04
  • jdk版本:1.8.0_211
  • Elasticsearch版本:5.6.8

新建elasticsearch用户

创建用户

在root用户下执行下述命令

1
useradd elasticsearch

设置密码

1
passwd elasticsearch

根据提示输入两次密码,新用户便创建完成

jdk安装

elasticsearch环境依赖jdk,因此启动elasticsearch必须要有jdk环境

上传jdk安装包到指定文件夹

这里通过xftp 将jdk安装包上传至 /usr/local/ 目录下

解压jdk安装包

1
tar -zxvf jdk-8u211-linux-x64.tar.gz

解压之后会在 /usr/local/ 目录下生成 jdk1.8.0_211 文件

配置环境

在 /etc/profile 文件下添加以下配置

1
2
3
4
export JAVA_HOME=/usr/local/jdk1.8.0_211/
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
export PATH=$PATH:${JAVA_HOME}/bin:$JRE_HOME/bin:$PATH

编辑完退出之后执行以下命令,使环境生效

1
source /etc/profile

测试

执行 java -version 命令,出现以下提示则表示jdk安装完成

1
2
3
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

ElasticSearch安装

系统配置

修改文件数配置

在 /etc/security/limits.conf 文件下添加以下内容

1
2
3
4
* soft nofile 65536  # 单个用户可用的最大进程数量(超过会警告)
* hard nofile 131072 # 单个用户可用的最大进程数量(超过会报错)
* soft nproc 2048 # 可打开的文件描述符的最大数(超过会警告)
* hard nproc 4096 # 可打开的文件描述符的最大数(超过会报错)

在 /etc/profile 文件下添加以下内容

1
ulimit -SHn 65536

linux下用ulimit设置连接数最大值,默认是1024。在高负载下要设置为更高,但最高只能为65535

在 /etc/sysctl.conf 文件下添加以下内容

1
vm.max_map_count=655360 # 文件包含限制一个进程可以拥有的VMA(虚拟内存区域)的数量

添加完后执行以下命令使上面添加的内容生效

1
2
3
source /etc/profile

sudo sysctl -p

配置ElasticSearch

上传并解压ElasticSearch文件

通过xftp 将elasticsearch-5.6.8.tar.gz 文件上传至 /usr/local/ 目录下,在root用户下通过以下命令进行文件解压,配置环节都在root用户下进行

1
2
3
4
5
// 先进入root用户下,否则没有权限
su root

// 再进行解压
tar -zxvf elasticsearch-5.6.8.tar.gz

此时/usr/local/ 目录下便有了解压后的 elasticsearch-5.6.8 文件夹

创建数据和日志存放文件目录

进入elasticsearch-5.6.8文件

1
cd elasticsearch-5.6.8

创建数据和日志存放文件目录

1
2
mkdir data
mkdir logs

修改ElasticSearch配置文件

进入编辑elasticsearch-5.6.8/config目录下的elasticsearch.yml

1
vim /usr/local/elasticsearch-5.6.8/config/elasticsearch.yml

找到以下的条目并进行相应修改,或者直接复制了放在配置文件下方也行

1
2
3
4
5
6
7
8
cluster.name: es-cluster  #设置集群的名称
node.name: es-node #修改当前节点的名称
path.data: /usr/local/elasticsearch-5.6.8/data #修改数据路径
path.logs: /usr/local/elasticsearch-5.6.8/logs #修改日志路径
bootstrap.memory_lock: false #设置ES节点允许内存交换
bootstrap.system_call_filter: false #禁用系统调用过滤器
network.host: master #设置当前主机名称
discovery.zen.ping.unicast.hosts: ["master"] #设置集群的主机列表

修改elasticsearch用户组

通过下述命令将elasticsearch文件权限赋给elasticsearch用户

1
chown -R elasticsearch:elasticsearch /usr/local/elasticsearch-5.6.8

至此,elasticsearch便配置完成

ElasticSearch启动与关闭

进入elasticsearch用户!

启动elasticsearch不能在root用户下进行

1
su elasticsearch

启动elasticsearch

使用以下命令启动elasticsearch

1
/usr/local/elasticsearch-5.6.8/bin/elasticsearch -d

此时在 /usr/local/elasticsearch-5.6.8/logs/es-cluster.log日志文件下便能看到启动成功的消息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[2020-04-09T02:03:48,934][INFO ][o.e.n.Node               ] [es-node] initializing ...
[2020-04-09T02:03:49,035][INFO ][o.e.e.NodeEnvironment ] [es-node] using [1] data paths, mounts [[/ (/dev/sda1)]], net usable_space [16.6gb], net total_space [23.5gb], spins? [possibly], types
[ext4][2020-04-09T02:03:49,035][INFO ][o.e.e.NodeEnvironment ] [es-node] heap size [1.9gb], compressed ordinary object pointers [true]
[2020-04-09T02:03:49,036][INFO ][o.e.n.Node ] [es-node] node name [es-node], node ID [u9kDBekMRxi8KWOBIGcwAg]
[2020-04-09T02:03:49,037][INFO ][o.e.n.Node ] [es-node] version[5.6.8], pid[27500], build[688ecce/2018-02-16T16:46:30.010Z], OS[Linux/4.4.0-142-generic/amd64], JVM[Oracle Corporation
/Java HotSpot(TM) 64-Bit Server VM/1.8.0_211/25.211-b12][2020-04-09T02:03:49,037][INFO ][o.e.n.Node ] [es-node] JVM arguments [-Xms2g, -Xmx2g, -XX:+UseConcMarkSweepGC, -XX:CMSInitiatingOccupancyFraction=75, -XX:+UseCMSInitiatingOccupancyO
nly, -XX:+AlwaysPreTouch, -Xss1m, -Djava.awt.headless=true, -Dfile.encoding=UTF-8, -Djna.nosys=true, -Djdk.io.permissionsUseCanonicalPath=true, -Dio.netty.noUnsafe=true, -Dio.netty.noKeySetOptimization=true, -Dio.netty.recycler.maxCapacityPerThread=0, -Dlog4j.shutdownHookEnabled=false, -Dlog4j2.disable.jmx=true, -Dlog4j.skipJansi=true, -XX:+HeapDumpOnOutOfMemoryError, -Des.path.home=/usr/local/elasticsearch][2020-04-09T02:03:49,955][INFO ][o.e.p.PluginsService ] [es-node] loaded module [aggs-matrix-stats]
[2020-04-09T02:03:49,955][INFO ][o.e.p.PluginsService ] [es-node] loaded module [ingest-common]
[2020-04-09T02:03:49,955][INFO ][o.e.p.PluginsService ] [es-node] loaded module [lang-expression]
[2020-04-09T02:03:49,955][INFO ][o.e.p.PluginsService ] [es-node] loaded module [lang-groovy]
[2020-04-09T02:03:49,956][INFO ][o.e.p.PluginsService ] [es-node] loaded module [lang-mustache]
[2020-04-09T02:03:49,957][INFO ][o.e.p.PluginsService ] [es-node] loaded module [lang-painless]
[2020-04-09T02:03:49,957][INFO ][o.e.p.PluginsService ] [es-node] loaded module [parent-join]
[2020-04-09T02:03:49,957][INFO ][o.e.p.PluginsService ] [es-node] loaded module [percolator]
[2020-04-09T02:03:49,957][INFO ][o.e.p.PluginsService ] [es-node] loaded module [reindex]
[2020-04-09T02:03:49,957][INFO ][o.e.p.PluginsService ] [es-node] loaded module [transport-netty3]
[2020-04-09T02:03:49,957][INFO ][o.e.p.PluginsService ] [es-node] loaded module [transport-netty4]
[2020-04-09T02:03:49,958][INFO ][o.e.p.PluginsService ] [es-node] no plugins loaded
[2020-04-09T02:03:51,680][INFO ][o.e.d.DiscoveryModule ] [es-node] using discovery type [zen]
[2020-04-09T02:03:52,176][INFO ][o.e.n.Node ] [es-node] initialized
[2020-04-09T02:03:52,176][INFO ][o.e.n.Node ] [es-node] starting ...
[2020-04-09T02:03:52,317][INFO ][o.e.t.TransportService ] [es-node] publish_address {192.168.0.106:9300}, bound_addresses {192.168.0.106:9300}
[2020-04-09T02:03:52,326][INFO ][o.e.b.BootstrapChecks ] [es-node] bound or publishing to a non-loopback address, enforcing bootstrap checks
[2020-04-09T02:03:55,420][INFO ][o.e.c.s.ClusterService ] [es-node] new_master {es-node}{u9kDBekMRxi8KWOBIGcwAg}{Zlaf7S5BRbqosFtqkgvR-A}{master}{192.168.0.106:9300}, reason: zen-disco-elected-as
-master ([0] nodes joined)[2020-04-09T02:03:55,453][INFO ][o.e.h.n.Netty4HttpServerTransport] [es-node] publish_address {192.168.0.106:9200}, bound_addresses {192.168.0.106:9200}
[2020-04-09T02:03:55,453][INFO ][o.e.n.Node ] [es-node] started
[2020-04-09T02:03:55,455][INFO ][o.e.g.GatewayService ] [es-node] recovered [0] indices into cluster_state

并且在本地浏览器中输入 192.168.0.106:9200 也能看到elasticsearch推送出来的消息

1
2
3
4
5
6
7
8
9
10
11
12
13
{
"name" : "es-node",
"cluster_name" : "es-cluster",
"cluster_uuid" : "oCrLIXMJQ_WzSd1-7lLeYw",
"version" : {
"number" : "5.6.8",
"build_hash" : "688ecce",
"build_date" : "2018-02-16T16:46:30.010Z",
"build_snapshot" : false,
"lucene_version" : "6.6.1"
},
"tagline" : "You Know, for Search"
}

关闭elasticsearch

通过jps命令找到Elasticsearch进程号,直接kill就可以

1
2
3
4
5
6
elasticsearch@master:/usr/local$ jps
27500 Elasticsearch
27631 Jps
elasticsearch@master:/usr/local$ kill -9 27500
elasticsearch@master:/usr/local$ jps
27643 Jps