首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >安装 Rally:运行你的第一个 Elasticsearch 性能基准测试并在无网络环境中操作

安装 Rally:运行你的第一个 Elasticsearch 性能基准测试并在无网络环境中操作

原创
作者头像
点火三周
发布2025-05-19 15:20:49
发布2025-05-19 15:20:49
4621
举报

Rally,也被称为 ES Rally,是 Elastic 依赖的基准测试工具,用于识别 Elasticsearch 的性能改进、回归等。它会在每晚针对 Elasticsearch 的每晚构建版本运行。你也可以用它来测试自己的 Elasticsearch 集群,识别与设置相关的任何性能问题。如果你想每天处理 5TB 的数据,请按照这篇博客文章及其系列来确保你的硬件能够支持这一需求。如果你的工作负载主要在搜索方面,我们也会涵盖这部分内容!

Elastic 不建议在生产集群上运行 Rally。Rally 会执行一些破坏性的操作,可能导致数据丢失。此外,在已有负载的集群上进行基准测试并不那么有用,因为 Rally 的指标无法被正确解释。

安装 Rally

所有安装步骤都在官方的 Rally 文档中进行了描述。如果不进行源代码构建的基准测试,你不需要安装 Java。本系列博文重点关注在云环境中运行的 Elasticsearch 的基准测试。

你可以选择使用官方的 Docker 镜像,而不是在本地安装。

你需要准备:

  • 最新版本的 Git(至少高于 1.8)
  • Pbzip2
  • 最新版本的 Python(至少高于 3.8)
  • Python 的 pip

一旦安装了所有这些前提条件,你可以选择是否在 Python 中使用虚拟环境。为了简单起见,我们不使用虚拟环境。

使用以下命令安装 Rally:pip3 install esrally。这将安装所有必需的包,你就快准备好了。在我的情况下,我不是以 root 用户安装 Rally 的,因此我的安装路径不在 $PATH 中。

在无网络环境中安装 Rally

无网络环境的安装有两种类型。我们所说的无网络环境是指没有公共互联网连接的服务器。第一种方式是自建 PiPy 仓库,另一种是使用代理。还有一种方式是没有互联网连接和本地 PiPy 的情况下进行安装。在第一种情况下,你只需要按照上面的说明来安装 Rally。更多信息请参阅 官方文档

  1. 准备机器,安装所有必要的包。
  2. 下载最新版本,并复制到你的机器上。下载 esrally-dist-linux-$VERSION.tar.gz,地址为 https://github.com/elastic/rally/releases/tag/2.10.0
  3. 使用命令 tar -xzvf esrally-dist-linux-$VERSION.tar.gz 解压。
  4. 根据你的设置,运行 chmod +x install.sh 以允许执行脚本。
  5. 执行脚本。这将安装 Rally,不需要下载任何依赖项。

配置 Rally

不论使用何种安装方法,我们在运行第一次基准测试前必须配置 Rally。执行 esrally 命令,你会看到一条关于所有选项的消息。

代码语言:shell
复制
$ esrally
usage: esrally [-h] [--version] {race,list,delete,info,create-track,compare,build,download,install,start,stop,add} ...

    ____        ____
   / __ \____ _/ / /_  __
  / /_/ / __ `/ / / / / /
 / _, _/ /_/ / / / /_/ /
/_/ |_|\__,_/_/_/\__, /
                /____/

 You Know, for Benchmarking Elasticsearch.

options:
  -h, --help            show this help message and exit
  --version             show program's version number and exit

subcommands:
  {race,list,delete,info,create-track,compare,build,download,install,start,stop,add}
    race                Run a benchmark
    list                List configuration options
    delete              Delete records
    info                Show info about a track
    create-track        Create a Rally track from existing data
    compare             Compare two races
    build               Builds an artifact
    download            Downloads an artifact
    install             Installs an Elasticsearch node locally
    start               Starts an Elasticsearch node locally
    stop                Stops an Elasticsearch node locally
    add                 Add records

Find out more about Rally at https://esrally.readthedocs.io/en/2.10.0/

如果我们运行 race 之类的命令,会遇到预期的错误。

代码语言:shell
复制
$ esrally race

    ____        ____
   / __ \____ _/ / /_  __
  / /_/ / __ `/ / / / / /
 / _, _/ /_/ / / / /_/ /
