大数据学习初级入门教程(九) —— Elasticsearch 7.6.2 伪分布式集群的安装、配置、启动和测试
2020年04月04日 12:59:43 Elasticsearch ⁄ 共 13773字 暂无评论 ⁄ 被围观 3,044次

在前一篇文章《大数据学习初级入门教程(八) —— Elasticsearch 7.6.2 单节点的安装、启动和测试》中,简要说明了在一台测试服务器上如何安装单节点 Elasticsearch 并做了简单的测试,这篇主要说下在一台机器上如何配置多个节点,需要在前一篇文章操作后的基础上,搭建 Elasticsearch 伪分布式集群,这里配置 3 个代理节点。

特别注意:由于需要在一台机器上同时启动 3 个及以上 es 实例节点,所以建议机器内存>=4g。

第一步:复制节点

以上篇文章中搭建的节点为第一节点,再完全复制两个节点出来,并重命名。详细如下:

$ cd /opt/es/

$ cp -R elasticsearch-7.6.2 elasticsearch-7.6.2-2

$ cp -R elasticsearch-7.6.2 elasticsearch-7.6.2-3

第二步:修改配置

分别修改 elasticsearch-7.6.2-2 和 elasticsearch-7.6.2-2 实例下的配置文件,修改后三个节点的配置分别如下:

elasticsearch-7.6.2/config/elasticsearch.yml:

  1. # ======================== Elasticsearch Configuration =========================  
  2. #  
  3. # NOTE: Elasticsearch comes with reasonable defaults for most settings.  
  4. #       Before you set out to tweak and tune the configuration, make sure you  
  5. #       understand what are you trying to accomplish and the consequences.  
  6. #  
  7. # The primary way of configuring a node is via this file. This template lists  
  8. # the most important settings you may want to configure for a production cluster.  
  9. #  
  10. # Please consult the documentation for further information on configuration options:  
  11. # https://www.elastic.co/guide/en/elasticsearch/reference/index.html  
  12. #  
  13. # ---------------------------------- Cluster -----------------------------------  
  14. #  
  15. # Use a descriptive name for your cluster:  
  16. #  
  17. # 设置集群名称,集群内所有节点的名称必须一致  
  18. cluster.name: my-application  
  19. #  
  20. # ------------------------------------ Node ------------------------------------  
  21. #  
  22. # Use a descriptive name for the node:  
  23. #  
  24. # 设置节点名称,集群内节点名称必须唯一  
  25. node.name: node-1  
  26. #  
  27. # Add custom attributes to the node:  
  28. #  
  29. #node.attr.rack: r1  
  30. #  
  31. # 表示该节点会不会作为主节点,true表示会;false表示不会  
  32. node.master: true  
  33. # 当前节点是否用于存储数据,是:true、否:false  
  34. node.data: true  
  35. #  
  36. # ----------------------------------- Paths ------------------------------------  
  37. #  
  38. # Path to directory where to store the data (separate multiple locations by comma):  
  39. #  
  40. # 索引数据存放的位置  
  41. #path.data: /path/to/data  
  42. #  
  43. # Path to log files:  
  44. #  
  45. # 日志文件存放的位置  
  46. #path.logs: /path/to/logs  
  47. #  
  48. # ----------------------------------- Memory -----------------------------------  
  49. #  
  50. # Lock the memory on startup:  
  51. #  
  52. # 需要锁住物理内存,是:true、否:false  
  53. #bootstrap.memory_lock: true  
  54. # 系统调用过滤器检查,是:true、否:false  
  55. bootstrap.system_call_filter: false  
  56. #  
  57. # Make sure that the heap size is set to about half the memory available  
  58. # on the system and that the owner of the process is allowed to use this  
  59. # limit.  
  60. #  
  61. # Elasticsearch performs poorly when the system is swapping the memory.  
  62. #  
  63. # ---------------------------------- Network -----------------------------------  
  64. #  
  65. # Set the bind address to a specific IP (IPv4 or IPv6):  
  66. #  
  67. # 监听地址,用于访问该es  
  68. network.host: 192.168.220.242  
  69. #  
  70. # Set a custom port for HTTP:  
  71. #  
  72. # es对外提供的http端口,默认 9200  
  73. http.port: 9200  
  74. #  
  75. # For more information, consult the network module documentation.  
  76. #  
  77. # TCP的默认监听端口,默认 9300  
  78. transport.tcp.port: 9300  
  79. #  
  80. # 是否支持跨域,是:true,在使用head插件时需要此配置  
  81. http.cors.enabled: true  
  82. # “*” 表示支持所有域名  
  83. http.cors.allow-origin: "*"  
  84. #  
  85. # --------------------------------- Discovery ----------------------------------  
  86. #  
  87. # 设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点。默认为1,对于大的集群来说,可以设置大一点的值(2-4)  
  88. discovery.zen.minimum_master_nodes: 2  
  89. #  
  90. # Pass an initial list of hosts to perform discovery when this node is started:  
  91. # The default list of hosts is ["127.0.0.1", "[::1]"]  
  92. #  
  93. # es7.x 之后新增的配置,写入候选主节点的设备地址,在开启服务后可以被选为主节点  
  94. discovery.seed_hosts: ["192.168.220.242:9300""192.168.220.242:9301""192.168.220.242:9302"]  
  95. # 判断结点是否脱离时间配置  
  96. discovery.zen.fd.ping_timeout: 60s  
  97. # 判断结点是否脱离次数配置  
  98. discovery.zen.fd.ping_retries: 5  
  99. #  
  100. # Bootstrap the cluster using an initial set of master-eligible nodes:  
  101. #  
  102. # es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举master  
  103. cluster.initial_master_nodes: ["node-1""node-2""node-3"]  
  104. #  
  105. # For more information, consult the discovery and cluster formation module documentation.  
  106. #  
  107. # ---------------------------------- Gateway -----------------------------------  
  108. #  
  109. # Block initial recovery after a full cluster restart until N nodes are started:  
  110. #  
  111. #gateway.recover_after_nodes: 3  
  112. #  
  113. # For more information, consult the gateway module documentation.  
  114. #  
  115. # ---------------------------------- Various -----------------------------------  
  116. #  
  117. # Require explicit names when deleting indices:  
  118. #  
  119. #action.destructive_requires_name: true  

