【docker专题_01】docker搭建elasticsearch集群 -

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 【docker专题_01】docker搭建elasticsearch集群 -

1.拉取镜像

docker pull elasticsearch:7.7.0

2.创建挂载卷

#创建目录
mkdir -p /docker/ES
mkdir /docker/ES/data1
mkdir /docker/ES/data2
mkdir /docker/ES/data3
mkdir /docker/ES/data4
mkdir /docker/ES/data5
mkdir /docker/ES/config
cd /docker/ES
chmod 777 data1 data2 data3 data4 data5
cd /docker/ES/config
#创建文件
touch es1.yml es2.yml es3.yml es4.yml es5.yml

3.编写es的yml配置文件

注意:云服务器需要设置安全策略放行9200,9300,9301,9302,9303,9304端口,否则访问失败;

注意:discovery.seed_hosts配置的是集群服务器,部署几台就写几台,集群最低3台,否则需要修改discovery.zen.minimum_master_nodes为1;

注意:124.71.65.69地址需要换成自己的服务器ip

编辑es1.yml
cluster.name: hzmt-es-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: 124.71.65.6
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.zen.minimum_master_nodes: 2
discovery.seed_hosts: ["124.71.65.6.9:9300","124.71.65.69:9301","124.71.65.69:9302","124.71.65.69:9303","124.71.65.69:9304"]
cluster.initial_master_nodes: ["es-node1","es-node2","es-node3","es-node4","es-node5"]
#编辑es2.yml
cluster.name: hzmt-es-cluster
node.name: es-node2
network.bind_host: 0.0.0.0
network.publish_host: 124.71.65.69
http.port: 9201
transport.tcp.port: 9301
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true 
node.attr.box_type: "hot"  # 标识为热数据节点
discovery.zen.minimum_master_nodes: 2
discovery.seed_hosts: ["124.71.65.69:9300","124.71.65.69:9301","124.71.65.69:9302","124.71.65.69:9303","124.71.65.69:9304"]
cluster.initial_master_nodes: ["es-node1","es-node2","es-node3","es-node4","es-node5"]
#编辑es3.yml文件
cluster.name: hzmt-es-cluster
node.name: es-node3
network.bind_host: 0.0.0.0
network.publish_host: 124.71.65.69
http.port: 9202
transport.tcp.port: 9302
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true 
node.attr.box_type: "warm"  # 标识为热数据节点
discovery.zen.minimum_master_nodes: 2
discovery.seed_hosts: ["124.71.65.69:9300","124.71.65.69:9301","124.71.65.69:9302","124.71.65.69:9303","124.71.65.69:9304"]
cluster.initial_master_nodes: ["es-node1","es-node2","es-node3","es-node4","es-node5"]
#编辑es4.yml文件
cluster.name: hzmt-es-cluster
node.name: es-node4
network.bind_host: 0.0.0.0
network.publish_host: 124.71.65.69
http.port: 9203
transport.tcp.port: 9303
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true 
node.attr.box_type: "cold"  # 标识为热数据节点
discovery.zen.minimum_master_nodes: 2
discovery.seed_hosts: ["124.71.65.69:9300","124.71.65.69:9301","124.71.65.69:9302","124.71.65.69:9303","124.71.65.69:9304"]
cluster.initial_master_nodes: ["es-node1","es-node2","es-node3","es-node4","es-node5"]
#编辑es5.yml文件
cluster.name: hzmt-es-cluster
node.name: es-node5
network.bind_host: 0.0.0.0
network.publish_host: 124.71.65.69
http.port: 9204
transport.tcp.port: 9304
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true 
node.attr.box_type: "cold"  # 标识为热数据节点
discovery.zen.minimum_master_nodes: 2
discovery.seed_hosts: ["124.71.65.69:9300","124.71.65.69:9301","124.71.65.69:9302","124.71.65.69:9303","124.71.65.69:9304"]
cluster.initial_master_nodes: ["es-node1","es-node2","es-node3","es-node4","es-node5"]

4.设置防火墙

firewall-cmd --add-port=9300/tcp
firewall-cmd --add-port=9301/tcp
firewall-cmd --add-port=9302/tcp
firewall-cmd --add-port=9303/tcp
firewall-cmd --add-port=9304/tcp

5.docker运行es服务

#docker启动es 
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 -v /docker/ES/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /docker/ES/data1:/usr/share/elasticsearch/data --name ES01 elasticsearch:7.7.0
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9201:9201 -p 9301:9301 -v /docker/ES/config/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /docker/ES/data2:/usr/share/elasticsearch/data --name ES02 elasticsearch:7.7.0
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9202:9202 -p 9302:9302 -v /docker/ES/config/es3.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /docker/ES/data3:/usr/share/elasticsearch/data --name ES03 elasticsearch:7.7.0
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9203:9203 -p 9303:9303 -v /docker/ES/config/es4.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /docker/ES/data4:/usr/share/elasticsearch/data --name ES04 elasticsearch:7.7.0
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9204:9204 -p 9304:9304 -v /docker/ES/config/es5.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /docker/ES/data5:/usr/share/elasticsearch/data --name ES05 elasticsearch:7.7.0

