默认端口号
对外服务http 9200
对内集群tcp 9300

1. windows集群部署(了解)

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

image.png

  • 修改集群文件目录中每个节点的 config/elasticsearch.yml 配置文件

    • node-1001 节点

      1. # 节点 1 的配置信息:
      2. # 集群名称,节点之间要保持一致
      3. cluster.name: my-elasticsearch
      4. # 节点名称,集群内要唯一
      5. node.name: node-1001
      6. # 是不是有资格主节点
      7. node.master: true
      8. node.data: true
      9. # ip 地址
      10. network.host: localhost
      11. # http 端口
      12. http.port: 1001
      13. # tcp 监听端口
      14. transport.tcp.port: 9301
      15. # es7.x 之后新增的配置,节点发现
      16. #discovery.seed_hosts: ["localhost:9301", "localhost:9302","localhost:9303"]
      17. #discovery.zen.fd.ping_timeout: 1m
      18. #discovery.zen.fd.ping_retries: 5
      19. # 集群内的可以被选为主节点的节点列表
      20. #cluster.initial_master_nodes: ["node-1", "node-2","node-3"]
      21. # 跨域配置
      22. #action.destructive_requires_name: true
      23. http.cors.enabled: true
      24. http.cors.allow-origin: "*"
    • node-1002 节点

      1. # 节点 2 的配置信息:
      2. # 集群名称,节点之间要保持一致
      3. cluster.name: my-elasticsearch
      4. # 节点名称,集群内要唯一
      5. node.name: node-1002
      6. # 是不是有资格主节点
      7. node.master: true
      8. node.data: true
      9. # ip 地址
      10. network.host: localhost
      11. # http 端口
      12. http.port: 1002
      13. #tcp 监听端口
      14. transport.tcp.port: 9302
      15. #es7.x 之后新增的配置,节点发现
      16. discovery.seed_hosts: ["localhost:9301"]
      17. discovery.zen.fd.ping_timeout: 1m
      18. discovery.zen.fd.ping_retries: 5
      19. # 集群内的可以被选为主节点的节点列表
      20. #cluster.initial_master_nodes: ["node-1", "node-2","node-3"]
      21. # 跨域配置
      22. #action.destructive_requires_name: true
      23. http.cors.enabled: true
      24. http.cors.allow-origin: "*"
    • node-1003 节点

      1. # 节点 3 的配置信息:
      2. # 集群名称,节点之间要保持一致
      3. cluster.name: my-elasticsearch
      4. # 节点名称,集群内要唯一
      5. node.name: node-1003
      6. node.master: true
      7. node.data: true
      8. # ip 地址
      9. network.host: localhost
      10. # http 端口
      11. http.port: 1003
      12. # tcp 监听端口
      13. transport.tcp.port: 9303
      14. # 候选主节点的地址,在开启服务后可以被选为主节点
      15. discovery.seed_hosts: ["localhost:9301", "localhost:9302"]
      16. discovery.zen.fd.ping_timeout: 1m
      17. discovery.zen.fd.ping_retries: 5
      18. # 集群内的可以被选为主节点的节点列表
      19. #cluster.initial_master_nodes: ["node-1", "node-2","node-3"]
      20. # 跨域配置
      21. #action.destructive_requires_name: true
      22. http.cors.enabled: true
      23. http.cors.allow-origin: "*"
  • 启动集群

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

      image.png

    • 分别双击执行 bin/elasticsearch.bat, 启动节点服务器,启动后,会自动加入指定名称的集群

image.png

  • 测试集群
    • 查看集群状态
      • node-1001 节点

image.png

  1. - node-1002 节点

image.png

  1. - node-1003 节点

