ElasticSearch Docker-compose 集群搭建

  • 测试

    本节使用,docker-compose在虚拟机上搭建一个集群。
  • 准备步骤

    本例是以ubantu操作系统为例,新建一个,搭建一个三个节点的服务器,一个主节点,两个从节点。
    新建文件夹
    
    mkdir  你的目录/config
    mkdir  你的目录/config/master
    mkdir  你的目录/config/master/data
    mkdir  你的目录/config/master/log
    mkdir  你的目录/config/node1
    mkdir  你的目录/config/node1/data
    mkdir  你的目录/config/node1/log
    mkdir  你的目录/config/node2
    mkdir  你的目录/config/node2/data
    mkdir  你的目录/config/node2/log
    mkdir  你的目录/config/kibana
    
    新建配置文件 - 你的目录/config/master/es.yml
    
    cluster.name: es-cluster
    node.name: es-master
    node.master: true
    node.data: true
    
    network.host: es-master
    http.port: 9200
    transport.tcp.port: 9300
    http.cors.enabled: true
    http.cors.allow-origin: "*"
    
    discovery.zen.ping.unicast.hosts: ["es-master:9300", "es-node1:9300", "es-node2:9300"]
    discovery.zen.minimum_master_nodes: 2
    discovery.zen.ping_timeout: 5s
    
    bootstrap.memory_lock: true
    action.destructive_requires_name: true
    cluster.initial_master_nodes: ["es-master"]
    
    新建配置文件 - 你的目录/config/node1/es.yml
    
    cluster.name: es-cluster
    node.name: es-node1
    node.master: false
    node.data: true
    
    network.host: es-node1
    http.port: 9200
    transport.tcp.port: 9300
    http.cors.enabled: true
    http.cors.allow-origin: "*"
    
    discovery.zen.ping.unicast.hosts: ["es-master:9300", "es-node1:9300", "es-node2:9300"]
    discovery.zen.minimum_master_nodes: 2
    discovery.zen.ping_timeout: 5s
    
    bootstrap.memory_lock: true
    action.destructive_requires_name: true
    cluster.initial_master_nodes: ["es-master"]
    
    新建配置文件 - 你的目录/config/node2/es.yml
    
    cluster.name: es-cluster
    node.name: es-node2
    node.master: false
    node.data: true
    
    network.host: es-node2
    http.port: 9200
    transport.tcp.port: 9300
    http.cors.enabled: true
    http.cors.allow-origin: "*"
    
    discovery.zen.ping.unicast.hosts: ["es-master:9300", "es-node1:9300", "es-node2:9300"]
    discovery.zen.minimum_master_nodes: 2
    discovery.zen.ping_timeout: 5s
    
    bootstrap.memory_lock: true
    action.destructive_requires_name: true
    cluster.initial_master_nodes: ["es-master"]
    
    新建配置文件 - 你的目录/config/node2/kibana.yml
    
    server.name: kibana
    server.host: "0"
    #此处为es的master地址
    elasticsearch.hosts: [ "http://es-master:9200" ]
    xpack.monitoring.ui.container.elasticsearch.enabled: true
    i18n.locale: "zh-CN"
    
    新建docker-composer配置文件 - 你的目录docker-composer.yml
    
    version: "3"
    services:
      es-master:
        image: elasticsearch:7.10.1
        container_name: es-master
        environment:
          - "ES_JAVA_OPTS=-Xms256m -Xmx256m"
        ulimits:
          memlock:
            soft: -1
            hard: -1
          nofile:
            soft: 65536
            hard: 65536
        ports:
          - "9200:9200"
          - "9300:9300"
        volumes:
          - $PWD/config/master/es.yml:/usr/share/elasticsearch/config/elasticsearch.yml
          - $PWD/config/master/data:/usr/share/elasticsearch/data
          - $PWD/config/master/log:/usr/share/elasticsearch/log
        networks:
          - net-es
      es-node1:
        image: elasticsearch:7.10.1
        container_name: es-node1
        environment:
          - "ES_JAVA_OPTS=-Xms256m -Xmx256m"
        ulimits:
          memlock:
            soft: -1
            hard: -1
          nofile:
            soft: 65536
            hard: 65536
        ports:
          - "9201:9200"
          - "9301:9300"
        volumes:
          - $PWD/config/node1/es.yml:/usr/share/elasticsearch/config/elasticsearch.yml
          - $PWD/config/node1/data:/usr/share/elasticsearch/data
          - $PWD/config/node1/log:/usr/share/elasticsearch/log
        networks:
          - net-es
      es-node2:
        image: elasticsearch:7.10.1
        container_name: es-node2
        environment:
          - "ES_JAVA_OPTS=-Xms256m -Xmx256m"
        ulimits:
          memlock:
            soft: -1
            hard: -1
          nofile:
            soft: 65536
            hard: 65536
        ports:
          - "9202:9200"
          - "9302:9300"
        volumes:
          - $PWD/config/node2/es.yml:/usr/share/elasticsearch/config/elasticsearch.yml
          - $PWD/config/node2/data:/usr/share/elasticsearch/data
          - $PWD/config/node2/log:/usr/share/elasticsearch/log
        networks:
          - net-es
      kibana:
        image: kibana:7.10.1
        container_name: kibana
        restart: always
        environment:
          - TZ="Asia/Shanghai"
        ports:
          - 5601:5601
        volumes:
          - $PWD/config/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml:rw
        depends_on:
          - es-master
        networks:
          - net-es
      eshead:
        image: mobz/elasticsearch-head:5
        container_name: eshead
        networks:
          - net-es
        ports:
          - 9100:9100 
    networks:
      net-es:
        driver: bridge
    
    启动集群:
    
    docker-composr up -d