Loading

Docker创建ElasticSearch集群以及常见问题

本篇文章演示在WSL2中通过DockerCompose搭建ES集群,解决其中遇到的问题。

虚拟内存最大映射数

第一个问题是几乎所有机器上搭建ES集群都会遇到的,即虚拟内存最大映射数默认为65535,而ES需要更大,最少是262144,所以你需要设置这个参数

sudo vim /etc/sysctl.conf

添加下面这句

vm.max_map_count=262144

执行以下命令

➜  ~ sudo sysctl -p
vm.max_map_count=262144

内存不足

第二个问题,三个ES节点总是有一个莫名其妙的自动退出,如果你docker start重启它,会导致更多的节点退出,这是由于内存不足所致。

从WSL2的层面,你可以通过在%UserProfile%路径下创建一个.wslconfig文件,并写入如下内容来限制WSL的内存大小,但我觉得问题不在于此,因为WSL2的内存好像本身就是动态且没有限制的,不过如果尝试了所有的办法都不行,也可以试试。

[wsl2]
memory=4GB
swap=4GB
localhostForwarding=true

从Docker层面,可以(在DockerCompose中)通过使用下面代码来限制一个容器的内存用量:

services:
  es01:
    image: "elasticsearch:7.12.1"
    container_name: "es01"
    deploy:
      resources:
        limits:
          memory: 1G
        reservations:
          memory: 512M

其它奇怪问题

Caused by:java.lang.IllegalStateException: failure when sending a validation request to node

可能是你的data目录不干净了,删除里面的所有文件即可

java.lang.IllegalStateException: failed to obtain node locks, tried [[/var/lib/elasticsearch/sunaw-cluster]] with lock id [0]; maybe these locations are not writable or multiple nodes were started without increasing [node.max_local_storage_nodes] (was [1])?

先查看是否有其它已经启动的es实例:

ps aux | grep elasticsearch

如果有,使用kill -9 <pid>杀掉它

否则,有可能是你yaml文件写错了,仔细检查下,比如每一个实例对应的datavolume对不对,我真的就是这个文件复制时有个地方忘改了。

最终效果:

img

最终yaml:

version: '2.2'
services:
  es01:
    image: "elasticsearch:7.12.1"
    container_name: "es01"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    deploy:
      resources:
        limits:
          memory: 1G
        reservations:
          memory: 512M
    privileged: true
    environment:
      - node.name=es01
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es02,es03
      - cluster.initial_master_nodes=es01,es02,es03
      - "ES_JAVA_OPS=-Xms512m -Xmx512m"
    volumes:
      - "./es-data01:/usr/share/elasticsearch/data"
      - "./es-plugins:/usr/share/elasticsearch/plugins"
    ports:
      - 9201:9200
    networks:
      - elastic

  es02:
    image: "elasticsearch:7.12.1"
    container_name: "es02"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    deploy:
      resources:
        limits:
          memory: 1G
        reservations:
          memory: 512M
    privileged: true
    environment:
      - node.name=es02
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es01,es03
      - cluster.initial_master_nodes=es01,es02,es03
      - "ES_JAVA_OPS=-Xms512m -Xmx512m"
    volumes:
      - "./es-data02:/usr/share/elasticsearch/data"
      - "./es-plugins:/usr/share/elasticsearch/plugins"
    ports:
      - 9202:9200
    networks:
      - elastic

  es03:
    image: "elasticsearch:7.12.1"
    container_name: "es03"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    deploy:
      resources:
        limits:
          memory: 1G
        reservations:
          memory: 512M
    privileged: true
    environment:
      - node.name=es03
      - cluster.name=es-docker-cluster
      - discovery.seed_hosts=es01,es02
      - cluster.initial_master_nodes=es01,es02,es03
      - "ES_JAVA_OPS=-Xms512m -Xmx512m"
    volumes:
      - "./es-data03:/usr/share/elasticsearch/data"
      - "./es-plugins:/usr/share/elasticsearch/plugins"
    ports:
      - 9203:9200
    networks:
      - elastic

networks:
  elastic:
    driver: bridge
posted @ 2022-08-20 10:59  yudoge  阅读(335)  评论(0编辑  收藏  举报