elasticsearch 配置 TLS 证书

在生产环境中,我们需要为 elasticsearch 集群配置 TLS 证书以保证安全。elasticsearch 的 TLS 证书配置分别是节点之间通讯证书、https 访问证书和 kibana https 证书。

配置节点证书

在节点之间配置 TLS 是防止未授权节点访问 Elasticsearch 集群的基本安全设置,对于多节点集群来说是必需的。在安全的集群中,Elasticsearch 节点在与其他节点通信时会使用证书来识别自己。

集群中每个节点的 TLS 证书必须是同一 CA 颁发,所以需要先生成一个 CA 证书。

生成 CA

在启动 Elasticsearch 之前,在集群中的任意单个节点上使用 elasticsearch-certutil 工具生成集群的 CA。

1
./bin/elasticsearch-certutil ca

生成过程:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
➜  es-node1 ./bin/elasticsearch-certutil ca
This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.

The 'ca' mode generates a new 'certificate authority'
This will create a new X.509 certificate and private key that can be used
to sign certificate when running in 'cert' mode.

Use the 'ca-dn' option if you wish to configure the 'distinguished name'
of the certificate authority

By default the 'ca' mode produces a single PKCS#12 output file which holds:
* The CA certificate
* The CA's private key

If you elect to generate PEM format certificates (the -pem option), then the output will
be a zip file containing individual files for the CA certificate and private key

Please enter the desired output file [elastic-stack-ca.p12]: # 证书文件名,保持默认即可
Enter password for elastic-stack-ca.p12 : # 证书密码,可以为空

生成后会在当前目录创建 elastic-stack-ca.p12 证书文件。

生成节点证书

1
2
3
4
5
./bin/elasticsearch-certutil cert \
--ca elastic-stack-ca.p12
--days 3650 \
--ip 192.168.1.1,192.168.1.2,192.168.1.3 \
--dns node1.example.com,node2.example.com,node3.example.com
  • --ca 指定刚刚生成的 ca 证书
  • --days 指定证书有效期
  • --ip 指定所有节点的 IP 地址
  • --dns 指定所有节点的 hostname,指定了上面的 ip 地址时可以删除此项

生成过程中需要输入 CA 证书密码、节点证书名称和节点证书密码:

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
40
41
42
43
44
45
46
47
48
49
50
➜  es-node1 ./bin/elasticsearch-certutil cert \
--ca elastic-stack-ca.p12
--days 3650 \
--ip 192.168.1.1,192.168.1.2,192.168.1.3
This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.

The 'cert' mode generates X.509 certificate and private keys.
* By default, this generates a single certificate and key for use
on a single instance.
* The '-multiple' option will prompt you to enter details for multiple
instances and will generate a certificate and key for each one
* The '-in' option allows for the certificate generation to be automated by describing
the details of each instance in a YAML file

* An instance is any piece of the Elastic Stack that requires an SSL certificate.
Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats
may all require a certificate and private key.
* The minimum required value for each instance is a name. This can simply be the
hostname, which will be used as the Common Name of the certificate. A full
distinguished name may also be used.
* A filename value may be required for each instance. This is necessary when the
name would result in an invalid file or directory name. The name provided here
is used as the directory name (within the zip) and the prefix for the key and
certificate files. The filename is required if you are prompted and the name
is not displayed in the prompt.
* IP addresses and DNS names are optional. Multiple values can be specified as a
comma separated string. If no IP addresses or DNS names are provided, you may
disable hostname verification in your SSL configuration.

* All certificates generated by this tool will be signed by a certificate authority (CA)
unless the --self-signed command line option is specified.
The tool can automatically generate a new CA for you, or you can provide your own with
the --ca or --ca-cert command line options.

By default the 'cert' mode produces a single PKCS#12 output file which holds:
* The instance certificate
* The private key for the instance certificate
* The CA certificate

If you specify any of the following options:
* -pem (PEM formatted output)
* -keep-ca-key (retain generated CA key)
* -multiple (generate multiple certificates)
* -in (generate certificates from an input file)
then the output will be be a zip file containing individual certificate/key files

Enter password for CA (elastic-stack-ca.p12) : # CA 证书的密码,可为空
Please enter the desired output file [elastic-certificates.p12]: # 要输出的节点证书名称
Enter password for elastic-certificates.p12 : # 节点证书密码,可为空

修改节点配置

将生成的 elastic-certificates.p12 证书复制到 {ES_HOME}/config/certs 目录下,没有目录时新建。

elasticsearch.yml 配置文件中增加安全配置:

1
2
3
4
5
6
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.client_authentication: required
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12

(可选)如果在创建节点证书时输入了密码,运行以下命令将密码存储在 elasticsearch 密钥库中:

1
2
./bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
./bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password

