实验示例图
💥注意事项
- elasticsearch安装之后,十分消耗内存资源,需要手动配置限制内存大小。
- elasticsearch和Kibana安装时,版本号需要一致。
- 本实验采用的Linux系统是
CentOS Linux release 7.9.2009 (Core)
,docker版本是20.10.14
- elasticsearch和kibana版本都是
8.2.0
一、什么是elasticsearch?
Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。作为 Elastic Stack 的核心,它集中存储您的数据,帮助您发现意料之中以及意料之外的情况。
二、什么是Kibana?
Kibana 是一个开源分析和可视化平台,旨在与 Elasticsearch 配合使用。您可以使用 Kibana 搜索、查看存储在 Elasticsearch 索引中的数据并与之交互。您可以轻松执行高级数据分析,并在各种图表、表格和地图中可视化数据。
三、使用docker安装部署elasticsearch
创建专属网络
docker network create xybnet docker network ls docker inspect xybnet
执行命令结果
[root@docker ~]# docker network create xybnet b4562c006813576d161c84f729c1a6aebf0eecb1ced954159ba02f32cd6ee656 [root@docker ~]# docker network ls NETWORK ID NAME DRIVER SCOPE b2ac7dc0d1c0 bridge bridge local 9fd62dbfb07f host host local 27700772b8f7 none null local b4562c006813 xybnet bridge local [root@docker ~]# docker inspect xybnet [ { "Name": "xybnet", "Id": "b4562c006813576d161c84f729c1a6aebf0eecb1ced954159ba02f32cd6ee656", "Created": "2022-05-13T23:03:55.546299236+08:00", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": {}, "Config": [ { "Subnet": "172.18.0.0/16", "Gateway": "172.18.0.1" } ] }, "Internal": false, "Attachable": false, "Ingress": false, "ConfigFrom": { "Network": "" }, "ConfigOnly": false, "Containers": {}, "Options": {}, "Labels": {} } ] [root@docker ~]#
下载elasticsearch镜像
docker search elasticsearch docker pull elasticsearch:8.2.0 docker images
执行命令结果
[root@docker ~]# docker search elasticsearch [root@docker ~]# docker pull elasticsearch:8.2.0 8.2.0: Pulling from library/elasticsearch e0b25ef51634: Already exists 860caabdf263: Already exists 9fbe6bc43ac5: Already exists 9d4f6737f430: Already exists 10f01841fd3e: Already exists dae1e3bba098: Already exists 0a3767e40ef9: Already exists 7d786dfd085d: Already exists 7ce904f28ed3: Already exists Digest: sha256:6bd33a35f529d349d8d385856b138d73241555abf2851287c055665494680b8d Status: Downloaded newer image for elasticsearch:8.2.0 docker.io/library/elasticsearch:8.2.0 [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis 6-alpine3.15 6d12d0de5a46 2 weeks ago 32.4MB elasticsearch 8.2.0 f75ee9faf718 3 weeks ago 1.21GB tomcat latest fb5657adc892 4 months ago 680MB elasticsearch latest 5acf0e8da90b 3 years ago 486MB [root@docker ~]# docker inspect elasticsearch:8.2.0 [ { "Id": "sha256:f75ee9faf7183b931afb70d416647824c9b344e83905bbe7f70062b5eab91e43", "RepoTags": [ "elasticsearch:8.2.0" ], "RepoDigests": [ ******以下输出内容省略******
创建并启动Elasticsearch容器服务
# 此命令限制运行内存大小以及挂载卷 docker run -d --name xybes --net xybnet -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms1024m -Xmx2048m" -p 5601:5601 -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data elasticsearch:8.2.0 # 此命令不设置自定义网络 docker run -d --name xybes -p 9200:9200 -p 9300:9300 -p 5601:5601 -e "discovery.type=single-node" elasticsearch:8.2.0 # 本实验执行此命令 docker run -d --name xybes --net xybnet -p 9200:9200 -p 9300:9300 -p 5601:5601 -e "discovery.type=single-node" elasticsearch:8.2.0 docker ps
# 参数解释说明 # -d 后台运行 # --name xybes 指定容器唯一的名称,方便管理 # --net xybnet 指定网络 # -p 9200:9200 -p 9300:9300 映射容器端口到宿主机上 # -e "discovery.type=single-node" 环境变量配置为单机模式 # -v /mydata/elasticsearch/data:/usr/share/elasticsearch/data 持久化数据存储 # -e ES_JAVA_OPTS="-Xms1024m -Xmx2048m" 设置内存大小 # elasticsearch:8.2.0 镜像名称和版本号
执行命令结果
[root@docker ~]# docker run -d --name xybes --net xybnet -p 9200:9200 -p 9300:9300 -p 5601:5601 -e "discovery.type=single-node" elasticsearch:8.2.0 900c8e4dbe11c4460543859b8c887d1fbb21b33071474e079de430e087fdb92f [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 900c8e4dbe11 elasticsearch:8.2.0 "/bin/tini -- /usr/l…" 17 seconds ago Up 16 seconds 0.0.0.0:5601->5601/tcp, :::5601->5601/tcp, 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp xybes [root@docker ~]# docker inspect 900c8e4dbe11 [ { "Id": "900c8e4dbe11c4460543859b8c887d1fbb21b33071474e079de430e087fdb92f", "Created": "2022-05-14T12:56:51.502325109Z", "Path": "/bin/tini", "Args": [ "--", "/usr/local/bin/docker-entrypoint.sh", "eswrapper" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 7605, "ExitCode": 0, "Error": "", "StartedAt": "2022-05-14T12:56:52.189429009Z", "FinishedAt": "0001-01-01T00:00:00Z" }, ******以下输出内容省略****** # 进入xybes容器 [root@docker ~]# docker exec -it xybes /bin/bash cluster.name: "docker-cluster" # 查看配置目录 elasticsearch@900c8e4dbe11:~$ ls LICENSE.txt NOTICE.txt README.asciidoc bin config data jdk lib logs modules plugins # 查看设置es用户密码帮助命令 elasticsearch@900c8e4dbe11:~$ ./bin/elasticsearch-setup-passwords -h Sets the passwords for reserved users Commands -------- auto - Uses randomly generated passwords interactive - Uses passwords entered by a user Non-option arguments: command Option Description ------ ----------- -E <KeyValuePair> Configure a setting -h, --help Show help -s, --silent Show minimal output -v, --verbose Show verbose output # 以自定义的方式设置密码 elasticsearch@900c8e4dbe11:~$ ./bin/elasticsearch-setup-passwords interactive ****************************************************************************** Note: The 'elasticsearch-setup-passwords' tool has been deprecated. This command will be removed in a future release. ****************************************************************************** Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,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_system]: Reenter password for [kibana_system]: 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_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] elasticsearch@900c8e4dbe11:~$ ls LICENSE.txt NOTICE.txt README.asciidoc bin config data jdk lib logs modules plugins # 进入elasticsearch.yml配置文件,关闭SSL(即修改此命令xpack.security.enabled: false) elasticsearch@900c8e4dbe11:~$ vi config/elasticsearch.yml elasticsearch@900c8e4dbe11:~$ cat config/elasticsearch.yml cluster.name: "docker-cluster" network.host: 0.0.0.0 #----------------------- BEGIN SECURITY AUTO CONFIGURATION ----------------------- # # The following settings, TLS certificates, and keys have been automatically # generated to configure Elasticsearch security features on 14-05-2022 12:56:54 # # -------------------------------------------------------------------------------- # Enable security features xpack.security.enabled: false xpack.security.enrollment.enabled: true # Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents xpack.security.http.ssl: enabled: true keystore.path: certs/http.p12 # Enable encryption and mutual authentication between cluster nodes xpack.security.transport.ssl: enabled: true verification_mode: certificate keystore.path: certs/transport.p12 truststore.path: certs/transport.p12 #----------------------- END SECURITY AUTO CONFIGURATION ------------------------- # 退出容器 elasticsearch@900c8e4dbe11:~$ exit exit # 系统重新加载 [root@docker ~]# systemctl daemon-reload # 重启xybes容器 [root@docker ~]# docker restart xybes xybes # 测试访问 [root@docker ~]# curl http://localhost:9200 curl: (56) Recv failure: Connection reset by peer # 使用IP:9200访问成功 [root@docker ~]# curl http://192.168.200.66:9200 { "name" : "900c8e4dbe11", "cluster_name" : "docker-cluster", "cluster_uuid" : "wDwmop88TiO1Rkf1fecHvg", "version" : { "number" : "8.2.0", "build_flavor" : "default", "build_type" : "docker", "build_hash" : "b174af62e8dd9f4ac4d25875e9381ffe2b9282c5", "build_date" : "2022-04-20T10:35:10.180408517Z", "build_snapshot" : false, "lucene_version" : "9.1.0", "minimum_wire_compatibility_version" : "7.17.0", "minimum_index_compatibility_version" : "7.0.0" }, "tagline" : "You Know, for Search" } [root@docker ~]#
# 以SSL安全模式访问。 [root@docker ~]# docker cp xybes:/usr/share/elasticsearch/config/certs/http_ca.crt ./ # 访问成功 [root@docker ~]# curl --cacert http_ca.crt -u elastic https://localhost:9200 Enter host password for user 'elastic': { "name" : "900c8e4dbe11", "cluster_name" : "docker-cluster", "cluster_uuid" : "wDwmop88TiO1Rkf1fecHvg", "version" : { "number" : "8.2.0", "build_flavor" : "default", "build_type" : "docker", "build_hash" : "b174af62e8dd9f4ac4d25875e9381ffe2b9282c5", "build_date" : "2022-04-20T10:35:10.180408517Z", "build_snapshot" : false, "lucene_version" : "9.1.0", "minimum_wire_compatibility_version" : "7.17.0", "minimum_index_compatibility_version" : "7.0.0" }, "tagline" : "You Know, for Search" } [root@docker ~]#
四、使用docker安装部署Kibana
下载kibana镜像(注意对应版本)
docker search kibana docker pull kibana:8.2.0 docker images
执行命令结果
[root@docker ~]# docker pull kibana:8.2.0 8.2.0: Pulling from library/kibana e0b25ef51634: Already exists 16168a059524: Pull complete a7c5b97fb1b3: Pull complete b4997d90f2a3: Pull complete 08edfcb77367: Pull complete 162b89073472: Pull complete c27ed485e628: Pull complete c8ec5118d07e: Pull complete 3098c58d1611: Pull complete f0cd89b25439: Pull complete 90247f6ea1db: Pull complete 3bdda07522a2: Pull complete 36a9ec86c178: Pull complete Digest: sha256:0ba5d3d3ddab3212eadd15bcc701c24a2baafe2f8bd7ced9d2a750cf227b8a06 Status: Downloaded newer image for kibana:8.2.0 docker.io/library/kibana:8.2.0 [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis 6-alpine3.15 6d12d0de5a46 2 weeks ago 32.4MB kibana 8.2.0 58a692253df4 3 weeks ago 752MB elasticsearch 8.2.0 f75ee9faf718 3 weeks ago 1.21GB tomcat latest fb5657adc892 4 months ago 680MB elasticsearch 7.7.0 7ec4f35ab452 2 years ago 757MB elasticsearch latest 5acf0e8da90b 3 years ago 486MB [root@docker ~]#
创建并启动kibana容器
# 本实验执行此命令 docker run -it -d --name kibana --network=container:xybes kibana:8.2.0 docker run -it -d -e ELASTICSEARCH_URL=http://127.0.0.1:9200 --name kibana --network=container:xybes -v /data/kibana/config:/usr/share/kibana/config kibana:8.2.0
执行命令结果
[root@docker ~]# docker run -it -d --name kibana --network=container:xybes kibana:8.2.0 88969a52ec18c84fa7950a80f0211fc645c8de2df49b2b70ee8847e8903e026c [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 88969a52ec18 kibana:8.2.0 "/bin/tini -- /usr/l…" 50 seconds ago Up 49 seconds kibana 900c8e4dbe11 elasticsearch:8.2.0 "/bin/tini -- /usr/l…" 4 hours ago Up 3 hours 0.0.0.0:5601->5601/tcp, :::5601->5601/tcp, 0.0.0.0:9200->9200/tcp, :::9200->9200/tcp, 0.0.0.0:9300->9300/tcp, :::9300->9300/tcp xybes [root@docker ~]# [root@docker ~]# docker inspect 88969a52ec18 [ { "Id": "88969a52ec18c84fa7950a80f0211fc645c8de2df49b2b70ee8847e8903e026c", "Created": "2022-05-14T17:08:38.738984864Z", "Path": "/bin/tini", "Args": [ "--", "/usr/local/bin/kibana-docker" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 27371, "ExitCode": 0, "Error": "", "StartedAt": "2022-05-14T17:08:38.908540948Z", "FinishedAt": "0001-01-01T00:00:00Z" }, ******以下输出内容省略******