ElasticSearch Cluster 安装与配置

本文介绍了在 AWS 上安装与配置 ElasticSearch 集群,并通过 ALB 做负载均衡。同时,采用 filebeat 采集各节点的日志用于问题的分析与定位。

节点

节点角色

角色 简称 备注
master m 主节点
data d 数据节点
data_content s 内容数据节点
data_hot h 热点数据节点
data_warm w 中温数据节点
data_cold c 冷数据节点
data_frozen f 封存数据节点
ingest i 数据处理节点
ml l 机器学习节点
remote_cluster_client r 远程候选节点
transform t 转换节点
voting_only v 仅投票节点
coordinator / 仅协调节点

一个集群必须有以下角色:

  • master
  • (data_content & data_hot) OR data,不可同时设置为 datadata_ 节点
  • 候选主节点:
    1
    2
    3
    4
    node.roles: [master]

    # 只有选举权的节点,master/voting_only必须一起使用
    node.role: [master, voting_only]

节点配置

硬件配置

角色 存储 内存 计算 网络 描述
主节点 管理集群状态
数据节点 极高 存储和检索数据
Ingest 节点 转换输入数据
机器学习节点 极高 极高 机器学习
协调节点 请求转发和合并检索结果

注意事项:

  • 堆内存设置不超过 32GB,一般建议最大值为 31GB

    1
    2
    3
    4
    5
    6
    # 修改 jvm.options
    -Xms16g
    -Xmx16g

    # 或者
    ES_JAVA_OPTS="-Xms16g -Xmx16g" ./bin/elasticsearch -d
  • CPU:ES 会做动态分配,不建议更改线程池和队列大小

  • 硬盘

    • 热数据,推荐内存/磁盘比例 1:96
    • 冷数据,推荐内存/磁盘比例 1:480
    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 副本数量:至少 1 个副本
    # 索引开销:通常比源数据大 10%
    # 操作系统预留空间:5%
    # ElasticSearch 内部开销:段合并、日志等内部操作,预留 20%
    # 安全阈值:15%

    最小磁盘大小 = 源数据 * (1 + 副本数量) * 索引开销 / (1 - 操作系统预留空间) / (1 - Elasticsearch内部开销) / (1 - 安全阈值)
    = 源数据 * (1 + 副本数量) * 1.7
    = 源数据 * 3.4

实际配置

  • 3 个主节点
  • 4 个热数据节点
  • 3 个温/冷/冻结数据节点
  • 2 个协调节点
Role Instance CPU Memory Disk Count
master m6g.large 2 8GB 50GB/gp3 3
coordinator m6g.xlarge 4 16GB 50GB/gp3 2
data_hot
ingest
r6g.xlarge 4 32GB 1TB/gp3 4
data_warm
data_cold
data_frozen
r6g.xlarge 4 32GB 50GB/gp3
10TB/HDD
3

部署

Arch

基本配置

  • 创建用户 elastic,sudo useradd -m -s /bin/bash elastic,elasticsearch 进程以该用户运行
  • 安装工具
    1
    2
    3
    4
    sudo apt install build-essential net-tools
    sudo snap install nvim --classic
    sudo mkdir -p /opt/elasticsearch/data /opt/elasticsearch/logs
    sudo chown elastic:elastic -R /opt/elasticsearch
  • 修改机器的 hostname,如 sudo hostnamectl set-hostname es-master-01
  • 修改虚拟内存,将 vm.max_map_count = 262144 添加到配置文件 /etc/sysctl.conf中,并执行 sudo sysctl -p
  • 将如下配置添加到每个节点的 /etc/hosts 文件中
    1
    2
    3
    172.x.x.x es-master-01
    172.x.x.x es-master-02
    172.x.x.x es-master-03
  • 版本 elasticsearch-8.8.2-linux-aarch64.tar.gz
    1
    2
    3
    4
    5
    6
    # 切换到 elastic 用户
    sudo su - elastic

    wget -O elasticsearch-8.8.2-linux-aarch64.tar.gz https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.8.2-linux-aarch64.tar.gz

    tar -xvf elasticsearch-8.8.2-linux-aarch64.tar.gz
  • 生成 TLS 证书,各节点之间交互使用 9300/TLS 端口 security-basic-setup
    1
    2
    3
    4
    5
    6
    7
    8
    # 在 es-master-01 上执行
    cd elasticsearch-8.8.2

    # 生成 CA,elastic-stack-ca.p12
    ./bin/elasticsearch-certutil ca

    # 生成文件 elastic-certificates.p12,并将该文件拷贝至所有节点的 config/certs 目录
    ./bin/elasticsearch-certuil cert --ca elastic-stack-ca.p12