elasticsearch-7.6.2-2/config/elasticsearch.yml:

  1. # ======================== Elasticsearch Configuration =========================  
  2. #  
  3. # NOTE: Elasticsearch comes with reasonable defaults for most settings.  
  4. #       Before you set out to tweak and tune the configuration, make sure you  
  5. #       understand what are you trying to accomplish and the consequences.  
  6. #  
  7. # The primary way of configuring a node is via this file. This template lists  
  8. # the most important settings you may want to configure for a production cluster.  
  9. #  
  10. # Please consult the documentation for further information on configuration options:  
  11. # https://www.elastic.co/guide/en/elasticsearch/reference/index.html  
  12. #  
  13. # ---------------------------------- Cluster -----------------------------------  
  14. #  
  15. # Use a descriptive name for your cluster:  
  16. #  
  17. # 设置集群名称,集群内所有节点的名称必须一致  
  18. cluster.name: my-application  
  19. #  
  20. # ------------------------------------ Node ------------------------------------  
  21. #  
  22. # Use a descriptive name for the node:  
  23. #  
  24. # 设置节点名称,集群内节点名称必须唯一  
  25. node.name: node-2  
  26. #  
  27. # Add custom attributes to the node:  
  28. #  
  29. #node.attr.rack: r1  
  30. #  
  31. # 表示该节点会不会作为主节点,true表示会;false表示不会  
  32. node.master: true  
  33. # 当前节点是否用于存储数据,是:true、否:false  
  34. node.data: true  
  35. #  
  36. # ----------------------------------- Paths ------------------------------------  
  37. #  
  38. # Path to directory where to store the data (separate multiple locations by comma):  
  39. #  
  40. # 索引数据存放的位置  
  41. #path.data: /path/to/data  
  42. #  
  43. # Path to log files:  
  44. #  
  45. # 日志文件存放的位置  
  46. #path.logs: /path/to/logs  
  47. #  
  48. # ----------------------------------- Memory -----------------------------------  
  49. #  
  50. # Lock the memory on startup:  
  51. #  
  52. # 需要锁住物理内存,是:true、否:false  
  53. #bootstrap.memory_lock: true  
  54. # 系统调用过滤器检查,是:true、否:false  
  55. bootstrap.system_call_filter: false  
  56. #  
  57. # Make sure that the heap size is set to about half the memory available  
  58. # on the system and that the owner of the process is allowed to use this  
  59. # limit.  
  60. #  
  61. # Elasticsearch performs poorly when the system is swapping the memory.  
  62. #  
  63. # ---------------------------------- Network -----------------------------------  
  64. #  
  65. # Set the bind address to a specific IP (IPv4 or IPv6):  
  66. #  
  67. # 监听地址,用于访问该es  
  68. network.host: 192.168.220.242  
  69. #  
  70. # Set a custom port for HTTP:  
  71. #  
  72. # es对外提供的http端口,默认 9200  
  73. http.port: 9201  
  74. #  
  75. # For more information, consult the network module documentation.  
  76. #  
  77. # TCP的默认监听端口,默认 9300  
  78. transport.tcp.port: 9301  
  79. #  
  80. # 是否支持跨域,是:true,在使用head插件时需要此配置  
  81. http.cors.enabled: true  
  82. # “*” 表示支持所有域名  
  83. http.cors.allow-origin: "*"  
  84. #  
  85. # --------------------------------- Discovery ----------------------------------  
  86. #  
  87. # 设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点。默认为1,对于大的集群来说,可以设置大一点的值(2-4)  
  88. discovery.zen.minimum_master_nodes: 2  
  89. #  
  90. # Pass an initial list of hosts to perform discovery when this node is started:  
  91. # The default list of hosts is ["127.0.0.1", "[::1]"]  
  92. #  
  93. # es7.x 之后新增的配置,写入候选主节点的设备地址,在开启服务后可以被选为主节点  
  94. discovery.seed_hosts: ["192.168.220.242:9300""192.168.220.242:9301""192.168.220.242:9302"]  
  95. # 判断结点是否脱离时间配置  
  96. discovery.zen.fd.ping_timeout: 60s  
  97. # 判断结点是否脱离次数配置  
  98. discovery.zen.fd.ping_retries: 5  
  99. #  
  100. # Bootstrap the cluster using an initial set of master-eligible nodes:  
  101. #  
  102. # es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举master  
  103. cluster.initial_master_nodes: ["node-1""node-2""node-3"]  
  104. #  
  105. # For more information, consult the discovery and cluster formation module documentation.  
  106. #  
  107. # ---------------------------------- Gateway -----------------------------------  
  108. #  
  109. # Block initial recovery after a full cluster restart until N nodes are started:  
  110. #  
  111. #gateway.recover_after_nodes: 3  
  112. #  
  113. # For more information, consult the gateway module documentation.  
  114. #  
  115. # ---------------------------------- Various -----------------------------------  
  116. #  
  117. # Require explicit names when deleting indices:  
  118. #  
  119. #action.destructive_requires_name: true  

