CentOS 搭建Elasticsearch服务

配置Java环境

  1. 检查Java版本,目前版本 6.5 的可安装版需要Java 8

$ java -version
openjdk version “1.8.0_191”
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)

如果不是的话,可以考虑安装OpenJDK,如果Java版本已经匹配了可以跳过

yum list | grep jdk | grep 1.8
java-1.8.0-openjdk.x86_64 1:1.8.0.191.b12-1.el7_6 @updates
java-1.8.0-openjdk-headless.i686 1:1.8.0.191.b12-1.el7_6 @updates
中间略过一大堆
java-1.8.0-openjdk-src-debug.i686 1:1.8.0.191.b12-1.el7_6 updates
java-1.8.0-openjdk-src-debug.x86_64 1:1.8.0.191.b12-1.el7_6 updates

选择和机器匹配的版本,以java-1.8.0-openjdk.x86_64为例

yum install java-1.8.0-openjdk.x86_64
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
–> Running transaction check
—> Package java-1.8.0-openjdk.x86_64 1:1.8.0.191.b12-1.el7_6 will be installed
–> Processing Dependency: java-1.8.0-openjdk-headless(x86-64) = 1:1.8.0.191.b12-1.el7_6 for package: 1:java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64
中间略过一大堆
Total download size: 34 M
Installed size: 108 M
Is this ok [y/d/N]: y
Downloading packages:
中间又略过一大堆
Installed:
java-1.8.0-openjdk.x86_64 1:1.8.0.191.b12-1.el7_6
Dependency Installed:
fontconfig.x86_64 0:2.13.0-4.3.el7 giflib.x86_64 0:4.1.6-9.el7 java-1.8.0-openjdk-headless.x86_64 1:1.8.0.191.b12-1.el7_6 libICE.x86_64 0:1.0.9-9.el7
libSM.x86_64 0:1.2.2-2.el7 libX11.x86_64 0:1.6.5-2.el7 libXau.x86_64 0:1.0.8-2.1.el7 libXcomposite.x86_64 0:0.4.4-4.1.el7
libXext.x86_64 0:1.3.3-3.el7 libXi.x86_64 0:1.7.9-1.el7 libXrender.x86_64 0:0.9.10-1.el7 libXtst.x86_64 0:1.2.3-1.el7
libjpeg-turbo.x86_64 0:1.2.90-6.el7 libxcb.x86_64 0:1.13-1.el7 lksctp-tools.x86_64 0:1.0.17-2.el7
Complete!

这时再check一下Java版本

java -version
openjdk version “1.8.0_191”
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK Server VM (build 25.191-b12, mixed mode)

  1. 配置JAVA_HOME

vi /etc/profile

在里面添加以下几行

JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64/jre
CLASS_PATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
export JAVA_HOME JRE_HOME CLASS_PATH PATH

其中**/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64**是你的Java的安装路径。
然后执行

source /etc/profile

让配置生效

echo $JAVA_HOME
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64

准备ES服务

  1. 下载ES
    es6.5.2版本为例,也可以按需要选择自己喜欢的版本

cd /usr/local
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.2.tar.gz
–2018-12-11 15:26:25–
中间省略一大堆
2018-12-11 15:26:46 (5.63 MB/s) - ‘elasticsearch-6.5.2.tar.gz’ saved [113320120/113320120]

解压缩

tar -xzf elasticsearch-6.5.2.tar.gz

然后验证一下

ls -ltr
total 110720
中间省略无关的folder
-rw-r–r-- 1 root root 113320120 Dec 5 21:37 elasticsearch-6.5.2.tar.gz
-rw-r–r-- 9 root root 4096 Dec 11 15:30 elasticsearch-6.5.2

为了能正常运行,修改下权限

chmod -R 777 elasticsearch-6.5.2

然后成为

ls -ltr
total 110720
-rw-r–r-- 1 root root 113320120 Dec 5 21:37 elasticsearch-6.5.2.tar.gz
drwxrwxrwx 9 root root 4096 Dec 11 15:30 elasticsearch-6.5.2

启动ES

  1. 修改ES配置文件

cd elasticsearch-6.5.2
vi config/elasticsearch.yml

修改以下几个配置,并且把前面的**#**去掉使配置生效:

