elasticsearch集群升级登录认证

打印 上一主题 下一主题

主题 965|帖子 965|积分 2895

背景:

因项目信息安全考虑 原es版本5.6.12,没有登录认证,不安全。要求升级为6.8.0,并添加认证,整个过程记录下。
环境:

Elasticsearch: 6.8.0
Kibana: 6.8.0
预备三台服务器:10.25.169.50 10.25.169.51 10.25.169.52
springboot2.x
 简单安装设置:

1.下载Elasticsearch的tar.gz包
    所在:Elasticsearch安装包
2.解压 到相应的目录下面
    tar -zxvf https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.8.0.tar.gz
3.单机版略过,另es安装过程不在记录,网上许多,可以参考
4.集群搭建:
   vim /config/elasticsearch.yml
  设置如下:
  1. #集群名称
  2. cluster.name: elasticsearch-cluster
  3. #节点名称
  4. node.name: es-node1
  5. #0.0.0.0表示其他机器都可以访问 类似白名单
  6. network.bind_host: 0.0.0.0
  7. #设置其他节点连接此节点的地址,如果不设置的话,则自动获取,publish_host的地址必须为真实地址
  8. network.publish_host: 10.25.169.50
  9. #Http传输监听端口 本机访问
  10. http.port: 9200
  11. #该节点与其他节点交互的端口
  12. transport.tcp.port: 9300
  13. #是否支持跨域,默认不支持
  14. http.cors.enabled: true
  15. http.cors.allow-origin: "*"
  16. #是否允许该节点参加master选举
  17. node.master: true
  18. #允许该节点存储数据,默认开启
  19. node.data: true
  20. #存储数据和日志到指定服务器路径
  21. path.data: /home/soft/elasticsearch-6.8.0/elastic/data
  22. path.logs: /home/soft/elasticsearch-6.8.0/elastic/logs
  23. #节点内部通信地址
  24. discovery.zen.ping.unicast.hosts: ["10.25.169.50","10.25.169.51","10.25.169.52"]
  25. #这个参数控制的是,一个节点需要看到的具有master节点资格的最小数量,然后才能在集群中做操作。官方的推荐值是(N/2)+1,其中N是具有master资格的节点的数量
  26. discovery.zen.minimum_master_nodes: 2
  27. bootstrap.memory_lock: false
  28. bootstrap.system_call_filter: false
复制代码
其他2个节点:
  1. cluster.name: elasticsearch-cluster
  2. node.name: es-node2
  3. network.bind_host: 0.0.0.0
  4. network.publish_host: 10.25.169.50
  5. http.port: 9200
  6. transport.tcp.port: 9300
  7. http.cors.enabled: true
  8. http.cors.allow-origin: "*"
  9. node.master: true
  10. node.data: true
  11. path.data: /home/soft/elasticsearch-6.8.0/elastic/data
  12. path.logs: /home/soft/elasticsearch-6.8.0/elastic/logs
  13. discovery.zen.ping.unicast.hosts: ["10.25.169.50","10.25.169.51","10.25.169.52"]
  14. discovery.zen.minimum_master_nodes: 2
  15. bootstrap.memory_lock: false
  16. bootstrap.system_call_filter: false
复制代码
  1. cluster.name: elasticsearch-cluster
  2. node.name: es-node3
  3. network.bind_host: 0.0.0.0
  4. network.publish_host: 10.25.169.50
  5. http.port: 9200
  6. transport.tcp.port: 9300
  7. http.cors.enabled: true
  8. http.cors.allow-origin: "*"
  9. node.master: true
  10. node.data: true
  11. path.data: /home/soft/elasticsearch-6.8.0/elastic/data
  12. path.logs: /home/soft/elasticsearch-6.8.0/elastic/logs
  13. discovery.zen.ping.unicast.hosts: ["10.25.169.50","10.25.169.51","10.25.169.52"]
  14. discovery.zen.minimum_master_nodes: 2
  15. bootstrap.memory_lock: false
  16. bootstrap.system_call_filter: false
复制代码
逐个启动节点:/elasticsearch-6.8.0/bin   -d 后台启动 看不到启动日志
   ./bin/elasticsearch -d
   访问:http://localhost:9200 看到如下界面,说明启动成功了。

