利用 docker 部署 elasticsearch 集群(单节点多实例)

简介: 利用 docker 部署 elasticsearch 集群(单节点多实例)

1、环境介绍

Linux:~ # cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

2、拉取 elasticserach 镜像

Linux:~ # docker pull elasticsearch
"不加版本,表示拉取最新版本"

3、创建 elasticsearch 数据目录

Linux:~ # mkdir -p /opt/elasticsearch/{es1,es2,es3}/{config,data,logs}
Linux:~ # tree /opt/elasticsearch/
/opt/elasticsearch/
|-- es1
|   |-- config
|   |-- data
|   `-- logs
|-- es2
|   |-- config
|   |-- data
|   `-- logs
`-- es3
    |-- config
    |-- data
    `-- logs
12 directories, 0 files

4、创建 elasticsearch 配置文件

  • 注意将下列内容中的 本机IP 字段,替换成自己机器的IP地址
Linux:~ # cat > /opt/elasticsearch/es1/config/elasticsearch.yml <<EOF
cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: 本机IP
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true 
node.data: true  
discovery.zen.ping.unicast.hosts: ["本机IP:9300","本机IP:9301","本机IP:9302"]
discovery.zen.minimum_master_nodes: 1
EOF
Linux:~ # cat > /opt/elasticsearch/es2/config/elasticsearch.yml <<EOF
cluster.name: elasticsearch-cluster
node.name: es-node2
network.bind_host: 0.0.0.0
network.publish_host: 本机IP
http.port: 9201
transport.tcp.port: 9301
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true 
node.data: true  
discovery.zen.ping.unicast.hosts: ["本机IP:9300","本机IP:9301","本机IP:9302"]
discovery.zen.minimum_master_nodes: 1
EOF
Linux:~ # cat > /opt/elasticsearch/es3/config/elasticsearch.yml <<EOF
cluster.name: elasticsearch-cluster
node.name: es-node3
network.bind_host: 0.0.0.0
network.publish_host: 本机IP
http.port: 9202
transport.tcp.port: 9302
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true 
node.data: true  
discovery.zen.ping.unicast.hosts: ["本机IP:9300","本机IP:9301","本机IP:9302"]
discovery.zen.minimum_master_nodes: 1
EOF

5、配置JVM线程数量限制

Linux:~ # echo "vm.max_map_count=262144" >> /etc/sysctl.conf
Linux:~ # sysctl -p

注:这一步是为了防止启动容器时,报出如下错误:

bootstrap checks failed max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]*

6、启动 elasticsearch docker 集群

#!/bin/bash
xms='-Xms256m'
xmx='-Xmx256m'
portes=(
9200
9201
9202
)
portcluster=(
9300
9301
9302
)
espath='/opt/elasticsearch'
dockerespath='/usr/share/elasticsearch'
image='elasticsearch:latest'
num=1
portc=0
for port in ${portes[*]}
do
   portc=$(( ${num} - 1 ))
   docker run -d -e  ES_JAVA_OPTS=""${xms}" "${xmx}"" \
   -p ${port}:${port} \
   -p ${portcluster[$portc]}:${portcluster[$portc]}  \
   -v ${espath}/es${num}/config/elasticsearch.yml:${dockerespath}/config/elasticsearch.yml \
   -v ${espath}/es${num}/data:${dockerespath}/data \
   -v ${espath}/es${num}/logs:${dockerespath}/logs \
   --name es-0${num} ${image}
   let num++
done

7、验证 elasticsearch 集群

"因为是在公有云服务器上部署的,ip不方便透露,就用localhostip代替了"
Linux:~ # curl "http://localhostip:9200/_cat/nodes"
localhostip 31 96 2 0.15 1.12 1.06 mdi - es-node2
localhostip 36 96 2 0.15 1.12 1.06 mdi - es-node1
localhostip 32 96 2 0.15 1.12 1.06 mdi * es-node3
Linux:~ # curl "http://localhostip:9200/_cluster/health?pretty"
{
  "cluster_name" : "elasticsearch-cluster",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 3,
  "number_of_data_nodes" : 3,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "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" : 100.0
}

8、插入以及查看 elasticsearch 索引信息

