elasticsearch 部署以及相关重要配置

1,033 阅读11分钟

系统环境为: ubuntu 16 server

注:想要测试同一台机器上配置两个节点并放在同一个集群中,如果只是想在一台机器上配置一个节点,方法更简单一些

1.java 环境部署

下载java jdk安装

jdk版本
解压后,放到目标目录下,配置环境变量: vim ~/.bashrc 在文件最后添加

export JAVA_HOME='/opt/jdk1.8.0_131'
export CLASSPATH=${JAVA_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH                             

执行source ~/.bashrc生效 检测是否安装成功java -version

2.下载elasticsearch

curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.1-linux-x86_64.tar.gz 解压软件包 tar -xvf elasticsearch-7.6.1-linux-x86_64.tar.gz 如果本地部署单个实例的集群,直接启动即可

cd elasticsearch-7.6.1/bin
./elasticsearch

3. 配置两个节点

Elasticsearch具有良好的默认设置,并且只需要很少的配置。可以使用群集更新设置API(PUT /_cluster/settings)在正在运行的群集上更改大多数设置。配置文件应包含特定于节点的设置(例如node.name),或节点为了能够加入集群而需要的设置(例如cluster.name或者network.host)。 修改node-1的配置文件/opt/elasticsearch6/config

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: search-controler
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /elastic/data
#
# Path to log files:
#
path.logs: /elastic/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.9.128
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#
transport.tcp.port: 9300
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.zen.ping.unicast.hosts: ["192.168.9.128", "192.168.9.128:9301"]

节点2的配置文件修改为:

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: search-controler
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-2
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /elastic/data-node2
#
# Path to log files:
#
path.logs: /elastic/logs-node2
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.9.128
#
# Set a custom port for HTTP:
#
http.port: 9201
#
# For more information, consult the network module documentation.
#
transport.tcp.port: 9301
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.zen.ping.unicast.hosts: ["192.168.9.128:9300", "192.168.9.128:9301"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes: 3
#
# For more information, consult the zen discovery module documentation.
#

介绍一下几个比较重要的配置项

  • path.datapath.logs: 如果使用的是.zip.tar.gz归档文件,则datalogs目录是$ES_HOME的子文件夹。 如果这些重要文件夹保留在其默认位置,则在将Elasticsearch升级到新版本时,很有可能将其删除。 在生产中使用时,几乎可以肯定要更改data和log文件夹的位置:
path:
  logs: /var/log/elasticsearch
  data: /var/data/elasticsearch

可以将path.data设置设置为多个路径,在这种情况下,所有路径都将用于存储数据(尽管属于单个分片的文件将全部存储在同一数据路径上):

path:
  data:
    - /mnt/elasticsearch_1
    - /mnt/elasticsearch_2
    - /mnt/elasticsearch_3
  • cluster.name: 一个节点只有在与集群中的所有其他节点共享其cluster.name时才能加入集群。默认的名称是elasticsearch,但是应该将其更改为适当的名称,以描述集群的用途。
cluster.name: logging-prod

确保不要在不同的环境中重复使用相同的集群名称,否则最终可能会导致节点加入错误的集群。

  • node.name:

Elasticsearch使用node.name作为Elasticsearch特定实例的人类可读标识符,因此它被包含在许多API的响应中。它默认为计算机在Elasticsearch启动时具有的主机名,但可以在elasticsearch.yml中显式配置,如下所示:

node.name: prod-data-2
  • network.host:

默认情况下,Elasticsearch仅绑定到环回地址。 127.0.0.1[:: 1]。 这足以在服务器上运行单个开发节点。 为了与其他服务器上的节点形成集群,您的节点将需要绑定到非环回地址。虽然有许多网络设置,但是通常您需要配置的只是network.host:

network.host: 192.168.1.10

network.host设置还可以理解一些特殊值,例如_local__site__global_以及诸如:ip4:ip6的修饰符。

特殊值 含义
_[networkInterface]_ Addresses of a network interface, for example _en0_.
_local_ Any loopback addresses on the system, for example 127.0.0.1.
_site_ Any site-local addresses on the system, for example 192.168.0.1.
_global_ Any globally-scoped addresses on the system, for example 8.8.8.8.

这些特殊值默认的可以适用于ipv4和ipv6,但是你可以通过:ipv4或者:ipv6限制其使用于某一个,例如: _en0:ipv4_将仅绑定到接口en0的ipv4地址。

在投入生产之前,应该配置两个重要的发现和集群形成设置,以便集群中的节点可以彼此发现并选举一个主节点。

  • discovery.seed_hosts 无需任何网络配置,Elasticsearch将绑定到可用的环回地址,并扫描本地端口9300到9305,以尝试连接到同一服务器上运行的其他节点。这提供了一种无需进行任何配置的自动集群体验。当您要与其他主机上的节点组成集群时,应使用discovery.seed_hosts设置提供集群中其他主机的列表,这些主机符合主机要求并且可能处于活动状态且可联系,以便为发现过程提供种子 。 此设置应该是群集中所有符合主机资格的节点的地址的列表。 每个地址可以是IP地址,也可以是通过DNS解析为一个或多个IP地址的主机名。如果符合主机资格的节点没有固定的名称或地址,请使用备用主机提供程序动态查找其地址。
  • cluster.initial_master_nodes首次启动全新的Elasticsearch群集时,会出现一个群集引导步骤,该步骤确定了在第一次选举中便对其票数进行计数的主资格节点的集合。在开发模式下,未配置发现设置,此步骤由节点本身自动执行。由于这种自动引导从本质上讲是不安全的,因此当您在生产模式下启动全新的群集时,必须显式列出符合资格的主机节点,并在第一次选举中对其投票进行计数。使用cluster.initial_master_nodes设置来设置此列表。 重新启动集群或将新节点添加到现有集群时,不应使用此设置。
discovery.seed_hosts:
   - 192.168.1.10:9300
   - 192.168.1.11 
   - seeds.mydomain.com 
   - [0:0:0:0:0:ffff:c0a8:10c]:9301 
cluster.initial_master_nodes: 
   - master-node-a
   - master-node-b
   - master-node-c

4.启动两个节点

分别执行 ./bin/elasticsearch 两个节点启动,这个时候,如果再虚拟机会碰到一个错误 max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] 原因及解决方案: Elasticsearch默认使用mmapfs目录存储其索引。默认的操作系统对mmap计数的限制可能太低,这可能会导致内存不足异常。在Linux上,您可以通过以root用户身份运行以下命令来增加限制:

sysctl -w vm.max_map_count=262144

要永久设置此值,请更新/etc/sysctl.conf中的vm.max_map_count设置。 要在重新引导后进行验证,请运行sysctl vm.max_map_count

RPM和Debian软件包将自动配置此设置。 不需要进一步的配置。

当您启动一个Elasticsearch实例时,您是在启动一个节点。Elasticsearch集群是一组具有相同cluster.name属性的节点。当节点加入或离开集群时,集群会自动重新组织自己,以便在可用节点上均匀分布数据。

如果您运行的是Elasticsearch的单个实例,那么您将拥有一个节点的集群。所有主分片都驻留在单个节点上。 无法分配副本分片,因此群集状态保持黄色。群集可以完全正常运行,但是如果发生故障,则存在数据丢失的风险。

您可以向集群添加节点来增加其容量和可靠性。默认情况下,节点既是数据节点,也有资格被选为控制集群的主节点。您还可以为特定目的配置新节点,例如处理写入请求。

将更多节点添加到群集时,它将自动分配副本分片。当所有主分片和副本分片均处于活动状态时,群集状态将变为绿色。 您可以在本地计算机上运行多个节点,以试验多个节点的Elasticsearch集群的行为。要将节点添加到本地计算机上运行的集群中,请执行以下操作:

  • 建立一个新的Elasticsearch实例。
  • 使用elasticsearch.yml中的cluster.name设置指定集群的名称。 例如,要将节点添加到logging-prod集群,请将行cluster.name:"logging-prod"添加到elasticsearch.yml
  • 启动Elasticsearch。 节点自动发现并加入指定的集群。

要将节点添加到在多台计算机上运行的集群中,还必须设置discover.seed_hosts,以便新节点可以发现其其余集群。

再次执行就可成功启动两个节点了,通过同一个ip地址的9200端口和9201端口分别访问的是集群的不同节点

5.全集群重启或滚动重启

在某些情况下,您需要执行全集群重启或滚动重启。在完全集群重新启动的情况下,您将关闭并重新启动集群中的所有节点,而在滚动重新启动的情况下,一次将仅关闭一个节点,因此服务不会中断。

全集群重启

  • 1.禁用分片分配

当您关闭一个节点时,在开始将该节点上的分片复制到群集中的其他节点之前,分配过程将等待index.unassigned.node_left.delayed_timeout(默认为一分钟),这可能会涉及大量的输入/输出。由于节点很快就要重新启动,所以这个输入/输出是不必要的。通过在关闭节点之前禁用副本分配,可以避免争用时间:

curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
  "persistent": {
    "cluster.routing.allocation.enable": "primaries"
  }
}
'
  • 2.停止索引并执行同步刷新。 执行同步刷新可加快分片恢复。
