相关概念

单台 Elasticsearch 服务器提供服务,往往都有最大的负载能力,超过这个阈值,服务器性能就会大大降低甚至不可用,所以生产环境中,一般都是运行在指定服务器集群中。

单机

除了负载能力,单点服务器也存在其他问题:

  • 单台机器存储容量有限
  • 单服务器容易出现单点故障,无法实现高可用
  • 单服务的并发处理能力有限

集群 Cluster

一个集群就是由一个或多个服务器节点组织在一起,共同持有整个的数据,并一起提供索引和搜索功能。一个 Elasticsearch 集群有一个唯一的名字标识,这个名字默认就是”elasticsearch”。这个名字是重要的,因为一个节点只能通过指定某个集群的名字,来加入这个集群。

  • 配置服务器集群时,集群中节点数量没有限制,大于等于 2 个节点就可以看做是集群了。
  • 一般出于高性能及高可用方面来考虑集群中节点数量都是 3 个以上。

节点 Node

集群中包含很多服务器,一个节点就是其中的一个服务器。作为集群的一部分,它存储数据,参与集群的索引和搜索功能

  • 一个节点也是由一个名字来标识的,默认情况下,这个名字是一个随机的漫威漫画角色的名字,这个名字会在启动的时候赋予节点。这个名字对于管理工作来说挺重要的,因为在这个管理过程中,你会去确定网络中的哪些服务器对应于 Elasticsearch 集群中的哪些节点。

  • 一个节点可以通过配置集群名称的方式来加入一个指定的集群。默认情况下,每个节点都会被安排加入到一个叫做“elasticsearch”的集群中,这意味着,如果你在你的网络中启动了若干个节点,并假定它们能够相互发现彼此,它们将会自动地形成并加入到一个叫做“elasticsearch”的集群中。

  • 在一个集群里,只要你想,可以拥有任意多个节点。而且,如果当前你的网络中没有运行任何 Elasticsearch 节点,这时启动一个节点,会默认创建并加入一个叫做“elasticsearch”的集群。

Windows 集群

部署集群

  1. 创建 elasticsearch-cluster 文件夹,在内部复制三个 elasticsearch 服务

    1
    2
    3
    4
    PS D:\Program\dev> mkdir elasticsearch-cluster
    PS D:\Program\dev> cp -r .\elasticsearch-7.8.0\ .\elasticsearch-cluster\node-9001
    PS D:\Program\dev> cp -r .\elasticsearch-7.8.0\ .\elasticsearch-cluster\node-9002
    PS D:\Program\dev> cp -r .\elasticsearch-7.8.0\ .\elasticsearch-cluster\node-9003
  2. 修改集群文件目录中每个节点的 config/elasticsearch.yml 配置文件

    • node-1001 节点

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      #节点 1 的配置信息:
      #集群名称,节点之间要保持一致
      cluster.name: my-elasticsearch
      # 节 点 名 称 , 集 群 内 要 唯 一
      node.name: node-9001
      node.master: true
      node.data: true

      #ip 地址
      network.host: localhost
      #http 端口
      http.port: 9001
      #tcp 监听端口
      transport.tcp.port: 9301

      #discovery.seed_hosts: ["localhost:9301", "localhost:9302","localhost:9303"]
      #discovery.zen.fd.ping_timeout: 1m
      #discovery.zen.fd.ping_retries: 5

      # 集 群 内 的 可 以 被 选 为 主 节 点 的 节 点 列 表
      #cluster.initial_master_nodes: ["node-1", "node-2","node-3"]

      #跨域配置
      #action.destructive_requires_name: true
      http.cors.enabled: true
      http.cors.allow-origin: "*"
    • node-1002 节点

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      #节点 2 的配置信息:
      #集群名称,节点之间要保持一致
      cluster.name: my-elasticsearch
      # 节 点 名 称 , 集 群 内 要 唯 一
      node.name: node-9002
      node.master: true
      node.data: true

      #ip 地址
      network.host: localhost
      #http 端口
      http.port: 9002
      #tcp 监听端口
      transport.tcp.port: 9302

      discovery.seed_hosts: ["localhost:9301"]
      discovery.zen.fd.ping_timeout: 1m
      discovery.zen.fd.ping_retries: 5

      # 集 群 内 的 可 以 被 选 为 主 节 点 的 节 点 列 表
      #cluster.initial_master_nodes: ["node-1", "node-2","node-3"]

      #跨域配置
      #action.destructive_requires_name: true
      http.cors.enabled: true
      http.cors.allow-origin: "*"
    • node-1003 节点

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      #节点 3 的配置信息:
      #集群名称,节点之间要保持一致
      cluster.name: my-elasticsearch
      # 节 点 名 称 , 集 群 内 要 唯 一
      node.name: node-9002
      node.master: true
      node.data: true

      #ip 地址
      network.host: localhost
      #http 端口
      http.port: 9003
      #tcp 监听端口
      transport.tcp.port: 9303

      #候选主节点的地址,在开启服务后可以被选为主节点
      discovery.seed_hosts: ["localhost:9301", "localhost:9302"]
      discovery.zen.fd.ping_timeout: 1m
      discovery.zen.fd.ping_retries: 5

      # 集 群 内 的 可 以 被 选 为 主 节 点 的 节 点 列 表
      #cluster.initial_master_nodes: ["node-1", "node-2","node-3"]

      #跨域配置
      #action.destructive_requires_name: true
      http.cors.enabled: true
      http.cors.allow-origin: "*"

