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

2024-03-04 17:18:33 浏览数 (1)

1.拉取镜像

代码语言:javascript复制
docker pull elasticsearch:7.7.0

2.创建挂载卷

代码语言:javascript复制
#创建目录
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

代码语言:javascript复制
编辑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.设置防火墙

代码语言:javascript复制
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服务

代码语言:javascript复制
#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集群是否正常启动

代码语言:javascript复制
#检查服务进程
docker ps |grep ES

#检查es集群是否健康,必须是green才是健康
curl -X GET "localhost:9200/_cluster/health?pretty"

6.其他命令

代码语言:javascript复制
#备注
#查看集群状态
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"

0 人点赞