curl -X POST "localhost:9200/_flush/synced?pretty"

执行同步刷新时,请检查响应以确保没有失败。 尽管请求本身仍返回200 OK状态,但在响应正文中列出了由于挂起索引操作而失败的同步刷新操作。 如果失败,请重新发出请求。

请注意,同步刷新已被弃用,并将在8.0中删除。在Elasticsearch 7.6或更高版本中,刷新与同步刷新的效果相同。

  • 3.暂时停止与活动机器学习作业和数据仓库相关的任务。(可选)

  • 4.关闭所有节点。

  • 5.执行任何需要的更改。

  • 6.重新启动节点。 如果您有专用的主节点,请先启动它们,然后等待它们形成集群并选举一个主节点,然后再继续处理数据节点。 您可以通过查看日志来检查进度。

只要有足够的符合主机资格的节点相互发现,它们就会形成一个集群并选举一个主机。 那时,您可以使用cat health和cat node API监视加入集群的节点:

curl -X GET "localhost:9200/_cat/health?pretty"
curl -X GET "localhost:9200/_cat/nodes?pretty"
  • 7.等待所有节点加入群集,并报告黄色状态。

_cat/health返回的status列显示集群中每个节点的运行状况:红色,黄色或绿色。

当节点加入集群时,它开始恢复本地存储的所有主分片。 _cat/health API最初报告状态为红色,表示并非所有主分片都已分配。

