利用 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"

猜你喜欢

转载自blog.csdn.net/u010383467/article/details/112971939