/_/ |_|\__,_/_/_/\__, /
                /____/

usage: esrally [-h] [--version] {race,list,delete,info,create-track,compare,build,download,install,start,stop,add} ...
esrally: error: argument --track is required
[ERROR] Cannot race. 2.

Getting further help:
*********************
* Check the log files in /home/philippkahr/.rally/logs for errors.
* Read the documentation at https://esrally.readthedocs.io/en/2.10.0/.
* Ask a question on the forum at https://discuss.elastic.co/tags/c/elastic-stack/elasticsearch/rally.
* Raise an issue at https://github.com/elastic/rally/issues and include the log files in /home/philippkahr/.rally/logs.

-------------------------------
[INFO] FAILURE (took 0 seconds)
-------------------------------

我们即将配置所需的一切。我们要配置什么呢?让我们从一个图示开始,使我们的设置更容易理解。

我们有一个目标集群要进行基准测试,还有一个堆栈监控集群。在 Rally 中,有两种方式来配置指标收集。这并不是指你从堆栈监控中了解的 Elasticsearch 指标,而是基准测试期间收集和生成的指标。Rally 会跟踪每个任务所花费的时间、延迟等。这些可以导出到 Elasticsearch 中,使我们能进行可视化。

由于我们还有堆栈监控数据,我们可以进行叠加,查看集群的 CPU 使用率与每次调用的延迟。我们不建议将指标发送到你正在进行基准测试的同一集群,因为这会给该集群增加额外的不可控负载。查看 官方文档

1 - rally elasticsearch
1 - rally elasticsearch

要更改指标导出器,我们需要修改安装用户主目录中的 Rally 配置文件。在我的情况下,是 /home/philippkahr/.rally/rally.ini。一个空白配置如下所示:

代码语言:ini
复制
[meta]
config.version = 17

[system]
env.name = local

[node]
root.dir = /home/philippkahr/.rally/benchmarks
src.root.dir = /home/philippkahr/.rally/benchmarks/src

[source]
remote.repo.url = https://github.com/elastic/elasticsearch.git
elasticsearch.src.subdir = elasticsearch

[benchmarks]
local.dataset.cache = /home/philippkahr/.rally/benchmarks/data

[reporting]
datastore.type = in-memory
datastore.host =
datastore.port =
datastore.secure = False
datastore.user =
datastore.password =

[tracks]
default.url = https://github.com/elastic/rally-tracks

[teams]
default.url = https://github.com/elastic/rally-teams

[defaults]
preserve_benchmark_candidate = false

[distributions]
release.cache = true

重要的配置部分是 reporting 部分。这里我们将配置堆栈监控集群。我们将创建一个新的超级用户,名为 “rallymetrics”。使用这个超级用户是因为 Rally 可以在任何发行版本中更改所需的权限。

代码语言:ini
复制
[reporting]
datastore.type = elasticsearch
datastore.host = monitoring.elastic.cloud.com
datastore.port = 443
datastore.secure = True
datastore.user = rallymetrics
datastore.password = abc

对于任何使用本地集群且有自签名证书并因此出现错误的人,有一个设置可用:datastore.ssl.verification_mode。你可以通过设置为 none 来禁用它,或者将本地证书添加到证书存储中,并指向该设置:datastore.ssl.certificate_authorities了解更多关于这些设置的信息

理解什么是 race

首先,许多现成的 tracks 可用,但并非所有都适合你的用例。你想要运行的第一个命令是 esrally list tracks。这将显示所有可用的 tracks。这份列表包含 track 名称、压缩和未压缩的大小以及挑战。大小很重要,因为这就是你要下载的,未压缩的则需要在本地磁盘上。我们没有修改 Rally 放置其 track 数据的位置。一旦你运行了 esrally list tracks 命令,你会在 ~/.rally 目录中看到一个名为 benchmarks 的文件夹。我们在这个文件夹中存储每个 track 的数据。这只是 GitHub 仓库 的一个副本。

一个 race 是在一个 track 上运行的。每个 track 需要至少一个挑战。一个挑战描述了一个特定的工作负载(API 调用)。一个仅限于 ingest 的挑战将只执行与 ingest 相关的 API 调用。默认挑战已经配置好。我们想使用 http_logs track,挑战为 append-no-conflicts-index-only

