创建目录
[root@192 opt]# mkdir -m 777 -p /opt/es/config
[root@192 es]# mkdir -m 777 -p  /opt/es/data1/plugin
[root@192 es]# mkdir -m 777 -p  /opt/es/data2/plugin
[root@192 es]# mkdir -m 777 -p  /opt/es/data3/plugin

使用tree命令查看目录结构:

#安装
yum -y install tree
#设置编码
alias tree='tree --charset ASCII'
#查看结构
tree /opt/es/
-------------------
/opt/es/
|-- config
|   |-- es1.yml
|   |-- es2.yml
|   `-- es3.yml
|-- data1
|   `-- plugin
|-- data2
|   `-- plugin
|-- data3
|   `-- plugin
`-- docker-compose.yml
1、config下为es各个节点的配置文件
2、docker-compose.yml为启动文件
3、三个data目录会挂载到容器的/usr/share/elasticsearch/data目录
4、plugin目录用于插件的安装,需要安装什么插件,上传到该目录即可
启动

docker-compose up -d

启动时无法挂载目录

一般是因为权限不够,可以为es下的文件夹赋予读写权限,因为es会在这些目录中写数据。

如果赋予权限也无法解决问题,尝试:

#启动前执行
setenforce 0
#启动完成后执行
setenforce 1
配置文件

es1.yml、es2.yml、es3.yml

es1.yml:es2.yml、es3.yml修改其中的node.name为es-node2和es-node3即可。

cluster.name: elasticsearch-cluster
node.name: es-node1
network.bind_host: 0.0.0.0
network.publish_host: _eth0_
http.port: 9200
transport.tcp.port: 9300
http.cors.enabled: true
http.cors.allow-origin: "*"
node.master: true
node.data: true
discovery.seed_hosts: ["ES01:9300","ES02:9301","ES03:9302"]
cluster.initial_master_nodes: ["es-node1","es-node2","es-node3"]
discovery.zen.minimum_master_nodes: 1

docker-compose.yml

version: '3.7'
services:
  es1:
    image: docker.io/elasticsearch:7.4.0
    container_name: ES01
    environment:
      - ES_JAVA_OPTS=-Xms256m -Xmx256m
    ports:
      - "9200:9200"
      - "9300:9300"
    volumes:
      - /opt/es/config/es1.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /opt/es/data1:/usr/share/elasticsearch/data
      - /opt/es/data1/plugin:/usr/share/elasticsearch/plugins
    networks:
      - es-net
  es2:
    image: docker.io/elasticsearch:7.4.0
    container_name: ES02
    environment:
      - ES_JAVA_OPTS=-Xms256m -Xmx256m
    ports:
      - "9201:9200"
      - "9301:9300"
    volumes:
      - /opt/es/config/es2.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /opt/es/data2:/usr/share/elasticsearch/data
      - /opt/es/data2/plugin:/usr/share/elasticsearch/plugins
    networks:
      - es-net
  es3:
    image: docker.io/elasticsearch:7.4.0
    container_name: ES03
    environment:
      - ES_JAVA_OPTS=-Xms256m -Xmx256m
    ports:
      - "9202:9200"
      - "9302:9300"
    volumes:
      - /opt/es/config/es3.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /opt/es/data3:/usr/share/elasticsearch/data
      - /opt/es/data3/plugin:/usr/share/elasticsearch/plugins
    networks:
      - es-net
  es-head:
    image: tobias74/elasticsearch-head
    ports:
      - "9100:9100"

networks:
  es-net:
    driver: bridge