linux下安装Elasticsearch集群

摘要

  • 本文介绍如何在linux下安装Elasticsearch集群(三节点)

  • Elasticsearch版本8.17.3

  • 单节点安装参考linux下安装Elasticsearch,本文在此基础上完成集群安装

集群安装

  • 三个节点分别安照单节点的安装方式完成下载,用户配置,环境变量配置,系统参数配置,等等。

  • 任选一个节点作为集群的master节点,我们命名为node-1,其它节点命名为node-2node-3

  • 先配置node-1节点的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 集群名称
cluster.name: test-elk
# 节点名称
node.name: node-1
# 数据存储路径,默认值 $ES_HOME/data
path.data: /usr/local/elasticsearch/data
# 日志存储路径,默认值 $ES_HOME/logs
path.logs: /usr/local/elasticsearch/logs
# 配置ES启动时是否进行内存锁定检查,默认值true,本机内存比较小时设置为false
bootstrap.memory_lock: false
# 允许通过任何地址访问,开启远程访问
network.host: 0.0.0.0
# 配置当前ES节点对外提供服务的http端口,默认 9200
http.port: 9200
# 配置当前ES节点对外提供服务的tcp端口,默认 9300
transport.port: 9300
# 集群内的主机列表,开启安全认证时无需配置,注意这里不要进行配置
# discovery.seed_hosts: ["127.0.0.1"]
# 第一次启动时需要参与选主的节点名称,这里只配置node-1
cluster.initial_master_nodes: ["node-1"]
#解决跨域问题
http.cors.enabled: true
http.cors.allow-origin: "*"
  • 配置node-2节点的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 集群名称
cluster.name: test-elk
# 节点名称
node.name: node-2
# 数据存储路径,默认值 $ES_HOME/data
path.data: /usr/local/elasticsearch/data
# 日志存储路径,默认值 $ES_HOME/logs
path.logs: /usr/local/elasticsearch/logs
# 配置ES启动时是否进行内存锁定检查,默认值true,本机内存比较小时设置为false
bootstrap.memory_lock: false
# 允许通过任何地址访问,开启远程访问
network.host: 0.0.0.0
# 配置当前ES节点对外提供服务的http端口,默认 9200
http.port: 9200
# 配置当前ES节点对外提供服务的tcp端口,默认 9300
transport.port: 9300
#解决跨域问题
http.cors.enabled: true
http.cors.allow-origin: "*"
  • 配置node-3节点的配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 集群名称
cluster.name: test-elk
# 节点名称
node.name: node-3
# 数据存储路径,默认值 $ES_HOME/data
path.data: /usr/local/elasticsearch/data
# 日志存储路径,默认值 $ES_HOME/logs
path.logs: /usr/local/elasticsearch/logs
# 配置ES启动时是否进行内存锁定检查,默认值true,本机内存比较小时设置为false
bootstrap.memory_lock: false
# 允许通过任何地址访问,开启远程访问
network.host: 0.0.0.0
# 配置当前ES节点对外提供服务的http端口,默认 9200
http.port: 9200
# 配置当前ES节点对外提供服务的tcp端口,默认 9300
transport.port: 9300
#解决跨域问题
http.cors.enabled: true
http.cors.allow-origin: "*"
  • 启动node-1,启动成功后我们会在日志中看到如下安全认证信息信息,记录其中的加入集群的节点的token信息

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
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# 这些日志信息提供了 Elasticsearch 8 在首次启动时自动配置的安全特性、生成的默认密码、证书指纹以及如何配置 Kibana 和其他节点加入集群的详细说明。
✅ Elasticsearch security features have been automatically configured!
✅ Authentication is enabled and cluster connections are encrypted.

# 这是 elastic 用户的默认密码。系统建议你使用 bin/elasticsearch-reset-password -u elastic 命令来重置此密码。
ℹ️ Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
BNb=*qz6_M*mXL9uZiSP # 这里会打印出ES自动生成的密码

# 这是 HTTP CA 证书的 SHA-256 指纹。CA 证书用于验证 HTTPS 连接的身份。
ℹ️ HTTP CA certificate SHA-256 fingerprint:
3f141c16dfc6e165894bc1672084b220dcdd22ffe02116d51ac18003cfaa5a1d

# 提供了将 Kibana 配置为使用此 Elasticsearch 集群的步骤。
ℹ️ Configure Kibana to use this cluster:
# 启动 Kibana 并按照终端中显示的配置链接进行操作。
• Run Kibana and click the configuration link in the terminal when Kibana starts.
# 复制提供的注册令牌,并在浏览器中打开 Kibana 时粘贴此令牌。令牌在接下来的 30 分钟内有效。
# 补充说明:生成新的kibana注册令牌:bin/elasticsearch-create-enrollment-token -s kibana
• Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):
eyJ2ZXIiOiI4LjE0LjAiLCJhZHIiOlsiMTAuMjUwLjAuMjM5OjkyMDAiXSwiZmdyIjoiM2YxNDFjMTZkZmM2ZTE2NTg5NGJjMTY3MjA4NGIyMjBkY2RkMjJmZmUwMjExNmQ1MWFjMTgwMDNjZmFhNWExZCIsImtleSI6ImFSWFBzcFVCTVVmc1d5aUJnbjBtOmRzbVBLQV95UXhDZkJpXzQyWDNEMVEifQ==

