使用 docker 安装ES、Kibana
- 安装并启动 docker
- 为 elasticsearch 创建数据目录(目的是为了可以直接在宿主机中进行一些操作和查看)
[root@localhost ~]# mkdir -p /mydata/elasticsearch/config
[root@localhost ~]# mkdir -p /mydata/elasticsearch/data
[root@localhost ~]# echo "http.host: 0.0.0.0" > /mydata/elasticsearch/config/elasticsearch.yml
# http.host: 0.0.0.0 表示可以允许任何源去访问 ES 集群
- 拉取 elasticsearch 镜像
[root@localhost ~]# docker pull elasticsearch:7.4.2
- 启动 elasticsearch
1) 查看是否启动成功
[root@localhost ~]# docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms64m -Xmx128m" \
-v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data \
-v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-d elasticsearch:7.4.2
备注:
# -e ES_JAVA_OPTS="-Xms64m -Xmx128m" 是用来控制 ES 启动时占用的内存的,如果不进行限制,那么对于配置较低的服务器会直接卡死
2) 查看是否启动成功
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
37f847c9ecce elasticsearch:7.4.2 "/usr/local/bin/dock…" 8 seconds ago Exited (1) 4 seconds ago elasticsearch
# 异常退出,查看日志
[root@localhost ~]# docker logs 37f
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
2021-11-14 08:30:09,104 main ERROR No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2
Exception in thread "main" SettingsException[Failed to load settings from [elasticsearch.yml]]; nested: ParsingException[Failed to parse object: expecting token of type [START_OBJECT] but found [VALUE_STRING]];
at org.elasticsearch.common.settings.Settings$Builder.loadFromStream(Settings.java:1097)
at org.elasticsearch.common.settings.Settings$Builder.loadFromPath(Settings.java:1070)
at org.elasticsearch.node.InternalSettingsPreparer.prepareEnvironment(InternalSettingsPreparer.java:83)
at org.elasticsearch.cli.EnvironmentAwareCommand.createEnv(EnvironmentAwareCommand.java:95)
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:115)
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
Caused by: ParsingException[Failed to parse object: expecting token of type [START_OBJECT] but found [VALUE_STRING]]
at org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken(XContentParserUtils.java:78)
at org.elasticsearch.common.settings.Settings.fromXContent(Settings.java:617)
at org.elasticsearch.common.settings.Settings.access$400(Settings.java:82)
at org.elasticsearch.common.settings.Settings$Builder.loadFromStream(Settings.java:1093)
... 8 more
# 这里是因为 yml 文件中格式不对,“http.host: 0.0.0.0” 中 host: 跟 0.0.0.0 之间是有空格的,我在第一次写的时候忘记了这个空格,所以报了这个错误。
3) 更改上述错误之后,继续尝试启动
[root@localhost ~]# docker start elasticsearch
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
37f847c9ecce elasticsearch:7.4.2 "/usr/local/bin/dock…" About a minute ago Up 2 seconds 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch
# 依旧未启动成功,查看日志
[root@localhost ~]# docker logs bae
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
{"type": "server", "timestamp": "2021-11-14T08:34:37,889Z", "level": "WARN", "component": "o.e.b.ElasticsearchUncaughtExceptionHandler", "cluster.name": "elasticsearch", "node.name": "bae2d26f1d28", "message": "uncaught exception in thread [main]",
"stacktrace": ["org.elasticsearch.bootstrap.StartupException: ElasticsearchException[failed to bind service]; nested: AccessDeniedException[/usr/share/elasticsearch/data/nodes];",
"at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:163) ~[elasticsearch-7.4.2.jar:7.4.2]",
"at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-7.4.2.jar:7.4.2]",
# 捕捉关键信息: AccessDeniedException[/usr/share/elasticsearch/data/nodes];
# 是由于权限问题导致
[root@localhost ~]# chmod -R 777 /mydata/elasticsearch/
4) 再次启动
[root@localhost ~]# docker start elasticsearch
elasticsearch
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bae2d26f1d28 elasticsearch:7.4.2 "/usr/local/bin/dock…" 3 minutes ago Up 3 seconds 0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp elasticsearch
5) 访问测试
[root@localhost ~]# curl -I http://*.*.*.*:9200
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 540
[root@localhost ~]# curl http://*.*.*.*:9200
{
"name" : "bae2d26f1d28",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "GQUvbPwwQn2Kw5t8oZU7OQ",
"version" : {
"number" : "7.4.2",
"build_flavor" : "default",
"build_type" : "docker",
"build_hash" : "2f90bbf7b93631e52bafb59b3b049cb44ec25e96",
"build_date" : "2019-10-28T20:40:44.881551Z",
"build_snapshot" : false,
"lucene_version" : "8.2.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
- 拉取 kibana 镜像
[root@localhost ~]# docker pull kibana:7.4.2
- 启动 kibana
[root@localhost ~]# docker run --name kibana -e ELASTICSEARCH_HOSTS=http://*.*.*.*:9200 -p 5601:5601 -d kibana:7.4.2
e90730f07262cfc47cbc52e4da8ef672a20d6c59ead4a0b6dba7322200c5e01c
# -e ELASTICSEARCH_HOSTS=http://*.*.*.*:9200 这里的*.*.*.*是 ES 的访问地址
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e90730f07262 kibana:7.4.2 "/usr/local/bin/dumb…" 6 seconds ago Up 5 seconds 0.0.0.0:5601->5601/tcp kibana
- 访问测试 kibana
[root@localhost ~]# curl -I http://*.*.*.*:5601
HTTP/1.1 302 Found
location: /app/kibana
kbn-name: kibana
kbn-xpack-sig: 2322f16350a0e25cfa4e70c8fc757145
content-type: text/html; charset=utf-8
cache-control: no-cache
content-length: 0
Date: Sun, 14 Nov 2021 10:54:59 GMT
Connection: keep-alive