master 节点

  • 节点命名规则:es-master-01、es-master-02…

配置

  • 第一次启动 es-master-01 的时候加入 cluster.initial_master_nodes 配置,待节点启动成功后,可以删除该配置并重新启动节点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
cluster.name: es-demo
node.name: <节点的名字, es-master-01>
node.attr.rack: us-west-1
node.roles:
- master

path:
data: /opt/elasticsearch/data
logs: /opt/elasticsearch/logs

network.host: 0.0.0.0
http.port: 9200
transport.port: 9300

# 每次启动均需要
discovery.seed_hosts:
- es-master-01
- es-master-02
- es-master-03

# 仅在集群首次启动会使用,然后可以删除
cluster.initial_master_nodes:
- es-master-01

xpack.security.enabled: true
xpack.security.enrollment.enabled: true
xpack.security.http.ssl:
enabled: false

xpack.security.transport.ssl:
enabled: true
verification_mode: certificate
client_authentication: required
keystore.path: certs/elastic-certificates.p12
truststore.path: certs/elastic-certificates.p12

热数据节点

热数据节点同时需要包含 data_content & data_hot 2 个角色,同时为了简化部署将 ingest 角色也添加到热数据节点

  • 角色:data_content、data_host、ingest
  • 节点命名规则:es-data-01、es-data-02…

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
cluster.name: es-demo
node.name: <节点的名字, es-data-01>
node.attr.rack: us-west-1
node.roles:
- data_content
- data_hot
- ingest

path:
data: /opt/elasticsearch/data
logs: /opt/elasticsearch/logs

network.host: 0.0.0.0
http.port: 9200
transport.port: 9300

# 每次启动均需要
discovery.seed_hosts:
- es-master-01
- es-master-02
- es-master-03

xpack.security.enabled: true
xpack.security.enrollment.enabled: true
xpack.security.http.ssl:
enabled: false

xpack.security.transport.ssl:
enabled: true
verification_mode: certificate
client_authentication: required
keystore.path: certs/elastic-certificates.p12
truststore.path: certs/elastic-certificates.p12

冷数据节点

  • 需要先挂载 HDD 硬盘到 /opt/elasticsearch/data 目录,如果条件允许可以直接使用 gp3 的 EBS
  • 节点命名规则:es-data-wcf-01、es-data-wcf-02…

如何挂载 HDD 硬盘

  • EBS 上新建 10TB HDD 存储,HDD 要和所需挂载的 EC2 在一个可用区
  • 通过控制台将 HDD attach 到指定的 EC2
  • 在 EC2 上执行如下操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
nvme0n1 259:0 0 50G 0 disk
├─nvme0n1p1 259:1 0 49.9G 0 part /
└─nvme0n1p15 259:2 0 99M 0 part /boot/efi
nvme1n1 259:3 0 10T 0 disk

# 说明该 EBS 上没有文件系统
$ sudo file -s /dev/nvme1n1
/dev/nvme1n1: data

$ sudo mkfs -t ext4 /dev/nvme1n1
$ sudo mount /dev/nvme1n1 /opt/elasticsearch/data
$ sudo chown elastic:elastic -R /opt/elasticsearch/data

# 设置自动挂载,先查找磁盘的 UUID
$ sudo blkid
/dev/nvme1n1: UUID="xxxxx" BLOCK_SIZE="4096" TYPE="ext4"

$ 修改 /etc/fstab,加入如下字段
UUID="xxxxx" /opt/elasticsearch/data ext4 defaults 0 2

