elasticsearch 设置用户名和密码

一给  金牌会员 | 2024-8-21 15:00:22 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 847|帖子 847|积分 2541

无密码的其他设置项在:https://blog.csdn.net/Xeon_CC/article/details/132064295
elasticsearch.yml设置文件:
留意,这里的elastic-cert.p12文件是根据elastic-stack-ca.p12来天生的,以是要先天生elastic-stack-ca.p12。
  1. xpack.security.enabled: true
  2. xpack.security.http.ssl.enabled: true
  3. xpack.security.http.ssl.keystore.path: /path/to/elastic-cert.p12
  4. xpack.security.transport.ssl:
  5.     enabled: true
  6.     verification_mode: certificate
  7.     client_authentication: required
  8.     keystore.path: /path/to/elastic-cert.p12
  9.     truststore.path: /path/to/elastic-cert.p12
复制代码
  1. 证书的密码:
  2. abcd1234
  3. 所有用户的密码:
  4. bcda1234
  5. 生成证书
  6. elasticsearch-certutil ca --out /path/to/elastic-stack-ca.p12 --pass abcd1234
  7. 如果你不想生成单独的证书,也就是三个节点共用一个证书,请不要执行这行命令,这行命令将被下文所提到的证书生成方式代替。
  8. elasticsearch-certutil cert --ca /path/to/elastic-stack-ca.p12
  9. 输入指定路径
  10. /path/to/elastic-cert.p12
  11. 注意:设置密码这一步,三个节点都要执行。
  12. 设置密码
  13. elasticsearch-keystore add xpack.security.http.ssl.keystore.secure_password
  14. elasticsearch-keystore add xpack.security.http.ssl.truststore.secure_password
  15. elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
  16. elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
  17. 查看密码
  18. elasticsearch-keystore show xpack.security.http.ssl.keystore.secure_password
  19. elasticsearch-keystore show xpack.security.http.ssl.truststore.secure_password
  20. elasticsearch-keystore show xpack.security.transport.ssl.truststore.secure_password
  21. elasticsearch-keystore show xpack.security.transport.ssl.keystore.secure_password
  22. 如果你希望集群共享一个整数,那么三个节点,确保elastic-stack-ca.p12证书文件是复制到每个节点的。
  23. 设置为false
  24. xpack.security.http.ssl.enabled: false
  25. 修改密码
  26. elasticsearch-setup-passwords interactive
  27. 最后,配置文件改回去
  28. xpack.security.http.ssl.enabled: true
  29. 最后在elasticsearch-head访问:https://192.168.0.111:9200/?auth_user=xxx&auth_password=xxx
复制代码
查看证书过期时间:
  1. #生成pem文件
  2. openssl pkcs12 -in /path/to/your/elastic-stack-ca.p12 -clcerts -nokeys -out extracted_certificate.pem
  3. #通过pem文件查看证书过期时间:
  4. openssl x509 -in /path/to/your/extracted_certificate.pem -noout -enddate
复制代码
在上面的“天生证书”这个步骤中的第二行命令,可以换为以下的步骤,安全性会更高,留意,确生存在elastic-stack-ca.p12文件,也就上述“天生证书”步骤中的第一行命令天生的文件。
首先恣意选择一个节点:在任何目录下创建instances.yml,并键入如下内容:
  1. instances:
  2.   - name: "node-1"
  3.     dns:
  4.       - "localhost"
  5.     ip:
  6.       - "192.168.1.105"
  7.   - name: "node-2"
  8.     dns:
  9.       - "localhost"
  10.     ip:
  11.       - "192.168.1.105"
  12.   - name: "node-3"
  13.     dns:
  14.       - "localhost"
  15.     ip:
  16.       - "192.168.1.105"
复制代码
假如要设置公网域名:
  1. instances:
  2.   - name: "node-1"
  3.     dns: ["abc.cde.com", "localhost"]
  4.     ip: ["192.168.1.123"]
  5.   - name: "node-2"
  6.     dns: ["abc.cde.com", "localhost"]
  7.     ip: ["192.168.1.123"]
  8.   - name: "node-3"
  9.     dns: ["abc.cde.com", "localhost"]
  10.     ip: ["192.168.1.123"]
复制代码
比如我放在config目录下
  1. elasticsearch-certutil cert --in config/instances.yml --out config/certs/certificates.zip --ca config/certs/elastic-stack-ca.p12 --pass abcd1234
复制代码
然后,解压certificates.zip文件,会得到node-1、node-2、node-3文件夹
把node-1.p12、node-2.p12、node-3.p12复制到每个对应es节点中的config/certs目录下,certs目录假如没有则本身新建。
最后更改elasticsearch.yml设置文件

把这些证书文件更换为各自的证书文件。
这种设置方式的安全性要更高,保举使用这种每个节点的证书都差别的方式。
怎样用python通过SSL证书毗连ES集群?
在每一个节点实行一下命令:
使用openssl设置客户端证书
  1. openssl pkcs12 -in node-1.p12 -out node-1.crt -nokeys -nodes
  2. openssl pkcs12 -in node-2.p12 -out node-2.crt -nokeys -nodes
  3. openssl pkcs12 -in node-3.p12 -out node-3.crt -nokeys -nodes
复制代码
然后,归并证书;
  1. type node-1.crt node-2.crt node-3.crt > combined_ca.crt
复制代码
最后在python中使用combined_ca.crt文件,把服务器天生的这个文件拷贝到python项目的目录中。
  1. es = Elasticsearch(
  2.     ['https://192.168.1.105:9201', 'https://192.168.1.105:9202', 'https://192.168.1.105:9203'],  # 你可以指定一个或多个节点
  3.     http_auth=('elastic', 'your_password'),
  4.     scheme='https',
  5.     use_ssl=True,
  6.     ca_certs='combined_ca.crt', # 指定证书的路径。
  7.     verify_certs=True  # 设置为True意味着客户端将验证服务器的证书
  8. )
  9. index_name='abccc_2023-08-09'
  10. if not es.indices.exists(index=index_name):
  11.     body = {
  12.         "settings": {
  13.             "number_of_shards": 3,
  14.         },
  15.         "mappings": {
  16.             "properties": {
  17.                 "location": {
  18.                     "type": "geo_shape"
  19.                 },
  20.                 "geo_loctn": {
  21.                     "type": "geo_point"
  22.                 }
  23.             }
  24.         }
  25.     }
  26.     es.indices.create(index=index_name, body=body)
  27.     print('create es index ...')
复制代码
假如能创建索引,阐明验证成功。

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

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

一给

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表