image.png

  1. - ![image.png](https://cdn.nlark.com/yuque/0/2021/png/668367/1627639200320-a6455d6d-384c-4515-85a5-a3536da120e1.png#crop=0&crop=0&crop=1&crop=1&height=153&id=doqfk&margin=%5Bobject%20Object%5D&name=image.png&originHeight=305&originWidth=985&originalType=binary&ratio=1&rotation=0&showTitle=false&size=76686&status=done&style=none&title=&width=492.5)
  • 向集群中的 node-1001 节点增加索引

image.png

  • 向集群中的 node-1002 节点查询索引

image.png

2. Linux单机部署(了解)

2.1 软件下载

软件下载地址:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-8-0

2.2 软件安装

2.2.1 解压软件

  1. # 解压缩
  2. tar -zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz -C /opt/module
  3. # 改名
  4. mv elasticsearch-7.8.0 es

2.2.2 创建用户

因为安全问题,Elasticsearch 不允许 root 用户直接运行,所以要创建新用户,在 root 用户中创建新用户

  1. useradd es #新增 es 用户
  2. passwd es #为 es 用户设置密码
  3. userdel -r es #如果错了,可以删除再加
  4. chown -R es:es /opt/module/es #文件夹所有者

2.2.3 修改配置文件

  • 修改 /opt/module/es/config/elasticsearch.yml 文件 ```yaml

    加入如下配置

    cluster.name: elasticsearch node.name: node-1 network.host: 0.0.0.0 http.port: 9200 cluster.initial_master_nodes: [“node-1”]

启动报错添加如下配置

bootstrap.memory_lock: false bootstrap.system_call_filter: false

  1. - 修改 `/etc/security/limits.conf`
  2. ```yaml
  3. # 在文件末尾中增加下面内容
  4. # 每个进程可以打开的文件数的限制
  5. es soft nofile 65536
  6. es hard nofile 65536
  • 修改 /etc/security/limits.d/90-nproc.conf ```yaml

    * soft nproc 1024

    root soft nproc unlimited

    每个进程可以打开的文件数的限制

    es soft nofile 65536 es hard nofile 65536

    操作系统级别对每个用户创建的进程数的限制

  • hard nproc 4096

    注:* 带表 Linux 所有用户名称

    ```
  • 修改 /etc/sysctl.conf

    1. # 在文件中增加下面内容
    2. # 一个进程可以拥有的 VMA(虚拟内存区域)的数量,默认值为 65536
    3. vm.max_map_count=655360
  • 重新加载

    1. sysctl -p

    2.3 启动软件

    使用 ES 用户启动

  1. cd /opt/module/es/
  2. #启动
  3. bin/elasticsearch
  4. #后台启动
  5. bin/elasticsearch -d
  • 启动时,会动态生成文件,如果文件所属用户不匹配,会发生错误,需要重新进行修改用户和用户组,重新执行以下命令

    1. chown -R es:es /opt/module/es #文件夹所有者
  • 启动时,如果不报错但启动失败,日志打印如下,可在 /opt/module/es/config/elasticsearch.yml 添加如下配置解决:

image.png

  1. # 启动报错添加如下配置
  2. bootstrap.memory_lock: false
  3. bootstrap.system_call_filter: false
  • 关闭防火墙
    1. # 暂时关闭防火墙
    2. systemctl stop firewalld
    3. # 永久关闭防火墙
    4. systemctl enable firewalld.service #打开防火墙永久性生效,重启后不会复原
    5. systemctl disable firewalld.service #关闭防火墙,永久性生效,重启后不会复原

    2.4 测试软件

    http://hadoop101:9200/
    image.png

    2.5 单机启动脚本

    ```shell

    !/bin/bash

    单机启动Elasticsearch

    es_path=/opt/module/es es_pid=ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}' case “$1” in start) su - es -c “$es_path/bin/elasticsearch -d” echo “elasticsearch startup” ;; stop) su - es -c “kill -15 $es_pid” echo “elasticsearch stopped” ;; restart) su - es -c “kill -15 $es_pid” su - es -c “$es_path/bin/elasticsearch -d” echo “elasticsearch startup” ;; *) echo “error choice ! please input start or stop or restart” ;; esac

exit $?

  1. <a name="dYrDr"></a>
  2. # 3. Linux集群部署(掌握)
  3. <a name="Uwkzb"></a>
  4. ## 3.1 软件下载
  5. > 软件下载地址:[https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-8-0](https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-8-0)
  6. <a name="kZxCE"></a>
  7. ## 3.2 软件安装
  8. <a name="m8DF9"></a>
  9. ### 3.2.1 解压软件
  10. > **解压后分发**
  11. ```bash
  12. # 解压缩
  13. tar -zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz -C /opt/module
  14. # 改名
  15. mv elasticsearch-7.8.0 es-cluster

3.2.2 创建用户

因为安全问题,Elasticsearch 不允许 root 用户直接运行,所以要在每个节点中创建新用户,在 root 用户中创建新用户

  1. useradd es #新增 es 用户
  2. passwd es #为 es 用户设置密码
  3. userdel -r es #如果错了,可以删除再加
  4. chown -R es:es /opt/module/es-cluster #文件夹所有者

3.2.3 修改配置文件

  • 修改 /opt/module/es/config/elasticsearch.yml 文件,分发文件,分发后去修改各节点下方标*配置** ```yaml

    加入如下配置

    集群名称

    cluster.name: cluster-es

    **节点名称,每个节点的名称不能重复**

    node.name: node-1

    **ip 地址,每个节点的地址不能重复**

    network.host: hadoop101

    是不是有资格主节点

    node.master: true

    是不是数据节点

    node.data: true

    http端口9200

    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 之后新增的配置,节点发现**

    **tcp监听端口9300**

    discovery.seed_hosts: [“hadoop101:9300”,”hadoop102:9300”,”hadoop103: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

启动报错添加如下配置

bootstrap.memory_lock: false bootstrap.system_call_filter: false

  1. - 修改 `/etc/security/limits.conf` ,**分发文件**
  2. ```yaml
  3. # 在文件末尾中增加下面内容
  4. es soft nofile 65536
  5. es hard nofile 65536
  • 修改 /etc/security/limits.d/90-nproc.conf分发文件 ```yaml

    * soft nproc 1024

    root soft nproc unlimited

    每个进程可以打开的文件数的限制

    es soft nofile 65536 es hard nofile 65536

    操作系统级别对每个用户创建的进程数的限制

  • hard nproc 4096

    注:* 带表 Linux 所有用户名称

    ```
  • 修改 /etc/sysctl.conf分发文件

    1. # 在文件中增加下面内容
    2. # 一个进程可以拥有的 VMA(虚拟内存区域)的数量,默认值为 65536
    3. vm.max_map_count=655360
  • 群发重新加载

    1. xcall.sh sysctl -p

    3.3 启动软件

    分别在不同节点上启动 ES 软件 启动时如果报错,参考:2.3 启动软件

  1. cd /opt/module/es-cluster
  2. # 启动
  3. bin/elasticsearch
  4. # 后台启动
  5. bin/elasticsearch -d

3.4 测试集群

IP:9200/_cat/nodes

image.png

IP:9200/_cluster/health

**status**字段只是这当前集群在总体上是否工作正常,他的三种颜色含义如下:
green 所有的主分片和副本分片都正常运行
yellow
所有的主分片都运行正常,但不是所有的副本分片都正常运行
red
有主分片没能正常运行

image.png

3.5 群起/停脚本

  1. #!/bin/bash
  2. # Elasticsearch集群 群起/停脚本
  3. es_path=/opt/module/es-cluster
  4. case $1 in
  5. "start"){
  6. for i in hadoop101 hadoop102 hadoop103
  7. do
  8. echo "************ $i ************"
  9. ssh $i "su - es -c 'nohup $es_path/bin/elasticsearch -d' 1>/dev/null 2>&1 &"
  10. done
  11. };;
  12. "stop"){
  13. for i in hadoop101 hadoop102 hadoop103
  14. do
  15. echo "************ $i ************"
  16. es_pid=`ssh $i ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
  17. ssh $i "su - es -c 'kill -15 $es_pid'"
  18. done
  19. };;
  20. esac

4. ES-Head浏览器插件