你要使用的命令是:

代码语言:shell
复制
esrally race --user-tags={"benchmark_id": "1.1-http_logs-w20p1r1"} --track=http_logs --kill-running-processes --target-hosts=https://to-benchmark.elastic.co:9200 --pipeline=benchmark-only --client-options="verify_certs:false,basic_auth_user:'rally',basic_auth_password:'rally123'" --track-params='{"bulk_indexing_clients":20,"number_of_shards":1,"number_of_replicas":1}' --challenge=append-no-conflicts-index-only

我将逐步解析这个命令。

esrally race:调用 esrally 并告诉它执行一个 race。

--user-tags={"benchmark_id": "1.1-http_logs-w20p1r1"}:用于唯一标识任何 race,而不是每次执行。我希望相同的 race,具有相同的设置,拥有相同的标签。在这种情况下,我只是称之为 1.1-http_logs-w20p1r1。根据我的操作,我使用了无数不同的命名。当你创建一个基准测试剧本时,可以使用这个编号。它需要是唯一的,以便将不同的 race 进行比较。类似 1.1-http_logs-w20p1r1 的名称很不错。你从基准测试剧本中运行任务 1.1,track,以及 10 个工作者的主副本和副本数。你可以在类似于 client-options 参数的键值对中指定它们。阅读更多。

--track=http_logs:这是你正在运行的 track。这个 track 称为 HTTP 日志,是来自 1998 年世界杯 的 Web 服务器日志。对原始日志进行了一些修改,所有修改都在 track 的 GitHub 上进行了描述:http_logs。以下是我们正在摄取的示例文档的简单预览:

代码语言:json
复制
{
  "@timestamp": 898459201,
  "clientip": "211.11.9.0",
  "request": "GET /english/index.html HTTP/1.0",
  "status": 304,
  "size": 0
}

--kill-running-processes:通常不需要此选项。有时当你取消一个 track 因为你遇到了问题,esrally 可能无法正常退出,并且你会有一些 esrally 进程存在。Rally 会拒绝启动,直到你杀死所有正在运行的进程。如果指定了此标志,Rally 将自行终止这些进程。

--target-hosts=https://to-benchmark.elastic.co:9200:这只是你设置目标主机的地方。根据你的设置,它可以是一个或多个。始终测试真实的数据流;如果你在其前面有负载均衡器,请使用负载均衡器的端点进行测试。给你的负载均衡器团队一个提醒。

--pipeline=benchmark-only:在这里你可以告诉 Rally 为你启动一个集群,或者仅仅对你指向的集群进行基准测试。在大多数情况下,benchmark-only 是你想要使用的。

--client-options="verify_certs:false,basic_auth_user:'rally',basic_auth_password:'rally123'":客户端选项总是很难在没有示例的情况下弄清楚。它需要采用键值对的形式,并且当需要更多设置时,你可以用 , 分隔。由于这是一个命令行参数,不要忘记转义像 ! 这样的特殊字符。verify_certs 是一个很好的工具,可以告诉 Rally 忽略证书,这对本地测试可能很有帮助。在这种情况下,我只是将用户名和密码以明文形式写在命令中。你也可以使用环境变量。如果你愿意,更多信息可以在此找到:https://esrally.readthedocs.io/en/stable/command_line_reference.html#client-options

--track-params='{"bulk_indexing_clients":20,"number_of_shards":1,"number_of_replicas":1}':最后一个是任何 track 的重头戏。你可以像上面的客户端选项一样指定 track 参数。选择 JSON 并熟悉它的原因是因为更复杂的 track,比如 Elastic Logs 或 Security track,有很多选项。根据 track 的不同,可以设置更多或更少的参数。所有可能的参数都在这里描述:https://github.com/elastic/rally-tracks/tree/master/http_logs。任何默认设置都有说明。它需要是 json 的形式。可以像这样作为命令行参数,也可以作为本地参数文件。

在表格表示中,我们使用以下设置。这里还需要描述一些其他设置,因为它们不适用于我们。

参数名

默认值

覆盖值

bulk_indexing_clients

8

20

number_of_shards

5

1

