ElasticSearch 集群6.8 开启 x-pack安全认证登录同时配置kibana、logstash、file beat、cerebro

一、ElasticSearch 集群6.8 开启 x-pack安全认证登录(用户名和密码)

ElasticSearch 6.8 默认是没有开启x-pack安全认证登录;6.8以前的版本,x-pack安全认证登录需要license的,只能试用30天,6.8(含)以后都可以免费使用基本(basic)license

二、x-pack安全认证登录 开启的条件

xpack.security.enabled:true
elasticsearch x-pack安全认证登录 需要启用tcp的TLS(针对集群,非集群不用设置)

三、开启步骤及命令

  1. 生成CA证书
  2. 通过ca为集群中每个节点生成证书和私钥
  3. 修改elasticsearch.yml配置文件,启用x-pack安全认证登录
  4. 重启ElasticSearch服务
  5. 创建elasticsearch.keystore文件(默认已经创建位置为/etc/elasticsearch/elasticsearch.keystore)
  6. 设置密码

1.生成CA证书(在其中一个节点执行)

在其中一个节点上配置
切换到/etc/elasticsearch目录下,执行elasticsearch内部命令

cd /etc/elasticsearch
/usr/share/elasticsearch/bin/elasticsearch-certutil ca      #用户名和密码不用设置,一路回车

生成ca证书文件elastic-stack-ca.p12

2. 通过ca为集群生成证书和私钥(只在一个节点执行)

在生成ca证书的节点上配置
使用elasticsearch内部命令

/usr/share/elasticsearch/bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
用户名和密码可以为空,如果为空一路回车执行到底即可

生成一个证书和私钥文件

elastic-certificates.p12 

给这个文件赋权 chmod 660 elastic-certificates.p12 或者 改变文件的所属用户 chown elasticsearch:elasticsearch elastic-certificates.p12 不然启动时会报,无法读取该文件
此文件放在elasticsearch的/etc/elasticsearch目录下

将此文件elastic-certificates.p12分发到集群的其他机器上,并放到/etc/elasticsearch下(因为rpm安装的es默认的配置文件路径是)

3. 修改elasticsearch.yml配置文件,启用x-pack安全认证登录 (集群每个节点都要配置)

在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

4. 重启ElasticSearch服务

修改配置后必须重启elasticsearch

systemctl restart elasticsearch

5. 创建elasticsearch.keystore文件(存在可以跳过)

默认已经创建位置为/etc/elasticsearch/elasticsearch.keystore
如果已经存在可以忽略此步

#判断是否存在elasticsearch.keystore
ls /etc/elasticsearch/elasticsearch.keystore

#不存在时执行
/usr/share/elasticsearch/bin/elasticsearch-keystore create 

6. 设置密码(只在其中一个节点上执行)

6.1 查看主节点的方法

方法一: 在浏览器里执行或者在linux下执行curl 查看(因为还没有设置密码,此方法行不通

# 请求 可能需要用户名和密码,因为还没有设置密码,此方法行不通
http://ip:9200/_cat/master 
或者命令行
curl -XGET "http://ip:9200/_cat/master"

方法二: 可以将其他节点服务关闭只留一个节点,存活节点即为主节点
因为上面的elasticsearch.yml配置后,web访问需要用户名和密码,无法确认对应的主节点,可以将其他节点服务关闭,只留一个节点

6.2 设置密码

  • 设置密码即为设置内置用户的密码

内置用户

用户名 _作用
elastic 超级用户
kibana 用于负责Kibana连接Elasticsearch
logstash_system Logstash将监控信息存储在Elasticsearch中时使用
beats_system Beats在Elasticsearch中存储监视信息时使用
apm_system APM服务器在Elasticsearch中存储监视信息时使用
remote_monitoring_user Metricbeat用户在Elasticsearch中收集和存储监视信息时使用

只在集群中一个节点上执行,默认会创建一个索引.secutity-6

注意注意:执行之前一定要看一下集群的日志,由于刚才进行了重启,集群的状态从red还未变成yellow或者green,集群状态变成yellow时就可以操作了,目的是要能创建索引。否则创建失败。

设置密码过程

#如需要自动生成密码可以将interactive 替换为 auto,让系统自动生成
 
/usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive

Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
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]

elastic用户设置密码后,密码的引导过程(即命令行提示)将不再有效,并且再次执行elasticsearch-setup-passwords命令会抛出异常

Failed to authenticate user 'elastic' against http://192.168.200.220:9200/_xpack/security/_authenticate?pretty
Possible causes include:
 * The password for the 'elastic' user has already been changed on this cluster
 * Your elasticsearch node is running against a different keystore
   This tool used the keystore at /etc/elasticsearch/elasticsearch.keystore

ERROR: Failed to verify bootstrap password

6.3 设置完成后,启动其他节点上的elasticsearch服务

systemctl start elasticsearch

观察日志,并在浏览器中访问一下es服务

四、删除密码或者重置密码

