目录
一、准备工作
二、编写docker-compose.yml
三、编写ElasticSearch和kibana的配置文件
四、执行构建ElasticSearch集群
五、验证结果:
六、可视化工具
ElasticSearch可视化工具介绍(elasticsearch-head、kibana、elasticHD)
一、elasticsearch-head
二、kibana
三、elasticHD
一、准备工作
1、拉取ElasticSearch镜像
docker pull elasticsearch:7.14.1
docker tag docker.io/elasticsearch:7.14.1 elasticsearch
docker rmi docker.io/elasticsearch:7.14.1
2、创建相关文件夹,用于存放配置文件
mkdir -p /data//docker-compose/elasticsearch
mkdir -p /data/docker-data/elasticsearch/101/config
mkdir -p /data/docker-data/elasticsearch/102/config
mkdir -p /data/docker-data/elasticsearch/103/config
3、赋予文件夹权限
chmod -R 777 /data/docker-data/elasticsearch
ElasticSearch对共享目录的文件夹权限有要求,因此这里为共享目录设置权限
4、将当前用户的内存权限变大
查看当前用户的内存权限
sysctl -a|grep vm.max_map_count
# 显示 vm.max_map_count = 65530
elasticsearch要求该值至少是262144,否则会报错,max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
# 将用户拥有的内存权限增大
sysctl -w vm.max_map_count=262144
二、编写docker-compose.yml
cd /data/docker-compose/elasticsearch
vi docker-compose.yml
version: '3.6'
services:
es_101:
image: elasticsearch
container_name: es_101
hostname: es_101
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- /data/docker-data/elasticsearch/101/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- /data/docker-data/elasticsearch/101/data:/usr/share/elasticsearch/data
privileged: true
ports:
- 9201:9200
- 9301:9300
es_102:
image: elasticsearch
container_name: es_102
hostname: es_102
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- /data/docker-data/elasticsearch/102/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- /data/docker-data/elasticsearch/102/data:/usr/share/elasticsearch/data
privileged: true
ports:
- 9202:9200
- 9302:9300
es_103:
image: elasticsearch
container_name: es_103
hostname: es_103
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
volumes:
- /data/docker-data/elasticsearch/103/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- /data/docker-data/elasticsearch/103/data:/usr/share/elasticsearch/data
privileged: true
ports:
- 9203:9200
- 9303:9300
三、编写ElasticSearch和kibana的配置文件
cd /data/docker-data/elasticsearch/101/config
vi elasticsearch.yml
cluster.name: es_cluster
node.name: es_101
node.master: true
node.data: true
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
discovery.zen.ping.unicast.hosts: ["es_101","es_102","es_103"]
discovery.zen.minimum_master_nodes: 1
cluster.initial_master_nodes: es_101
http.cors.enabled: true
http.cors.allow-origin: "*"
cd /data/docker-data/elasticsearch/102/config
vi elasticsearch.yml
cluster.name: es_cluster
node.name: es_102
node.master: true
node.data: true
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.unicast.hosts: ["es_101","es_102","es_103"]
http.cors.enabled: true
http.cors.allow-origin: "*"
cd /data/docker-data/elasticsearch/103/config
vi elasticsearch.yml
cluster.name: es_cluster
node.name: es_103
node.master: true
node.data: true
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.unicast.hosts: ["es_101","es_102","es_103"]
http.cors.enabled: true
http.cors.allow-origin: "*"
四、执行构建ElasticSearch集群
1、开始构建
cd /data/docker-compose/elasticsearch
docker-compose up -d
此时,因为没有授予共享目录文件夹权限,因此es集群其实是启动不成功的
2、授予文件夹权限
chmod -R 777 /data/docker-data/elasticsearch/
3、重新执行构建
cd /data/docker-compose/elasticsearch
docker-compose restart
五、验证结果:
宿主机:192.168.78.200
访问192.168.78.200:9201 或 192.168.78.200:9202 或 192.168.78.200:9203
返回下面结果:
// 20210927041419
// http://192.168.78.200:9201/
{
"name": "es_101",
"cluster_name": "es_cluster",
"cluster_uuid": "_KgoQSikS6StSHpBK4Yr0Q",
"version": {
"number": "7.14.1",
"build_flavor": "default",
"build_type": "docker",
"build_hash": "66b55ebfa59c92c15db3f69a335d500018b3331e",
"build_date": "2021-08-26T09:01:05.390870785Z",
"build_snapshot": false,
"lucene_version": "8.9.0",
"minimum_wire_compatibility_version": "6.8.0",
"minimum_index_compatibility_version": "6.0.0-beta1"
},
"tagline": "You Know, for Search"
}
说明集群搭建成功了。
六、可视化工具
ElasticSearch可视化工具介绍(elasticsearch-head、kibana、elasticHD)
一、elasticsearch-head
1、拉取镜像
docker pull mobz/elasticsearch-head:5
docker tag docker.io/mobz/elasticsearch-head:5 es_head
docker rmi docker.io/mobz/elasticsearch-head:5
2、创建容器
docker run -itd --name es_head -p 9100:9100 es_head
3、访问 192.168.78.200:9100
4、通过postman添加为es集群添加一个索引
测试json:
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"country": {
"type": "keyword"
},
"remark": {
"type": "text"
}
}
}
}
5、我们再访问192.168.78.200:9100,查看结果:
6、总结及注意
通过elasticsearch-head,我们可以清晰看到数据分片情况,这点是kibana做不到的,后续使用时会有深刻感受,二者结合才是王道。
注意:如果这一步没有添加成功,返回结果是{ "acknowledged": true, "shards_acknowledged": false}
,那很有可能是磁盘空间不足了。
使用df -h
命令查看一下,超过95%使用率,es就不会再允许往es里写数据了。
二、kibana
1、拉取镜像
docker pull kibana:7.14.1
docker tag docker.io/kibana:7.14.1 kibana
docker rmi docker.io/kibana:7.14.1
2、创建容器
docker run -itd --name kibana --network elasticsearch_default --link es_101:elasticsearch -p 5800:5601 kibana
注意,我这里指定了kibana容器和es集群的三个容器使用了同一个网络,所以可以直接用容器名做外连,将kibana内部配置文件中的elasticsearch直接解析为es_101。
如果不指定使用es的网络,则需要进入容器内配置ElasticSearch的实际ip地址
3、查看结果
访问 http://192.168.78.200:5800/app/dev_tools#/console
三、elasticHD
1、下载镜像
docker pull containerize/elastichd
docker tag docker.io/containerize/elastichd elastichd
docker rmi docker.io/containerize/elastichd
2、创建容器
docker run -p 5900:9800 -d --network elasticsearch_default --link es_101:demo elastichd
3、查看结果
访问 http://192.168.78.200:5900