number_of_replicas

0

1

bulk_size

5000

未修改

ingest_percentage

1000

未修改

--challenge=append-no-conflicts-index-only:我们明确定义了从 esrally list tracks 输出中获取的挑战。否则,Rally 将运行 track 所有的挑战。这个挑战是 append-no-conflicts-index-only,因此是一个纯粹的摄取相关任务。

一旦第一次运行完成,你会在控制台输出中看到 Rally 所做的所有任务。此输出和更多详细信息也会在配置文件旁边的 Rally 日志文件中收集,路径为 .rally/logs。在这篇博文中,我们不会详细介绍每个输出以及如何解释它们。有关所有步骤的更多信息可以在 官方文档中找到。

什么是挑战?

我们在上面快速谈到了这个话题,并解释了每个 track 都有一个默认挑战。运行 http_logs track 而没有任何挑战命令行参数时,我们运行的是 append-no-conflicts-index-only。这是 rally 命令的第二个输出:INFO Racing on track http_logs, challenge append-no-conflicts-index-only and car 'external' with version 8.12.1。

每个挑战的目的在于编写基准测试的规范。因此,每个挑战可以有不同的参数,如上所述,并分配了不同的任务。一个挑战在一个增强了 Jinja 的 JSON 文件中描述。append-no-conflicts-index-only 及其相关的每个任务都在 GitHub 上详细描述

让我们抓取这个文件,看看我们需要了解哪些内容。

代码语言:json
复制
{
  "operation": "delete-index"
},
{
  "operation": {
    "operation-type": "create-index",
    "settings": {{index_settings | default({}) | tojson}}
  }
},
{
  "name": "check-cluster-health",
  "operation": {
    "operation-type": "cluster-health",
    "index": "logs-*",
    "request-params": {
      "wait_for_status": "{{cluster_health | default('green')}}",
      "wait_for_no_relocating_shards": "true"
    },
    "retry-until-success": true
  }
},
{%- if runtime_fields is defined %}
  {
    "operation": "create-timestamp-pipeline"
  },
  {
    "operation": "index-append-with-timestamp-pipeline",
    "warmup-time-period": 240,
    "clients": {{bulk_indexing_clients | default(8)}},
    "ignore-response-error-level": "{{error_level | default('non-fatal')}}"
  },
{%- else %}
  {
    "operation": "index-append",
    "warmup-time-period": 240,
    "clients": {{bulk_indexing_clients | default(8)}},
    "ignore-response-error-level": "{{error_level | default('non-fatal')}}"
  },
{%- endif %}
{
  "name": "refresh-after-index",
  "operation": "refresh"
},
{
  "operation": {
    "operation-type": "force-merge",
    "request-timeout": 7200
  }
},
{
  "name": "refresh-after-force-merge",
  "operation": "refresh"
},
{
  "name": "wait-until-merges-finish",
  "operation": {
    "operation-type": "index-stats",
    "index": "_all",
    "condition": {
      "path": "_all.total.merges.current",
      "expected-value": 0
    },
    "retry-until-success": true,
    "include-in-reporting": false
  }
},
...

原始文件比这个摘录要长得多,但这有助于我们理解正在发生的事情以及如何处理。

每个 track 的第一个任务是 delete-index。我们希望删除任何现有索引,否则我们无法正确计算所有文档是否成功索引,任何段、强制合并持续时间等都不是可代表的值。将其作为第一个步骤而不是基准测试“清理”阶段的原因是,也许你希望在运行后检查集群中的数据,而如果我们作为最后一步删除它,你就无法做到这一点。或者当你取消正在运行的基准测试时,这确保我们总是从头开始。

第二个任务通常是创建与此关联的索引。我们只是做一个 PUT indexname

第三个任务非常重要,它是 check-cluster-health,等待集群变为绿色,表示所有分片都已分配,集群处于良好的健康状态。如果你对单节点集群进行基准测试,除非你设置所有副本为 0,否则这个集群永远不会是绿色的。

这一部分:{{cluster_health | default('green')}} 表示有一个参数叫 cluster_health,你可以像我们对 bulk_indexing_clients 一样覆盖。传递 cluster_health: 'yellow' 如果你想对黄色状态集群进行基准测试。不过我不建议这样做。这个任务也会重试,直到达到成功标准。