cluster.name: my-es (给自己的es集群起一个响亮的名字,所有节点都会通过这个名字寻找彼此)
node.name: node-60 (一般以当前节点ip地址的最后一位或两位命名,也可以起一个喜欢的节点名字)
network.host: 10.0.0.60 (当前机器的外网ip,可以通过ifconfig命令来获取)
http.port: 9200 (ES监听端口,默认9200)
discovery.zen.ping.unicast.hosts: [“10.0.0.60”, “10.0.0.61”] (如果多于一个节点,同时需要彼此心跳,则需要把其他节点ip地址配置在这里)

保存退出

  1. 修改JAVA配置

vi config/jvm.options

修改Java的堆栈信息

-Xms2g
-Xmx2g
(一般为当前机器内存的一半,当前机器内存为4G所以配置2g在这里)
保存退出

运行命令

exit (退出root用户,ES一般不支持root身份启动)
./bin/elasticsearch
[2018-12-11T16:01:22,525][INFO ][o.e.e.NodeEnvironment ] [node-60] using [1] data paths, mounts [[/ (rootfs)]], net usable_space [35gb], net total_space [39.2gb], types [rootfs]
[2018-12-11T16:01:22,528][INFO ][o.e.e.NodeEnvironment ] [node-60] heap size [1.9gb], compressed ordinary object pointers [true]
省略一大堆
[2018-12-11T16:16:15,153][INFO ][o.e.h.n.Netty4HttpServerTransport] [node-60] publish_address {10.0.3.60:9200}, bound_addresses {10.0.3.60:9200}
[2018-12-11T16:16:15,153][INFO ][o.e.n.Node ] [node-60] started

至此,ES节点启动成功,验证一下

curl 10.0.0.60:9200
{
“name” : “node-60”,
“cluster_name” : “media-es”,
“cluster_uuid” : “VQL5BWbYRzKKXsMCXQvyUQ”,
“version” : {
“number” : “6.5.2”,
“build_flavor” : “default”,
“build_type” : “tar”,
“build_hash” : “9434bed”,
“build_date” : “2018-11-29T23:58:20.891072Z”,
“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”
}

还可以后台启动,只需要在启动命令后面加上 -d 就好

./bin/elasticsearch -d

安装IK分词器

进入es的plugins目录,创建ik自己的目录

cd plugins
mkdir ik
cd ik

然后下载对应的分词器包,这里以ES6.5为例

wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.2/elasticsearch-analysis-ik-6.5.2.zip
中间省略一大堆
2018-12-11 16:30:39 (2.28 MB/s) - ‘elasticsearch-analysis-ik-6.5.2.zip’ saved [4504580/4504580]

安装unzip,如果有了的话跳过

yum install -y unzip
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
中间略过一大堆
Complete!

然后解压缩刚才下的那个包

unzip elasticsearch-analysis-ik-6.5.2.zip
Archive: elasticsearch-analysis-ik-6.5.2.zip
inflating: elasticsearch-analysis-ik-6.5.2.jar
inflating: httpclient-4.5.2.jar
inflating: httpcore-4.4.4.jar
inflating: commons-logging-1.2.jar
inflating: commons-codec-1.9.jar
creating: config/
inflating: config/quantifier.dic
inflating: config/preposition.dic
inflating: config/extra_single_word_low_freq.dic
inflating: config/stopword.dic
inflating: config/suffix.dic
inflating: config/extra_main.dic
inflating: config/IKAnalyzer.cfg.xml
inflating: config/main.dic
inflating: config/extra_stopword.dic
inflating: config/extra_single_word_full.dic
inflating: config/surname.dic
inflating: config/extra_single_word.dic
inflating: plugin-descriptor.properties
inflating: plugin-security.policy

进入config目录,创建自己的扩展词/停止词词典…

vi my.dic

然后修改IKAnalyzer.cfg.xml加入词典信息

vi IKAnalyzer.cfg.xml
CentOS 搭建Elasticsearch服务_第1张图片

重启ES使之生效

可能遇到的报错

  1. 机器虚拟内存/线程限制

[1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
[2]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

对于[1]执行以下命令

sudo vi /etc/sysctl.conf

添加这一行

vm.max_map_count=655360

然后运行

sysctl -p
省略不重要的部分
vm.max_map_count = 655360

对于[2]执行以下命令

sudo vi /etc/security/limits.conf

把最后两行

* soft nofile 65535
* hard nofile 65535

改成

* soft nofile 65536
* hard nofile 65536

然后重启系统

reboot

你可能感兴趣的:(运维)