docker

Elasticsearch 单机集群及多机集群Docker实践

2020-03-31  本文已影响0人  雨初眠

(一)Docker 单机集群部署

拉取镜像

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

搭建集群

1. docker-compose 搭建elasticsearch 集群

    version: '2.2'
    services:
        es01:
            image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
            container_name: es01
            environment:
            - node.name=es01
            - cluster.name=es-docker-cluster
            - discovery.seed_hosts=es02,es03
            - cluster.initial_master_nodes=es01,es02,es03
            - bootstrap.memory_lock=true
            - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
            ulimits:
            memlock:
                soft: -1
                hard: -1
            volumes:
            - data01:/usr/share/elasticsearch/data
            ports:
            - 9200:9200
            networks:
            - elastic
        es02:
            image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
            container_name: es02
            environment:
            - node.name=es02
            - cluster.name=es-docker-cluster
            - discovery.seed_hosts=es01,es03
            - cluster.initial_master_nodes=es01,es02,es03
            - bootstrap.memory_lock=true
            - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
            ulimits:
            memlock:
                soft: -1
                hard: -1
            volumes:
            - data02:/usr/share/elasticsearch/data
            networks:
            - elastic
        es03:
            image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
            container_name: es03
            environment:
            - node.name=es03
            - cluster.name=es-docker-cluster
            - discovery.seed_hosts=es01,es02
            - cluster.initial_master_nodes=es01,es02,es03
            - bootstrap.memory_lock=true
            - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
            ulimits:
            memlock:
                soft: -1
                hard: -1
            volumes:
            - data03:/usr/share/elasticsearch/data
            networks:
            - elastic

    volumes:
        data01:
            driver: local
        data02:
            driver: local
        data03:
            driver: local

    networks:
        elastic:
            driver: bridge

2. 非docker-compose 搭建集群

现在也来建三个节点分别是: es01,es02,es03;端口分别是: 9300,9301,9302

#es01
 cluster.name: "es-docker-cluster"
 network.host: 0.0.0.0
 network.publish_host: 172.16.5.1
 discovery.seed_hosts: ["172.16.5.1:9301","172.16.5.1:9302"] #所有master 节点ip 如果本机为master这里配置除本机以外的master  ip:93端口( transport.tcp.port)
 transport.tcp.port: 9300 #es02 9301 es03 9302
 cluster.initial_master_nodes: ["172.16.5.1:9301","172.16.5.1:9302","172.16.5.1:9300"]
#node1
docker run -p 9200:9200 -p 9300:9300 --name="es01" -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e node.name="es01" -v path/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml  docker.elastic.co/elasticsearch/elasticsearch:7.4.2

#node2
docker run -p 9201:9200 -p 9301:9301 --name="es02" -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e node.name="es02" -v path/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml docker.elastic.co/elasticsearch/elasticsearch:7.4.2

#node3
docker run -p 9202:9200 -p 9302:9302 --name="es03" -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -e node.name="es03" -v path/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml docker.elastic.co/elasticsearch/elasticsearch:7.4.2

(二)Docker 多机集群部署

镜像获取同单机部署一样,须在每一个节点都拉取docker镜像.

node ip
es01 172.16.5.1
es02 172.16.5.2
es03 172.16.5.3

搭建多机集群

这里就直接用 docker-compose 来搭建elasticsearch 多机集群

  1. 创建elasticsearch.yml
 #es01
 cluster.name: "docker-cluster"
 network.host: 0.0.0.0
 network.publish_host: 172.16.5.1  ## 改成相应的 node ip

  1. 创建docker-compose.yml
version: '2.2'
services:
  es01: #相应node的服务名
    image: docker.elastic.co/elasticsearch/elasticsearch:7.4.2
    container_name: es01
    environment:
      - node.name=es01 #相应的node名
      - cluster.name=es-docker-cluster
      - bootstrap.memory_lock=true
      - "discovery.seed_hosts=172.16.5.2, 172.16.5.3" #所有master 节点ip 如果本机为master这里配置除本机以外的master ip
      - "cluster.initial_master_nodes=172.16.5.1,172.16.5.2,172.16.5.3" #ES启动时所有具有master选举权的节点ip
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - /home/elasticsearch/data:/usr/share/elasticsearch/data:rw  #把数据挂载在外面
      - /config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml #挂载elasticsearch.yml
    ports:
      - 9200:9200
      - 9300:9300
    networks:
      - elastic
networks:
  elastic:
    driver: bridge

注意问题

  1. 挂载数据的时候出现访问受限以至于无法启动时,可将挂载数据的目录所有者和用户组改为 elastic 前提条件得先创建用户和用户组,省事一点就直接 chown -R 1000:1000 /home/elasticsearch/data
  2. 在运行elasticsearch时出现 max virtual memory areas vm.max_map_count [65530], 请在宿主机中执行: sudo sysctl -w vm.max_map_count=262144 或者运行sudo vi /etc/sysctl.conf在文件中追加vm.max_map_count=262144 保存后执行sudo sysctl -p
  3. cluster.initial_master_nodes 为集群创建初期具有master选举权的节点。
  4. discovery.seed_hosts 发送信息给具有master选举权的节点,如果当前节点具有master节点的选举权,则需要向其他同样具有master节点选举权的发送消息。如果无master 节点选举权,则须把所有具有master选举权的节点host 加入。
  5. 集群master节点须大于等于3个,因为如果当前主节点挂了之后es须从具有master 选举权的节点中重新选举出新的主节点,如果具有master 选举权的节点存活少于一半时,节点是无法完成选举的,所以必须有大于一半以上且具有master选举权的节点存在时es才能重新选举出新的主节点。不然节点无法工作。
  6. elasticsearch.yml里的配置都可在docker-compose.ymlenvironment里覆盖。

the end enjoy!

上一篇下一篇

猜你喜欢

热点阅读