如何安装和配置 Elasticsearch

[ad_1]

什么是弹性搜索?

Elasticsearch 是一个分布式的、开源的、全文搜索引擎,它提供了用于分析多种数据类型的多租户功能。 它存储和索引来自 Logstash 或 Kibana 等来源的数据。 然后可以查询 Elasticsearch 以获取特定数据,以返回有关特定应用程序、日志分析、应用程序性能数据或其他信息的有用信息。

在本教程中,我们将介绍如何在 Ubuntu 18.04 上安装 Elasticsearch。 聚合和转换来自不同系统的日志信息是分布式系统中应用程序管理和监控的重要组成部分。 需要收集能力很重要,但能够将其置于上下文中并能够梳理相关数据的能力至关重要。 这就是 Elasticsearch 的用武之地。

预检

  • 运行 Ubuntu 18.04 LTS 的服务器
  • 本教程假设服务器上有可用的 Java 安装。
  • 用户在终端中具有 CLI 的使用知识

安装依赖

由于 Elasticsearch 使用 Java,我们需要确保安装了 Java Development Kit (JDK)。 我们可以使用此命令检查 Ubuntu 服务器上的 Java 安装。

[email protected]:~$ java -version
 -bash: java: command not found

如果未安装 Java,您可以运行以下命令进行安装,或查看我们的知识库文章以获取更详细的说明。

[email protected]:~$ apt install openjdk-8-jdk

现在,我们可以通过再次运行以下命令来重新验证我们的 Java JDK 安装。

[email protected]:~$ java -version
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment (build 13.0.2+8)
OpenJDK 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)

准备环境

接下来,作为最佳实践,我们应该通过运行以下命令来更新我们的系统包。

[email protected]:~# apt update -y

接下来,我们将运行以下 wget 命令来为 Logstash 包存储库下拉并安装 GPG 公共签名密钥。

[email protected]:~# apt update -y

以下步骤在所有系统上可能是必要的,也可能不是必要的,但要确保所有必备软件包都可用,并且我们可以通过 HTTPS 访问所有存储库,我们将安装以下名为 apt-transport-https 的软件包。

[email protected]:~# apt install apt-transport-https -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  apt-transport-https
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 1692 B of archives.
After this operation, 153 kB of additional disk space will be used.
Get:1 https://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 apt-transport-https all 1.6.12ubuntu0.1 [1692 B]
Fetched 1692 B in 1s (2311 B/s)
Selecting previously unselected package apt-transport-https.
(Reading database ... 35064 files and directories currently installed.)
Preparing to unpack .../apt-transport-https_1.6.12ubuntu0.1_all.deb ...
Unpacking apt-transport-https (1.6.12ubuntu0.1) ...
Setting up apt-transport-https (1.6.12ubuntu0.1) ...
[email protected]:~# 

最后,我们将使用以下命令安装 Logstash 存储库并将其添加到我们的 /etc/apt/sources.list.d 文件中。

[email protected]:~# echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

安装 Elasticsearch

现在我们添加了 Logstash 存储库,需要更新 apt 以了解新源。

[email protected]:~# apt update -y

现在我们可以像使用 apt 命令安装任何其他软件包一样安装 Elasticsearch。

[email protected]:~# apt install elasticsearch -y

如果有机会您收到以下错误:

Target Packages (main/binary-amd64/Packages) is configured multiple times in /etc/apt/sources.list.d/elastic-7.x.list:1 and /etc/apt/sources.list.d/elastic-7.x.list:2

运行上述安装命令后,使用 vim 或 nano 编辑 /etc/apt/sources.list.d/elastic-7.x.list 文件。

vim /etc/apt/sources.list.d/elastic-7.x.list 

打开文件后,使用 dd 命令删除两个双重条目之一。 按两次 dd 将从文件中删除一行。

deb https://artifacts.elastic.co/packages/7.x/apt stable main 
deb https://artifacts.elastic.co/packages/7.x/apt stable main

现在,再次重新运行 apt install 命令。

