[TOC]
1. 前言
- 目標:用三臺服務器搭建ES集群,并且給集群開通安全服務,增加權限控制.
- 博主使用的ES版本為 6.3.1, 6以上的版本自帶 x-pack,但是開通權限控制還需要向ES官方購買鉑金會員或者進行破解.開通權限后,ES才可以設置登錄賬號和密碼,保證服務安全.
- 搭建過程中發現, 市面上基本都是沒做安全控制的ES集群搭建教程,本文主要針對需要做安全控制的ES集群搭建提供詳細搭建流程.后者搭建過程的復雜度高很多,博主踩的坑也很多,因此聊做記錄.
- 假設三臺服務器地址為: 10.100.0.10(主節點)10.100.0.11 ,10.100.0.12
2. 每臺服務器的ES組件配置
ES服務有很多配套組件, 很多人在搭建集群的時候不知道這些組件是否需要在每臺服務器上都做相同的配置,會產生怎樣的影響,博主全部嘗試后得出以下經驗。
坑點1: kibana和x-head服務只需要搭建在一臺服務器上就行了,當集群啟動后,kibana和x-head會自動監聽整個集群,而不再是僅監聽當前服務器。破解了權限登陸后,訪問其它子服務器也需要輸入賬號密碼,完成對其它節點的權限控制,而不需要對每臺服務器都裝相同的組件
- 10.100.0.10
主節點服務器
配件:- elasticsearch6.3.1
- kibana6.3.1
- x-head6.3.1
- 10.100.0.11 配件:
- elasticsearch6.3.1
- 10.100.0.12 配件:
- elasticsearch6.3.1
3. 集群關鍵配置的安裝和步驟
3.1 IK分詞器的安裝
[下載地址]https://github.com/medcl/elasticsearch-analysis-ik/releases
ik的版本必須與es的版本一一對應
ik安裝目錄 ./elasticsearch6.3.1/plugins
ik分詞工具需要在每臺服務器的對應目錄安裝
3.2 kibana漢化
3.3 開通x-pack高級功能
只有開通了鉑金會員, 才能給es設置登錄密碼. 為了安全性考慮最好開通.如何購買鉑金會員可以在kibana后臺的
系統管理-許可管理
看到,或者破解x-pack,詳細教程看我的另一篇文章 [10分鐘內破解elasticsearch x-pack插件]https://blog.csdn.net/qq_29202513/article/details/82747798
此步驟操作只需要在10.100.0.10主服務器上設置
直接覆蓋改完的jar包和提交json是必要操作
3.4 開通高級功能后,如何設置登錄賬號和密碼,開啟登錄驗證功能
此步驟操作只需要在10.100.0.10主服務器上設置
啟動ES
bin/elasticsearch-setup-passwords interactive 自定義設置elastic、kibana....等所有工具的登錄密碼
最高級賬號elastic 可以登錄所有組件
-
修改elasticsearch.yml配置
# 添加如下2行,打開安全配置功能 xpack.security.enabled: true xpack.security.transport.ssl.enabled: true
-
修改elasticsearch.yml配置
# 在kibana.yml下添加如下兩行 elasticsearch.username: elastic elasticsearch.password: {你修改的password}
重啟ES和kibana服務就需要登陸賬號和密碼了
3.5 ES6.0以后的集群版本需要安裝 SSL和CA證書
坑點2: 開通了高級權限,登錄功能的集群必須安裝SSL和CA證書, 一般的其實都不需要
[參考教程地址]https://blog.csdn.net/qq_25475209/article/details/81906701
- 配置生成SSL
在主服務器上生成就行
./elasticsearch-certgen
#####################################
Please enter the desired output file [certificate-bundle.zip]: cert.zip (壓縮包名稱)
Enter instance name: my-application(實例名)
Enter name for directories and files [p4mES]: elasticsearch(文件夾名)
Enter IP Addresses for instance (comma-separated if more than one) []: 127.0.0.1(實例ip,多個ip用逗號隔開)
Enter DNS names for instance (comma-separated if more than one) []: node-1(節點名,多個節點用逗號隔開)
Would you like to specify another instance? Press 'y' to continue entering instance information: (到達這一步,不需要按y重新設置,按空格鍵就完成了)
Certificates written to /usr/local/elasticsearch/bin/cert.zip(這個是生成的文件存放地址,不用填寫)
解壓生成的 cert.zip文件,將壓縮包下的2個文件夾復制到
./elasticsearch6.3.1/conf/
目錄下,每臺服務器都要復制一遍這個生成的證書
-
配置每臺服務器的elasticsearch.yml文件,加入
xpack.security.transport.ssl.enabled: true xpack.ssl.key: elasticsearch/elasticsearch.key xpack.ssl.certificate: elasticsearch/elasticsearch.crt xpack.ssl.certificate_authorities: ca/ca.crt
3.6 安裝X-head(可選操作)
安裝x-head是可選操作,如果在主服務器安裝了x-head后,需要在yml文件中添加特定的配置,否則head無法訪問登錄
# 在elasticsearch.yml中添加如下三行配置
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
- 重啟服務,并通過如下形式訪問head端
http://10.100.0.10:9100/?auth_user=elastic&auth_password=passwd
4. 最終三臺服務器的配置文件
4.1 10.100.0.10 主服務器 elasticsearch.yml
# 集群名稱,必須統一
cluster.name: es_test
# 節點名稱
node.name: node-1
network.host: 0.0.0.0
# 配置集群的節點地址
discovery.zen.ping.unicast.hosts: ["10.100.0.10","10.100.0.11","10.100.0.12"]
discovery.zen.minimum_master_nodes: 2
gateway.recover_after_nodes: 3
# x-head 訪問配置
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization,X-Requested-With,Content-Length,Content-Type
# 開通高級權限后,打開安全配置功能
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
# 配置ssl和CA證書配置
xpack.ssl.key: elasticsearch/elasticsearch.key
xpack.ssl.certificate: elasticsearch/elasticsearch.crt
xpack.ssl.certificate_authorities: ca/ca.crt
4.2 10.100.0.10 主服務器 kibana.yml
server.host: "0.0.0.0"
elasticsearch.username: elastic
elasticsearch.password: {password}
4.3 10.100.0.11 子服務器 elasticsearch.yml
# 集群名稱,必須統一
cluster.name: es_test
# 節點名稱
node.name: node-2
network.host: 0.0.0.0
# 配置集群的節點地址
discovery.zen.ping.unicast.hosts: ["10.100.0.10","10.100.0.11","10.100.0.12"]
discovery.zen.minimum_master_nodes: 2
gateway.recover_after_nodes: 3
# 開通高級權限后,打開安全配置功能
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
# 配置ssl和CA證書配置
xpack.ssl.key: elasticsearch/elasticsearch.key
xpack.ssl.certificate: elasticsearch/elasticsearch.crt
xpack.ssl.certificate_authorities: ca/ca.crt
4.4 10.100.0.12子服務器 elasticsearch.yml
# 集群名稱,必須統一
cluster.name: es_test
# 節點名稱
node.name: node-3
network.host: 0.0.0.0
# 配置集群的節點地址
discovery.zen.ping.unicast.hosts: ["10.100.0.10","10.100.0.11","10.100.0.12"]
discovery.zen.minimum_master_nodes: 2
gateway.recover_after_nodes: 3
# 開通高級權限后,打開安全配置功能
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
# 配置ssl和CA證書配置
xpack.ssl.key: elasticsearch/elasticsearch.key
xpack.ssl.certificate: elasticsearch/elasticsearch.crt
xpack.ssl.certificate_authorities: ca/ca.crt
5. 配置完畢后,集群啟動時,其它的一些坑點
1. gateway.recover_after_nodes 的坑
- gateway.recover_after_nodes: 3
- 設置了最小恢復節點為3后,第一次集群啟動,必須要三臺節點同時啟動es服務,集群才可以被訪問到.否則會一直提示
登陸賬號密碼錯誤,此時不要慌張
- 只要集群啟動了可以被訪問之后,萬一有節點掛掉, 集群還是可以訪問到的,不會出現第一次的登錄錯誤提示,此時重啟掛掉的節點就行了.
2. 不能在根目錄下運行的問題
elaticsearch默認不能用root用戶啟動,所以會報
java.lang.RuntimeException: can not run elasticsearch as root
異常
adduser es #新建用戶
passwd es|get@#123 #新建用戶密碼
#文件夾歸屬組
#chown -R [用戶]:[所屬組] 目錄
chown -R es:es elasticsearch-6.3.1/
#修改文件夾權限
chmod 770 elasticsearch-6.3.1/
#切換到用戶目錄
su es
#重新執行成功
./elasticsearch -d
3. 虛擬機最大map數量不夠
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[root@centos7.4-64 ~]# vim /etc/sysctl.conf
添加配置:vm.max_map_count=655360,然后執行命令
[root@centos7.4-64 ~]# sysctl -p
4. java內存不夠
max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
[root@centos7.4-64 ~]# vim /etc/security/limits.conf
* hard nofile 65536
* hard nofile 65536
如有問題或者需要幫忙搭建集群服務的,也可以咨詢本人
微信:w63594021