一旦节点恢复了其本地分片,群集状态就会切换为黄色,表示所有主分片都已恢复,但并非所有副本分片都已分配。 这是可以预期的,因为您尚未重新启用分配。 将副本的分配延迟到所有节点都变黄之前,允许主服务器将副本分配给已经具有本地分片副本的节点。

  • 8.重新启用分配。 当所有节点都已加入集群并恢复了其主要分片后,可通过将cluster.routing.allocation.enable恢复为默认值来重新启用分配:
curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
  "persistent": {
    "cluster.routing.allocation.enable": null
  }
}
'

重新启用分配后,集群便开始将副本分片分配给数据节点。 此时,恢复索引和搜索是安全的,但是如果您可以等待直到成功分配了所有主分片和副本分片并且所有节点的状态为绿色,则群集将恢复得更快。 您可以使用_cat/health_cat/recovery API监视进度:

curl -X GET "localhost:9200/_cat/health?pretty"
curl -X GET "localhost:9200/_cat/recovery?pretty"
  • 9.重新启动机器学习工作。 (可选)

滚动重启

  • 1、禁用分片分配。
  • 2、停止索引并执行同步刷新。
  • 3、暂时停止与活动的机器学习作业和数据馈送相关的任务。 (可选的)
  • 4、在滚动重启的情况下关闭单个节点。
  • 5、执行任何所需的更改。
  • 6、重新启动您更改的节点。
  • 7、重新启用切分分配。
  • 8、在滚动重启的情况下重复上述步骤。
  • 9、重新启动机器学习工作。 (可选)

6.使用x-pack对elasticsearch添加认证

X-Pack是Elastic Stack扩展,可提供安全性,警报,监视,报告,机器学习和许多其他功能。 默认情况下,当您安装Elasticsearch时,会安装X-Pack。

在配置文件elasticsearch.yml中,新增加一个配置项 xpack.security.enabled:true 在集群中的所有节点上,都要增加该配置项 然后重启节点elasticsearch实例。 重启完成后,执行密码设置的命令,将对相关内部用户设置用户密码

➜  elasticsearch-7.6.1 bin/elasticsearch-setup-passwords interactive
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y


Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana]:
Reenter password for [kibana]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]
➜  elasticsearch-7.6.1

7.最终我们就可以通过使用认证的方法访问我们的集群节点

image.png

注意:在同时安装了Kibana的时候,首次登录Kibana时的用户名密码为elasticsearch设置的用户名和密码。

8.安装中文分词器IK Analysis

image.png
不同版本下载对应版本的分词器
image.png
安装方式有两种,安装完成后重启即可。

9.新特性

自elasticsearch 5.6之后的版本,一个索引只有一个type,多于一个会报错。