安全认证:

如今这个集群是没有安全认证的,在生产环境,如许的集群是及其容易被人攻击的。
接下来开启ES集群的安全认证之路(这里说明一下,ES集群之间的节点是通过凭证来通信的,所以才有生成凭证这一步骤)
1. 生成证书:/elasticsearch-6.8.0/bin 
   ./bin/elasticsearch-certutil ca
  中间会让输入路径和密码,路径可以不输,直接回车(生成的文件到当前目录),密码设置一下的,设置密码:123456
完成后会生成一个文件:elastic-stack-ca.p12
2.生成秘钥
   ./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
  中间需要输入刚才设置的密码就直接输入就可以了,需要输入路径的地方就直接回车,和上面一样,然后会生成一个文件:elastic-certificates.p12
这个证书就是ES的各个节点之间通信的凭证了。
强调说明一下,一个ES集群生成一个凭证就可以了,其他节点不许要生成凭证。
3.将上步成成的证书迁移到指定目录
   mv /bin/elastic-certificates.p12 /config/certificates/
  
3.1记得修改一下文件的权限(否则启动的时间会有权限题目):
   chmod 777 /config/certificates/elastic-certificates.p12
  4.修改设置文件 :
   vim /config/elasticsearch.yml
  1. cluster.name: elasticsearch-cluster
  2. node.name: es-node1
  3. network.bind_host: 0.0.0.0
  4. network.publish_host: 10.25.169.50
  5. http.port: 9200
  6. transport.tcp.port: 9300
  7. http.cors.enabled: true
  8. http.cors.allow-origin: "*"
  9. node.master: true
  10. node.data: true
  11. path.data: /home/soft/elasticsearch-6.8.0/elastic/data
  12. path.logs: /home/soft/elasticsearch-6.8.0/elastic/logs
  13. discovery.zen.ping.unicast.hosts: ["10.25.169.50","10.25.169.51","10.25.169.52"]
  14. discovery.zen.minimum_master_nodes: 2
  15. bootstrap.memory_lock: false
  16. bootstrap.system_call_filter: false
  17. # 以下配置用于设置密码访问ES集群,在三台es服务上都配置
  18. xpack.security.enabled: true
  19. xpack.license.self_generated.type: basic
  20. xpack.security.transport.ssl.enabled: true
  21. xpack.security.transport.ssl.verification_mode: certificate
  22. xpack.security.transport.ssl.keystore.path: /elasticsearch/config/certificates/elastic-certificates.p12
  23. xpack.security.transport.ssl.truststore.path: /elasticsearch/config/certificates/elastic-certificates.p12
复制代码
5.将证书:elastic-certificates.p12文件 同步复制到别的两个服务器节点:指定的文件目录
  1. cluster.name: elasticsearch-cluster
  2. node.name: es-node2
  3. network.bind_host: 0.0.0.0
  4. network.publish_host: 10.25.169.50
  5. http.port: 9200
  6. transport.tcp.port: 9300
  7. http.cors.enabled: true
  8. http.cors.allow-origin: "*"
  9. node.master: true
  10. node.data: true
  11. path.data: /home/soft/elasticsearch-6.8.0/elastic/data
  12. path.logs: /home/soft/elasticsearch-6.8.0/elastic/logs
  13. discovery.zen.ping.unicast.hosts: ["10.25.169.50","10.25.169.51","10.25.169.52"]
  14. discovery.zen.minimum_master_nodes: 2
  15. bootstrap.memory_lock: false
  16. bootstrap.system_call_filter: false
  17. # 以下设置用于设置密码访问ES集群,在三台es服务上都设置xpack.security.enabled: truexpack.license.self_generated.type: basicxpack.security.transport.ssl.enabled: truexpack.security.transport.ssl.verification_mode: certificatexpack.security.transport.ssl.keystore.path: /elasticsearch/config/certificates/elastic-certificates.p12xpack.security.transport.ssl.truststore.path: /elasticsearch/config/certificates/elastic-certificates.p12