接下来有趣的事情是一个 if 条件。在这种情况下,当我们运行带有 runtime_fields 参数时,我们会运行不同的 index-append 任务,更具体地说是:index-append-with-timestamp-pipeline。我们尚未使用此参数,因此我们落在 else 部分并运行正常的 index-append。这就是这些 track 文件的构建方式及其工作原理。

在无网络环境中运行你的第一个 race

在无网络环境中运行 track 会稍微复杂一些,因为 Rally 不能直接从 GitHub 获取并下载所需的文件。让我们逐步了解一下。我们想要运行相同的 http_logs track。这个 track 是这个 GitHub 仓库 的一部分。运行 esrally list tracks,它会报错并告诉你无法克隆这个仓库。

  1. 始终运行 esrally list tracks。不管错误如何,它会设置你稍后需要的文件夹结构和配置文件。
  2. 从 GitHub 仓库下载当前的 master 压缩包。
  3. 将压缩包复制到你的 Rally 主机上。
  4. 然后解压 master.zip。
  5. 现在有一个名为 rally-tracks-master 的文件夹——进入其中。然后你应该看到一个文件夹列表。现在这些文件夹必须进入 $HOME/.rally/benchmarks/tracks/default。最好使用 cp -r * ~/.rally/benchmarks/tracks/default
  6. 现在运行 esrally list tracks --offline。这应该给你一个错误,提示该仓库不是一个 git 仓库。你也可以使用 --track-path=~/Projects/tracks/app-logs 选项并指向包含你的 track 描述和数据的目录。
  7. 进入 ~/.rally/benchmarks/tracks/default,运行 git init
  8. 运行 git add --all 然后 git commit -m "init"
  9. 现在 esrally list tracks --offline 应该可以工作并列出所有 track,并给你一条警告信息 [WARNING] Local changes in [/home/philipp/.rally/benchmarks/tracks/default] prevent tracks update from remote. Please commit your changes.
  10. 尝试使用上面的命令运行一个 race:
代码语言:bash
复制
esrally race --track=http_logs --kill-running-processes --target-hosts=https://to-benchmark.elastic.co:9200 --pipeline=benchmark-only --client-options="verify_certs:false,basic_auth_user:'rally',basic_auth_password:'rally123'" --track-params='{"bulk_indexing_clients":20,"number_of_shards":1,"number_of_replicas":1}'

,但在最后加上 --offline。如第 6 步所示,你可以将其切换为 --track-path 而不是 --track

  1. 这将导致错误:ERROR Cannot race. Error in task executor Cannot find /home/philipp/.rally/benchmarks/data/http_logs/documents-181998.json.bz2. Please disable offline mode and retry。
  2. 现在我们知道缺少基准测试数据以及放置它的位置。基准测试数据目录可以在 rally.ini 中使用 local.dataset.cache 设置进行更改。
  3. 有两种方法可以做到这一点。第一种变体是使用 离线下载器。第二种变体可能会在离线下载器对某个特定 track 无法正常工作时使用。
  4. 首先:使用离线下载器,例如:
    1. curl -O https://raw.githubusercontent.com/elastic/rally-tracks/master/download.sh && chmod u+x download.sh
    2. ./download.sh http_logs
    3. 将创建的 tar 文件复制到你的无网络 Rally 主机上。
    4. 你现在可以解压此文件,它将为你提供 track 描述和基准测试数据。但是,由于我们已经有了 track 描述,你只需要将提取的 benchmarks/data 目录中的内容复制到你的数据路径,默认为 .rally/benchmarks/data/
  5. 像第 10 步一样重新启动 track,它将毫无问题地工作。

当下载器不起作用时,可以使用备用方案。你可以在有互联网的机器上使用你想要的 track 运行 ES Rally。运行 track,并等待直到它开始执行 DELETE index 或类似操作。这意味着所有数据都已下载并解压。你现在可以去 ES Rally 的数据文件夹并复制它,然后将其传输到无网络的主机上。不过,我不推荐这种方法!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装 Rally
  • 在无网络环境中安装 Rally
  • 配置 Rally
  • 理解什么是 race
    • 什么是挑战?
  • 在无网络环境中运行你的第一个 race
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档