ES - 安装ElasticSearch
# 官网相关教程
安装ElasticSearch还是先要看下官方网站。
- 官方网站 (opens new window)
- 官方2.x中文教程中安装教程 (opens new window)
- 官方ElasticSearch下载地址 (opens new window)
- 官方Kibana下载地址 (opens new window)
本系列教程基于ElasticSearch 7.x版本。
# 安装ElasticSearch
ElasticSearch 是使用 java 开发的,且本版本的 es 需要的 jdk 版本要是 1.8 以上,所以安装ElasticSearch 之前保证 JDK1.8+ ,并正确的配置好JDK环境变量,否则会启动ElasticSearch失败。
# windows 版本
ElasticSearch
win下载:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.1.zip
- 文件:
bin:启动文件
config:配置文件
log4j2.properties:日志配置文件
jvm.options:java虚拟机的配置
elasticsearch.yml:es的配置文件
data:索引数据目录
lib:相关类库Jar包
logs:日志目录
modules:功能模块
plugins:插件
2
3
4
5
6
7
8
9
10
- 启动:
双击 ElasticSearch 下的 bin 目录中的 elasticsearch.bat 启动
- 访问:
http://localhost:9200
安装ES的图形化界面插件客户端Head
注意:需要 NodeJS 的环境
Head 是 elasticsearch 的集群管理工具,可以用于数据的浏览查询!被托管在 github 上面!
地址: https://github.com/mobz/elasticsearch-head/
1、下载并解压
下载 elasticsearch-head-master.zip
#安装依赖!
cnpm install
#运行
npm run start
2
3
4
2、修改配置文件 elasticsearch.yml
由于 ES 进程和客户端进程端口号不同,存在跨域问题,所以我们要在 ES 的配置文件中配置下跨域问题
# 跨域配置:
http.cors.enabled: true
http.cors.allow-origin: "*"
2
3
3、启动测试
启动 ElasticSearch
并使用head工具进行连接测试!
访问url:http://localhost:9100/
安装Kibana
Kibana 是一个针对 Elasticsearch 的开源分析及可视化平台,用来搜索、查看交互存储在 Elasticsearch 索引中的数据。
使用Kibana,可以通过各种图表进行高级数据分析及展示。Kibana让海量数据更容易理解。它操作简单,基于浏览器的用户界面可以快速创建仪表板(dashboard)实时显示Elasticsearch查询动态。
设置Kibana非常简单。无需编码或者额外的基础架构,几分钟内就可以完成Kibana安装并启动 Elasticsearch索引监测。
- 下载Kibana
注意版本对应关系: https://www.elastic.co/cn/downloads/kibana
解压后进入,双击 kibana.bat
启动服务就可以了,ELK基本上都是拆箱即用的
然后访问 http://localhost:5601/
kibana会自动去访问9200,也就是 elasticsearch 的端口号(当然 elasticsearch 这个时候必须启动着),然后就可以使用 kibana 了!
- 访问界面是英文,可修改成中文
只需要在配置文件 kibana.yml 中加入
i18n.locale: "zh-CN"
重启即可!
# linux 版本
请先确保安装了 jdk1.8 以上版本
安装ElasticSearch
- 进入一个目录,下载
curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.1-linux-x86_64.tar.gz
- 解压
tar zxvf /opt/elasticsearch-7.6.1-linux-x86_64.tar.gz
- 增加elasticSearch用户
必须创建一个非root用户来运行ElasticSearchElasticSearch5及以上版本,基于安全考虑,强制规定不能以root身份运行。
如果你使用root用户来启动ElasticSearch,则会有如下错误信息:
[root@VM-0-6-centos elasticsearch-7.6.1]# ./bin/elasticsearch
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
[2021-06-11T17:15:56,884][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [VM-0-6-centos] 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:174) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125) ~[elasticsearch-cli-7.6.1.jar:7.6.1]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.6.1.jar:7.6.1]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.6.1.jar:7.6.1]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.6.1.jar:7.6.1]
... 6 more
uncaught exception in thread [main]
java.lang.RuntimeException: can not run elasticsearch as root
at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105)
at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172)
at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349)
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125)
at org.elasticsearch.cli.Command.main(Command.java:90)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
For complete error details, refer to the log at /opt/elasticsearch-7.6.1/logs/elasticsearch.log
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
28
29
30
增加 elasticsearch 用户:
[root@VM-0-6-centos elasticsearch-7.6.1]# useradd elasticsearch
[root@VM-0-6-centos elasticsearch-7.6.1]# passwd elasticsearch
Changing password for user elasticsearch.
New password:
BAD PASSWORD: The password contains the user name in some form
Retype new password:
passwd: all authentication tokens updated successfully.
2
3
4
5
6
7
修改目录权限至新增的 elasticsearch 用户(没权限启动会报错)
在 root 用户下执行命令
[root@VM-0-6-centos ~]# chown -R elasticsearch /opt/elasticsearch-7.6.1
- 启动
# 切换用户
su elasticsearch
# 启动
./bin/elasticsearch -d
2
3
4
- 查看安装是否成功
[root@VM-0-6-centos ~]# netstat -ntlp | grep 9200
tcp6 0 0 127.0.0.1:9200 :::* LISTEN 11601/java
tcp6 0 0 ::1:9200 :::* LISTEN 11601/java
# 访问成功
[root@VM-0-6-centos ~]# curl 127.0.0.1:9200
{
"name" : "VM-0-6-centos",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "sLGiUbfQQ5-btWB9QiOnxg",
"version" : {
"number" : "7.6.1",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "aa751e09be0a5072e8570670309b1f12348f023b",
"build_date" : "2020-02-29T00:15:25.529771Z",
"build_snapshot" : false,
"lucene_version" : "8.4.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
安装Kibana
安装 Kibana 及 ElasticSearch 相关配置请访问:https://www.pdai.tech/md/db/nosql-es/elasticsearch-x-install.html
# 使用 Docker
官方文档:https://www.elastic.co/guide/en/kibana/current/docker.html
创建网络
docker network create elastic
加上内存限制启动 ES
docker run -d --name es01-test --net elastic -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.1
启动 Kibana
docker run -d --name kib01-test --net elastic -p 5601:5601 -e "ELASTICSEARCH_HOSTS=http://es01-test:9200" kibana:7.6.1
可视化工具 Elasticsearch-head(可选)
docker run -d --name head-test --net elastic -p 9100:9100 mobz/elasticsearch-head:5
# 配置密码访问
修改Elastic配置
使用基本许可证时,默认情况下禁用Elasticsearch安全功能。由于我测试环境是放在公网上的,所以需要设置下密码访问。相关文档可以参考这里 (opens new window)
1、进入正在运行的容器
#
docker exec -it 容器id /bin/bash
#安装 vim
yum -y install vim
2
3
4
2、更改 config/elasticsearch.yml
文件,设置 xpack.security.enabled
为 true
[root@06c5914709a5 config]# vim elasticsearch.yml
[root@06c5914709a5 config]# cat elasticsearch.yml
cluster.name: "docker-cluster"
network.host: 0.0.0.0
xpack.security.enabled: true
2
3
4
5
3、重启elasticsearch容器
[root@VM-0-6-centos z-test]# docker restart es01-test
es01-test
2
4、进入容器设置各个组件的密码
[root@06c5914709a5 elasticsearch]# ./bin/elasticsearch-setup-passwords interactive
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana]:
Reenter password for [kibana]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
修改 Kibana 配置
1、修改 kibana.yml
进入容器
添加配置: elasticsearch.username: "elastic"
bash-4.2$ vi kibana.yml
bash-4.2$ cat kibana.yml
#
# ** THIS IS AN AUTO-GENERATED FILE **
#
# Default Kibana configuration for docker target
server.name: kibana
server.host: "0"
elasticsearch.hosts: [ "http://elasticsearch:9200" ]
xpack.monitoring.ui.container.elasticsearch.enabled: true
elasticsearch.username: "elastic"
2
3
4
5
6
7
8
9
10
11
12
2、创建kibana keystore
./bin/kibana-keystore create
3、在kibana keystore 中添加密码
./bin/kibana-keystore add elasticsearch.password
4、重启容器
[root@VM-0-6-centos /]# docker restart kib01-test
kib01-test
2
再次访问:ip:9200
,则需要输入账号和密码
账号为:elastic