使用docker构建ElasticSearch集群

2024-04-17 09:28:03 浏览数 (1)

# 一、准备工作
# 1、拉取ElasticSearch镜像
代码语言:javascript复制
docker pull elasticsearch:7.14.1
docker tag docker.io/elasticsearch:7.14.1 elasticsearch
docker rmi docker.io/elasticsearch:7.14.1
# 2、创建相关文件夹,用于存放配置文件
代码语言:javascript复制
mkdir -p /data//docker-compose/elasticsearch
mkdir -p /data/docker-data/elasticsearch/101/config
mkdir -p /data/docker-data/elasticsearch/102/config
mkdir -p /data/docker-data/elasticsearch/103/config
# 3、赋予文件夹权限
代码语言:javascript复制
chmod -R 777 /data/docker-data/elasticsearch

ElasticSearch对共享目录的文件夹权限有要求,因此这里为共享目录设置权限

# 4、将当前用户的内存权限变大
代码语言:javascript复制
查看当前用户的内存权限
sysctl -a|grep vm.max_map_count
# 显示 vm.max_map_count = 65530

elasticsearch要求该值至少是262144,否则会报错,max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

代码语言:javascript复制
# 将用户拥有的内存权限增大
sysctl -w vm.max_map_count=262144
# 二、编写docker-compose.yml
代码语言:javascript复制
cd /data/docker-compose/elasticsearch
vi docker-compose.yml

version: '3.6'
services:
  es_101:
    image: elasticsearch
    container_name: es_101
    hostname: es_101
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    volumes:
      - /data/docker-data/elasticsearch/101/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /data/docker-data/elasticsearch/101/data:/usr/share/elasticsearch/data
    privileged: true
    ports:
      - 9201:9200
      - 9301:9300

  es_102:
    image: elasticsearch
    container_name: es_102
    hostname: es_102
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    volumes:
      - /data/docker-data/elasticsearch/102/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /data/docker-data/elasticsearch/102/data:/usr/share/elasticsearch/data
    privileged: true
    ports:
      - 9202:9200
      - 9302:9300

  es_103:
    image: elasticsearch
    container_name: es_103
    hostname: es_103
    environment:
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    volumes:
      - /data/docker-data/elasticsearch/103/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
      - /data/docker-data/elasticsearch/103/data:/usr/share/elasticsearch/data
    privileged: true
    ports:
      - 9203:9200
      - 9303:9300
# 三、编写ElasticSearch和kibana的配置文件
代码语言:javascript复制
cd /data/docker-data/elasticsearch/101/config
vi elasticsearch.yml

cluster.name: es_cluster
node.name: es_101
node.master: true
node.data: true
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
discovery.zen.ping.unicast.hosts: ["es_101","es_102","es_103"]
discovery.zen.minimum_master_nodes: 1
cluster.initial_master_nodes: es_101
http.cors.enabled: true
http.cors.allow-origin: "*"
代码语言:javascript复制
cd /data/docker-data/elasticsearch/102/config
vi elasticsearch.yml

cluster.name: es_cluster
node.name: es_102
node.master: true
node.data: true
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.unicast.hosts: ["es_101","es_102","es_103"]
http.cors.enabled: true
http.cors.allow-origin: "*"
代码语言:javascript复制
cd /data/docker-data/elasticsearch/103/config
vi elasticsearch.yml

cluster.name: es_cluster
node.name: es_103
node.master: true
node.data: true
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.unicast.hosts: ["es_101","es_102","es_103"]
http.cors.enabled: true
http.cors.allow-origin: "*"
# 四、执行构建ElasticSearch集群
# 1、开始构建
代码语言:javascript复制
cd /data/docker-compose/elasticsearch
docker-compose up -d

此时,因为没有授予共享目录文件夹权限,因此es集群其实是启动不成功的

# 2、授予文件夹权限
代码语言:javascript复制
chmod -R 777 /data/docker-data/elasticsearch/
# 3、重新执行构建
代码语言:javascript复制
cd /data/docker-compose/elasticsearch
docker-compose restart
# 五、验证结果:

宿主机:192.168.78.200

访问192.168.78.200:9201 或 192.168.78.200:9202 或 192.168.78.200:9203

返回下面结果:

代码语言:javascript复制
// 20210927041419
// http://192.168.78.200:9201/

{
  "name": "es_101",
  "cluster_name": "es_cluster",
  "cluster_uuid": "_KgoQSikS6StSHpBK4Yr0Q",
  "version": {
    "number": "7.14.1",
    "build_flavor": "default",
    "build_type": "docker",
    "build_hash": "66b55ebfa59c92c15db3f69a335d500018b3331e",
    "build_date": "2021-08-26T09:01:05.390870785Z",
    "build_snapshot": false,
    "lucene_version": "8.9.0",
    "minimum_wire_compatibility_version": "6.8.0",
    "minimum_index_compatibility_version": "6.0.0-beta1"
  },
  "tagline": "You Know, for Search"
}

说明集群搭建成功了。

# 六、ElasticSearch可视化工具

详见文章:elasticsearch可视化工具介绍

0 人点赞