Elasticsearch启用https访问

12 篇文章 0 订阅
5 篇文章 0 订阅

1.生成p12格式的CA证书

[sandwich@centos-elk elasticsearch-7.17.1]$ ./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,然后输入密码: aaaaaa并记住。
执行完生成以下文件

[sandwich@centos-elk elasticsearch-7.17.1]$ ls *p12
elastic-stack-ca.p12

2.生成p12格式的certificate证书

接着用如下命令来生成一个新的证书

[sandwich@centos-elk elasticsearch-7.17.1]$ ./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12 
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证书的密码: aaaaaa
输入CA证书密码后,接受新证书的默认文件名,然后为新证书elastic-certificates.p12添加新密码: bbbbbb

Enter password for CA (elastic-stack-ca.p12) : 
Please enter the desired output file [elastic-certificates.p12]: 
Enter password for elastic-certificates.p12 : 

Certificates written to /home/sandwich/app/elk/elasticsearch-7.17.1/elastic-certificates.p12

This file should be properly secured as it contains the private key for 
your instance.

This file is a self contained file and can be copied and used 'as is'
For each Elastic product that you wish to configure, you should copy
this '.p12' file to the relevant configuration directory
and then follow the SSL configuration instructions in the product guide.

For client applications, you may only need to copy the CA certificate and
configure the client to trust this certificate.

完成后,在相同路径下多了一个elastic-certificates.p12证书文件

[sandwich@centos-elk elasticsearch-7.17.1]$ ls *p12
elastic-certificates.p12  elastic-stack-ca.p12

3.把elastic-certificates.p12 证书拷入到 Elasticsearch 安装目录下的config子目录

[sandwich@centos-elk elasticsearch-7.17.1]$ mv elastic-certificates.p12 config/

4.生成pem格式的证书

[sandwich@centos-elk elasticsearch-7.17.1]$ openssl pkcs12 -in elastic-stack-ca.p12 -out ca.crt.pem -clcerts -nokeys
Enter Import Password:
MAC verified OK
[sandwich@centos-elk elasticsearch-7.17.1]$ ls
bin  ca.crt.pem  config  elastic-stack-ca.p12  jdk  lib  LICENSE.txt  logs  modules  NOTICE.txt  plugins  README.asciidoc  startup.log

注意,这里需要输入elastic-stack-ca.p12证书的密码

5.把pem证书拷到Kibana安装目录下的config目录中:

[sandwich@centos-elk elasticsearch-7.17.1]$ cp ca.crt.pem ../kibana-7.17.1-linux-x86_64/config/

6.修改Elasticsearch配置

config/elasticsearch.yml文件添加以下配置:

[sandwich@centos-elk config]$ vi elasticsearch.yml
[sandwich@centos-elk config]$ tail -n 5 elasticsearch.yml
xpack.security.transport.ssl.enabled: true
xpack.security.http.ssl.enabled: true
xpack.security.authc.api_key.enabled: true
xpack.security.http.ssl.keystore.path: elastic-certificates.p12
xpack.security.http.ssl.truststore.path: elastic-certificates.p12
xpack.security.http.ssl.keystore.password: bbbbbb
xpack.security.http.ssl.truststore.password: bbbbbb

重启ES

7.配置Kibana

为了能让Kibana访问带有https的Elasticsearch。我们也需要做相应的配置,给config/kibana.yml添加如下配置

elasticsearch.ssl.certificateAuthorities: ["/home/sandwich/app/elk/kibana-7.17.1-linux-x86_64/config/ca.crt.pem"]
elasticsearch.ssl.verificationMode: none

并且把es的host改成https

elasticsearch.hosts: ["https://localhost:9200"]
Elasticsearch 7.6.2 是一个流行的开源全文搜索引擎,支持远程访问是为了方便从外部系统检索数据。以下是远程访问Elasticsearch的一些关键步骤: 1. **配置HTTP/REST API**: Elasticsearch默认提供HTTP RESTful API,通过HTTPS可以保证安全通信。你需要确保Elasticsearch服务器启用了对应的端口,并允许来自外部的连接。 2. **认证设置**: - 如果需要身份验证,可以在`elasticsearch.yml`文件中设置`xpack.security.transport.ssl.enabled: true`启用SSL并配置证书。 - 使用基本认证(username/password),可以在`http.cors.enabled: true`的情况下指定允许的源 (`http.cors.allow-origin`) 和使用的HTTP头(`http.cors.header`). 3. **防火墙与代理**: 确保Elasticsearch监听的IP地址和端口对所需访问它的网络服务开放。如有必要,可通过防火墙规则或代理服务器配置访问控制。 4. **客户端库**: 使用支持Elasticsearch REST API的编程语言(如Python的`elasticsearch`库,Java的`elasticsearch-rest-high-level-client`等),你可以轻松地编写代码来发送查询请求到远程Elasticsearch实例。 5. **示例代码**: ```python from elasticsearch import Elasticsearch es = Elasticsearch([{'host': 'your_es_host', 'port': your_es_port}], http_auth=('username', 'password')) response = es.search(index='your_index', body={'query': {'match_all': {}}}) ``` 在这个例子中,你需要将`your_es_host`、`your_es_port`、`username`和`password`替换为你实际的Elasticsearch服务器信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT三明治

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值