elasticsearch-7.6.2-3/config/elasticsearch.yml:

  1. # ======================== Elasticsearch Configuration =========================  
  2. #  
  3. # NOTE: Elasticsearch comes with reasonable defaults for most settings.  
  4. #       Before you set out to tweak and tune the configuration, make sure you  
  5. #       understand what are you trying to accomplish and the consequences.  
  6. #  
  7. # The primary way of configuring a node is via this file. This template lists  
  8. # the most important settings you may want to configure for a production cluster.  
  9. #  
  10. # Please consult the documentation for further information on configuration options:  
  11. # https://www.elastic.co/guide/en/elasticsearch/reference/index.html  
  12. #  
  13. # ---------------------------------- Cluster -----------------------------------  
  14. #  
  15. # Use a descriptive name for your cluster:  
  16. #  
  17. # 设置集群名称,集群内所有节点的名称必须一致  
  18. cluster.name: my-application  
  19. #  
  20. # ------------------------------------ Node ------------------------------------  
  21. #  
  22. # Use a descriptive name for the node:  
  23. #  
  24. # 设置节点名称,集群内节点名称必须唯一  
  25. node.name: node-3  
  26. #  
  27. # Add custom attributes to the node:  
  28. #  
  29. #node.attr.rack: r1  
  30. #  
  31. # 表示该节点会不会作为主节点,true表示会;false表示不会  
  32. node.master: true  
  33. # 当前节点是否用于存储数据,是:true、否:false  
  34. node.data: true  
  35. #  
  36. # ----------------------------------- Paths ------------------------------------  
  37. #  
  38. # Path to directory where to store the data (separate multiple locations by comma):  
  39. #  
  40. # 索引数据存放的位置  
  41. #path.data: /path/to/data  
  42. #  
  43. # Path to log files:  
  44. #  
  45. # 日志文件存放的位置  
  46. #path.logs: /path/to/logs  
  47. #  
  48. # ----------------------------------- Memory -----------------------------------  
  49. #  
  50. # Lock the memory on startup:  
  51. #  
  52. # 需要锁住物理内存,是:true、否:false  
  53. #bootstrap.memory_lock: true  
  54. # 系统调用过滤器检查,是:true、否:false  
  55. bootstrap.system_call_filter: false  
  56. #  
  57. # Make sure that the heap size is set to about half the memory available  
  58. # on the system and that the owner of the process is allowed to use this  
  59. # limit.  
  60. #  
  61. # Elasticsearch performs poorly when the system is swapping the memory.  
  62. #  
  63. # ---------------------------------- Network -----------------------------------  
  64. #  
  65. # Set the bind address to a specific IP (IPv4 or IPv6):  
  66. #  
  67. # 监听地址,用于访问该es  
  68. network.host: 192.168.220.242  
  69. #  
  70. # Set a custom port for HTTP:  
  71. #  
  72. # es对外提供的http端口,默认 9200  
  73. http.port: 9202  
  74. #  
  75. # For more information, consult the network module documentation.  
  76. #  
  77. # TCP的默认监听端口,默认 9300  
  78. transport.tcp.port: 9302  
  79. #  
  80. # 是否支持跨域,是:true,在使用head插件时需要此配置  
  81. http.cors.enabled: true  
  82. # “*” 表示支持所有域名  
  83. http.cors.allow-origin: "*"  
  84. #  
  85. # --------------------------------- Discovery ----------------------------------  
  86. #  
  87. # 设置这个参数来保证集群中的节点可以知道其它N个有master资格的节点。默认为1,对于大的集群来说,可以设置大一点的值(2-4)  
  88. discovery.zen.minimum_master_nodes: 2  
  89. #  
  90. # Pass an initial list of hosts to perform discovery when this node is started:  
  91. # The default list of hosts is ["127.0.0.1", "[::1]"]  
  92. #  
  93. # es7.x 之后新增的配置,写入候选主节点的设备地址,在开启服务后可以被选为主节点  
  94. discovery.seed_hosts: ["192.168.220.242:9300""192.168.220.242:9301""192.168.220.242:9302"]  
  95. # 判断结点是否脱离时间配置  
  96. discovery.zen.fd.ping_timeout: 60s  
  97. # 判断结点是否脱离次数配置  
  98. discovery.zen.fd.ping_retries: 5  
  99. #  
  100. # Bootstrap the cluster using an initial set of master-eligible nodes:  
  101. #  
  102. # es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举master  
  103. cluster.initial_master_nodes: ["node-1""node-2""node-3"]  
  104. #  
  105. # For more information, consult the discovery and cluster formation module documentation.  
  106. #  
  107. # ---------------------------------- Gateway -----------------------------------  
  108. #  
  109. # Block initial recovery after a full cluster restart until N nodes are started:  
  110. #  
  111. #gateway.recover_after_nodes: 3  
  112. #  
  113. # For more information, consult the gateway module documentation.  
  114. #  
  115. # ---------------------------------- Various -----------------------------------  
  116. #  
  117. # Require explicit names when deleting indices:  
  118. #  
  119. #action.destructive_requires_name: true  

