Elasticsearch: 使用 Debian 安装包来安装 Elasticsearch 8.x

790 阅读6分钟

在我之前的文章 “Elastic Stack 8.0 安装 - 保护你的 Elastic Stack 现在比以往任何时候都简单” 我详细地描述了如何在 macOS 安装 Elastic Stack。当时的目的也是为了显示这个按照和之前的 Elastic Stack 7.x 有什么不同的地方。当然最重要的是它的默认安全配置和之前的版本是完全不同。我也在 B 站上传了一个视频。之后,有又收到一些反馈,说在那个视频里的安装是在前台运行的。如果 Elasticsearch 是在后台,也即是以服务的方式 ,那么我们该如何得到那些密码及 token 呢?

在今天的文章中,我将详述如何使用 Debian 安装包的形式来进行安装 Elastic Stack 8.1。

在哪里下载安装包

可以从我们的网站或我们的 APT 存储库下载 Elasticsearch 的 Debian 软件包。 它可用于在任何基于 Debian 的系统上安装 Elasticsearch,例如 Debian 和 Ubuntu。

该软件包包含免费和订阅功能。 开始 30 天试用以试用所有功能。

可以在下载 Elasticsearch 页面上找到 Elasticsearch 的最新稳定版本。 其他版本可以在过去的版本页面上找到。

注意:Elasticsearch 包含来自 JDK 维护者 (GPLv2+CE) 的 OpenJDK 捆绑版本。 要使用你自己的 Java 版本,请参阅 JVM 版本要求

导入 Elasticsearch PGP key

我们使用带有指纹的 Elasticsearch 签名密钥(PGP 密钥 D88E42B4,可从 pgp.mit.edu 获得)对我们所有的包进行签名:

4609 5ACC 8548 582C 1A26 99A9 D27D 666C D88E 42B4

下载并安装公共签名密钥:

wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg

从 APT 存储库安装

在继续之前,你可能需要在 Debian 上安装 apt-transport-https 软件包:

sudo apt-get install apt-transport-https

将存储库定义保存到 /etc/apt/sources.list.d/elastic-8.x.list:

echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list

注意:这些指令不使用 add-apt-repository 有几个原因:

  • add-apt-repository 将条目添加到系统 /etc/apt/sources.list 文件,而不是在 /etc/apt/sources.list.d 中干干净净地为每个存储库文件添加

  • add-apt-repository 不是许多发行版上默认安装的一部分,并且需要许多非默认依赖项。

  • 旧版本的 add-apt-repository 总是添加一个 deb-src 条目,这会导致错误,因为我们不提供源包。 如果你添加了 deb-src 条目,你将看到类似以下的错误,直到你删除 deb-src 行:

    Unable to find expected entry 'main/source/Sources' in Release file
    (Wrong sources.list entry or malformed file)
    
    

你可以使用以下命令安装 Elasticsearch Debian 软件包:

sudo apt-get update && sudo apt-get install elasticsearch

警告:如果同一个 Elasticsearch 存储库存在两个条目,你将在 apt-get update 期间看到如下错误:

Duplicate sources.list entry https://artifacts.elastic.co/packages/8.x/apt/ ...`

检查 /etc/apt/sources.list.d/elasticsearch-8.x.list 中的重复条目或在 /etc/apt/sources.list.d/ 和 /etc/apt/ 中的文件中找到重复条目 source.list 文件。

注意:在基于 systemd 的发行版上,安装脚本将尝试设置内核参数(例如 vm.max_map_count); 你可以通过屏蔽 systemd-sysctl.service 单元来跳过此步骤。



1.  parallels@liuxg:~$ sudo apt-get update && sudo apt-get install elasticsearch
2.  [sudo] password for parallels: 
3.  Hit:1 https://artifacts.elastic.co/packages/8.x/apt stable InRelease
4.  Hit:2 http://ports.ubuntu.com/ubuntu-ports focal InRelease
5.  Hit:3 http://ports.ubuntu.com/ubuntu-ports focal-updates InRelease
6.  Hit:4 http://ports.ubuntu.com/ubuntu-ports focal-backports InRelease
7.  Hit:5 http://ports.ubuntu.com/ubuntu-ports focal-security InRelease
8.  Reading package lists... Done
9.  Reading package lists... Done
10.  Building dependency tree       
11.  Reading state information... Done
12.  The following NEW packages will be installed:
13.    elasticsearch
14.  0 upgraded, 1 newly installed, 0 to remove and 142 not upgraded.
15.  Need to get 328 MB of archives.
16.  After this operation, 560 MB of additional disk space will be used.
17.  Get:1 https://artifacts.elastic.co/packages/8.x/apt stable/main arm64 elasticsearch arm64 8.1.0 [328 MB]
18.  Fetched 328 MB in 30s (11.0 MB/s)                                                                                      
19.  Selecting previously unselected package elasticsearch.
20.  (Reading database ... 141426 files and directories currently installed.)
21.  Preparing to unpack .../elasticsearch_8.1.0_arm64.deb ...
22.  Creating elasticsearch group... OK
23.  Creating elasticsearch user... OK
24.  Unpacking elasticsearch (8.1.0) ...
25.  Setting up elasticsearch (8.1.0) ...
26.  --------------------------- Security autoconfiguration information ------------------------------

