HTTPS不启用的话,很多功能无法使用,例如启用Fleet之后,我在客户机安装agent,却始终无法enroll,提示:fail to enroll: fail to execute request to fleet-server: fail to read original error: read tcp 172.30.2.196:33106->172.30.2.108:8220: read: connection reset by peer

查询后,说是需要https才行,然后摸索了好几天才搞定。

环境:

安装Elastic Stack: https://blog.51cto.com/helpdesk/11339212

启用Fleet:https://blog.51cto.com/helpdesk/11399936

1.- 生成CA证书

通过 docker ps 找到 container ID

Docker Elastic Stack 启用https_https

进入elasticsearch

docker exec -it ffc20b2ca593 /bin/bash

进入bin文件夹

Docker Elastic Stack 启用https_kibana_02

输入命令:elasticsearch-certutil ca

按默认回车即可,不需设置密码

Docker Elastic Stack 启用https_elasticsearch_03

生成证书,默认在 /usr/share/elasticsearch/elastic-stack-ca.p12

基于CA继续生成节点证书

继续输入命令:elasticsearch-certutil cert --ca elastic-stack-ca.p12

按默认回车即可,不需设置密码

默认保存在 /usr/share/elasticsearch/elastic-certificates.p12

输入 exit 退出container

编辑elasticsearch yml文件,启用对应选项和添加这2个证书

vim /docker-elk/elasticsearch/config/elasticsearch.yml

添加如下红框中的参数

Docker Elastic Stack 启用https_elasticsearch_04

具体如下,注意,要将证书放在config文件夹里(/usr/share/elasticsearch/config/),不然会提示没有权限

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: /usr/share/elasticsearch/config/elastic-certificates.p12

xpack.security.transport.ssl.truststore.path: /usr/share/elasticsearch/config/elastic-certificates.p12

保存退出,继续去生成elastic search和kibana之间通讯需要的http证书

再次进入elasticsearch

docker exec -it ffc20b2ca593 /bin/bash

先将2个证书复制到 config文件夹

cp elastic-* ./config/

这次就不进入bin文件夹,直接执行命令:

./bin/elasticsearch-certutil http

第一步提示生成CSR,选 no

Docker Elastic Stack 启用https_kibana_05

第二步使用已有CA,选 yes

Docker Elastic Stack 启用https_docker_06

第三步 CA路径,输入完整的证书路径:

Docker Elastic Stack 启用https_https_07

后面提示密码,直接回车即可

默认证书有效期5年,默认回车即可

Docker Elastic Stack 启用https_https_08

为每个节点生成单独证书,选 no

Docker Elastic Stack 启用https_https_09

设置hostname,IP,默认回车即可

Docker Elastic Stack 启用https_elasticsearch_10

确认不输入,选 yes

Docker Elastic Stack 启用https_https_11

IP也为空,回车即可

Docker Elastic Stack 启用https_docker_12

确认不需要IP,继续回车

默认的SAN,选 no,不需更改

Docker Elastic Stack 启用https_docker_13

http.p12密码,不需要,回车即可

Docker Elastic Stack 启用https_kibana_14

默认保存路径和文件名,回车即可

Docker Elastic Stack 启用https_elasticsearch_15

完成后提示

Zip file written to /usr/share/elasticsearch/elasticsearch-ssl-http.zip

输入 unzip elasticsearch-ssl-http.zip

解压在当前路径下会有 elasticsearch 和 kibana 2个文件夹

Docker Elastic Stack 启用https_https_16

Docker Elastic Stack 启用https_docker_17

将 http.p12 复制到 /usr/share/elasticsearch/config/ 中

cp ./elasticsearch/http.p12 ./config/

然后输入 exit 退出 container

将 elastic search container 里面的 kibana文件夹里的证书复制出来

docker cp ffc20b2ca593:/usr/share/elasticsearch/kibana/elasticsearch-ca.pem ./

Docker Elastic Stack 启用https_docker_18

然后传到 kibana的config文件夹中

 docker cp ./elasticsearch-ca.pem 5d24eac4c00f:/usr/share/kibana/config/

Docker Elastic Stack 启用https_kibana_19

编辑elasticsearch yml文件,添加http证书

vim /docker-elk/elasticsearch/config/elasticsearch.yml

Docker Elastic Stack 启用https_kibana_20

具体如下:

xpack.security.http.ssl.enabled: true

xpack.security.http.ssl.keystore.path: /usr/share/elasticsearch/config/http.p12

接着添加证书到 kibana yml

Docker Elastic Stack 启用https_docker_21

具体如下:

elasticsearch.ssl.certificateAuthorities: /usr/share/kibana/config/elasticsearch-ca.pem

2.- 创建浏览器与kibana之间的 https认证

继续进入elasticsearch container

docker exec -it ffc20b2ca593 /bin/bash

输入命令:./bin/elasticsearch-certutil csr -name kibana-server -dns example.com,www.example.com

默认回车即可

Docker Elastic Stack 启用https_docker_22

默认生成 csr-bundle.zip文件

Docker Elastic Stack 启用https_https_23

解压出来会在 kibana-server这个文件夹里有2个key文件

Docker Elastic Stack 启用https_kibana_24

根据这2个key生成1个自签证书

Docker Elastic Stack 启用https_elasticsearch_25

命令:openssl x509 -req -in ./kibana-server/kibana-server.csr -signkey ./kibana-server/kibana-server.key -out kibana-server/kibana-server.crt

输入 exit 退出 elasticsearch container

将证书copy到本机然后再传到 kibana container config文件夹里

docker cp -a ffc20b2ca593:/usr/share/elasticsearch/kibana-server/ ./

Docker Elastic Stack 启用https_https_26

然后将crt 和 key文件传到 kiban的config文件夹

docker cp ./kibana-server/kibana-server.crt 5d24eac4c00f:/usr/share/kibana/config/

docker cp ./kibana-server/kibana-server.key 5d24eac4c00f:/usr/share/kibana/config/

添加参数到kibana yml

Docker Elastic Stack 启用https_https_27

具体如下:

server.ssl.enabled: true

server.ssl.certificate: /usr/share/kibana/config/kibana-server.crt

server.ssl.key: /usr/share/kibana/config/kibana-server.key

同时将所有 http地址改为 https

重启docker服务

sudo service docker restart

再次打开kiban网页,点 accept risk 即可见到熟悉的登陆界面

Docker Elastic Stack 启用https_https_28

Docker Elastic Stack 启用https_https_29