启动集群

  • 启动前先删除每个节点中的 data 目录中所有内容(如果存在)

    1
    2
    3
    PS D:\Program\dev\elasticsearch-cluster> rm -r .\node-1001\data\nodes\
    PS D:\Program\dev\elasticsearch-cluster> rm -r .\node-1002\data\nodes\
    PS D:\Program\dev\elasticsearch-cluster> rm -r .\node-1003\data\nodes\
  • 分别双击执行 bin/elasticsearch.bat, 启动节点服务器,启动后,会自动加入指定名称的集群

    1
    2
    3
    .\node-1001\bin\elasticsearch.bat
    .\node-1002\bin\elasticsearch.bat
    .\node-1003\bin\elasticsearch.bat

测试集群

  • 启动 node1 节点,查看集群状态

    • status: 字段标识当前集群在总体上是否正常工作,有三种颜色
    • green: 所有主分片和副本分片都可以正常工作
    • yello: 所有的主分片都正常工作,但不是所有的副本分片都正常工作
    • red: 有主分片没有正常运行
  • 启动 node-9001 节点

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    Get http://127.0.0.1:9001/_cluster/health

    {
    "cluster_name": "my-elasticsearch",
    "status": "green",
    "timed_out": false,
    "number_of_nodes": 1,
    "number_of_data_nodes": 1,
    "active_primary_shards": 0,
    "active_shards": 0,
    "relocating_shards": 0,
    "initializing_shards": 0,
    "unassigned_shards": 0,
    "delayed_unassigned_shards": 0,
    "number_of_pending_tasks": 0,
    "number_of_in_flight_fetch": 0,
    "task_max_waiting_in_queue_millis": 0,
    "active_shards_percent_as_number": 100.0
    }
  • 启动 node-9002 节点

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    Get http://127.0.0.1:9002/_cluster/health

    {
    "cluster_name": "my-elasticsearch",
    "status": "green",
    "timed_out": false,
    "number_of_nodes": 2,
    "number_of_data_nodes": 2,
    "active_primary_shards": 0,
    "active_shards": 0,
    "relocating_shards": 0,
    "initializing_shards": 0,
    "unassigned_shards": 0,
    "delayed_unassigned_shards": 0,
    "number_of_pending_tasks": 0,
    "number_of_in_flight_fetch": 0,
    "task_max_waiting_in_queue_millis": 0,
    "active_shards_percent_as_number": 100.0
    }
  • 启动 node-9003 节点

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    Get http://127.0.0.1:9003/_cluster/health

    {
    "cluster_name": "my-elasticsearch",
    "status": "green",
    "timed_out": false,
    "number_of_nodes": 3,
    "number_of_data_nodes": 3,
    "active_primary_shards": 0,
    "active_shards": 0,
    "relocating_shards": 0,
    "initializing_shards": 0,
    "unassigned_shards": 0,
    "delayed_unassigned_shards": 0,
    "number_of_pending_tasks": 0,
    "number_of_in_flight_fetch": 0,
    "task_max_waiting_in_queue_millis": 0,
    "active_shards_percent_as_number": 100.0
    }
  • 向集群中的 node-1001 节点增加索引

    1
    2
    3
    4
    5
    6
    7
    PUT http://127.0.0.1:9001/user

    {
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "user"
    }
  • 向集群中的 node-1002 节点查询索引

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    Get http://127.0.0.1:9002/user

    {
    "user": {
    "aliases": {},
    "mappings": {},
    "settings": {
    "index": {
    "creation_date": "1637466026624",
    "number_of_shards": "1",
    "number_of_replicas": "1",
    "uuid": "Ho7_z64XQB-MUWAOAyzSPA",
    "version": {
    "created": "7080099"
    },
    "provided_name": "user"
    }
    }
    }
    }

