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

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 利用 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"



相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
3天前
|
测试技术 Linux 网络安全
【Docker项目实战】使用Docker部署RSS阅读器yarr
【6月更文挑战第22天】使用Docker部署RSS阅读器yarr
14 3
|
3天前
|
Docker 容器
如何使用Docker部署WPS Office服务并实现无公网IP远程处理文档表格(二)
使用Docker部署的WPS Office服务可以通过内网穿透工具Cpolar实现远程访问。首先,创建一个名为“wps office”的隧道,选择HTTP协议和3000端口,分配免费的随机域名,并指定中国地区。然后,通过Cpolar的管理界面获取HTTP公网地址,用以远程访问WPS Office。由于随机域名会变化,可以升级Cpolar套餐并保留一个二级子域名,确保长期稳定的远程访问。配置子域名后,更新隧道设置,完成固定公网地址的绑定,从而实现随时随地通过固定地址访问WPS Office。
|
3天前
|
数据采集 JavaScript Go
蓝易云 - crawlab通过docker单节点部署简单爬虫
以上就是通过Docker单节点部署Crawlab并运行简单爬虫的步骤。在实际操作中,你需要根据自己的需求对爬虫代码进行编写。
7 2
|
4天前
|
搜索推荐 测试技术 Linux
【Docker项目实战】使用Docker部署EnBizCard数字名片工具
【6月更文挑战第21天】使用Docker部署EnBizCard数字名片工具
18 3
|
4天前
|
域名解析 网络协议 Ubuntu
docker快速部署DNS,实现快速上线
docker快速部署DNS,实现快速上线
|
5天前
|
Java Docker 容器
使用 Spring Boot 构建 Docker 镜像并进行多模式部署
使用 Spring Boot 构建 Docker 镜像并进行多模式部署
24 2
|
6天前
|
测试技术 Linux 数据库
【Docker项目实战】使用Docker部署Xnote轻量级笔记系统
【6月更文挑战第19天】使用Docker部署Xnote轻量级笔记系统
20 3
|
5天前
|
测试技术 Linux 网络安全
【Docker项目实战】使用Docker部署RSS阅读器yarr
【6月更文挑战第20天】使用Docker部署RSS阅读器yarr
12 1
|
1天前
|
应用服务中间件 Linux 数据安全/隐私保护
Linux+docker部署项目
Linux+docker部署项目
8 0
|
3天前
|
安全 Linux 网络安全
如何使用Docker部署WPS Office服务并实现无公网IP远程处理文档表格(一)
在群晖NAS上使用Docker部署WPS Office并结合Cpolar内网穿透的步骤包括: 1. 通过SSH命令行拉取`linuxserver/wps-office`镜像。 2. 在群晖容器管理界面运行镜像,设置启动选项和端口映射。 3. 本地访问群晖IP:3000端口以使用WPS Office。 4. 安装Cpolar套件,手动添加并安装到群晖,通过9200端口访问其Web管理界面。 5. 使用Cpolar配置内网穿透,实现远程访问WPS Office。 这一过程允许用户即使在没有公网IP的情况下,也能通过Cpolar将内网的WPS Office服务暴露到公网,便于远程办公和文档处理。