28.  Authentication and authorization are enabled.
29.  TLS for the transport and HTTP layers is enabled and configured.

31.  The generated password for the elastic built-in superuser is : kHbQ9_rzSo31vBpCpWdX

33.  If this node should join an existing cluster, you can reconfigure this with
34.  '/usr/share/elasticsearch/bin/elasticsearch-reconfigure-node --enrollment-token <token-here>'
35.  after creating an enrollment token on your existing cluster.

37.  You can complete the following actions at any time:

39.  Reset the password of the elastic built-in superuser with 
40.  '/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic'.

42.  Generate an enrollment token for Kibana instances with 
43.   '/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana'.

45.  Generate an enrollment token for Elasticsearch nodes with 
46.  '/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node'.

48.  -------------------------------------------------------------------------------------------------
49.  ### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
50.   sudo systemctl daemon-reload
51.   sudo systemctl enable elasticsearch.service
52.  ### You can start elasticsearch service by executing
53.   sudo systemctl start elasticsearch.service


如上所示,它清楚地显示了超级用户 elastic 的密码:kHbQ9_rzSo31vBpCpWdX。

首先,我们查看 elasticsearch 服务是否已经成功地运行起来了:



1.  parallels@liuxg:~$ service elasticsearch status
2.  ● elasticsearch.service - Elasticsearch
3.       Loaded: loaded (/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled)
4.       Active: inactive (dead)
5.         Docs: https://www.elastic.co


如上所示,elasticsearch 服务还没有被运行起来。我们一种方式是执行如上所示的命令:



1.  ### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
2.   sudo systemctl daemon-reload
3.   sudo systemctl enable elasticsearch.service
4.  ### You can start elasticsearch service by executing
5.   sudo systemctl start elasticsearch.service


这样当我们的 Debian/Ubuntu 机器启动后,elasticsearch 服务会被自动启动。我们不再需要手动来启动这个服务了。

另外一种方式是我们手动来启动 elasticsearch 服务:

sudo service elasticsearch start

当我们以这种方式启动后,那么每次 Debian/Ubuntu 机器启动后,我们需要再次使用同样的命令来启动 elasticsearch 服务。

我们使用如下的命令来查看 elasticsearch 服务的状态:



1.  parallels@liuxg:~$ service elasticsearch status
2.  ● elasticsearch.service - Elasticsearch
3.       Loaded: loaded (/lib/systemd/system/elasticsearch.service; disabled; vendor preset: enabled)
4.       Active: active (running) since Wed 2022-03-16 16:21:48 CST; 1min 20s ago
5.         Docs: https://www.elastic.co
6.     Main PID: 6551 (java)
7.        Tasks: 59 (limit: 4576)
8.       Memory: 2.3G
9.       CGroup: /system.slice/elasticsearch.service
10.               ├─6551 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkadd>
11.               └─6830 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-aarch64/bin/controller

13.  Mar 16 16:21:40 liuxg systemd[1]: Starting Elasticsearch...
14.  Mar 16 16:21:48 liuxg systemd[1]: Started Elasticsearch.


显然我们的 elasticsearch 服务已经成功地运行起来了。

我们可以使用如下的命令来检查 Elasticsearch 是否成功地运行起来了:

curl --insecure --user elastic:qTfj3Vfm+6RT1VrCbmwV https://localhost:9200


1.  parallels@liuxg:~$ curl --insecure --user elastic:kHbQ9_rzSo31vBpCpWdX https://localhost:9200
2.  {
3.    "name" : "liuxg",
4.    "cluster_name" : "elasticsearch",
5.    "cluster_uuid" : "0GaowezJRY2f2RGpgYBIOQ",
6.    "version" : {
7.      "number" : "8.1.0",
8.      "build_flavor" : "default",
9.      "build_type" : "deb",
10.      "build_hash" : "3700f7679f7d95e36da0b43762189bab189bc53a",
11.      "build_date" : "2022-03-03T14:20:00.690422633Z",
12.      "build_snapshot" : false,
13.      "lucene_version" : "9.0.0",
14.      "minimum_wire_compatibility_version" : "7.17.0",
15.      "minimum_index_compatibility_version" : "7.0.0"
16.    },
17.    "tagline" : "You Know, for Search"
18.  }