6.检查es集群是否正常启动

#检查服务进程
docker ps |grep ES
#检查es集群是否健康,必须是green才是健康
curl -X GET "localhost:9200/_cluster/health?pretty"

6.其他命令

#备注
#查看集群状态
curl -X GET "localhost:9200/_cat/health?v"
#查看集群健康状态
curl -X GET "localhost:9200/_cluster/health?pretty"
#查看集群中的节点
curl -X GET "localhost:9200/_cat/nodes?v"
#查看索引
curl -XGET 'http://localhost:9200/_cat/indices?v'
#查看索引的setting信息
curl -XGET 'http://localhost:9200/forseti-20220101/_settings?pretty=true'
#设动态置冷热分离
curl -XPUT http://localhost:9200/forseti-20220101/_settings -H "Content-Type:application/json" -d '{
"index.routing.allocation.include.tag": "cold"
}'
#定义通用的索引模板保证新创建索引的分片不会分配到warm节点上
curl -XPUT http://localhost:9200/_template/hot_template
{
  "template": "*",
  "order": 0,
  "version": 0,
  "settings": {
    "index": {
      "routing": {
        "allocation": {
          "require": {
            "box_type": "hot"
          },
          "exclude":{
            "box_type": "warm"
          }
        }
      },
      "number_of_shards": 3,
      "number_of_replicas": 1,
      "refresh_interval": "50s"
    },
    "index.unassigned.node_left.delayed_timeout": "3d"
  }
}
#手动执行reroute命令
curl -XPUT http://localhost:9200/_cluster/reroute
{
  "commands": [
    {
      "move": {
        "index": "forseti-2021*",
        "shard": 10,
        "from_node": "node-248",
        "to_node": "node-12"
      }
    }
  ]
}
#创建索引
curl -X PUT "localhost:9200/forseti-20220101?pretty"
#删除索引
curl -X DELETE "localhost:9200/forseti-20220101?pretty"
#设置数据类型
curl -X PUT "localhost:9200/allcountry?pretty" -H "Content-Type:application/json" -d '
{
    "settings": {
        "index.number_of_replicas": 0
    },
    "mappings": {
        "country_info": {
            "properties": {
                "geonameid": {
                    "type": "long"
                },
                "name": {
                    "type": "text"
                },
                "latitude": {
                    "type": "double"
                },
                "longitude": {
                    "type": "double"
                },
                "population": {
                    "type": "long"
                }
            }
        }
    }
}'
#查看指定节点信息
curl -XGET 'http://localhost:9200/_nodes/nodeName?pretty=true'
#创建索引,指定分片和副本的数量
curl -H "Content-Type: application/json" -XPUT "http://localhost:9200/test_index?pretty" -d'
{undefined
"settings": {undefined
"number_of_shards": 2,
"number_of_replicas": 1
}
}'
#添加document
    curl -X PUT "localhost:9200/customer/customer_info/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}'
#查询document
curl -X GET "localhost:9200/customer/customer_info/1?pretty"
#替换Document
curl -X POST "localhost:9200/customer/customer_info/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "Apple" }
}'
#更新Document---添加新字段
curl -X POST "localhost:9200/customer/customer_info/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "Tom", "age": 20 }
}'
#删除document
curl -X DELETE "localhost:9200/customer/customer_info/2?pretty"
相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
25天前
|
网络安全 Docker 容器
|
25天前
|
安全 Docker 容器
|
28天前
|
网络安全 Docker 容器
|
25天前
|
网络安全 数据安全/隐私保护 Docker
|
1月前
|
运维 监控 数据安全/隐私保护
管理 Docker Swarm 集群注意事项
【10月更文挑战第7天】
35 3
|
1月前
|
负载均衡 监控 安全
如何管理 Docker Swarm 集群和节点
【10月更文挑战第7天】
47 3
|
8天前
|
存储 监控 安全
Elasticsearch 集群
【11月更文挑战第3天】
84 54
|
4天前
|
监控 API 索引
Elasticsearch集群健康检查
【11月更文挑战第4天】
18 3
|
8天前
|
API Docker 容器
【赵渝强老师】构建Docker Swarm集群
本文介绍了如何使用三台虚拟主机构建Docker Swarm集群。首先在master节点上初始化集群,然后通过特定命令将node1和node2作为worker节点加入集群。最后,在master节点上查看集群的节点信息,确认集群构建成功。文中还提供了相关图片和视频教程,帮助读者更好地理解和操作。
|
8天前
|
调度 Docker 容器
【赵渝强老师】Docker Swarm集群的体系架构
Docker Swarm自1.12.0版本起集成至Docker引擎,无需单独安装。它内置服务发现功能,支持跨多服务器或宿主机创建容器,形成集群提供服务。相比之下,Docker Compose仅限于单个宿主机。Docker Swarm采用主从架构,Swarm Manager负责管理和调度集群中的容器资源,用户通过其接口发送指令,Swarm Node根据指令创建容器运行应用。