配置 elasticsearch HTTPS 证书

在上面的章节中配置的是节点之间通讯所使用的 TLS 证书,还需要配置 elasticsearch 客户端使用 https。

生成 HTTPS 证书

生成 HTTPS 证书同样需要 CA,可以复用上面章节生成的 CA。

在集群任意节点上执行命令生成 HTTPS 证书:

1
./bin/elasticsearch-certutil http

该命令会生成一个包含用于 Elasticsearch 和 Kibana 的证书和密钥的 .zip 文件。每个文件夹中都包含一个 README.txt ,解释了如何使用这些文件。

生成过程是交互式的,需要根据提示填写,示例如下:

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
➜  es-node1 ./bin/elasticsearch-certutil http

## Elasticsearch HTTP Certificate Utility

The 'http' command guides you through the process of generating certificates
for use on the HTTP (Rest) interface for Elasticsearch.

This tool will ask you a number of questions in order to generate the right
set of files for your needs.

## Do you wish to generate a Certificate Signing Request (CSR)?

A CSR is used when you want your certificate to be created by an existing
Certificate Authority (CA) that you do not control (that is, you don't have
access to the keys for that CA).

If you are in a corporate environment with a central security team, then you
may have an existing Corporate CA that can generate your certificate for you.
Infrastructure within your organisation may already be configured to trust this
CA, so it may be easier for clients to connect to Elasticsearch if you use a
CSR and send that request to the team that controls your CA.

If you choose not to generate a CSR, this tool will generate a new certificate
for you. That certificate will be signed by a CA under your control. This is a
quick and easy way to secure your cluster with TLS, but you will need to
configure all your clients to trust that custom CA.

# 是否生成 CSR,因为要复用 CA 证书,所以这里写 n
Generate a CSR? [y/N]n

## Do you have an existing Certificate Authority (CA) key-pair that you wish to use to sign your certificate?

If you have an existing CA certificate and key, then you can use that CA to
sign your new http certificate. This allows you to use the same CA across
multiple Elasticsearch clusters which can make it easier to configure clients,
and may be easier for you to manage.

If you do not have an existing CA, one will be generated for you.

# 是否使用现有的 CA,因为要复用 CA 证书,写 y
Use an existing CA? [y/N]y

## What is the path to your CA?

Please enter the full pathname to the Certificate Authority that you wish to
use for signing your new http certificate. This can be in PKCS#12 (.p12), JKS
(.jks) or PEM (.crt, .key, .pem) format.
# 这里要填写要复用的 CA 证书路径,如果不存在会提示
CA Path: /opt/es-node1/elastic-stack-ca.p12
Reading a PKCS12 keystore requires a password.
It is possible for the keystore's password to be blank,
in which case you can simply press <ENTER> at the prompt
Password for elastic-stack-ca.p12: # 输入 CA 密码,没有密码按回车

## How long should your certificates be valid?

Every certificate has an expiry date. When the expiry date is reached clients
will stop trusting your certificate and TLS connections will fail.

Best practice suggests that you should either:
(a) set this to a short duration (90 - 120 days) and have automatic processes
to generate a new certificate before the old one expires, or
(b) set it to a longer duration (3 - 5 years) and then perform a manual update
a few months before it expires.

You may enter the validity period in years (e.g. 3Y), months (e.g. 18M), or days (e.g. 90D)

# 证书有效期,默认 5 年
For how long should your certificate be valid? [5y]

## Do you wish to generate one certificate per node?

If you have multiple nodes in your cluster, then you may choose to generate a
separate certificate for each of these nodes. Each certificate will have its
own private key, and will be issued for a specific hostname or IP address.

Alternatively, you may wish to generate a single certificate that is valid
across all the hostnames or addresses in your cluster.

If all of your nodes will be accessed through a single domain
(e.g. node01.es.example.com, node02.es.example.com, etc) then you may find it
simpler to generate one certificate with a wildcard hostname (*.es.example.com)
and use that across all of your nodes.

However, if you do not have a common domain name, and you expect to add
additional nodes to your cluster in the future, then you should generate a
certificate per node so that you can more easily generate new certificates when
you provision new nodes.

# 是否为每个节点生成单独的证书,这里选择 n
Generate a certificate per node? [y/N]n

## Which hostnames will be used to connect to your nodes?

These hostnames will be added as "DNS" names in the "Subject Alternative Name"
(SAN) field in your certificate.

You should list every hostname and variant that people will use to connect to
your cluster over http.
Do not list IP addresses here, you will be asked to enter them later.

If you wish to use a wildcard certificate (for example *.es.example.com) you
can enter that here.

Enter all the hostnames that you need, one per line.
When you are done, press <ENTER> once more to move on to the next step.