请注意上面的 --insecure 选项。这是因为我们使用的是自签名的证书,所以我们可以通过这种方式来进行访问。请注意上面的 kHbQ9_rzSo31vBpCpWdX 是超级用户 elastic 的密码。

如果我们想要使用证书来进行访问,首先,我们需要切换到 root 用户,然后使用如下的命令:



1.  root@liuxg:/etc/elasticsearch/certs# ls
2.  http_ca.crt  http.p12  transport.p12
3.  root@liuxg:/etc/elasticsearch/certs# curl --cacert http_ca.crt --user elastic:kHbQ9_rzSo31vBpCpWdX https://localhost:9200
4.  {
5.    "name" : "liuxg",
6.    "cluster_name" : "elasticsearch",
7.    "cluster_uuid" : "0GaowezJRY2f2RGpgYBIOQ",
8.    "version" : {
9.      "number" : "8.1.0",
10.      "build_flavor" : "default",
11.      "build_type" : "deb",
12.      "build_hash" : "3700f7679f7d95e36da0b43762189bab189bc53a",
13.      "build_date" : "2022-03-03T14:20:00.690422633Z",
14.      "build_snapshot" : false,
15.      "lucene_version" : "9.0.0",
16.      "minimum_wire_compatibility_version" : "7.17.0",
17.      "minimum_index_compatibility_version" : "7.0.0"
18.    },
19.    "tagline" : "You Know, for Search"
20.  }


在上面,我们可以看到在路径 /etc/elasticsearch/certs 它含有访问所需要的证书。我们或者在普通用户下使用如下的命令来进行访问:

 当然我们也可以在浏览器中进行访问:

请注意上面的  192.168.0.9 是我们的 ubuntu 机器的私有 IP 地址。我们必须使用 https 的形式来进行访问。在上面的浏览器中,我们打入 thisisunsafe。我们就可以看到:

点击 Sign In:

 

从上面,我们看出来访问是成功的。 

下载 debian 安装包并手动安装

上面介绍的步骤是使用 API 仓库来进行安装的。接下来,我们可以直接从 Elastic 的官方网站下载安装包进行安装。Elasticsearch v8.1.0 的 Debian 包可以从网站下载并安装如下:



1.  wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.1.0-amd64.deb
2.  wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.1.0-amd64.deb.sha512
3.  shasum -a 512 -c elasticsearch-8.1.0-amd64.deb.sha512 
4.  sudo dpkg -i elasticsearch-8.1.0-amd64.deb


比较下载的 Debian 包的 SHA 和发布的校验和,应该输出 elasticsearch-{version}-amd64.deb: OK。

使用 systemd 运行 Elasticsearch

要将 Elasticsearch 配置为在系统启动时自动启动,请运行以下命令:



1.  sudo /bin/systemctl daemon-reload
2.  sudo /bin/systemctl enable elasticsearch.service


Elasticsearch 可以按如下方式启动和停止:



1.  sudo systemctl start elasticsearch.service
2.  sudo systemctl stop elasticsearch.service


这些命令不提供关于 Elasticsearch 是否成功启动的反馈。 相反,此信息将写入位于 /var/log/elasticsearch/ 的日志文件中。

如果你对 Elasticsearch 密钥库进行了密码保护,则需要使用本地文件和 systemd 环境变量向 systemd 提供密钥库密码。 这个本地文件应该在它存在时受到保护,一旦 Elasticsearch 启动并运行,就可以安全地删除它。



1.  echo "keystore_password" > /path/to/my_pwd_file.tmp
2.  chmod 600 /path/to/my_pwd_file.tmp
3.  sudo systemctl set-environment ES_KEYSTORE_PASSPHRASE_FILE=/path/to/my_pwd_file.tmp
4.  sudo systemctl start elasticsearch.service


默认情况下,Elasticsearch 服务不会在 systemd 日志中记录信息。 要启用 journalctl 日志记录,必须从 elasticsearch.service 文件的 ExecStart 命令行中删除 --quiet 选项。

启用 systemd 日志记录后,可以使用 journalctl 命令获得日志记录信息:

为了查看最新的日志:

sudo journalctl -f

要列出 elasticsearch 服务的日记条目:

sudo journalctl --unit elasticsearch

要列出从给定时间开始的 elasticsearch 服务的日志条目:

sudo journalctl --unit elasticsearch --since  "2016-10-30 18:17:16"