在ElasticSearch6.8及以上版本开启安全认证功能
es之前版本一直无用户验证功能,不过官方有提供一x-pack,但是问题是付费。在es的6.7.0版本中,已经集成了x-pack,虽然es团队已经对x-pack开源,但是在该版本中如果需要使用到安全加密功能,依然还是需要付费购买license,search guard社区版可以免费提供用户验证功能。
本文章使用的是6.5.4版本,内置已经带有X-pack我们只需要启用一下就可以、如果你用的版本没有自带x-pack就需要安装一下
bin/elasticsearch-plugin install x-pack
实验环境:
主机 | 服务 |
---|---|
172.16.3.225/21 | Elasticsearch |
172.16.3.226/21 | Elasticsearch |
172.16.3.227/21 | Elasticsearch |
实验步骤:
首选需要查看一下当前master是谁
[root@k8s-master01 ~]# curl 172.16.3.225:9200/_cat/master
M6Uqfm0tQOOSu6rtTegEAw 172.16.3.226 172.16.3.226 elk02
1、在Master启动trial license
如果已经有正式license可以忽略这个步骤
随便在一台机器上执行命令激活xpack
[root@k8s-node01 ~]# curl -H "Content-Type:application/json" -XPOST http://172.16.3.226:9200/_xpack/license/start_trial?acknowledge=true
1.2、es开启xpack
[root@k8s-node01 ~]# su - elk -s /bin/bash
[elk@k8s-node01 ~]$ vim /usr/local/elasticsearch-6.5.4/config/elasticsearch.yml
xpack.security.enabled: true
设置密码,在master设置,node节点可以同步该用户名/密码
到此为止完成xpack集群,目前无SSL。
2、使用SSL
2.1、在master节点生成证书
[root@k8s-node01 ~]$ cd /usr/local/elasticsearch-6.5.4/
[root@k8s-node01 elasticsearch-6.5.4]$ ./bin/elasticsearch-certutil ca
2.2、为集群中的每个节点生成证书和私钥
[root@k8s-node01 elasticsearch-6.5.4]$ ./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
回车即可,若创建密码切记一致
2.3、将证书拷贝到elasticsearch的每个节点下面config/certs目录下
[root@k8s-node01 elasticsearch-6.5.4]$ mkdir config/certs
[root@k8s-node01 elasticsearch-6.5.4]$ mv elastic-certificates.p12 config/certs/
2.4、每个节点配置elasticsearch.yml
[root@k8s-node01 elasticsearch-6.5.4]$ vim config/elasticsearch.yml
最后一行添加:
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12
2.5、5.如果在创建证书的过程中加了密码,需要将你的密码加入到你的Elasticsearch keystore中去。每个节点都需要
[root@k8s-node01 elasticsearch-6.5.4]$ bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
[root@k8s-node01 elasticsearch-6.5.4]$ bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
3、给认证的集群创建用户密码
这里我用的是自动生成,因为我使用手动生成他老是出现apm_system设置失败
[root@k8s-node01 elasticsearch-6.5.4]$ bin/elasticsearch-setup-passwords interactive # 手动创建密码
[root@k8s-node01 elasticsearch-6.5.4]$ bin/elasticsearch-setup-passwords auto # 自动生成
elastic 账号:拥有 superuser 角色,是内置的超级用户。
kibana 账号:拥有 kibana_system 角色,用户 kibana 用来连接 elasticsearch 并与之通信。Kibana 服务器以该用户身份提交请求以访问集群监视 API 和 .kibana 索引。不能访问 index。
logstash_system 账号:拥有 logstash_system 角色。用户 Logstash 在 Elasticsearch 中存储监控信息时使用。
beats_system账号:拥有 beats_system 角色。用户 Beats 在 Elasticsearch 中存储监控信息时使用。
elastic是超级用户
3.5、修改密码
curl -H "Content-Type:application/json" -XPOST -u elastic 'http://172.16.3.226:9200/_xpack/security/user/elastic/_password' -d '{ "password" : "123456" }'
测试一下es是否现在需要用户登录么
http://172.16.3.225:9200/、http://172.16.3.226:9200/、http://172.16.3.227:9200/
如果通过密码可以登录进来就说明没有问题
4、在Kibana中设置登录es的用户
在Kibana.yml中配置
elasticsearch.username: "xxx"
elasticsearch.password: "xxx"
如果你不想将用户ID和密码放在kibana.yml文件中明文配置,可以将它们存储在密钥库中。运行以下命令以创建Kibana密钥库并添加配置
bin/kibana-keystore create
bin/kibana-keystore add elasticsearch.name
bin/kibana-keystore add elasticsearch.password
#删除
bin/kibana-keystore remove xxxx
重启Kinan
5、带密码查询es
curl -XGET --user user:passwd 'http://XXXX:9200/XX/XXX'
比如想要清空某个索引下的数据:
curl -XPOST --user admin:admin 'http://XXXX:9200/XXXX/XXX/_delete_by_query' -H "Content-Type: application/json" -d '{ "query":{"match_all":{}}}'
查看X-pack过期时间
[root@k8s-node02 kibana-6.5.4-linux-x86_64]# curl -u elastic:123456 -XGET 'http://172.16.3.225:9200/_xpack/license' # 如果用的是默认的话过期时间是一个月,一个月过后就无法使用
{
"license" : {
"status" : "active",
"uid" : "6015cd0f-afa8-4869-9e95-264bb69fd10e",
"type" : "platinum",
"issue_date" : "2021-03-20T00:00:00.000Z",
"issue_date_in_millis" : 1616198400000,
"expiry_date" : "2021-04-19T06:50:00.000Z",
"expiry_date_in_millis" : 1561420799999 ,
"max_nodes" : 100,
"issued_to" : "chinaedu chinaedu (chinaedu)",
"issuer" : "Web Form",
"start_date_in_millis" : 1616198400000
}
}