Docker部署ElaticSearch集群

ElaticSearch官方网址:https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

拉取镜像

docker pull docker.elastic.co/elasticsearch/elasticsearch:6.6.1

启动容器

内核参数优化

Linux下/etc/sysctrl.conf增加如下设置:sysctl -w vm.max_map_count=262144

grep vm.max_map_count /etc/sysctl.conf
vm.max_map_count=262144

这是最低要求,至于多少合适,还未实践

启动单体模式(开发用)

docker run -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.6.1

集群模式

官方给出例子,是基于docker-compose管理得,其配置文件如下:

version: '2.2'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.6.1
    container_name: elasticsearch
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata1:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - esnet
  elasticsearch2:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.6.1
    container_name: elasticsearch2
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "discovery.zen.ping.unicast.hosts=elasticsearch"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata2:/usr/share/elasticsearch/data
    networks:
      - esnet

volumes:
  esdata1:
    driver: local
  esdata2:
    driver: local

networks:
  esnet:

这里看来,2个节点是布置在同一机器节点上,如果所有节点在同一个机器上,集群也没多大意义了。
那么怎么配置为多机器节点呢?

首先es集群是怎样加入新的节点的呢?

在新节点的配置文件中,通过discovery.zen.ping.unicast.hosts属性来配置集群可以发现的主节点:

discovery.zen.ping.unicast.hosts

所以把配置文件分成2份:
docker-compose1.yaml

version: '2.2'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.6.1
    container_name: elasticsearch
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata1:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - esnet
volumes:
  esdata1:
    driver: local

networks:
  esnet:

docker-compose2.yaml

version: '2.2'
services:
  elasticsearch2:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.6.1
    container_name: elasticsearch2
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "discovery.zen.ping.unicast.hosts=xxx.xxx.xxx.xxx"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata2:/usr/share/elasticsearch/data
    networks:
      - esnet
volumes:
  esdata2:
    driver: local

networks:
  esnet:

此时1节点的ip为:xxx.xxx.xxx.xxx。可是当启动容器时,节点2的容器居然去访问172.18.0.2的机器,这个明显是节点1的容器的内部ip,网络无法通信。

发布外网ip

在配置文件中加入"network.publish_host=xxx.xxx.xxx.xxx",配置如下:

version: '2.2'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.6.1
    container_name: elasticsearch
    environment:
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "network.publish_host=xxx.xxx.xxx.xxx"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata1:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - esnet
volumes:
  esdata1:
    driver: local

networks:
  esnet:

此时发布容器,就没有问题了,就可以通过宿主机器IP进行通信了。
通信是双向的,记着所有节点都要配置这个选项,否则加入集群时还是会失败。

这里docker所用的网络配置为bridge模式的。本来想配置host模式,没有成功,不知为何,之后再进行查看。如有知晓者,欢迎指教。

中文分词器的安装

进入容器内部

docker exec -it 容器名 /bin/bash

安装命令,版本号要和ES对应上

elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.6.1/elasticsearch-analysis-ik-6.6.1.zip

你可能感兴趣的:(docker,elaticsearch,docker,elaticsearch,全文检索)