"插入索引"
Linux:~ # curl -XPUT "http://localhostip:9200/2021-01-21/myelasticsearch/1?pretty" -H "Content-type: application/json" -d '{"name": "bandian", "country": "China", "age": "25", "birthday": "1995-03-20", "sex": "gentleman", "style":"tie han han"}'
{
  "_index" : "2021-01-21",
  "_type" : "myelasticsearch",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : true
}
"查看索引"
Linux:~ # curl -XGET "http://localhostip:9200/2021-01-21/myelasticsearch/1?pretty" -H "Content-type: application/json"
{
  "_index" : "2021-01-21",
  "_type" : "myelasticsearch",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "bandian",
    "country" : "China",
    "age" : "25",
    "birthday" : "1995-03-20",
    "sex" : "gentleman",
    "style" : "tie han han"
  }
}
"从这里可以看出,索引的创建模板格式:_index,_type,_id,_version,found,_source"



相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。 &nbsp;
目录
相关文章
|
6月前
|
NoSQL 算法 Redis
【Docker】(3)学习Docker中 镜像与容器数据卷、映射关系!手把手带你安装 MySql主从同步 和 Redis三主三从集群!并且进行主从切换与扩容操作,还有分析 哈希分区 等知识点!
Union文件系统(UnionFS)是一种**分层、轻量级并且高性能的文件系统**,它支持对文件系统的修改作为一次提交来一层层的叠加,同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual filesystem) Union 文件系统是 Docker 镜像的基础。 镜像可以通过分层来进行继承,基于基础镜像(没有父镜像),可以制作各种具体的应用镜像。
759 6
|
11月前
|
Prometheus 监控 Cloud Native
除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
881 79
|
消息中间件 监控 RocketMQ
Docker部署RocketMQ5.2.0集群
本文详细介绍了如何使用Docker和Docker Compose部署RocketMQ 5.2.0集群。通过创建配置文件、启动集群和验证容器状态,您可以快速搭建起一个RocketMQ集群环境。希望本文能够帮助您更好地理解和应用RocketMQ,提高消息中间件的部署和管理效率。
1851 91
|
Prometheus 监控 Cloud Native
如何使用Prometheus监控Docker Swarm集群的资源使用情况?
还可以根据实际需求进行进一步的配置和优化,如设置告警规则,当资源使用超出阈值时及时发出警报。通过这些步骤,能够有效地使用 Prometheus 对 Docker Swarm 集群的资源进行监控和管理。
760 161
|
Prometheus 监控 Cloud Native
如何监控Docker Swarm集群的性能?
如何监控Docker Swarm集群的性能?
914 163
|
11月前
|
JSON 安全 数据可视化
Elasticsearch(es)在Windows系统上的安装与部署(含Kibana)
Kibana 是 Elastic Stack(原 ELK Stack)中的核心数据可视化工具,主要与 Elasticsearch 配合使用,提供强大的数据探索、分析和展示功能。elasticsearch安装在windows上一般是zip文件,解压到对应目录。文件,elasticsearch8.x以上版本是自动开启安全认证的。kibana安装在windows上一般是zip文件,解压到对应目录。elasticsearch的默认端口是9200,访问。默认用户是elastic,密码需要重置。
5747 0
|
安全 Java Linux
Linux安装Elasticsearch详细教程
Linux安装Elasticsearch详细教程
2092 64
|
存储 安全 数据管理
如何在 Rocky Linux 8 上安装和配置 Elasticsearch
本文详细介绍了在 Rocky Linux 8 上安装和配置 Elasticsearch 的步骤,包括添加仓库、安装 Elasticsearch、配置文件修改、设置内存和文件描述符、启动和验证 Elasticsearch,以及常见问题的解决方法。通过这些步骤,你可以快速搭建起这个强大的分布式搜索和分析引擎。
623 5
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
存储 JSON Java
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。
这篇文章是关于Elasticsearch的学习指南,包括了解Elasticsearch、版本对应、安装运行Elasticsearch和Kibana、安装head插件和elasticsearch-ik分词器的步骤。
1432 0
elasticsearch学习一:了解 ES,版本之间的对应。安装elasticsearch,kibana,head插件、elasticsearch-ik分词器。