基於ES內置及自定義用戶實現kibana和filebeat的認證
關閉服務
先關閉所有ElasticSearch、kibana、filebeat進程
elasticsearch-修改elasticsearch.yml配置
按以上表格對應的實例新增conf目錄下elasticsearch.yml配置參數
# 在所有實例上加上以下配置
# 開啟本地用戶
xpack.security.enabled: true
# xpack的版本
xpack.license.self_generated.type: basic
elasticsearch-開啟服務
開啟所有ES服務
sudo -u elasticsearch ./bin/elasticsearch
elasticsearch-建立本地內置用戶
本地內置elastic、apmsystem、kibana、logstashsystem、beatssystem、remotemonitoring_user用戶
# 在其中一台master節點操作
# interactive 自定密碼 auto自動生密碼
sudo -u elasticsearch ./bin/elasticsearch-setup-passwords interactive
# 輸入elastic密碼
# 輸入apm_system密碼
# 輸入kibana密碼
# 輸入logstash_system密碼
# 輸入beats_system密碼
# 輸入remote_monitoring_user密碼
測試內部用戶
通過base64將elastic用戶進行加密,格式為“elastic:elastic的密碼“
# 例如以下格式
curl -H "Authorization: Basic ZWxhc3RpYzplbGFzdGkxMjM0NTY3OA==" "http://192.168.1.31:9200/_cat/nodes?v"
如果不通過Basic訪問或base64加密錯誤會報以下錯誤:
"status": 401
kibana-創建私鑰庫
在192.168.1.21創建私鑰庫
cd /opt/kibana/
# 創建密鑰庫
sudo -u kibana ./bin/kibana-keystore create
# 連接ES用戶名,這里輸入kibana
sudo -u kibana ./bin/kibana-keystore add elasticsearch.username
# 連接ES密碼,這里輸入剛剛設置kibana的密碼
sudo -u kibana ./bin/kibana-keystore add elasticsearch.password
在192.168.1.21確認私鑰庫
sudo -u kibana ./bin/kibana-keystore list
啟動服務
sudo -u kibana /opt/kibana/bin/kibana -c /opt/kibana/config/kibana.yml
filebeat-服務器上創建密鑰庫
在192.168.1.11創建filebeat密鑰庫
cd /opt/filebeat/
#創建密鑰庫
./filebeat keystore create
#創建test-filebeat用戶私鑰
./filebeat keystore add test-filebeat
確認filebeat密鑰庫
./filebeat keystore list
filebeat-配置filebeat.yml
配置filebeat.yml
# 文件輸入
filebeat.inputs:
# 文件輸入類型
- type: log
# 開啟加載
enabled: true
# 文件位置
paths:
- /var/log/nginx/access.log
# 自定義參數
fields:
type: nginx_access # 類型是nginx_access,和上面fields.type是一致的
# 輸出至elasticsearch
output.elasticsearch:
# 連接ES集群的用戶名
username: test-filebeat
# 連接ES集群的密碼
password: "${test-filebeat密碼}"
# elasticsearch集群
hosts: ["http://192.168.1.31:9200",
"http://192.168.1.32:9200",
"http://192.168.1.33:9200"]
# 索引配置
indices:
# 索引名
- index: "nginx_access_%{+yyy.MM}"
# 當類型是nginx_access時使用此索引
when.equals:
fields.type: "nginx_access"
# 關閉自帶模板
setup.template.enabled: false
# 開啟日志記錄
logging.to_files: true
# 日志等級
logging.level: info
# 日志文件
logging.files:
# 日志位置
path: /opt/logs/filebeat/
# 日志名字
name: filebeat
# 日志輪轉期限,必須要2~1024
keepfiles: 7
# 日志輪轉權限
permissions: 0600
啟動filebeat
/opt/filebeat/filebeat -e -c /opt/filebeat/filebeat.yml -d "publish"
測試
寫入一條數據
curl -I "http://192.168.1.11"
在kibana中查看
附錄
kibana角色權限相關文檔鏈接
https://www.elastic.co/guide/en/elasticsearch/reference/7.3/security-privileges.html#privileges-list-cluster