【elasticsearch】elasticsearch8.0.1使用rpm包安装并启用TLS

背景

公司的业务需要在加密的情况下使用,为此,研究测试了一下es8是如何启用TLS的。以下是测试使用过程。

x-pack了解

在 Elasticsearch 7.11.0 版本及更高版本中,X-Pack 功能在默认情况下已经整合到 Elastic Stack 的各个组件中,并且具有不同的名称和许可证。以下是 Elastic Stack 中一些主要的 X-Pack 功能和对应的新名称:

  • 安全性(Security):提供了身份验证、授权、加密通信和安全审计等功能,用于保护 Elasticsearch 和 Kibana 的访问和数据安全。

  • 警报(Alerting):用于配置和管理警报规则,当符合特定条件时,发送通知、触发操作或运行自定义脚本。

  • 监控(Monitoring):提供了集群健康状况、节点性能和资源使用情况等实时监控指标,以及用于可视化和分析的监控仪表板。

  • 报告(Reporting):用于生成漂亮的报表和可视化图表,支持将报告导出为 PDF、CSV 或 PNG 格式,并支持计划定期生成和发送报告。

  • 图形探索(Graph Exploration):用于分析和可视化数据之间的关系,帮助发现隐藏的模式和连接。

  • 机器学习(Machine Learning):提供了强大的机器学习功能,用于自动检测异常、预测趋势和进行故障检测。

请注意,具体的功能和名称可能会随着 Elastic Stack 版本的更新而有所变化,请参考官方文档以获取最新的信息和功能详细说明。

基本知识

1、默认情况下,服务器将使用两个端口进行通讯,9200与9300:

  • 9200,用于http通讯,各类restful客户端,例如kibana,浏览器直接访问、agent等等需要通过该端口与服务器连接。
  • 9300,用于elasticsearch服务器集群内各服务器节点间的通讯。

2、如果首次使用yum或者rpm按装elasticsearch服务器,安装程序会自动进行安全配置,并生成几个安全配置需要的文件,如ca的keystore、用于http加密通讯的keystore(http.p12)等,这些最好保留下来,当然,我们也可以用它提供的证书工具在后面自行创建。注意,如果不是首次安装,而又希望系统再次自动生成安全配置,需要先把/var/lib/elasticsearch里的东西删除干净。

3、通过自动安全配置生成的几个文件会放在/usr/share/elasticsearch下:

  • elastic-stack-ca.p12 keystore文件,存储了ca的公共证书及用于签发其他证书的密钥。(默认没有这个文件,可以自己手动生成)
  • transport.p12 包含了传输层密钥和证书,用于elasticsearch节点间加密通讯。
  • http.p12 这个keystore存储着http加密通讯需要的证书和密钥。
  • http_ca.crt 证书文件,用于集群内的http通讯加密。

es安装

环境介绍

单台 centos7部署单节点elasticsearch8.0.1

下载安装

elasticsearch中文下载地址:https://elasticsearch.cn/download/
选择自己需要的版本(windows、Linux、mac等),但需要注意不同版本之间的区别,我此次使用es 8.0.1的rpm包进行测试。

#rpm包下载
[root@k8s-m2 ~]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.0.1-x86_64.rpm
#直接使用rpm安装
warning: elasticsearch-8.0.1-x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID d88e42b4: NOKEY
Preparing...                          ################################# [100%]
Creating elasticsearch group... OK
Creating elasticsearch user... OK
Updating / installing...
   1:elasticsearch-0:8.0.1-1          ################################# [100%]
warning: ignoring JAVA_HOME=/opt/jdk1.8.0_65; using bundled JDK
warning: ignoring JAVA_HOME=/opt/jdk1.8.0_65; using bundled JDK
--------------------------- Security autoconfiguration information ------------------------------

Authentication and authorization are enabled.
TLS for the transport and HTTP layers is enabled and configured.

The generated password for the elastic built-in superuser is : =GAe2y2Rqi0mvxjFMDZk

If this node should join an existing cluster, you can reconfigure this with
'/usr/share/elasticsearch/bin/elasticsearch-reconfigure-node --enrollment-token <token-here>'
after creating an enrollment token on your existing cluster.