# 提供了将新的 Elasticsearch 节点加入现有集群的步骤。
ℹ️ Configure other nodes to join this cluster:
# 使用 bin/elasticsearch --enrollment-token <token> 命令启动新的 Elasticsearch 节点。令牌在接下来的 30 分钟内有效。
# 补充说明:生成新的节点注册令牌:bin/elasticsearch-create-enrollment-token -s node
• Copy the following enrollment token and start new Elasticsearch nodes with `bin/elasticsearch --enrollment-token <token>` (valid for the next 30 minutes):
eyJ2ZXIiOiI4LjE0LjAiLCJhZHIiOlsiMTAuMjUwLjAuMjM5OjkyMDAiXSwiZmdyIjoiM2YxNDFjMTZkZmM2ZTE2NTg5NGJjMTY3MjA4NGIyMjBkY2RkMjJmZmUwMjExNmQ1MWFjMTgwMDNjZmFhNWExZCIsImtleSI6ImF4WFBzcFVCTVVmc1d5aUJnbjFtOkFEallPdnpzUzh1MGJqOFpfVFE4a3cifQ==
# 如果你在 Docker 中运行 Elasticsearch,可以使用 docker run -e "ENROLLMENT_TOKEN=<token>" docker.elastic.co/elasticsearch/elasticsearch:8.17.3 命令启动新节点。
If you're running in Docker, copy the enrollment token and run:
`docker run -e "ENROLLMENT_TOKEN=<token>" docker.elastic.co/elasticsearch/elasticsearch:8.17.3`
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  • token信息有效时间只有30分钟,超时后可以使用如下命令重新生成新的token

1
2
3
# 进入node-1节点的ES安装目录,此命令会生成新的节点注册令牌
bin/elasticsearch-create-enrollment-token -s node
eyJ2ZXIiOiI4LjE0LjAiLCJhZHIiOlsiMTAuMjUwLjAuMjM5OjkyMDAiXSwiZmdyIjoiM2YxNDFjMTZkZmM2ZTE2NTg5NGJjMTY3MjA4NGIyMjBkY2RkMjJmZmUwMjExNmQ1MWFjMTgwMDNjZmFhNWExZCIsImtleSI6IlpoRVZ4NVVCeXRPajdLeTBqb3BvOmdHRW44ZW5mU0RDZHA2Yy1uSDF4eGcifQ==
  • 启动node-2节点,启动时要带上token信息,node-3与此一样。这里要注意,只有第一次加入集群时启动才需要带上注册令牌,后续启动不需要token信息

1
2
3
# 进入node-2节点的ES安装目录,此命令会启动ES节点,并加入到集群中
# bin/elasticsearch --enrollment-token <token>
bin/elasticsearch --enrollment-token eyJ2ZXIiOiI4LjE0LjAiLCJhZHIiOlsiMTAuMjUwLjAuMjM5OjkyMDAiXSwiZmdyIjoiM2YxNDFjMTZkZmM2ZTE2NTg5NGJjMTY3MjA4NGIyMjBkY2RkMjJmZmUwMjExNmQ1MWFjMTgwMDNjZmFhNWExZCIsImtleSI6IlpoRVZ4NVVCeXRPajdLeTBqb3BvOmdHRW44ZW5mU0RDZHA2Yy1uSDF4eGcifQ==

加入集群失败

  • 在执行bin/elasticsearch --enrollment-token <token>时有可能会遇到如下异常:
1
ERROR: Aborting auto configuration because the node keystore contains password settings already, with exit code 78
  • 这通常意味着节点的 keystore 已经包含了密码设置,因此自动配置过程被中止。
  • 产生这种错误的原因大概率是当前ES是从已经安装好的服务器上拷贝过来的,而不是全新安装的,此时我们除了需要清空data目录外,还需要删除 keystore 中的密码
1
2
3
4
5
6
7
8
9
10
11
# 查看keystore中的密码
$ bin/elasticsearch-keystore list
keystore.seed
xpack.security.http.ssl.keystore.secure_password
xpack.security.transport.ssl.keystore.secure_password
xpack.security.transport.ssl.truststore.secure_password

# 删除keystore中的密码
$ bin/elasticsearch-keystore remove xpack.security.http.ssl.keystore.secure_password
$ bin/elasticsearch-keystore remove xpack.security.transport.ssl.keystore.secure_password
$ bin/elasticsearch-keystore remove xpack.security.transport.ssl.truststore.secure_password
  • 再次执行bin/elasticsearch --enrollment-token <token>即可。

  • 另外还可能遇到如下报错:

