ElasticSearch集群搭建

需求

  1. 用docker搭建一个ElasticSearch一主一从的集群,加一个elasticsearch-head查看集群状态

步骤

  1. 在参考了一堆博客后,我写了个固定容器ip的docker-compose.yml
    es1.yml
network.bind_host: 0.0.0.0
cluster.name: es_cluster
node.name: master
node.master: true
node.data: true
http.cors.enabled: true
http.cors.allow-origin: "*"
#network.host: 0.0.0.0
network.publish_host: 172.20.0.2
discovery.zen.minimum_master_nodes: 1

es2.yml

network.bind_host: 0.0.0.0
cluster.name: es_cluster
node.name: node2
node.master: false
node.data: true
http.cors.enabled: true
http.cors.allow-origin: "*"
#network.host: 0.0.0.0
network.publish_host: 172.20.0.3
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.unicast.hosts: es1

docker-compose.yml

version: '2'
services:
  elasticsearch-central: 
    image: elasticsearch:5.6.4
    container_name: es1
    volumes:
      - /root/mydocker/docker-es/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /root/mydocker/docker-es/data1:/usr/share/elasticsearch/data
    restart: always
    environment:
      - ES_CLUSTERNAME=elasticsearch
      - "ES_JAVA_OPTS=-Xmx50m -Xms50m"
    command: elasticsearch
    ports:
      - "9200:9200"
      - "9300:9300"
    networks:
      extnetwork:
        ipv4_address: 172.20.0.2
  elasticsearch-data: 
    image: elasticsearch:5.6.4
    container_name: es2
    volumes:
      - /root/mydocker/docker-es/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /root/mydocker/docker-es/data2:/usr/share/elasticsearch/data
    restart: always
    environment:
      - ES_CLUSTERNAME=elasticsearch
      - "ES_JAVA_OPTS=-Xmx50m -Xms50m"
    command: elasticsearch
    ports:
      - "9201:9200"
      - "9301:9300"
    links:
      - elasticsearch-central:elasticsearch
    networks:
      extnetwork:
        ipv4_address: 172.20.0.3
  elasticsearch-head:
    image: mobz/elasticsearch-head:5
    container_name: head
    volumes:
      - /root/mydocker/docker-es/head-conf/Gruntfile.js:/usr/src/app/Gruntfile.js
      - /root/mydocker/docker-es/head-conf/app.js:/usr/src/app/_site/app.js
    ports:
      - "9100:9100"
    links:
      - elasticsearch-central:elasticsearch
    networks:
      extnetwork:
        ipv4_address: 172.20.0.4
networks:
  extnetwork:
    ipam:
      config:
        - subnet: 172.20.0.0/16
          gateway: 172.20.0.1

遇到的问题

  1. Caused by: java.nio.file.AccessDeniedException 这类异常原因是文件夹权限问题,把data1,data2都设置上读写权限就可以
  2. 5.0版本默认容器jvm占用2G内存,若机器内存不足也会报错Cannot allocate memory,这个问题我在脚本已经解决了 "ES_JAVA_OPTS=-Xmx50m -Xms50m" 加上,因为我只是测试玩玩,不用于生产,所以只设置了50M
  3. sysctl.conf文件修改后最好运行下 /etc/sysctl.conf 立即生效
  4. 如果你用的是elasticsearch6.0版本,又会有新的问题,详情看

https://www.elastic.co/cn/blog/strict-content-type-checking-for-elasticsearch-rest-requests

参考博客
https://blog.csdn.net/u012915455/article/details/78952068
https://blog.csdn.net/wanghao_0206/article/details/79583325
https://blog.csdn.net/hechaojie_com/article/details/83625265
https://blog.csdn.net/sinat_31908303/article/details/80496349
https://blog.csdn.net/moliyiran/article/details/53791515
https://segmentfault.com/a/1190000014283157

原文地址:https://www.cnblogs.com/sky-chen/p/9923716.html