Elasticsearch登陸認證


Elasticsearch 單節點登錄認證

1.在 elasticsearch.yml 中添加如下配置

# 配置X-Pack
http.cors.enabled: true
http.cors.allow-origin: "*"

http.cors.allow-headers: Authorization
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true

需要重啟es

2.設置密碼

cd /home/es/elasticsearch-7.8.1
./bin/elasticsearch-setup-passwords interactive -u 'http://192.168.88.110:7920'

Elasticsearch 集群節點登錄認證

1.0 啟動三台es

./bin/elasticsearch -d
#查看 master 節點
curl http://192.168.88.110:7920/_cat/master

1.1執行命令激活xpack**

curl -H "Content-Type:application/json" -XPOST  http://192.168.88.110:7920/_xpack/license/start_trial?acknowledge=true

1.2es開啟xpack

設置密碼,在master設置,node節點可以同步該用戶名/密碼

xpack.security.enabled: true

到此為止完成xpack集群,目前無SSL。

2.使用SSL

2.1master節點創建證書

#進入elasticsearch-7.8.1
cd /home/es/elasticsearch/
#生成CA證書【CA證書:elastic-stack-ca.p12】
./bin/elasticsearch-certutil ca
#保存elastic-stack-ca.p12路徑並輸入密碼(123456)
Please enter the desired output file [elastic-stack-ca.p12]:    #指定證書文件名直接回車為默認
Enter password for elastic-stack-ca.p12 : #輸入證書密碼
#創建證書私鑰
./bin/elasticsearch-certutil  cert --ca elastic-stack-ca.p12 
#保存elastic-certificates.p12路徑並輸入密碼(123456)
#將上面生成的兩個文件拷貝到elastic的config目錄下
Enter password for CA (elastic-stack-ca.p12) : #上邊設置ca的密碼 我自己設置的:123456
Please enter the desired output file [elastic-certificates.p12]: #私鑰的文件名,回車默認
Enter password for elastic-certificates.p12 : #私鑰密碼 123456
#如果是root用戶創建的,即需要修改權限,建議都用es用戶創建
chmod 664 elastic-certificates.p12
chmod 664 elastic-stack-ca.p12
#將生成的2個文件都移動到config文件夾中
mkdir xpack
mv elastic-* ./xpack
#傳證書和私鑰到其他節點[需要輸入當前用戶的密碼:我的是123456]
cd xpack 
scp elastic-* wsy2:$PWD   
scp elastic-* wsy3:$PWD
  • 修改配置文件elasticsearch.yml
[root@wsy1 elasticsearch-7.8.1]# cd /export/server/es/elasticsearch-6.5.4/config/
[root@wsy1 config]# vi elasticsearch.yml


#補充:tcp端口,集群內部通訊使用
transport.tcp.port: 9300
#節點配置【默認,在生產中可以修改】
node.master: true #有成為主節點的資格,建議3的倍數
node.data: true #存儲數據
#node.ingest: true #可以針對海量請求時進行負載均衡
#防止腦裂問題,官方推薦【N/2 +1】,其中N是具有master資格的節點的數量(我們的情況是3,因此這個參數設置為2
#discovery.zen.minimum_master_nodes: 2
#添加xpack認證參數【修改證書路徑】
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate # 證書認證級別
xpack.security.transport.ssl.keystore.path: /home/es/elasticsearch/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: /home/es/elasticsearch/elastic-certificates.p12

#
  • 各個節點為 xpack.security.transport 添加密碼,每個節點都執行這兩個命令【我這里123456】
./bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password

./bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
  • 查看集群狀態(也可以通過kibana或者其他工具查看)
http://192.168.88.111:7920/_cluster/health

curl -u elastic:123456 -XGET 192.168.88.110:7920/_cat/health?v
  • 啟動elasticsearch
cd /export/server/es/elasticsearch-6.5.4/bin
./elasticsearch -d #后台啟動 elasticsearch
  • 設置加密密碼【一個節點加密就行】【我全部弄的1234567】
./bin/elasticsearch-setup-passwords interactive
  • logstash增加訪問es集群的用戶及密碼
user => "elastic" # 注意:這里演示使用超級賬號,安全起見最好是使用自定義的賬號,並授予該用戶創建索引的權限,具體看下方地址
password => "1234567" # 密碼是上面步驟設置的
  • elasticsearch-head訪問es集群的用戶及密碼

elasticsearch-head插件此時再去訪問有安全認證的es集群時,會發現無法進行查看,打開控制台可以看到報錯:401 unauthorized
解決辦法是修改elasticsearch.yml文件,增加以下配置。

#配置elasticsearch-head訪問es集群的用戶及密碼
#http.cors.allow-headers: Authorization,content-type
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type 
#http://192.168.88.111:9100/?auth_user=elastic&auth_password=1234567

http://wsy1:9100/?auth_user=elastic&auth_password=changeme

修改三台es節點,然后重新啟動,再次url+認證信息方式可以正常訪問es集群。

http://192.168.88.111:9100/?auth_user=elkstack&auth_password=1234567

  • Kibana組件訪問帶有安全認證的Elasticsearch集群

配置文件kibana.yml中需要加入以下配置

elasticsearch.username: "kibana"  # 注意:此處不用超級賬號elastic,而是使用kibana跟es連接的賬號kibana
elasticsearch.password: "1234567"

然后重啟kibana,再次訪問的話就就需要輸入上述賬號密碼才能登陸訪問了

不一樣的地方:
在Management下面的Kibana最后出現一個Security,有User和Role
方便kibana多用戶創建及角色權限控制

ES X-pack 過時

現象:

{
    "error": {
        "root_cause": [
            {
                "type": "security_exception",
                "reason": "current license is non-compliant for [security]",
                "license.expired.feature": "security",
                "suppressed": [
                    {
                        "type": "security_exception",
                        "reason": "current license is non-compliant for [security]",
                        "license.expired.feature": "security"
                    }
                ]
            }
        ],
        "type": "security_exception",
        "reason": "current license is non-compliant for [security]",
        "license.expired.feature": "security",
        "suppressed": [
            {
                "type": "security_exception",
                "reason": "current license is non-compliant for [security]",
                "license.expired.feature": "security"
            }
        ]
    },
    "status": 403
}

6.3版本以后,Elastic Stack的默認發行版中包含基本(免費)層功能。無需許可證注冊。

只需要降級到基礎版本即可

可以在kibana執行:

POST /_license/start_basic?acknowledge=true

或者[記得替換 elasticsearch 為ip]

curl -XPOST "http://elasticsearch:9200/_license/start_basic?acknowledge=true"

例:
[elastic@node01 bin]$ curl -u elastic:123456 -XPOST "http://node01:7920/_license/start_basic?acknowledge=true"
{"acknowledged":true,"basic_was_started":true}[elastic@node01 bin]$ ll


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM