前言
剛開始使用ES接觸的就是rpm或者是云上提供的ES服務,基本上開箱即用。特別是云上的ES服務,開局就是集群版本,提供的是優化后的參數配置、開箱即匹配訪問鑒權及常用插件,如無特殊需要基本上屏蔽了所有細節,直接可投入生產使用。
現在遇到一個問題,驗證一下遠程集群的配置和使用,在沒有多套集群的情況下,想在本地模擬兩套ES集群,不得不從頭走一遍ES集群從下載、自定義配置到啟動kibana可使用的這一套流程。簡單記錄一下ES維護最原始的狀態。
task:
- 啟動一個單節點的ES服務
- ES開啟鑒權
- 創建用戶
- 配置kibana
環境介紹
操作系統: macOS 12.1
硬件架構: ARM
ES: 8.12.2
Jdk: 22.0.1
下載es包
本次部署使用8.12.2版本的ES,不是最新版本,需要到https://www.elastic.co/cn/downloads/past-releases#elasticsearch頁面查找所需要的版本并下載。
下載地址如下:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-8-12-2
選擇對應原版本: MACOS AARCH64 : https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.12.2-darwin-aarch64.tar.gz
配置jdk
mac上使用brew安裝jdk
brew install java #默認會安裝最新版的jdk
因為使用的ES基本也是最近的版本,最新版本的jdk可以支持ES運行。
手動安裝指定jdk版本
下載所需要的jdk版本后,解壓到指定的目錄
編輯{userhome}/.zshrc
export JAVA_HOME=/Users/abc-test/data/jdk1.8.0_411.jdk/Contents/Home
export PATH=$PATH:$JAVA_HOME/bin
重新加載.zshrc
source .zshrc
配置ES
運行ES
解壓es包
tar -xzf elasticsearch-8.12.2-darwin-x86_64.tar.gz
修改端口號(同一臺機器上要運行多個ES進程,修改端口號避免沖突)
vim elasticsearch-8.12.2/config/elasticsearch.yml
#
cluster.name: remote-test #修改集群名稱
node.name: node-101 #指定節點名稱
network.host: 0.0.0.0 #綁定的地址,如不打開端口只監聽127.0.0.1
http.port: 19200 #指定http端口
cluster.initial_master_nodes: ["node-101"] #master節點 單節點集群填本身就可以,多節點集群需要填上master節點的列表
transport.port: 19300 #tcp的監聽端口
修改完成啟動ES進程
#后臺執行
bin/elasticsearch -d
用戶
ES進程啟動后,會在第一次啟動時打印出內置用戶elastic的密碼。 日志類似這樣:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
? Elasticsearch security features have been automatically configured!
? Authentication is enabled and cluster connections are encrypted.?? Password for the elastic user (reset with
bin/elasticsearch-reset-password -u elastic
):
XQZ2EWK3LG8SaB*AYrdM?? HTTP CA certificate SHA-256 fingerprint:
79f5240be4f0a02af881d7c73a626741d6e8df9ff130e52603f668635ab746c6?? Configure Kibana to use this cluster:
? Run Kibana and click the configuration link in the terminal when Kibana starts.
? Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes):
eyJ2ZXIiOiI4LjEyLjIiLCJhZHIiOlsiMTAuMjUzLjMuMTU5OjE5MjAwIl0sImZnciI6Ijc5ZjUyNDBiZTRmMGEwMmFmODgxZDdjNzNhNjI2NzQxZDZlOGRmOWZmMTMwZTUyNjAzZjY2ODYzNWFiNzQ2YzYiLCJrZXkiOiJzd3hlbTVBQnRqajNYdHpDUFdHZzpZTnEtOGRTcFI0bVQ1c3phZjMtWVh3In0=?? Configure other nodes to join this cluster:
? Copy the following enrollment token and start new Elasticsearch nodes withbin/elasticsearch --enrollment-token <token>
(valid for the next 30 minutes):
eyJ2ZXIiOiI4LjEyLjIiLCJhZHIiOlsiMTAuMjUzLjMuMTU5OjE5MjAwIl0sImZnciI6Ijc5ZjUyNDBiZTRmMGEwMmFmODgxZDdjNzNhNjI2NzQxZDZlOGRmOWZmMTMwZTUyNjAzZjY2ODYzNWFiNzQ2YzYiLCJrZXkiOiJzZ3hlbTVBQnRqajNYdHpDUFdHZzpYdmMyc0NQN1EzMlIzODJYbVExNHdnIn0=If you’re running in Docker, copy the enrollment token and run:
docker run -e "ENROLLMENT_TOKEN=<token>" docker.elastic.co/elasticsearch/elasticsearch:8.12.2
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
可自行重置默認密碼
bin/elasticsearch-reset-password -u elastic
驗證ES啟動是否成功
curl localhost:19200/_cat/health -u "elastic:XQZ2EWK3LG8SaB*AYrdM"
正常返回
1720593690 06:41:30 remote-test green 1 1 1 1 0 0 0 0 - 100.0%
ES啟動成功。
配置kibana
使用了一段時間kibana作為ES的命令行和管理工具后,發現有界面的系統是真的香,索引管理,數據展示,dev tools 作常用功能模塊已基本上滿足日常管理需求,且不論更進一步的數據分析等模塊。緊跟ES也跑一個kibana的實例出來。 這里注意kibana要跟ES的版本保持一致。
下載包
匹配ES版本的kibana: https://artifacts.elastic.co/downloads/kibana/kibana-8.12.2-darwin-aarch64.tar.gz
修改配置
下載到本地后,解壓,修改配置
vim config/kibana.yml
server.port: 5602
server.ssl.enabled: false #本地使用,禁用https協議
elasticsearch.hosts: ["http://localhost:19200"] #配置ES的地址
elasticsearch.username: "kibana_system" #連接ES的用戶名
elasticsearch.password: "As4fpYOfz-ufMr7zI8dt" #連接ES的密碼
這里需要到ES中重置kibana_system的密碼,kibana_system是ES系統內置的用戶。
bin/elasticsearch-reset-password -u kibana_system
過程如下:
? elasticsearch-8.12.2 bin/elasticsearch-reset-password -u kibana_system
This tool will reset the password of the [kibana_system] user to an autogenerated value.
The password will be printed in the console.
Please confirm that you would like to continue [y/N]yPassword for the [kibana_system] user successfully reset.
New value: As4fpYOfz-ufMr7zI8dt
啟動
bin/kibana
附錄
知其所以然
內置用戶
es提供了一系列的內置用戶來運行和管理ES進程,這些用戶設置了固定的權限。但是這些用戶在沒有設置密碼前是不能使用的。elastic是內置的超級用戶,可以用來設置其他用戶的密碼。elastic用戶自己的密碼在ES啟動的時候會由進程自動設置,并在啟動日志中打印出來。
es的內置用戶:
- elastic #超級管理員用戶
- kibana_system #kibana用戶用于kibana連接elasticsearch
- logstash_system #logstash使用
- Beats_system #beats 程序使用
- apm_system #apm功能
- Remote_monitoring_user #遠程監控功能用戶
內置用戶存儲在.security這個特殊的索引中,當用戶的密碼改變時,會同步這些變更內容到各個節點。一旦這個特殊的索引被刪除或者被快照等其他數據所覆蓋,之前針對用戶所做的所有操作都會丟失。
針對內置用戶可以使用bin/elasticsearch-reset-password修改用戶密碼
bin/elasticsearch-reset-password -u kibana_system
初次啟動的集群只有重置密碼后才能使用。