# 检查自动挂载是否生效
$ sudo umount /opt/elasticsearch/data
$ sudo mount -a

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
cluster.name: es-demo
node.name: <节点的名字, es-data-wcf-01>
node.attr.rack: us-west-1
node.roles:
- data_warm
- data_cold
- data_frozen

path:
data: /opt/elasticsearch/data
logs: /opt/elasticsearch/logs

network.host: 0.0.0.0
http.port: 9200
transport.port: 9300

# 每次启动均需要
discovery.seed_hosts:
- es-master-01
- es-master-02
- es-master-03

xpack.security.enabled: true
xpack.security.enrollment.enabled: true
xpack.security.http.ssl:
enabled: false

xpack.security.transport.ssl:
enabled: true
verification_mode: certificate
client_authentication: required
keystore.path: certs/elastic-certificates.p12
truststore.path: certs/elastic-certificates.p12

协调节点

  • 节点命名规则:es-coordinator-01、es-coordinator-02…

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
cluster.name: es-demo
node.name: <节点的名字, es-data-wcf-01>
node.attr.rack: us-west-1
node.roles: []

path:
data: /opt/elasticsearch/data
logs: /opt/elasticsearch/logs

network.host: 0.0.0.0
http.port: 9200
transport.port: 9300

# 每次启动均需要
discovery.seed_hosts:
- es-master-01
- es-master-02
- es-master-03

xpack.security.enabled: true
xpack.security.enrollment.enabled: true
xpack.security.http.ssl:
enabled: false

xpack.security.transport.ssl:
enabled: true
verification_mode: certificate
client_authentication: required
keystore.path: certs/elastic-certificates.p12
truststore.path: certs/elastic-certificates.p12

性能测试

esrally 进行性能测试

1
2
3
cd ~/.local/bin

./esrally race --target-hosts es-nlb:9200 --track=http_logs --client-options="basic_auth_user:'elastic',basic_auth_password:'xxx'" --pipeline=benchmark-only --kill-running-processes

监控

  • 日志:es log & syslog
  • 指标 metrics

日志

采用 filebeat 收集 es 及系统的日志

1
2
3
4
5
6
7
8
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
sudo apt-get install apt-transport-https
echo "deb https://artifacts.elastic.co/packages/oss-8.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-8.x.list

sudo apt update
sudo apt install filebeat=8.8.2
sudo systemctl enable filebeat.service
sudo filebeat modules enable elasticsearch

修改 filebeat 配置文件:

/etc/filebeat/filebeat.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
filebeat.inputs:
- type: filestream
id: syslog
paths:
- /var/log/syslog
parsers:
- syslog:
format: auto

output.elasticsearch:
hosts: ["http://localhost:9200"]
protocol: "http"
username: "elastic"
password: "xxxxx"

# 如下配置可按需设置
# 新创建个 kibana_admin账户,具有 kibana_admin role
setup.dashboards.enabled: true
setup.kibana:
host: "kibana_host:5601"
username: "kibana_admin"
password: "xxxxx"

/etc/filebeat/modules.d/elasticsearch.yml

1
2
3
4
5
6
- module: elasticsearch
server:
enabled: true
var.paths:
- /opt/elasticsearch/logs/*.log
- /opt/elasticsearch/logs/*_server.json

指标

可以采用 categraf 的 elasticsearch 插件采集各指标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[[instances]]
servers = ["http://es-nlb:9200"]
labels = { cluster="elk" }
http_timeout = "10s"
local = false
cluster_health = true
# 拉取 cluster 级别的数据,还是拉取 indices 索引级别的数据
cluster_health_level = "cluster"
cluster_stats = true
indices_level = ""
node_stats = ["jvm", "breaker", "process", "os", "fs", "indices", "thread_pool", "transport"]

username = "elastic"
password = "xxxxx"

常用命令

重置密码

1
2
3
4
5
6
./bin/elasticsearch-reset-password -u elastic

# 设置指定的密码
./bin/elasticsearch-reset-password -i -u elastic

./bin/elasticsearch-reset-password -u kibana_system