第三步:启动集群

依次启动 3 个 es 节点实例:

./bin/elasticsearch

如果启动中发生如下错误,说明机器内存配置不足:

[elastic@test242 elasticsearch-7.6.2-2]$ ./bin/elasticsearch
Exception in thread "main" java.lang.RuntimeException: starting java failed with [1]
output:
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory.
# An error report file with more information is saved as:
# logs/hs_err_pid4427.log
error:
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Not enough space' (errno=12)
        at org.elasticsearch.tools.launchers.JvmErgonomics.flagsFinal(JvmErgonomics.java:123)
        at org.elasticsearch.tools.launchers.JvmErgonomics.finalJvmOptions(JvmErgonomics.java:88)
        at org.elasticsearch.tools.launchers.JvmErgonomics.choose(JvmErgonomics.java:59)
        at org.elasticsearch.tools.launchers.JvmOptionsParser.main(JvmOptionsParser.java:95)

解决方法:

第一种,增加机器内存配置。

第二种,修改配置文件elasticsearch-7.6.2/config/jvm.options 中的配置,调小内存需求。详细如下:

# Xms represents the initial size of total heap space
# Xmx represents the maximum size of total heap space

-Xms512m
-Xmx512m

如果启动中出现如下错误,信息如下:

org.elasticsearch.cluster.coordination.CoordinationStateRejectedException: not enough nodes discovered to form a quorum in the initial configuration

可以看到三个节点都报这个错误,这就是所谓的“脑裂”。脑裂是指在主备切换时,由于切换不彻底或其他原因,导致客户端和 Slave 误以为出现两个 active master,最终使得整个集群处于混乱状态。

discovery.zen.minimum_master_nodes 配置的数量如何才为最优?自动发现 master 节点的最小数,如果这个集群中配置进来的 master 节点少于这个数目,es 的日志会一直报 master 节点数目不足。(默认为1)为了避免脑裂,个数请遵从该公式 =》 total number of master - eligible nodes / 2 + 1,可以尝试修改该值为1。

再次启动集群,可以看到 3 个节点均已正常启动,无报错。通过浏览访问地址:http://192.168.220.242:9200/_cat/nodes,可以看到集群运行节点信息,如图:

备注:带 * 的为 master节点,即当前 node-2 节点为 master 节点。

到此,单节点的安装和伪分布式集群的安装都已介绍完毕,常见的一些错误也都提供了解决方法。

后续搭建完全分布式集群,还是这些步骤,还是这些报错,也就是把一台机器上的三个实例,拆分到了三台不同的机器,所以参考这两篇,即可提前尝试完成完全分布式 ES 集群的搭建。

原文链接:https://blog.csdn.net/tzhuwb/article/details/105306649

给我留言

留言无头像?