一、介绍
官方宣布从es的版本6.8和7.1开始,免费提供多项安全功能。其中包括tls加密通信,基于角色访问控制等功能。
可以使用企业CA证书来完成这一步骤,但是一般情况下,我们可以通过elasticsearch自带的elasticsearch-certutil的命令生成证书。然后各节点通过该证书可以进行安全通信。
为节点创建证书
TLS:TLS协议要求Trusted Certificate Authority(CA)签发的X.509的证书
证书认证的不同级别
Certificate:节点加入集群需要使用想用CA签发的证书
Full Verfication:节点加入集群需要使用想用CA签发的证书,还需要验证Hostname或IP地址
No Verfication:任何节点都可以加入,开发环境用于诊断目的
先签发CA证书,然后基于这个CA证书,签发每一个节点的证书
二、签发证书及部署
1.生成证书
#进到es的安装目录,如果是yum安装或者rpm安装的默认是/usr/share/elasticsearch下 [root@localhost ~]# cd /usr/share/elasticsearch/ #生成CA证书,遇到账号密码回车即可 [root@localhost elasticsearch]# bin/elasticsearch-certutil ca #生成节点证书,遇到账号密码回车即可 [root@localhost elasticsearch]# bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12 #生成证书到配置文件目录/etc/elasticsearch下 [root@localhost elasticsearch]# bin/elasticsearch-certutil cert -out /etc/elasticsearch/elastic-certificates.p12 -pass #上面命令执行成功后,会在`/etc/elasticsearch/`文件夹下生成elastic-certificates.p12证书 #证书授权 [root@localhost elasticsearch]# chmod 644 /etc/elasticsearch/elastic-certificates.p12
2.添加xpack配置
编辑配置文件/etc/elasticsearch/elasticsearch.yml
xpack.security.enabled: true xpack.security.transport.ssl.enabled: true xpack.security.transport.ssl.verification_mode: certificate # 证书认证级别 xpack.security.transport.ssl.keystore.path: elastic-certificates.p12 xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
3.重启es
[root@localhost elasticsearch]# systemctl restart elasticsearch.service #如果使用systemctl重启失败,可以使用bin路径下启动文件,-d参数为后台启动 [root@localhost bin]# cd /usr/share/elasticsearch/bin/ [root@localhost bin]# ./elasticsearch -d
4.设置密码
[root@localhost elasticsearch]# cd /usr/share/elasticsearch [root@localhost elasticsearch]# bin/elasticsearch-setup-passwords interactive # 输出结果 Your cluster health is currently RED. It is recommended that you resolve the issues with your cluster before running elasticsearch-setup-passwords. Do you want to continue with the password setup process [y/N] y Please confirm that you would like to continue [y/N]y # 直接输入密码,然后再重复一遍密码,中括号里是账号 Enter password for [elastic]: Reenter password for [elastic]: Enter password for [apm_system]: Reenter password for [apm_system]: Enter password for [kibana]: Reenter password for [kibana]: Enter password for [logstash_system]: Reenter password for [logstash_system]: Enter password for [beats_system]: Reenter password for [beats_system]: Enter password for [remote_monitoring_user]: Reenter password for [remote_monitoring_user]: Changed password for user [apm_system] Changed password for user [kibana] Changed password for user [logstash_system] Changed password for user [beats_system] Changed password for user [remote_monitoring_user] Changed password for user [elastic]
验证集群设置的账号和密码
打开浏览器访问这个地址,出现需要输入账号密码的界面证明设置成功,集群的一个节点
http://IP:9200/_security/_authenticate?pretty
三、增加访问密码
1.logstash增加访问es集群的用户及密码
logstash过滤数据之后往es中推送的时候,需要添加权限认证。增加访问es集群的用户及密码:
output { if [fields][log_source] == 'messages' { elasticsearch { hosts => ["http://192.168.80.104:9200", "http://192.168.80.105:9200","http://192.168.80.106:9200"] index => "messages-%{+YYYY.MM.dd}" user => "elastic" # 注意:这里演示使用超级账号,安全起见最好是使用自定义的账号,并授予该用户创建索引的权限,具体看下方地址 password => "123456" # 密码是上面步骤设置的 } } if [fields][log_source] == "secure" { elasticsearch { hosts => ["http://192.168.80.104:9200", "http://192.168.80.105:9200","http://192.168.80.106:9200"] index => "secure-%{+YYYY.MM.dd}" user => "elastic" # 注意:这里演示使用超级账号,安全起见最好是使用自定义的账号,并授予该用户创建索引的权限,具体看下方地址 password => "123456" } } }
2.elasticsearch-head访问es集群的用户及密码
elasticsearch-head插件此时再去访问有安全认证的es集群时,会发现无法进行查看,打开控制台可以看到报错:401 unauthorized
解决办法是修改elasticsearch.yml文件,增加以下配置。
http.cors.allow-headers: Authorization,content-type
修改三台es节点,然后重新启动,再次url+认证信息方式可以正常访问es集群。
http://IP:9100/?auth_user=elkstack&auth_password=123456
3.Kibana组件访问带有安全认证的Elasticsearch集群
配置文件kibana.yml中需要加入以下配置
elasticsearch.username: "kibana" # 注意:此处不用超级账号elastic,而是使用kibana跟es连接的账号kibana elasticsearch.password: "123456"
然后重启kibana,再次访问的话就就需要输入上述账号密码才能登陆访问了
不一样的地方:
在Management下面的Kibana最后出现一个Security,有User和Role
方便kibana多用户创建及角色权限控制