[email protected]:~# apt install elasticsearch -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  elasticsearch
0 upgraded, 1 newly installed, 0 to remove and 19 not upgraded.
Need to get 318 MB of archives.
After this operation, 531 MB of additional disk space will be used.
Selecting previously unselected package elasticsearch.able/main amd64 elasticsearch amd64 7.8.1 [318 MB]
(Reading database ... 35068 files and directories currently installed.)
Preparing to unpack .../elasticsearch_7.8.1_amd64.deb ...
Creating elasticsearch group... OK
Creating elasticsearch user... OK
Unpacking elasticsearch (7.8.1) ...
Setting up elasticsearch (7.8.1) ...
System has not been booted with systemd as init system (PID 1). Can't operate.
Created elasticsearch keystore in /etc/elasticsearch/elasticsearch.keystore
Processing triggers for ureadahead (0.100.0-21) ...
Processing triggers for systemd (237-3ubuntu10.41) …
[email protected]:~#

配置 Elasticsearch

Elasticsearch 在我们安装之后就有了一个基本的配置,但是我们可以修改位于 /etc/elasticsearch 目录中的默认 elasticsearch.yml 配置文件。 几乎所有 Elasticsearch 设置都存在于以下文件中。

  • /etc/elasticsearch/elasticsearch.yml – 主配置文件
  • /etc/elasticsearch/jvm.options – 配置 Elasticsearch JVM 设置
  • /etc/elasticsearch/log4j2.properties – 配置 Elasticsearch 日志记录
  • /etc/default/elasticsearch – Java 启动设置
  • /var/lib/elasticsearch – 主 Elasticsearch 数据文件
[email protected]:~# /etc/elasticsearch# ll
total 32
drwxr-s--- 1 root elasticsearch  4096 Aug 13 08:53 ./
drwxr-xr-x 1 root root           4096 Aug 13 08:04 ../
-rw-r--r-- 1 root elasticsearch    76 Aug 12 18:02 .elasticsearch.keystore.initial_md5sum
-rw-rw---- 1 root elasticsearch   199 Aug 12 18:02 elasticsearch.keystore
-rw-rw---- 1 root elasticsearch  2847 Jul 21 12:47 elasticsearch.yml
-rw-rw---- 1 root elasticsearch  2373 Jul 21 12:47 jvm.options
drwxr-s--- 1 root elasticsearch  4096 Jul 21 12:47 jvm.options.d/
-rw-rw---- 1 root elasticsearch 17419 Jul 21 12:47 log4j2.properties
-rw-rw---- 1 root elasticsearch   473 Jul 21 12:47 role_mapping.yml
-rw-rw---- 1 root elasticsearch   197 Jul 21 12:47 roles.yml
-rw-rw---- 1 root elasticsearch     0 Jul 21 12:47 users
-rw-rw---- 1 root elasticsearch     0 Jul 21 12:47 users_roles
[email protected]:~# /etc/elasticsearch#

该目录中还有其他配置文件,我们将不涉及,但可以从主目录中获取更多信息 弹性公司 网站。

默认配置文件

下面是一个默认的 Elasticsearch yaml 文件。 它包含我们的集群、节点、路径、内存、网络、发现和网关的配置选项。 同样,大多数默认值可以保持原样,但您的设置可能会因项目变量而异。

注意:由于 Elasticsearch 的配置文件是 YAML 格式,需要注意保持特定格式的完整。 这意味着我们在编辑文件时不应添加任何额外的空格或制表符。

# =========== 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: my-application
#
# ------------------------------------ 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: /var/lib/elasticsearch
#
# Path to log files:
#
path.logs: /var/log/elasticsearch
#
# ----------------------------------- 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.0.1
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
#
#cluster.initial_master_nodes: ["node-1", "node-2"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Gateway
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true

在本教程中,我们将只修改网络设置以允许访问主机。 要做到这一点,只需删除 ‘#‘ 字符在行前修改默认设置。

要通过其内置的 REST API 进一步限制对 Elasticsearch 的不需要的外部访问,请找到指定 network.host 的行,并取消注释,并将其值替换为 localhost,如下所示。

# ------------------------ Network -----------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.0.1network.host: localhost

通常,Elasticsearch 侦听端口 9200,但这可以修改为仅侦听特定接口。 我们可以使用 IP 指定该设置,将 localhost 替换为默认网关。

服务器角色

最重要的设置之一是定义服务器的用途。 通常在多服务器集群设置中,应选择主节点或从节点。 在较大的部署中,可以设置多个主节点来维护集群的稳定性和健康状况。 通常,Elasticsearch 服务器也有指定的从服务器,用于存储任务和共享工作负载。