复制代码
  1. cluster.name: elasticsearch-cluster
  2. node.name: es-node3
  3. network.bind_host: 0.0.0.0
  4. network.publish_host: 10.25.169.50
  5. http.port: 9200
  6. transport.tcp.port: 9300
  7. http.cors.enabled: true
  8. http.cors.allow-origin: "*"
  9. node.master: true
  10. node.data: true
  11. path.data: /home/soft/elasticsearch-6.8.0/elastic/data
  12. path.logs: /home/soft/elasticsearch-6.8.0/elastic/logs
  13. discovery.zen.ping.unicast.hosts: ["10.25.169.50","10.25.169.51","10.25.169.52"]
  14. discovery.zen.minimum_master_nodes: 2
  15. bootstrap.memory_lock: false
  16. bootstrap.system_call_filter: false# 以下设置用于设置密码访问ES集群,在三台es服务上都设置xpack.security.enabled: truexpack.license.self_generated.type: basicxpack.security.transport.ssl.enabled: truexpack.security.transport.ssl.verification_mode: certificatexpack.security.transport.ssl.keystore.path: /elasticsearch/config/certificates/elastic-certificates.p12xpack.security.transport.ssl.truststore.path: /elasticsearch/config/certificates/elastic-certificates.p12
复制代码
6.末了一步:你还需要在各个服务器节点上添加密码:
   bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
  输入密码:123456(之前设定的密码,往上看)
    bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
输入密码:123456(之前设定的密码,往上看)
   7.逐个启动节点
   ./bin/elasticsearch -d
   8.这时间打开  http://localhost:9200 看到如下画面

 
实在这个时间 我们的账号跟密码 还没有设置
9.设置密码:(设置密码 -- 你的elasticsearch 是启动运行的,只需要在一个节点上设置就可以了)
   ./bin/elasticsearch-setup-passwords interactive
  接下来按照提示一起设置密码以下仅供参考,:
  1. future versions of Elasticsearch will require Java 11; your Java version from [/kaysen/tools/java/jre] does not meet this requirement
  2. Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
  3. You will be prompted to enter passwords as the process progresses.
  4. Please confirm that you would like to continue [y/N]y
  5. ————————————————
  6. Enter password for [elastic]:
  7. Reenter password for [elastic]:
  8. Enter password for [apm_system]:
  9. Reenter password for [apm_system]:
  10. Enter password for [kibana]:
  11. Reenter password for [kibana]:
  12. Enter password for [logstash_system]:
  13. Reenter password for [logstash_system]:
  14. Enter password for [beats_system]:
  15. Reenter password for [beats_system]:
  16. Enter password for [remote_monitoring_user]:
  17. Reenter password for [remote_monitoring_user]:
  18. Changed password for user [apm_system]
  19. Changed password for user [kibana]
  20. Changed password for user [logstash_system]
  21. Changed password for user [beats_system]
  22. Changed password for user [remote_monitoring_user]
  23. Changed password for user [elastic]
复制代码
然后逐个设置吧,记住,中间会有一个elastic账号的密码(123456),然后在用户名跟密码栏填写上就可以了。
至此ES集群的账号跟密码就设置完成了
10.我们可以在恣意一个节点上可以访问其他节点了
   curl -user elastic:123456 "http://10.25.169.52:9200/_cluster/health?pretty"
   集成springboot:

11.末了es集群设置后还是需要用起来的,集成到我们体系中

添加设置到设置中(还是要找到对应的版本)否则启动会报连接上的题目
  1. spring:
  2.   data:
  3.     elasticsearch:
  4.       cluster-name: your-cluster-name
  5.       cluster-nodes: 10.25.169.50:9300,10.25.169.51::9300,10.25.169.52::9300
  6.       username: elastic
  7.       password: 123456
复制代码
报错:
  1. failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{jnstBkqwSkqiMcqaSmQeFg}{192.168.56.101}{192.168.56.101:9300}]
复制代码
报错缘故原由:
   在 elasticsearch/config/elasticsearch.yml设置文件中 tcp 端口与ES 客户端设置不同等导致
  elasticsearch 服务端与 客户端版本不同等导致
  办理办法:
1.统一版本(说实话升级springboot版本也是不容易)
2.也可以利用es的api 来操作,没有springdataElasticserach封装的更好用
3.有更好的办理方案,欢迎评论 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

曹旭辉

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表