实验示例图
💥注意事项
- 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"
},
******以下输出内容省略******
五、配置elasticsearch和Kibana相连接
进入elasticsearch容器中生成令牌
或
进入kibana容器中修改kibana.yml配置文件
# 方法一:
# 进入elasticsearch容器中生成令牌(失败)
elasticsearch@900c8e4dbe11:~$ ./bin/elasticsearch-create-enrollment-token --scope kibana
ERROR: Failed to determine the health of the cluster.
elasticsearch@900c8e4dbe11:~$
# 方法二:
# 进入kibana容器中修改kibana.yml配置文件(本实验采用此方法),修改访问的IP地址,为本地127.0.0.1访问。
kibana@900c8e4dbe11:~$cat >config/kibana.yml<<EOF
#
# ** THIS IS AN AUTO-GENERATED FILE **
#
# Default Kibana configuration for docker target
server.host: "0.0.0.0"
server.shutdownTimeout: "5s"
elasticsearch.hosts: [ "http://127.0.0.1:9200" ]
monitoring.ui.container.elasticsearch.enabled: true
# 此处设置显示语言为中文
i18n.locale: "zh-CN"
EOF
# 查看修改后的配置内容
kibana@900c8e4dbe11:~$ cat config/kibana.yml
#
# ** THIS IS AN AUTO-GENERATED FILE **
#
# Default Kibana configuration for docker target
server.host: "0.0.0.0"
server.shutdownTimeout: "5s"
elasticsearch.hosts: [ "http://127.0.0.1:9200" ]
monitoring.ui.container.elasticsearch.enabled: true
i18n.locale: "zh-CN"
kibana@900c8e4dbe11:~$
# 退出容器并重启容器
kibana@900c8e4dbe11:~$ exit
exit
[root@docker ~]# docker restart kibana
kibana
需要输入注册令牌,再elasticsearch容器中生成。或者修改kibana.yml配置文件
进入kiabana管理界面。
运行样例数据,查看效果。
六、Kibana设置成中文图形化界面
设置之前的图形界面如下
# Kibana将语言设置为中文 6.7以后系统开始支持中文,修改语言只需要添加一行配置即可。
# 在kibana.yml配置文件中添加一行配置即可。
[root@docker ~]# docker exec -it kibana bash
kibana@900c8e4dbe11:~$
# 修改kibana.yml配置文件
kibana@900c8e4dbe11:~$cat >config/kibana.yml<<EOF
#
# ** THIS IS AN AUTO-GENERATED FILE **
#
# Default Kibana configuration for docker target
server.host: "0.0.0.0"
server.shutdownTimeout: "5s"
elasticsearch.hosts: [ "http://127.0.0.1:9200" ]
monitoring.ui.container.elasticsearch.enabled: true
# 此处设置显示语言为中文
i18n.locale: "zh-CN"
EOF
# 查看修改后的kibana.yml配置文件
kibana@900c8e4dbe11:~$ cat config/kibana.yml
#
# ** THIS IS AN AUTO-GENERATED FILE **
#
# Default Kibana configuration for docker target
server.host: "0.0.0.0"
server.shutdownTimeout: "5s"
elasticsearch.hosts: [ "http://192.168.200.66:9200" ]
monitoring.ui.container.elasticsearch.enabled: true
i18n.locale: "zh-CN"
kibana@900c8e4dbe11:~$
设置中文显示图形界面如下
七、安装 Heartbeat
1、下载并安装 Heartbeat
curl -L -O https://artifacts.elastic.co/downloads/beats/heartbeat/heartbeat-8.2.0-x86_64.rpm
sudo rpm -vi heartbeat-8.2.0-x86_64.rpm
2、编辑配置
修改 /etc/heartbeat/heartbeat.yml 以设置连接信息:
output.elasticsearch:
hosts: ["<es_url>"]
username: "elastic"
password: "<password>"
# If using Elasticsearch's default certificate
ssl.ca_trusted_fingerprint: "<es cert fingerprint>"
setup.kibana:
host: "<kibana_url>"
其中,
<password>
是elastic
用户的密码,<es_url>
是 Elasticsearch 的 URL,<kibana_url>
是 Kibana 的 URL。要使用 Elasticsearch 生成的默认证书 配置 SSL,请在<es cert fingerprint>
中添加其指纹。
3、编辑配置 - 添加监测
# 在 heartbeat.yml 文件中编辑 heartbeat.monitors 设置。
heartbeat.monitors:
- type: http
urls: ["<http://localhost:9200>"]
schedule: "@every 10s"
其中
<http://localhost:9200>
是受监测 URL。
4、启动 Heartbeat
# setup 命令加载 Kibana 索引模式。
sudo heartbeat setup
sudo service heartbeat-elastic start
5、Heartbeat 状态
确认从 Heartbeat 收到数据
八、其他
kibana容器中没有vi编辑命令。可以使用cat>配置文件<<EOF
编辑方式进行编辑。
权限问题。
更新软件权限不足。使用管理员的身份进入容器。
# 查看集群状态
[root@docker ~]# curl -X GET "localhost:9200/_cluster/health?pretty"
{
"cluster_name" : "docker-cluster",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 14,
"active_shards" : 14,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 1,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 93.33333333333333
}
[root@docker ~]#
# 节点简要信息
[root@docker ~]# curl -X GET "localhost:9200/_cat/nodes?pretty&v"
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
172.18.0.2 54 77 6 0.01 0.05 0.11 cdfhilmrstw * 900c8e4dbe11
[root@docker ~]#
#索引列表
[root@docker ~]# curl -X GET "localhost:9200/_cat/indices?v"
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open .ds-heartbeat-8.2.0-2022.05.15-000001 AkhgkfJgQL2SHqBNVuBp5g 1 1 0 0 225b 225b
green open kibana_sample_data_logs 8mgvHLdDTIm5TwvOlB2QXA 1 0 14074 0 9.2mb 9.2mb
green open kibana_sample_data_ecommerce L5j4XqRhRqmA2mIDORvZpw 1 0 4675 0 4.3mb 4.3mb
[root@docker ~]#
九、参考链接
Elasticsearch - Official Image | Docker Hub
Kibana - Official Image | Docker Hub文章来源:https://www.toymoban.com/news/detail-431815.html
ElasticSearch常用命令文章来源地址https://www.toymoban.com/news/detail-431815.html
到了这里,关于【docker】使用docker安装部署elasticsearch+Kibana的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!