根据项目的需要,可能需要取消注释中的cluster.name 和node.name 设置并进行修改,以设置服务器和集群的名称。 如果我们不修改这些设置,node.name 和 cluster.name 将自动设置为默认节点和集群的名称(如果可用)。

确定服务器角色的设置称为 node.master。 默认情况下,第一个节点是主节点。 如果您只有一个 Elasticsearch 节点,则应将默认选项设置为 true 值,因为始终需要至少一个主节点。 或者,如果您希望将节点配置为从站,请为变量 node.master 分配一个 false 值。

启用弹性搜索

安装 Elasticsearch 后,我们将运行以下命令使其在启动时运行,并在当前会话中启动它。

 [email protected]:~# systemctl enable --now elasticsearch 
 * Starting Elasticsearch Server 
[email protected]:~#  

此命令可能需要一些时间才能完成。

防火墙修改

接下来,我们将在 Ubuntu 中配置 UFW 防火墙,以允许从受信任的主机访问默认的 Elasticsearch 端口 9200。 要允许访问,请在终端中键入以下命令。

[email protected]:~# ufw allow from 192.190.221.246  to any port 9200

完成后,您可以使用以下命令启用 UFW。 出现提示时键入 Y,后续消息将让我们知道 UFW 处于活动状态并已启用。

[email protected]:~# ufw enable 
Command may disrupt existing ssh connections. Proceed with operation (y|n)? Y
Firewall is active and enabled on system startup

最后,使用以下命令检查 UFW 的状态。

[email protected]:~# ufw status

验证配置

最后,我们可以通过运行以下命令来验证 Elasticsearch 现在是否已配置 curl 并获取命令。

[email protected]:~# curl -X GET "localhost:9200"
{
  "name" : "ubuntu18.awesome.com",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "ZPbl_EHjS5Kfb43P3O8o-w",
  "version" : {
    "number" : "7.8.1",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "b5ca9c58fb664ca8bf9e4057fc229b3396bf3a89",
    "build_date" : "2020-07-21T16:40:44.668009Z",
    "build_snapshot" : false,
    "lucene_version" : "8.5.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline": "Liquid Web Rocks!"
}

如果您看到与上述类似的输出,我们可以确定 Elasticsearch 正在按预期工作。 如果您收到类似于以下内容的错误:

[email protected]:~# curl -X GET "localhost:9200" 
curl: (7) Failed to connect to localhost port 9200: Connection refused 

只需等待几秒钟,然后再次重试该命令。

Elasticsearch 现已启动并运行。 它已准备好连接到各种数据源以开始收集数据并为其编制索引。 Elasticsearch 可以以分布式方式设置,多个节点在运行。 根据您的需要,它可以提供一种方法来搜索和使来自您的应用程序或系统的日志更有意义。

结论

Elasticsearch 现在是多个大型行业的分布式搜索和数据分析的中流砥柱。 其易用性、可配置性、功能集和可扩展性只是当今使用 Elasticsearch 的众多原因中的一部分。 此外,由于它是开源的并且可以免费使用,因此这些年来企业的使用量猛增。 最后,Elastic 提供了出色的支持和文档,以支持其在较小的简单测试环境中的使用,扩展到主要的全球供应商。 Liquid Web 提供了可以使用该软件的多个平台。 想知道更多?

我们的解决方案团队日夜待命,讨论您的需求,并就我们如何为您实施这样的系统提供有根据的可靠建议!

我们以成为 Hosting™ 中最有帮助的人而自豪!

我们才华横溢的支持团队由经验丰富的 Linux 技术人员和系统管理员组成,他们对多种网络托管技术(尤其是本文中讨论的技术)了如指掌。 我们一年 365 天,每周 7 天,每天 24 小时随时可以帮助解决与本文相关的任何问题。

如果您是完全托管的 VPS 服务器, Cloud 专用,VMWare 私有 Cloud, 私人父服务器或专用服务器所有者,并且您对执行列出的任何步骤感到不舒服,可以通过电话@800.580.4985 与我们联系,一个 聊天 或支持票以协助您完成此过程。

[ad_2]

Related Posts