1. 新建一个文件夹 es-cluster,在添加 777 权限,否则挂载文件会报错,权限不足或者文件无法打开

mkdir es-cluster
mkdir es-cluster/es01
mkdir es-cluster/es02
mkdir es-cluster/es03
mkdir es-cluster/es01/config
mkdir es-cluster/es02/config
mkdir es-cluster/es03/config

注意这里要进入 es-cluster目类下执行,将子文件也赋予权限,否则写入日志会报错

chmod -R 777 es-cluster

报错:

[0.001s][error][logging] Error opening log file 'logs/gc.log': Permission denied
[0.001s][error][logging] Initialization of output 'file=logs/gc.log' using options 'filecount=32,filesize=64m' failed.
error:
Invalid -Xlog option '-Xlog:gc*,gc+age=trace,safepoint:file=logs/gc.log:utctime,pid,tags:filecount=32,filesize=64m', see error log for details.
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
        at org.elasticsearch.tools.launchers.JvmErgonomics.flagsFinal(JvmErgonomics.java:126)
        at org.elasticsearch.tools.launchers.JvmErgonomics.finalJvmOptions(JvmErgonomics.java:88)
        at org.elasticsearch.tools.launchers.JvmErgonomics.choose(JvmErgonomics.java:59)
        at org.elasticsearch.tools.launchers.JvmOptionsParser.jvmOptions(JvmOptionsParser.java:137)
        at org.elasticsearch.tools.launchers.JvmOptionsParser.main(JvmOptionsParser.java:95)

2. 在 es-cluster 下新建 docker-compose.yml 文件

version: "3"
services:
  elasticsearch01:
    image: elasticsearch:7.8.1
    restart: always
    container_name: es-cluster01
    environment:
      - TZ=Asia/Shanghai
      - cluster.name=es-cluster
      - bootstrap.memory_lock=true
      - node.name=node1
      - node.data=true
      - http.cors.enabled=true
      - http.cors.allow-origin=*
      - "ES_JAVA_OPTS=-Xms2048m -Xmx2048m"
      - discovery.seed_hosts=es-cluster01,es-cluster02,es-cluster03
      - cluster.initial_master_nodes=node1
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - /dockerfile/es-cluster/es01/plugins:/usr/share/elasticsearch/plugins
      - /dockerfile/es-cluster/es01/logs:/usr/share/elasticsearch/logs
      - /dockerfile/es-cluster/es01/data:/usr/share/elasticsearch/data
      - /dockerfile/es-cluster/es01/config/elasticsearch.yml:/usr/share/elasticsearch/data/config/elasticsearch.yml
    ports:
      - 9201:9200
    privileged: true

  elasticsearch02:
    image: elasticsearch:7.8.1
    restart: always
    container_name: es-cluster02
    environment:
      - TZ=Asia/Shanghai
      - cluster.name=es-cluster
      - bootstrap.memory_lock=true
      - node.name=node2
      - node.data=true
      - http.cors.enabled=true
      - http.cors.allow-origin=*
      - "ES_JAVA_OPTS=-Xms2048m -Xmx2048m"
      - discovery.seed_hosts=es-cluster01,es-cluster02,es-cluster03
      - cluster.initial_master_nodes=node1
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - /dockerfile/es-cluster/es02/plugins:/usr/share/elasticsearch/plugins
      - /dockerfile/es-cluster/es02/logs:/usr/share/elasticsearch/logs
      - /dockerfile/es-cluster/es02/data:/usr/share/elasticsearch/data
      - /dockerfile/es-cluster/es02/config/elasticsearch.yml:/usr/share/elasticsearch/data/config/elasticsearch.yml
    ports:
      - 9202:9200
    privileged: true

  elasticsearch03:
    image: elasticsearch:7.8.1
    restart: always
    container_name: es-cluster03
    environment:
      - TZ=Asia/Shanghai
      - cluster.name=es-cluster
      - bootstrap.memory_lock=true
      - node.name=node3
      - node.data=true
      - http.cors.enabled=true
      - http.cors.allow-origin=*
      - "ES_JAVA_OPTS=-Xms2048m -Xmx2048m"
      - "discovery.seed_hosts=es-cluster01,es-cluster02,es-cluster03"
      - cluster.initial_master_nodes=node1
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - /dockerfile/es-cluster/es03/plugins:/usr/share/elasticsearch/plugins
      - /dockerfile/es-cluster/es03/logs:/usr/share/elasticsearch/logs
      - /dockerfile/es-cluster/es03/data:/usr/share/elasticsearch/data
      - /dockerfile/es-cluster/es03/config/elasticsearch.yml:/usr/share/elasticsearch/data/config/elasticsearch.yml
    ports:
      - 9203:9200
    privileged: true

注意上面的jvm大小参数,两个参数值必须一样,否则启动会报错

ERROR: [1] bootstrap checks failed
[1]: initial heap size [1073741824] not equal to maximum heap size [2147483648]; this can cause resize pauses and prevents mlockall from locking the entire heap
ERROR: Elasticsearch did not exit normally - check the logs at /usr/share/elasticsearch/logs/es-cluster.log
  • 创建完成后用 docker-compose config -q 检查配置文件是否有错误

3. 在每个 config 文件夹下创建 elasticsearch.yml

cluster.name: "es-cluster"
network.host: 0.0.0.0
gateway.recover_after_nodes: 2
network.tcp.keep_alive: true
network.tcp.no_delay: true
transport.tcp.compress: true
#集群内同时启动的数据任务个数,默认是 2 个
cluster.routing.allocation.cluster_concurrent_rebalance: 16
#添加或删除节点及负载均衡时并发恢复的线程个数,默认 4 个
cluster.routing.allocation.node_concurrent_recoveries: 16
#初始化数据恢复时,并发恢复线程的个数,默认 4 个
cluster.routing.allocation.node_initial_primaries_recoveries: 16

http.cors.enabled: true #跨域配置
http.cors.allow-origin: "*"
xpack.security.enabled: true  #开启密码配置
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true

4. 在 es-cluster 文件夹下执行

docker-compose up -d

5. 浏览器访问

  • 虚拟机上需要关闭防火墙
  • 服务器上自己放开端口,和在控制台里面的安全组开放对应端口
#查看是否启动
http://ip:9201 
#查看节点
http://ip:9201/_cat/nodes

image-20231213215347822
image-20231213215416999