You can complete the following actions at any time:

Reset the password of the elastic built-in superuser with 
'/usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic'.

Generate an enrollment token for Kibana instances with 
 '/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s kibana'.

Generate an enrollment token for Elasticsearch nodes with 
'/usr/share/elasticsearch/bin/elasticsearch-create-enrollment-token -s node'.

-------------------------------------------------------------------------------------------------
### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd
 sudo systemctl daemon-reload
 sudo systemctl enable elasticsearch.service
### You can start elasticsearch service by executing
 sudo systemctl start elasticsearch.service
warning: ignoring JAVA_HOME=/opt/jdk1.8.0_65; using bundled JDK
warning: ignoring JAVA_HOME=/opt/jdk1.8.0_65; using bundled JDK```

正常情况下,该版本的安装会出现以上的一些信息,如果没有,可能是由于不是第一次安装有之前遗留下的一些文件。仔细查看上面的输出信息说明,默认情况下认证已经启用并配置好,节点的加入方式,密码重置,token的生成等。

环境调整
由于es服务需要服务器调整连接数,修改limits.conf文件,如下:

vim /etc/security/limits.conf  
* soft nofile 655350
* hard nofile 655350

调整vm.max_map_coun,检查当前的 vm.max_map_count 值:如果当前的值小于 262144,需要调整。否则,如果当前的值已经达到或超过 262144,那么可以不进行任何更改,当然也可以适量调大一点。

[root@k8s-m2 ~]# sysctl vm.max_map_count
[root@k8s-m2 ~]# echo "vm.max_map_count=655360" >> /etc/sysctl.conf 
[root@k8s-m2 ~]# sysctl -p

jvm使用内存大小调整,默认情况下,es配置使用的是-Xms4g和-Xmx4g,可以按照自己所需进行调整。

启动

[root@k8s-m2 opt]# systemctl start elasticsearch.service
[root@k8s-m2 opt]# systemctl status  elasticsearch
● elasticsearch.service - Elasticsearch
   Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2023-10-09 10:44:39 CST; 20min ago
     Docs: https://www.elastic.co
 Main PID: 24259 (java)
    Tasks: 96
   Memory: 4.4G
   CGroup: /system.slice/elasticsearch.service
           ├─24259 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -Djava.security.manager=allow -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile...
           └─24694 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller

Oct 09 10:44:12 k8s-m2 systemd[1]: Starting Elasticsearch...
Oct 09 10:44:39 k8s-m2 systemd[1]: Started Elasticsearch.
[root@k8s-m2 opt]# 

常用命令说明

使用yum或者rpm安装时,和es相关的命令存放在/usr/share/elasticsearch/bin目录

#elastic用户命令重置,其他用户类似指定
[root@k8s-m2 opt]# /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic 
#默认生成证书时配置的密码等信息
[root@k8s-m2 opt]# /usr/share/elasticsearch/bin/elasticsearch-keystore list
warning: ignoring JAVA_HOME=/opt/jdk1.8.0_65; using bundled JDK
autoconfiguration.password_hash
keystore.seed
xpack.security.http.ssl.keystore.secure_password
xpack.security.transport.ssl.keystore.secure_password
xpack.security.transport.ssl.truststore.secure_password
#查看具体某项配置的密码
[root@k8s-m2 opt]# /usr/share/elasticsearch/bin/elasticsearch-keystore show xpack.security.http.ssl.keystore.secure_password
warning: ignoring JAVA_HOME=/opt/jdk1.8.0_65; using bundled JDK
0w9Z7YiEQG2rMWprA-8rYQ
#修改具体某项的密码,如果之前已经设置,可以覆盖
[root@k8s-m2 opt]# /usr/share/elasticsearch/bin/elasticsearch-keystore add xpack.security.http.ssl.keystore.secure_password

#命令行检查
[root@k8s-m2 certs]# curl --cacert http_ca.crt -u elastic:=GAe2y2Rqi0mvxjFMDZk  -XGET "https://192.168.2.141:9200/"
{
  "name" : "k8s-m2",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "v5HBM6ACRiy31DBZmSa58g",
  "version" : {
    "number" : "8.0.1",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "801d9ccc7c2ee0f2cb121bbe22ab5af77a902372",
    "build_date" : "2022-02-24T13:55:40.601285296Z",
    "build_snapshot" : false,
    "lucene_version" : "9.0.0",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

#更多选项查看,按自己所需查看
[root@k8s-m2 certs]# curl --cacert http_ca.crt -u elastic:=GAe2y2Rqi0mvxjFMDZk  -XGET "https://192.168.2.141:9200/_cat"
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates
/_cat/ml/anomaly_detectors
/_cat/ml/anomaly_detectors/{job_id}
/_cat/ml/trained_models
/_cat/ml/trained_models/{model_id}
/_cat/ml/datafeeds
/_cat/ml/datafeeds/{datafeed_id}
/_cat/ml/data_frame/analytics
/_cat/ml/data_frame/analytics/{id}
/_cat/transforms
/_cat/transforms/{transform_id}

kibana连接加密的es

kibana下载安装

[root@k8s-m2 opt]# wget https://artifacts.elastic.co/downloads/kibana/kibana-8.0.1-x86_64.rpm
[root@k8s-m2 opt]# rpm -ivh kibana-8.0.1-x86_64.rpm

生成kibana需要的相关证书

注意在这过程中要输入密码,具体密码为上面/usr/share/elasticsearch/bin/elasticsearch-keystore show xpack.security.http.ssl.keystore.secure_password查看到的密码。

[root@k8s-m2 certs]# pwd
/etc/elasticsearch/certs
[root@k8s-m2 certs]# ll
-rw-rw---- 1 root elasticsearch 1915 Oct  9 09:57 http_ca.crt
-rw-rw---- 1 root elasticsearch 9981 Oct  9 09:57 http.p12
-rw-rw---- 1 root elasticsearch 5822 Oct  9 09:57 transport.p12

# Private Key 私钥
[root@k8s-m2 certs]# openssl pkcs12 -in http.p12 -nocerts -nodes > client.key
# Public Certificate 公共证书
[root@k8s-m2 certs]# openssl pkcs12 -in http.p12 -clcerts -nokeys > client.cer
# CA Certificate 签署公共证书的CA
[root@k8s-m2 certs]# openssl pkcs12 -in http.p12 -cacerts -nokeys -chain > client-ca.cer

[root@k8s-m2 certs]# mkdir /etc/kibana/config/
[root@k8s-m2 certs]# cp /etc/elasticsearch/certs/client* /etc/kibana/config/

主要配置修改

注意:使用的是kibana账户,密码不知道可以用elasticsearch-reset-password(/usr/share/elasticsearch/bin/elasticsearch-reset-password -u kibana)命令进行重置。

server.port: 5601
server.host: "192.168.2.141"
server.publicBaseUrl: "http://192.168.2.141:5601" #最好添加上
elasticsearch.username: "kibana"
elasticsearch.password: "THR__INXYkH_lLCO3vV1"
elasticsearch.ssl.certificate: /etc/kibana/config/client.cer   #相关路径最好写绝对路径
elasticsearch.ssl.key: /etc/kibana/config/client.key
elasticsearch.ssl.certificateAuthorities: /etc/kibana/config/client-ca.cer
elasticsearch.ssl.verificationMode: certificate

kibana启动并检查

[root@k8s-m2 opt]# systemctl start kibana
[root@k8s-m2 opt]# systemctl status kibana -l
#如果kibana异常,可以查看日志进行排查
[root@k8s-m2 opt]# tail -f  /var/log/kibana/kibana.log

kibana访问

使用上面配置的服务器IP+5601地址进行访问。
在这里插入图片描述
账号为elastic,密码为es启动时输出到界面的密码。当然不记得也可以进行修改。

更多关于elasticsearch的知识分享,请前往博客主页。编写过程中,难免出现差错,敬请指出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

margu_168

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

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

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

打赏作者

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

抵扣说明:

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

余额充值