使用docker创建elasticsearch集群

这里使用的是自己制作的安装了elasticsearch-analysis-ik-6.3.0插件的docker镜像。
在自己的osx系统上安装了docker后,已经有了docker-compose命令可用,在服务器上(CentOS系统)安装docker后发现没有docker-compose命令,需要单独再安装docker-compose,安装方式参考install-compose。
这里的docker-compose.yml按照官网的示例写的,部分地方做了点修改,如下:

version: '2.2'
services:
  elasticsearch:
    # image: docker.elastic.co/elasticsearch/elasticsearch:6.3.0
    # 这里使用自己制作的安装了ik插件的镜像
    image: elasticsearch-with-ik:6.3.0
    container_name: elasticsearch
    environment:
      # 集群的名称,名称一样的组成一个集群
      - cluster.name=docker-es-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      # 设置允许跨域,方便head插件访问
      - "http.cors.enabled=true"
      - "http.cors.allow-origin=*"
      # 这里的elasticsearch,elasticsearch2,elasticsearch3是指的设置的container_name,容器内部可以通过container_name相互网络访问
      - "discovery.zen.ping.unicast.hosts=elasticsearch,elasticsearch2,elasticsearch3"
      - node.name=elasticsearch
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      # 将索引数据挂载到名为esdata1的docker volume,可以通过-v参数删除(如docker stop -v或者docker-compose down -v)
      - esdata1:/usr/share/elasticsearch/data
      # 将日志目录挂载到物理机docker-compose.yml目录下的logs/es目录下
      - ./logs/es/:/usr/share/elasticsearch/logs/:rw
      # 下面是将索引数据挂载到物理机docker-compose.yml目录下的data/es目录下
      # - ./data/es/:/usr/share/elasticsearch/data:rw
    ports:
      - 9200:9200
      - 9300:9300
    networks:
      - esnet
  elasticsearch2:
    # image: docker.elastic.co/elasticsearch/elasticsearch:6.3.0
    image: elasticsearch-with-ik:6.3.0
    container_name: elasticsearch2
    environment:
      - cluster.name=docker-es-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "discovery.zen.ping.unicast.hosts=elasticsearch,elasticsearch2,elasticsearch3"
      - "http.cors.enabled=true"
      - "http.cors.allow-origin=*"
      - node.name=elasticsearch2
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata2:/usr/share/elasticsearch/data
      - ./logs/es2/:/usr/share/elasticsearch/logs/:rw
      # - ./data/es2/:/usr/share/elasticsearch/data:rw
    ports:
      - 9201:9200
      - 9301:9300
    networks:
      - esnet
  elasticsearch3:
    # image: docker.elastic.co/elasticsearch/elasticsearch:6.3.0
    image: elasticsearch-with-ik:6.3.0
    container_name: elasticsearch3
    environment:
      - cluster.name=docker-es-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "discovery.zen.ping.unicast.hosts=elasticsearch,elasticsearch2,elasticsearch3"
      - "http.cors.enabled=true"
      - "http.cors.allow-origin=*"
      - node.name=elasticsearch3
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - esdata3:/usr/share/elasticsearch/data
      - ./logs/es3/:/usr/share/elasticsearch/logs/:rw
      # - ./data/es3/:/usr/share/elasticsearch/data:rw
    ports:
      - 9202:9200
      - 9302:9300
    networks:
      - esnet

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

networks:
  esnet:

执行docker-compose up启动,这里配置了三个实例,映射出来的端口分别是9200、9201、9202。这里遇到一个问题,在我的osx系统上一开始始终只能启动两个实例,会随机失败一个(有时候是elasticsearch失败,有时候是elasticsearch2失败,有时候是elasticsearch3失败),错误信息是exited with code 137。通过搜索发现是内存设置太小所致 ,将内存改大即可
修改内存大小

通过head插件可以看到集群相关信息


连接9200
连接9201

集群状态

你可能感兴趣的:(使用docker创建elasticsearch集群)