如果忘记了之前设置的密码可以根据以下操作进行密码重置:

在执行生成密码命令的时候可能会出现以下错误

1. 删除密码步骤

  1. 关闭ElasticSearch的xpack安全验证
    修改elasticsearch.yml 中的xpack.security.enabled 和 xpack.security.transport.ssl.enabledfalse
  2. 重启ElasticSearch服务
 systemctl restart elasticsearch
  1. 重启好后用以下命令删除索引 .secutity-6

curl -XDELETE http://localhost:9200/.secutity-6  -u elastic  -p es@yky
或者
在kinaba UI中进行删除

2. 重置密码可以使用api

五、kibana、logstash等相关增加用户名和密码

1. kibana 配置用户名和密码

# 在kibana.yml下添加或者修改如下两行
 vi /etc/kibana/kibana.yml
 elasticsearch.username: elastic
 elasticsearch.password: {你修改的password}

2. logstash 配置用户名和密码

打开配置文件conf,在output中的elasticsearch中添加user、password

output {
    elasticsearch {
      hosts => ["10.68.24.136:9200","10.68.24.137:9200"]
      index => "%{[indexName]}-%{+YYYY.MM.dd}"
      user => "有权限的用户名"
      password => "对应的密码"
    }

这个用户必须要有的权限,否则会报 401 权限错误

[WARN ][logstash.outputs.elasticsearch] Attempted to resurrect connection to dead ES instance, 
but got an error. {:url=>"http://*****:9200/", 
:error_type=>LogStash::Outputs::ElasticSearch::HttpClient::Pool::BadResponseCodeError,
 :error=>"Got response code '401' contacting Elasticsearch at URL 'http://***:9200/'"}

2.1 选择拥有权限的用户

  1. 配置的这个用户需要有es索引的写、删除、创建索引权限,对应的权限名称:write、delete、create_index
  2. 如果还要管理监控需要加上manage_index_templates、monitor、manage_ilm 权限;
  3. 最好是把这些权限放到一个角色中,可以命名为:logstash_write,也可以自定义。

2.1.1 方法一:使用系统内置的用户

最简单的方法是使用超管用户 elastic(不是很安全)

es系统内置的用户logstash_system 是logstash服务的监控专用用户,没有索引的读写权限
es系统内置的用户elastic 是es的超级管理员用户

2.1.2 方法二:新创建新用户

新创建用户和角色并分配权限

可以参考的官方配置
Configuring Security in Logstash | Logstash Reference [7.16] | Elastic

2.2 创建用户角色(使用内置用户请忽略此步骤)

有两种方式 UI界面和api

2.2.1 在kibana界面中操作

新用户和角色都可以在kibana中创建:kibana界面里的 管理>角色菜单下配置

  1. 新建一个角色logstash_writer,然后添加集群管理权限为:manage_index_templates、monitor、manage_ilm
  2. 为此角色添加索引权限,选择要配置的索引,接着此角色对索引的操作权限为:write、delete、create_index

2.2.2 api方式

POST _xpack/security/role/logstash_writer
{
  "cluster": ["manage_index_templates", "monitor", "manage_ilm"], 
  "indices": [
    {
      "names": [ "logstash-*" ], 
      "privileges": ["write","create","create_index","manage","manage_ilm"]  
    }
  ]
}

2.3 给logstash加监控(可选)

logstash.yml中将 x-pack monitoring 下面几行注释放开并修改,

xpack.monitoring.enable: true
xpack.monitoring.elasticsearch.username: logstash_system
xpack.monitoring.elasticsearch.password: 你定义的密码,如果没有密码直接注释
xpack.monitoring.elasticsearch.hosts: ["http://ip1:9200","http://ip2:9200"]

3. 给filebeat加密码

打开 filebeat.yml 修改如下内容

output.elasticsearch:
  # Array of hosts to connect to.
  hosts: ["192.168.0.130:9200", "192.168.0.131:9200", "192.168.0.132:9200"]
  index: "xxxxx-log-%{+yyyy-MM-dd}"
  # Optional protocol and basic auth credentials.
  #protocol: "https"     #不用管
  username: "elastic"    #es默认管理员用户
  password: "changeme"   #es默认管理员用户对应的密码

4. 给cerebro加密码

编辑配置文件 vim /etc/cerebro/application.conf  在hosts=的数组中


 

hosts = [
  {
    host = "http://xxxxx:9200"
    name = "ES"
    headers-whitelist = [ "x-proxy-user", "x-proxy-roles", "X-Forwarded-For" ]

    auth = {
      username = "用户名"
      password = "密码"
    }

  }

  {
    host = "http://xxxxxx2:9200"
    name = "ES"
    headers-whitelist = [ "x-proxy-user", "x-proxy-roles", "X-Forwarded-For" ]
    auth = {
      username = "用户名"
      password = "密码"
    }
  }
 
]

猜你喜欢

转载自blog.csdn.net/yy4545/article/details/121957479