Linux 集群

软件下载

软件安装

  1. 将下载的软件解压缩

    1
    2
    3
    4
    5
    6
    # 创建目录
    mkdir /opt/module
    # 解压缩
    tar -zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz -C /opt/module
    # 改名
    mv elasticsearch-7.8.0 es-cluster
  2. 将软件分发到其他节点:zy02, zy03

    1
    2
    sudo scp -r /opt/module/es-cluster/ root@zy02:/opt/module/es-cluster
    sudo scp -r /opt/module/es-cluster/ root@zy03:/opt/module/es-cluster
  3. 因为安全问题,Elasticsearch 不允许 root 用户直接运行,所以要在每个节点中创建新用户,在 root 用户中创建新用户

    1
    2
    3
    4
    5
    useradd es #新增 es 用户
    passwd es #为 es 用户设置密码
    userdel -r es #如果错了,可以删除再加

    chown -R es:es /opt/module/es-cluster #文件夹所有者

修改配置文件

  1. 修改 vim /opt/module/es-cluster/config/elasticsearch.yml 文件

    • 加入如下配置

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      # ctrl+v, 进入列编辑模式,横向选中列的个数(如"//"注释符号,需要选中两列), 然后按 d, 就会删除注释符号
      #集群名称
      cluster.name: cluster-es
      #节点名称,每个节点的名称不能重复
      node.name: node-1
      #ip 地址,每个节点的地址不能重复
      network.host: zy01
      #是不是有资格主节点
      node.master: true
      node.data: true
      http.port: 9200
      # head 插件需要这打开这两个配置
      http.cors.allow-origin: "*"
      http.cors.enabled: true
      http.max_content_length: 200mb
      #es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举 master
      cluster.initial_master_nodes: ["node-1"]
      #es7.x 之后新增的配置,节点发现
      discovery.seed_hosts: ["zy01:9300","zy02:9300","zy03:9300"]
      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
    • 分发文件,修改节点名称和 IP 地址

      1
      2
      sudo scp -r /opt/module/es-cluster/config/elasticsearch.yml root@zy02:/opt/module/es-cluster/config/elasticsearch.yml
      sudo scp -r /opt/module/es-cluster/config/elasticsearch.yml root@zy03:/opt/module/es-cluster/config/elasticsearch.yml
  2. 修改sudo vim /etc/security/limits.conf

    • 在文件末尾中增加下面内容

      1
      2
      3
      4
      5
      6
      7
      # 每个进程可以打开的文件数的限制
      es soft nofile 65536
      es hard nofile 65536
      # 操作系统级别对每个用户创建的进程数的限制
      # 注:* 带表 Linux 所有用户名称
      * hard nproc 4096
      * soft nproc 4096
    • 并分发文件

      1
      2
      sudo scp -r /etc/security/limits.conf root@zy02:/etc/security/limits.conf
      sudo scp -r /etc/security/limits.conf root@zy03:/etc/security/limits.conf
  3. 修改/etc/sysctl.conf

    • 在文件中增加下面内容

      1
      2
      # 一个进程可以拥有的 VMA(虚拟内存区域)的数量,默认值为 65536
      vm.max_map_count=655360
    • 并分发文件

      1
      2
      sudo scp -r /etc/sysctl.conf root@zy02:/etc/sysctl.conf
      sudo scp -r /etc/sysctl.conf root@zy03:/etc/sysctl.conf
    • 重新加载

      1
      sysctl -p

启动软件

  • 分别在不同节点上启动 ES 软件

    1
    2
    3
    4
    5
    6
    7
    8
    # 删除之前的数据
    rm -rf /opt/module/es-cluster/data/nodes/
    # 切换用户
    su es
    #启动
    /opt/module/es-cluster/bin/elasticsearch
    #后台启动
    /opt/module/es-cluster/bin/elasticsearch -d
  • 测试集群

    • _cat
    1
    2
    3
    4
    5
    6
    Get http://zy01:9200/_cat/nodes
    # Get http://zy01:9200//_cat/master

    192.168.61.45 9 92 52 1.81 0.94 0.42 dilmrt * node-1
    192.168.61.47 11 92 45 1.27 1.15 0.64 dilmrt - node-3
    192.168.61.46 10 92 44 1.63 0.74 0.29 dilmrt - node-2