docker-compose搭建elasticsearch集群
创建集群目录
mkdir -p /usr/local/elasticsearch/config \ /usr/local/elasticsearch/plugins/ik \ /usr/local/elasticsearch/node-1 \ /usr/local/elasticsearch/node-2 \ /usr/local/elasticsearch/node-3
修改目录权限
chmod 777 -R /usr/local/elasticsearch
在config目录下创建elasticsearch.yml文件
touch /usr/local/elasticsearch/config/elasticsearch-1.yml \ /usr/local/elasticsearch/config/elasticsearch-2.yml \ /usr/local/elasticsearch/config/elasticsearch-3.yml
node1的elasticsearch.yml
cluster.name: es-cluster node.name: es-node-1 node.master: true node.data: true network.host: es-node-1 http.port: 9200 transport.tcp.port: 9300 http.cors.enabled: true http.cors.allow-origin: "*" discovery.zen.ping.unicast.hosts: ["es-node-1:9300", "es-node-3:9300", "es-node-3:9300"] discovery.zen.minimum_master_nodes: 2 discovery.zen.ping_timeout: 5s bootstrap.memory_lock: true action.destructive_requires_name: true cluster.initial_master_nodes: ["es-node-1"]
node2的elasticsearch.yml
cluster.name: es-cluster node.name: es-node-2 node.master: false node.data: true network.host: es-node-2 http.port: 9200 transport.tcp.port: 9300 http.cors.enabled: true http.cors.allow-origin: "*" discovery.zen.ping.unicast.hosts: ["es-node-1:9300", "es-node-3:9300", "es-node-3:9300"] discovery.zen.minimum_master_nodes: 2 discovery.zen.ping_timeout: 5s bootstrap.memory_lock: true action.destructive_requires_name: true cluster.initial_master_nodes: ["es-node-1"]
node3的elasticsearch.yml
cluster.name: es-cluster node.name: es-node-3 node.master: false node.data: true network.host: es-node-3 http.port: 9200 transport.tcp.port: 9300 http.cors.enabled: true http.cors.allow-origin: "*" discovery.zen.ping.unicast.hosts: ["es-node-1:9300", "es-node-3:9300", "es-node-3:9300"] discovery.zen.minimum_master_nodes: 2 discovery.zen.ping_timeout: 5s bootstrap.memory_lock: true action.destructive_requires_name: true cluster.initial_master_nodes: ["es-node-1"
kibana可视化工具
mkdir /usr/local/elasticsearch/kibana && touch /usr/local/elasticsearch/kibana/kibana.yml
内容:
#设置Kibana映射端口 server.port: 5601 #设置网关地址 server.host: "0.0.0.0" #设置Kibana实例对外展示的名称 server.name: "kibana" #设置ES集群地址 elasticsearch.hosts: ["http://es-node-1:9200","http://es-node-2:9200","http://es-node-3:9200"] #设置请求超时时长 elasticsearch.requestTimeout: 120000 #设置页面语言 i18n.locale: "zh-CN"
编写docker-compose.yml
vim /usr/local/elasticsearch/docker-compose.yml
内容如下:
version: "3" services: es-node-1: image: elasticsearch:7.9.2 container_name: es-node-1 environment: - "ES_JAVA_OPTS=-Xms256m -Xmx256m" ulimits: memlock: soft: -1 hard: -1 nofile: soft: 65536 hard: 65536 ports: - "9201:9200" volumes: - /usr/local/elasticsearch/config/elasticsearch-1.yml:/usr/share/elasticsearch/config/elasticsearch.yml - /usr/local/elasticsearch/node-1:/usr/share/elasticsearch/data - /usr/local/elasticsearch/plugins/ik:/usr/share/elasticsearch/plugins/ik networks: es_net: ipv4_address: 172.20.0.2 es-node-2: image: elasticsearch:7.9.2 container_name: es-node-2 environment: - "ES_JAVA_OPTS=-Xms256m -Xmx256m" ulimits: memlock: soft: -1 hard: -1 nofile: soft: 65536 hard: 65536 ports: - "9202:9200" volumes: - /usr/local/elasticsearch/config/elasticsearch-2.yml:/usr/share/elasticsearch/config/elasticsearch.yml - /usr/local/elasticsearch/node-2:/usr/share/elasticsearch/data - /usr/local/elasticsearch/plugins/ik:/usr/share/elasticsearch/plugins/ik networks: es_net: ipv4_address: 172.20.0.3 es-node-3: image: elasticsearch:7.9.2 container_name: es-node-3 environment: - "ES_JAVA_OPTS=-Xms256m -Xmx256m" ulimits: memlock: soft: -1 hard: -1 nofile: soft: 65536 hard: 65536 ports: - "9203:9200" volumes: - /usr/local/elasticsearch/config/elasticsearch-3.yml:/usr/share/elasticsearch/config/elasticsearch.yml - /usr/local/elasticsearch/node-3:/usr/share/elasticsearch/data - /usr/local/elasticsearch/plugins/ik:/usr/share/elasticsearch/plugins/ik networks: es_net: ipv4_address: 172.20.0.4 # 可视化工具 kibana: image: kibana:7.9.2 container_name: kibana ports: - 5601:5601 volumes: - /usr/local/elasticsearch/kibana/kibana.yml:/usr/share/kibana/config/kibana.yml networks: es_net: ipv4_address: 172.20.0.5 depends_on: - es-node-1 - es-node-2 - es-node-3 # 可视化工具 elasticsearch-head: image: alivv/elasticsearch-head:latest container_name: elasticsearch-head ports: - 9100:9100 networks: es_net: ipv4_address: 172.20.0.6 networks: es_net: driver: bridge ipam: config: - subnet: 172.20.0.0/16
docker-compose up -d
如果ES启动时出现异常max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
修改文件
vim /etc/sysctl.conf
添加参数
vm.max_map_count = 262144
sysctl -p