ElasticSearch 安装配置
下载
# 官网下载压缩包
[root@localhost /home]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.4.2.tar.gz
# 解压
[root@localhost /home]# tar -zxvf elasticsearch-6.4.2.tar.gz
启动:
[root@localhost /home]# elasticsearch-6.4.2/bin/elasticsearch
报错:
[WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:140) ~[elasticsearch-6.4.2.jar:6.4.2]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:127) ~[elasticsearch-6.4.2.jar:6.4.2]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.4.2.jar:6.4.2]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.4.2.jar:6.4.2]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.4.2.jar:6.4.2]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:93) ~[elasticsearch-6.4.2.jar:6.4.2]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:86) ~[elasticsearch-6.4.2.jar:6.4.2]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:104) ~[elasticsearch-6.4.2.jar:6.4.2]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:171) ~[elasticsearch-6.4.2.jar:6.4.2]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:326) ~[elasticsearch-6.4.2.jar:6.4.2]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:136) ~[elasticsearch-6.4.2.jar:6.4.2]
大体意思就是 ElasticSearch 默认不允许以 root 用户启动(出于安全考虑)
解决方法:创建用户组及用户
具体步骤:
创建用户组:
# es_group:组名
[root@localhost bin]# groupadd esgroup
创建用户:
# esroot:用户名 -g:指定用户组 -p:指定密码
[root@localhost bin]# useradd esroot -g esgroup -p password
更改 ElasticSearch 文件夹及子文件的所属用户及用户组:
[root@localhost /home]# chown -R esroot:esgroup elasticsearch-6.4.2
切换到新创建的用户并重新执行启动命令:
[root@localhost /home]# su esroot
[esroot@localhost /home]$ elasticsearch-6.4.2/bin/elasticsearch
这个时候,正常情况下都是可以启动成功的,但是目前只能在内网访问,接下来我们还需要开启外网访问权限
[esroot@localhost /home]$ vim elasticsearch-6.4.2/config/elasticsearch.yml
主要修改两个地方: network.host 和 http.port
注意:每行配置前面都需要加一个空格,冒号后面也需要一个空格,即:
(空格)network.host:(空格)0.0.0.0
(空格)http.port:(空格)9200
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 0.0.0.0 <-- 打开注释,设置地址为 0.0.0.0
#
# Set a custom port for HTTP:
#
http.port: 9200 <-- 打开注释,默认端口为9200
#
# For more information, consult the network module documentation.
保存之后重新启动
[esroot@localhost /home]$ elasticsearch-6.4.2/bin/elasticsearch
再次报错:
[2018-10-19T10:35:29,459][INFO ][o.e.t.TransportService ] [wVDAxQP] publish_address {172.17.128.111:9300}, bound_addresses {0.0.0.0:9300}
[2018-10-19T10:35:29,493][INFO ][o.e.b.BootstrapChecks ] [wVDAxQP] bound or publishing to a non-loopback address, enforcing bootstrap checks
ERROR: [2] bootstrap checks failed
[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]
[2018-10-19T10:35:29,617][INFO ][o.e.n.Node ] [wVDAxQP] stopping ...
[2018-10-19T10:35:29,693][INFO ][o.e.n.Node ] [wVDAxQP] stopped
[2018-10-19T10:35:29,693][INFO ][o.e.n.Node ] [wVDAxQP] closing ...
[2018-10-19T10:35:29,719][INFO ][o.e.n.Node ] [wVDAxQP] closed
主要有两处报错:
1、elasticsearch可以创建的文件描述(file descriptors)太少
2、虚拟内存太小
[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]
继续解决
[esroot@localhost /home]$ su root
[root@localhost /home]# vim /etc/security/limits.conf
修改文件最后的几项配置
root soft nofile 65536
root hard nofile 65536
* soft nofile 65536
* hard nofile 65536
设置 vm.max_map_count
# 方法1:临时生效
[root@localhost /home]# sysctl -w vm.max_map_count=262144
# 方法2:永久生效
[root@localhost /home]# vim /etc/sysctl.conf
加入配置:
vm.max_map_count=262144
[root@localhost /home]# sysctl -p
保存之后切换用户并重新启动
[root@localhost /home]# su esroot
[esroot@localhost /home]$ elasticsearch-6.4.2/bin/elasticsearch (如果想后台运行的话,在后面加 -d 参数即可)
到此为止,ElasticSearch 安装完成,我们可以选择继续安装 ElasticSearch-Head 或 Kibana ,这两款软件都是 ElasticSearch 的客户端管理软件,个人比较推荐 Kibana。
ElasticSearch-Head 安装配置
因为安装 ElasticSearch-Head 需要使用到 npm 包管理器,所以需要我们提前安装好 NodeJS ,安装 NodeJS 的方法可以参考:https://juejin.im/post/5ad424c16fb9a028c06b48f0安装好 nodejs 之后,我们开始安装 ElasticSearch-Head
[root@localhost /home]# git clone git://github.com/mobz/elasticsearch-head.git
[root@localhost /home]# cd elasticsearch-head
[root@localhost /home]# npm install
若在 npm install 的过程中报错:
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! phantomjs-prebuilt@2.1.16 install: `node install.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the phantomjs-prebuilt@2.1.16 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
解决方法:
[root@localhost /home]# npm install phantomjs-prebuilt@2.1.16 --ignore-scripts
然后重新执行
[root@localhost /home]# npm install
依赖安装完成后,我们需要修改一些配置
1.修改Gruntfile.js,允许外网访问 elasticsearch-head
[root@localhost /home]# vim elasticsearch-head/Gruntfile.js
找到 grunt.initConfig 中的 connect 配置,在 options 中添加 hostname: '*',(注意,冒号后面有个空格)
connect: {
server: {
options: {
hostname: '*',
port: 9100,
base: '.',
keepalive: true
}
}
}
2.修改默认连接地址,配置 elasticsearch-head 监控的elasticsearch节点IP地址
[root@localhost /home]# vim elasticsearch-head/_site/app.js
找到 this.base_uri 的配置(在4354行左右,可以通过搜索找到)
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";
将 localhost 修改成 elasticsearch 所在的服务器地址
3.配置 elasticsearch 允许跨域访问
[root@localhost /home]# elasticsearch-6.4.2/config/elasticsearch.yml
在文件结尾添加两行配置:(同样的,每行开头和冒号后面都需要加一个空格)
http.cors.enabled: true
http.cors.allow-origin: '*'
4.防火墙开启9100端口,并重启防火墙
[root@localhost /home]# firewall-cmd --zone=public --add-port=9100/tcp --permanent
[root@localhost /home]# firewall-cmd --reload
这样,我们的 ElasticSearch 和 ElasticSearch-Head 已完成基本的配置,我们可以启动并查看效果
# 切换用户
[root@localhost /home]# su esroot
# 启动 ElasticSearch 服务
[esroot@localhost /home]$ elasticsearch-6.4.2/bin/elasticsearch
# 启动 ElasticSearch-Head
[esroot@localhost /home]$ cd elasticsearch-head
[esroot@localhost /home]$ npm run start
(注:如果需要放到后台运行,可以采用nohup的形式,即:nohup npm run start &)
我们可以查看当前端口情况
[root@localhost /home]$ netstat -tunlp
可以看到,目前9100、9200、9300端口正在运行,其中:
9100端口:ElasticSearch-Head 占用端口
9200、9300端口:ElasticSearch 占用端口,9200是http协议的RESTful接口,9300是tcp通讯端口,集群间和TCPClient都走的它
Kibana 安装配置
注:安装Kibana的版本需要与ElasticSearch的版本保持一致
1.下载、解压
# 下载
[root@localhost home]# wget https://artifacts.elastic.co/downloads/kibana/kibana-6.4.2-linux-x86_64.tar.gz
# 解压
[root@localhost home]# tar -zxvf kibana-6.4.2-linux-x86_64.tar.gz
[root@localhost home]# mv kibana-6.4.2-linux-x86_64 kibana-6.4.2
2.修改配置文件
# 提权
[root@localhost home]# su
# 编辑配置文件
[root@localhost home]# vim kibana-6.4.2/config/kibana.yml
修改 server.host 和 elasticsearch.url 的配置,分别指定 kibana 和 elasticsearch 所在的服务器及端口
server.host: "localhost"
elasticsearch.url: "http://localhost:9200"
3.开启5601端口
# 开启防火墙
[root@localhost home]# systemctl start firewalld
# 允许端口
[root@localhost home]# firewall-cmd --zone=public --add-port=5601/tcp --permanent
# 重启防火墙
[root@localhost home]# systemctl restart firewalld
4.启动 Kibana
# 启动 ElasticSearch 服务(若已启动,则忽略)
[root@localhost /home]# elasticsearch-6.4.2/bin/elasticsearch
# 启动 Kibana
[root@localhost /home]# kibana-6.4.2/bin/kibana
按照以上步骤 ElasticSearch 和 Kibana 应该是可以正常运行起来的,我们可以通过访问 http://<kibana_server_ip>:5601 看是否启动成功
中文分词器 IK
首先进入到 ElasticSearch 的路径下面,然后执行下面的命令,其中的 6.4.2 替换成你的 ElasticSearch 版本
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.2/elasticsearch-analysis-ik-6.4.2.zip
安装完后重启 ElasticSearch 即可(注意需要使用 esroot 用户启动)