1
ERROR: Aborting enrolling to cluster. Could not communicate with the node on any of the addresses from the enrollment token. All of [10.250.0.239:9200] were attempted., with exit code 69
  • 这是由于token过期了,重新生成新的token即可
  • 启动成功后会在node-2的配置文件中看到如下配置信息

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
#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically
# generated to configure Elasticsearch security features on 24-03-2025 07:45:11
#
# --------------------------------------------------------------------------------

# Enable security features
xpack.security.enabled: true

xpack.security.enrollment.enabled: true

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
enabled: true
keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
enabled: true
verification_mode: certificate
keystore.path: certs/transport.p12
truststore.path: certs/transport.p12
# Discover existing nodes in the cluster
discovery.seed_hosts: ["10.250.0.239:9300"] # 这里是node-1节点的IP地址和端口号

## 而node-3这里会显示如下信息,也就是越是后来加入进来的,这里就会加上之前所有节点的IP地址和端口号
discovery.seed_hosts: ["10.250.0.239:9300", "10.250.0.17:9300"] # 这里是node-1和node-2节点的IP地址和端口号

#----------------------- END SECURITY AUTO CONFIGURATION -------------------------

验证集群状态

  • 启动完成后,可以通过如下命令查看集群状态:

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
# 查看集群状态
$ curl -u elastic:123456 --cacert /usr/local/elasticsearch/elasticsearch-8.17.3/config/certs/http_ca.crt https://127.0.0.1:9200/_cluster/health?pretty
{
"cluster_name" : "test-elk",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 3,
"number_of_data_nodes" : 3,
"active_primary_shards" : 31,
"active_shards" : 62,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"unassigned_primary_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
# 查看节点信息,其中 * 表示当前节点为master节点
$ curl -u elastic:123456 --cacert /usr/local/elasticsearch/elasticsearch-8.17.3/config/certs/http_ca.crt https://127.0.0.1:9200/_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
10.250.0.17 8 63 28 1.03 0.38 0.14 cdfhilmrstw - node-2
10.250.0.173 9 63 22 0.86 0.37 0.19 cdfhilmrstw - node-3
10.250.0.239 9 78 6 0.38 0.22 0.16 cdfhilmrstw * node-1

重要说明

  • 集群一旦创建完成,则至少需要两个节点运行才能保证集群可用性,否则集群将无法运行。

  • 如果node-1节点挂了,集群中剩下的两个节点会重新选择一个新的master节点,不会影响集群的可用性。

  • 一旦关闭node-1,则重新启动node-1节点前要修改其配置文件,否则无法启动成功。

1
2
3
4
# 注释掉该配置
# cluster.initial_master_nodes: ["node-1"]
# 加入该配置,实际上最好在重启每个节点前,将每个节点都配置为这样
discovery.seed_hosts: ["10.250.0.239:9300", "10.250.0.17:9300", "10.250.0.173:9300"]

Kibana关联ES集群

  • Kibana 关联单节点ES参考 linux下安装Kibana,在此基础上对配置文件进行如下修改:

1
2
3
4
# 关联集群的节点地址,将集群内所有节点的地址都配置到该字段中
elasticsearch.hosts: ['https://10.250.0.239:9200', 'https://10.250.0.173:9200', 'https://10.250.0.17:9200']
# 关联集群的输出地址,将集群内所有节点的地址都配置到该字段中
xpack.fleet.outputs: [{id: fleet-default-output, name: default, is_default: true, is_default_monitoring: true, type: elasticsearch, hosts: ['https://10.250.0.239:9200', 'https://10.250.0.173:9200', 'https://10.250.0.17:9200'], ca_trusted_fingerprint: 3f141c16dfc6e165894bc1672084b220dcdd22ffe02116d51ac18003cfaa5a1d}]
  • 重启Kibana

  • 只要保证集群内至少有两个ES节点工作正常,即可正常访问Kibana

nginx反向代理ES集群

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
36
37
38
39
vim /etc/nginx/conf.d/es.conf # 添加如下内容
upstream es {
server 10.250.0.239:9200;
server 10.250.0.173:9200;
server 10.250.0.17:9200;
}
server {
listen 8888;
server_name localhost es.domain;

location / {
proxy_pass https://es; #这个名称和要上面 upstream es 对应,注意这里是 https 协议
proxy_redirect default;
proxy_http_version 1.1;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_max_temp_file_size 0;

#this is the maximum upload size
client_max_body_size 10m;
client_body_buffer_size 128k;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffering off;
proxy_request_buffering off; # Required for HTTP CLI commands
proxy_set_header Connection ""; # Clear for keepalive

}
}

# 重启nginx
systemctl restart nginx
# 访问ES集群
curl -u elastic:123456 http://127.0.0.1:8888/_cluster/health?pretty