# 这里填写 elasticsearch 集群每个节点的 hostname,一般使用 IP 地址,所以这里可以不填写

You did not enter any hostnames.
Clients are likely to encounter TLS hostname verification errors if they
connect to your cluster using a DNS name.

# 确认填写是否正确
Is this correct [Y/n]y

## Which IP addresses will be used to connect to your nodes?

If your clients will ever connect to your nodes by numeric IP address, then you
can list these as valid IP "Subject Alternative Name" (SAN) fields in your
certificate.

If you do not have fixed IP addresses, or not wish to support direct IP access
to your cluster then you can just press <ENTER> to skip this step.

Enter all the IP addresses that you need, one per line.
When you are done, press <ENTER> once more to move on to the next step.

# 这里填写 elasticsearch 集群每个节点的 IP 地址
192.168.1.1
192.168.1.2
192.168.1.3

You entered the following IP addresses.

- 192.168.1.1
- 192.168.1.2
- 192.168.1.3

# 确认填写是否正确
Is this correct [Y/n]y

## Other certificate options

The generated certificate will have the following additional configuration
values. These values have been selected based on a combination of the
information you have provided above and secure defaults. You should not need to
change these values unless you have specific requirements.

Key Name: elasticsearch
Subject DN: CN=elasticsearch
Key Size: 2048

# 确认还需要变动吗,n
Do you wish to change any of these options? [y/N]n

## What password do you want for your private key(s)?

Your private key(s) will be stored in a PKCS#12 keystore file named "http.p12".
This type of keystore is always password protected, but it is possible to use a
blank password.

# 是否需要设置证书密码
If you wish to use a blank password, simply press <enter> at the prompt below.
Provide a password for the "http.p12" file: [<ENTER> for none]

## Where should we save the generated files?

A number of files will be generated including your private key(s),
public certificate(s), and sample configuration options for Elastic Stack products.

These files will be included in a single zip archive.

What filename should be used for the output zip file? [/opt/es-node1/elasticsearch-ssl-http.zip]

Zip file written to /opt/es-node1/elasticsearch-ssl-http.zip

生成的 elasticsearch-ssl-http.zip 文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
➜  elasticsearch-ssl-http tree
.
├── elasticsearch
│   ├── http.p12
│   ├── README.txt
│   └── sample-elasticsearch.yml
└── kibana
├── elasticsearch-ca.pem
├── README.txt
└── sample-kibana.yml

3 directories, 6 files

配置 HTTPS 证书

解压 elasticsearch-ssl-http.zip 文件,将 elasticsearch 文件夹下的 http.p12 证书复制到每个节点的 ES_HOME/config/certs 目录。

elasticsearch.yml 配置文件中增加安全配置:

1
2
xpack.security.http.ssl.enabled: true
xpack.security.http.ssl.keystore.path: certs/http.p12

(可选)如果在创建节点证书时输入了密码,运行以下命令将密码存储在 elasticsearch 密钥库中:

1
./bin/elasticsearch-keystore add xpack.security.http.ssl.keystore.secure_password

配置 kibana HTTPS 证书

Kibana 需要加密两种类型的 HTTPS 流量:

  • 从 Kibana 到 Elasticsearch 的出站请求:Kibana 作为 HTTP 客户端,必须配置为信任 Elasticsearch 使用的 TLS 证书。
  • 从浏览器或 API 客户端的入站请求发送到 Kibana:Kibana 充当 HTTP 服务器,配置 https 证书加密客户端流量。

配置 Kibana 到 elasticsearch 的证书

复制 elasticsearch-ssl-http.zip 文件中的 kibana 目录下的 elasticsearch-ca.pem 文件到 KIBANA_HOME/config/certs 目录。

kibana.yml 配置文件中增加安全配置:

1
elasticsearch.ssl.certificateAuthorities: config/certs/elasticsearch-ca.pem

修改为 HTTPS 的 elasticsearch 集群地址:

1
elasticsearch.hosts: https://<your_elasticsearch_host>:9200

配置 Kibana 的 HTTPS 证书

为了安全地通过浏览器访问 Kibana,需要为 Kibana 生成一个 TLS 证书和私钥。Kibana 使用这些证书来加密 HTTP 流量,并与连接的浏览器或 API 客户端建立信任关系。

1
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout kibana.key -out kibana.crt

将生成的 kibana.keykibana.crt 复制到 KIBANA_HOME/config/certs 目录中。

kibana.yml 配置文件中增加安全配置并重启 kibana:

1
2
3
server.ssl.enabled: true
server.ssl.certificate: config/certs/kibana.crt
server.ssl.key: config/certs/kibana.key

参考

elasticsearch 配置 TLS 证书

https://cui.cc/elasticsearch-tls-cert/

作者

南山崔崔

发布于

2025-06-17

许可协议