docker环境,搭建elasticsearch 7.5集群

275 阅读2分钟

本文分享如何在docker环境下搭建elasticsearch集群。

bin/jdk:8u221镜像的构建,请参考 -- docker基础环境搭建

下载elasticsearch-7.5.2-linux-x86_64.tar.gz,构建bin/es:7.5.2镜像,Dockerfile如下

FROM bin/jdk:8u221

WORKDIR /usr/lib

COPY elasticsearch-7.5.2-linux-x86_64.tar.gz .
RUN tar -xzf elasticsearch-7.5.2-linux-x86_64.tar.gz  \
&& rm elasticsearch-7.5.2-linux-x86_64.tar.gz

COPY docker-entrypoint.sh /usr/local/bin

RUN groupadd -r es && useradd -r -g es es \
&& chown -R es:es elasticsearch-7.5.2 \
&& chmod 777  /usr/local/bin/docker-entrypoint.sh

VOLUME /usr/local/es/data
WORKDIR /usr/lib/elasticsearch-7.5.2
ENTRYPOINT ["docker-entrypoint.sh"]

docker-entrypoint.sh负责修改配置,并启动es进程

#!/bin/bash

# 由于宿主机内存有限,将es进程使用内存调整为500M
sed -i 's/-Xms1g/-Xms500m/'  config/jvm.options
sed -i 's/-Xmx1g/-Xmx500m/'  config/jvm.options

cat>>config/elasticsearch.yml<<EOF
path.data: /usr/local/es/data
node.master: true
node.data: true
node.ingest: true
cluster.name: $CLUSTER_NAME
node.name: $NODE_NAME
network.host: $IP
transport.tcp.port: $TRANS_PORT
http.port: $PORT
discovery.seed_hosts: $SEED_GROUP
cluster.initial_master_nodes: $INIT_NODES
http.cors.enabled: true
http.cors.allow-origin: "*"
EOF

chown -R es:es /usr/local/es/data
exec gosu es bin/elasticsearch

配置含义: path.data:存储数据目录 node.master:该节点是否可以成为master node.data:该节点是否存储数据 node.ingest:该节点对文档进行索引之前是否做预处理 discovery.seed_hosts:种子节点列表,用于节点启动时执行discovery操作。 cluster.initial_master_nodes:可成为master的节点列表,仅用于首次启动Elasticsearch集群。 http.cors.enabled/http.cors.allow-origin:允许elasticsearch-head插件连接到该es集群。

编写docker-bin.sh,用于启动es容器

#!/bin/bash

sudo docker network create es-net

seed_group='['
init_nodes='['
for i in `seq 1 3`; do
        seed_group="${seed_group}\"es-${i}:9300\","
        init_nodes="${init_nodes}\"node-${i}\","
done
# 删除最后一个,字符
seed_group=${seed_group%?}
init_nodes=${init_nodes%?}
seed_group=$seed_group']'
init_nodes=$init_nodes']'

for i in `seq 1 3`; do
        sudo docker run -d --name es-$i  --network es-net  --network-alias  es-$i \
        -e CLUSTER_NAME=my-es -e NODE_NAME=node-$i \
        -e PORT=9200 -e TRANS_PORT=9300  -e IP=es-$i \
        -e SEED_GROUP="$seed_group" -e INIT_NODES="$init_nodes" \
        bin/es:7.5.2
done

启动docker容器前,需修改宿主机的vm.max_map_count配置

sudo sysctl -w vm.max_map_count=262144

否则可能报错 max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

查看es集群健康状态

$ sudo docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'  es-1
172.19.0.2
$ curl http://172.19.0.2:9200/_cat/health?v
epoch      timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1583052306 08:45:06  my-es   green           3         3      0   0    0    0        0             0                  -                100.0%

使用elasticsearch-head插件 下载elasticsearch-head-5.0.0,解压,使用浏览器打开index.html,修改es地址,就可以连接到es集群了。 下面简单说一下如何使用elasticsearch,都是使用elasticsearch-head来操作。

创建索引

put blog
{
  "settings": {
    "number_of_shards": 2,
    "number_of_replicas": 2
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "author": {
        "type": "text"
      },
      "content": {
        "type": "text"
      },
      "ctm": {
        "type": "date"
      }
    }
  }
}

这里完整的url是http://172.19.0.2:9200/blog,省略了前缀。 创建了blog索引,字段为title,author,content,ctm。

es 7后,Type的概念被移除,可以简单(但不完全准确)地将es的索引Index理解为mysql的table,而mapping则对应表结构。

Index中单条数据称为Document,使用JSON表示,同一个Index的不要求相同的格式,但最好保持相同,这样有利于搜索。

插入数据

POST blog/_doc/1
result ->
{
  "title": "java",
  "author": "a",
  "content": "hello,java",
  "ctm": "2020-03-12"
}

POST blog/_doc
{
  "title": "es",
  "author": "b",
  "content": "hello,es",
  "ctm": "2020-03-12"
}

创建的一条数据指定了id为1

查询数据

  1. 通过id查询
GET blog/_doc/1

结果如下

{

    "_index": "blog",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "_seq_no": 0,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "java",
        "author": "a",
        "content": "hello,java",
        "ctm": "2020-03-12"
    }
}
  1. 通过字段查找
POST blog/_search
{
  "query": {
    "match": {
      "author": "a"
    }
  }
}

结果如下

{

    "took": 15,
    "timed_out": false,
    "_shards": {
        "total": 2,
        "successful": 2,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.6931472,
        "hits": [
            {
                "_index": "blog",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.6931472,
                "_source": {
                    "title": "java",
                    "author": "a",
                    "content": "hello,java",
                    "ctm": "2020-03-12"
                }
            }
        ]
    }

}

如果您觉得本文不错,欢迎关注我的微信公众号,您的关注是我坚持的动力!