docker-compose部署ElasticSearch集群

docker-compose部署ElasticSearch集群

leo 4285 2021-06-16

Elasticsearch Compose 配置文件

创建一个 docker-compose.yml 内容如下:

version: '2.2'
services:
  es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.10.1
    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:
      - ./es01/data:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - elastic

  es02:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.10.1
    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:
      - ./es02/data:/usr/share/elasticsearch/data
    networks:
      - elastic

  es03:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.10.1
    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:
      - ./es03/data:/usr/share/elasticsearch/data
    networks:
      - elastic

  kib01:
    image: docker.elastic.co/kibana/kibana:7.10.1
    container_name: kib01
    ports:
      - 5601:5601
    environment:
      ELASTICSEARCH_URL: http://es01:9200
      ELASTICSEARCH_HOSTS: '["http://es01:9200","http://es02:9200","http://es03:9200"]'
    networks:
      - elastic

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

networks:
  elastic:
    driver: bridge

事先创建如下数据挂载目录:

├── es01
│   └── data
├── es02
│   └── data
└── es03
    └── data

并且设置权限:

chmod -R 777 es01 es02 es03

执行 docker-compose up -d启动。

错误

文件权限

报错如下:

AccessDeniedException[/usr/share/elasticsearch/data/nodes]

提前将挂载目录创建好,并将权限设置好:

chmod -R 777 es01 es02 es03

内存不足

报错如下:

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

修改配置文件:

vi /etc/sysctl.conf

在文件末尾添加一行内容:

vm.max_map_count=262144

执行以下命令立即生效:

/sbin/sysctl -p

之后重新启动即可。

如果是 Win10 的 wsl 中的 docker

需执行以下命令:

wsl -d docker-desktop
sysctl -w vm.max_map_count=262144

之后重新启动。