本身是基於CentOS 7的。
- 通過tar包安裝
- Elasticsearch 7.4.2,默認集成JDK(Openjdk 13.0.1);這里簡稱ES
- Kibana 7.4.2
Elastic Stack License
在新版的Elastic中,基礎版(免費)的已經提供了基礎的核心安全功能,可以在生產中部署,不再需要Nginx + Basic Auth代理了。
參考官網,如下圖:
默認情況下Elastic中的安全功能是被禁用的,那么在本文中,就是采用基礎版,自動申請Basic License的,然后分別開啟Auth認證,以及Nodes間加密通信SSL。
安裝Elasticsearch
我這里只有一台EC2主機,需要安裝由三台Elasticsearch 組成的集群,所以采用使用不同端口的方式。
IP | Http Port(ES HTTP API) | Transport Port(ES集群內部通信使用) | 名稱 |
---|---|---|---|
172.17.0.87 | 9200 | 9331 | es01 |
172.17.0.87 | 9201 | 9332 | es02 |
172.17.0.87 | 9202 | 9333 | es03 |
下載Elasticsearch
$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.2-linux-x86_64.tar.gz |
如果你只是想快速的搭建單台ES,用於測試,可以直接啟動:
$ cd elasticsearch-7.4.2 |
此時默認是以development 方式啟動的,一些前提條件如果不符合其要求只會提示,但並不會無法啟動,此時只會監聽在127.0.0.1:9200
上,只能用於測試;當你更改了``elasticsearch.yml配置文件中的
network.host`參數時,就會以生產的方式啟動。
我們這采用生產的方式,也就是說他的前提依賴都必須滿足,否則無法啟動。
目錄結構
Type | Description | Default Location | Setting |
---|---|---|---|
home | Elasticsearch home directory or $ES_HOME |
Directory created by unpacking the archive | ES_ HOME |
bin | Binary scripts including elasticsearch to start a node and elasticsearch-plugin to install plugins |
$ES_HOME/bin |
|
conf | Configuration files including elasticsearch.yml |
$ES_HOME/config |
ES_PATH_CONF |
data | The location of the data files of each index / shard allocated on the node. Can hold multiple locations. | $ES_HOME/data |
path.data |
logs | Log files location. | $ES_HOME/logs |
path.logs |
plugins | Plugin files location. Each plugin will be contained in a subdirectory. | $ES_HOME/plugins |
|
repo | Shared file system repository locations. Can hold multiple locations. A file system repository can be placed in to any subdirectory of any directory specified here. | Not configured | path.repo |
script | Location of script files. | $ES_HOME/scripts |
path.scripts |
系統設置
ulimits
編輯配置文件/etc/security/limits.conf
,因為我這里使用默認的用戶ec2-user
來運行ES,所以這里的賬號填ec2-user
,你可以根據自己的情況填寫,或者寫成星號;
# - nofile - max number of open file descriptors 最大打開的文件描述符數 |
檢查:
$ ulimit -a |
禁用交換分區 swap
執行命令以立刻禁用swap:
$ sudo swapoff -a |
這里只是臨時的禁用了,系統重啟后還是會啟動的,編輯以下配置文件將swap的掛載去掉:
$ sudo vim /etc/fstab |
配置swappiness 以及虛擬內存
這是減少了內核的交換趨勢,並且在正常情況下不應該導致交換,同時仍然允許整個系統在緊急情況下交換。
# 增加如下兩行 |
開啟ES的內存鎖定:
在ES的配置文件中config/elasticsearch.yml
增加如下行:
bootstrap.memory_lock: true |
ES集群的前提依賴已經配置完畢了,在真正的配置ES以及啟動集群前,我們需要先明白一些概念,如下:
[Elasticsearch 基礎概念](#Elasticsearch 基礎概念),[Elasticsearch Note說明](#Elasticsearch Note說明),以便我們能更好的配置ES集群:
Elasticsearch 基礎概念
Cluster
Elasticsearch 集群,由一台或多台的Elasticsearch 節點(Node)組成。
Node
Elasticsearch 節點,可以認為是Elasticsearch的服務進程,在同一台機器上啟動兩個Elasticsearch實例(進程),就是兩個node節點。
Index
索引,具有相同結構的文檔的集合,類似於關系型數據庫的數據庫實例(6.0.0版本type廢棄后,索引的概念下降到等同於數據庫表的級別)。一個集群中可以有多個索引。
Type
類型,在索引中內進行邏輯細分,在新版的Elasticsearch中已經廢棄。
Document
文檔,Elasticsearch中的最小的數據存儲單元,JSON數據格式,很多相同結構的文檔組成索引。文檔類似於關系型數據庫中表內的一行記錄。
Shard
分片,單個索引切分成多個shard,分布在多台Node節點上存儲。可以利用shard很好的橫向擴展,以存儲更多的數據,同時shard分布在多台node上,可以提升集群整體的吞吐量和性能。在創建索引的時候可以直接指定分片的數量即可,一旦指定就不能再修改了。
Replica
索引副本,完全拷貝shard的內容,一個shard可以有一個或者多個replica,replica就是shard的數據拷貝,以提高冗余。
replica承擔三個任務:
- shard故障或者node宕機時,其中的一個replica可以升級成shard
- replica保證數據不丟失,保證高可用
- replica可以分擔搜索請求,提高集群的吞吐和性能
shard的全稱叫primary shard,replica全稱叫replica shard,primary shard數量在創建索引時指定,后期不能修改,replica shard后期可以修改。默認每個索引的primary shard值為5,replica shard值為1,含義是5個primary shard,5個replica shard,共10個shard。因此Elasticsearch最小的高可用配置是2台服務器。
Elasticsearch Note 說明:
在ES集群中的Note有如下幾種類型:
-
Master-eligible
:node.master:true
的節點,使其有資格唄選舉為控制集群的主節點。主節點負責集群范圍內的輕量級操作,例如創建或刪除索引,跟蹤哪些節點是集群的一部分以及確定將哪些碎片分配給哪些節點 -
data
:node.data:true
的節點,數據節點,保存數據並執行與數據有關的操作,例如CRUD(增刪改查),搜索和聚合。 -
ingest
:node.ingest:true
的節點,能夠將管道(Pipeline)應用於文檔,以便在建立所以之前轉換和豐富文檔。 -
machine-learning
:xpack.ml.enabled
andnode.ml
set totrue
,適用於x-pack版本,OSS版本不能增加,否則無法啟動。 -
coordinating node
: 協調節點,諸如搜索請求或批量索引請求之類的請求可能涉及保存在不同數據節點上的數據。例如,搜索請求在兩個階段中執行,由接收客戶端請求的節點(協調節點)進行協調。在分散階段,協調節點將請求轉發到保存數據的數據節點。每個數據節點在本地執行該請求,並將其結果返回給協調節點。在收集 階段,協調節點將每個數據節點的結果縮減為單個全局結果集。
每個節點都隱式地是一個協調節點。這意味着,有三個節點
node.master
,node.data
並node.ingest
都設置為false
只充當一個協調節點,不能被禁用。結果,這樣的節點需要具有足夠的內存和CPU才能處理收集階段。
ingest
英 /ɪnˈdʒest/ 美 /ɪnˈdʒest/ 全球(美國)
vt. 攝取;咽下;吸收;接待
過去式 ingested過去分詞 ingested現在分詞 ingesting第三人稱單數 ingestscoordinating
英 /kəʊˈɔːdɪneɪtɪŋ/ 美 /koˈɔrdɪnetɪŋ/ 全球(英國)
v. (使)協調;協同動作;(衣服等)搭配;調節,協調;配合;與……形成共價鍵(coordinate 的現在分詞)
adj. 協調的;並列的;同位的;對等的
默認值:
node.master: ture
node.voting_only: false
node.data: true
node.ml: true
xpack.ml.enabled: true
cluster.remote.connect: false
Master-eligible,合格主節點,主合格節點
主節點負責集群范圍內的輕量級操作,例如創建或刪除索引,跟蹤哪些節點是集群的一部分以及確定將哪些碎片分配給哪些節點。擁有穩定的主節點對於群集健康非常重要。
可以通過主選舉過程來選舉不是僅投票節點的任何符合主資格的節點成為主節點。
索引和搜索數據是占用大量CPU,內存和I / O的工作,這可能會對節點的資源造成壓力。為確保您的主節點穩定且不受壓力,在較大的群集中,最好將符合角色的專用主節點和專用數據節點分開。
雖然主節點也可以充當協調節點, 並將搜索和索引請求從客戶端路由到數據節點,但最好不要為此目的使用專用的主節點。對於符合主機要求的節點,其工作量應盡可能少,這對於群集的穩定性很重要。
設置節點成為主合格節點:
node.master: true |
對於OSS版本:
node.master: true |
僅投票節點
是參與投票過程,但是不能成為主節點的節點,只投票節點在選舉中充當決勝局。
設置節點成為僅投票節點:
node.master: true |
注意:
-
OSS版本不支持這個參數,如果設置了,將無法啟動。
-
只有符合主機資格的節點才能標記為僅投票。
高可用性(HA)群集至少需要三個主節點,其中至少兩個不是僅投票節點,可以將另一個節點設置成僅投票節點。這樣,即使其中一個節點發生故障,這樣的群集也將能夠選舉一個主節點。
數據節點
數據節點包含包含您已建立索引的文檔的分片。數據節點處理與數據相關的操作,例如CRUD,搜索和聚合。這些操作是I / O,內存和CPU密集型的。監視這些資源並在過載時添加更多數據節點非常重要。
具有專用數據節點的主要好處是將主角色和數據角色分開。
要在默認分發中創建專用數據節點,請設置:
node.master: false |
對於OSS版本的設置:
node.master: false |
Ingest 節點
接收節點可以執行由一個或多個接收處理器組成的預處理管道。根據攝取處理器執行的操作類型和所需的資源,擁有專用的攝取節點可能有意義,該節點僅執行此特定任務。
要在默認分發中創建專用的攝取節點,請設置:
node.master: false |
在OSS上設置:
node.master: false |
僅協調節點
如果您不具備處理主要職責,保存數據和預處理文檔的能力,那么您將擁有一個僅可路由請求,處理搜索縮減階段並分配批量索引的協調節點。本質上,僅協調節點充當智能負載平衡器。
僅協調節點可以通過從數據和符合資格的主節點上卸載協調節點角色來使大型集群受益。他們像其他節點一樣加入集群並接收完整的集群狀態,並且使用集群狀態將請求直接路由到適當的位置。
在集群中添加過多的僅協調節點會增加整個集群的負擔,因為選擇的主節點必須等待每個節點的集群狀態更新確認!僅協調節點的好處不應被誇大-數據節點也可以很好地達到相同的目的。
設置僅協調節點:
node.master: false |
在OSS上設置:
node.master: false |
機器學習節點
機器學習功能提供了機器學習節點,該節點運行作業並處理機器學習API請求。如果xpack.ml.enabled
設置為true且node.ml
設置為false
,則該節點可以處理API請求,但不能運行作業。
如果要在群集中使用機器學習功能,則必須在所有符合主機資格的節點上啟用機器學習(設置xpack.ml.enabled
為true
)。如果您只有OSS發行版,請不要使用這些設置。
有關這些設置的更多信息,請參閱機器學習設置。
要在默認分發中創建專用的機器學習節點,請設置:
node.master: false |
配置Elasticsearch
拷貝三台ES目錄:
$ ls |
配置Elasticsearch 名稱解析
我這里直接使用hosts
文件:
cat >> /etc/hosts <<EOF |
編輯ES配置文件config/elasticsearch.yml
默認的配置文件在$ES_HOME/config/elasticsearch.yml
中,配置文件是以yaml的格式配置,其中有三種配置方式:
path: |
或者寫成單行的格式:
path.data: /var/lib/elasticsearch |
再或者通過環境變量的方式:這種方式在Docker,Kubernetes環境中很有用。
node.name: ${HOSTNAME} |
下面詳解一下各個配置的信息,然后提供各個ES的elasticsearch.yml的配置文件:
Elasticsearch 配置詳解
配置ES 的PATH路徑,path.data
& path.logs
如果不配置默認是$ES_HOME
中的子目錄data
,logs
。
path: |
path.data
,可以設置多個目錄:
path: |
配置ES集群名稱:cluster.name
一個node節點只能加入一個集群當中,不同的節點配置同一個cluster.name
可以組成ES集群。請確保不同的cluster集群中使用不同的cluster.name
:
cluster.name: logging-prod |
配置ES節點名稱:node.name
node.name
代表節點名稱,是人類可讀用於區分node節點;如果不配置,默認是主機名
node.name: prod-data-002 |
配置ES節點監聽地址:network.host
如果不配置,默認是監聽在127.0.0.1
和 [::1]
,同時以development的方式啟動。
# 監聽在指定IP上 |
network.host
可用的配置:
_[networkInterface]_ |
Addresses of a network interface, for example _eth0_ . 指定網卡 |
---|---|
_local_ |
Any loopback addresses on the system, for example 127.0.0.1 . 本地回環IP |
_site_ |
Any site-local addresses on the system, for example 192.168.0.1 . 內網IP |
_global_ |
Any globally-scoped addresses on the system, for example 8.8.8.8 . 公網IP |
配置ES節點的發現和集群組成設置
這里主要有兩個主要的配置:發現和集群組成設置,集群間的node節點可以實現彼此發現、選舉主節點,從而組成ES集群。
discovery.seed_hosts
如果不配置,默認ES在啟動的時候會監聽本地回環地址,同時會掃描本地端口:9300-9305
,用於發現在本機啟動的其他節點。
所以如果不進行的任何配置,將$ES_HOME目錄拷貝三份,然后全部啟動,默認也是可以組成ES集群的,用於測試使用。 如果你需要在多台機器上啟動ES節點,以便組成集群,那么這個參數必須配置,以便nodes之間能夠發現彼此。
discovery.seed_hosts
是一個列表,多個元素用逗號隔開,元素可以寫成:
- host:port,指定自定義的transport集群間通信端口
- host,使用默認的transport集群間通信端口:9300-9400;參考
- 域名,可以解析成多個IP,會自動的與每個解析到的IP去連接測試
- 其他自定義可以解析的名稱
cluster.initial_master_nodes
在deveplopment模式中是一台主機上自動發現的nodes彼此之間自動配置的。但是在生產的模式中必須要配置。
這個參數用於在新的集群第一次啟動的時使用,以指定可以參與選舉合格主節點列表(node.master: true)。在集群重啟或者增加新節點的時候這個參數不起作用,因為在每個node節點上都已經保存有集群的狀態信息。
cluster.initial_master_nodes
也是一個列表,多個元素用逗號隔開,元素可以寫成:參考
- 配置的node.name名稱。
- 如果沒有配置node.name,那么使用完整主機名
- FQDN
- host,如果沒有配置node.name,使用
network.host
配置的公開地址 - host:port 如果沒有配置node.name,這里的端口是transport端口
ES節點http和transport的配置
http
和 transport
。
http用於暴露Elasticsearch的API,便於client端與ES通信;transport用於ES集群間節點通信使用。
http 配置參考:
Setting Description http.port
http端口配置A bind port range. Defaults to 9200-9300
.http.publish_port
The port that HTTP clients should use when communicating with this node. Useful when a cluster node is behind a proxy or firewall and the http.port
is not directly addressable from the outside. Defaults to the actual port assigned viahttp.port
.http.bind_host
http監聽的IPThe host address to bind the HTTP service to. Defaults to http.host
(if set) ornetwork.bind_host
.http.publish_host
The host address to publish for HTTP clients to connect to. Defaults to http.host
(if set) ornetwork.publish_host
.http.host
Used to set the http.bind_host
and thehttp.publish_host
.http.max_content_length
The max content of an HTTP request. Defaults to 100mb
.http.max_initial_line_length
The max length of an HTTP URL. Defaults to 4kb
http.max_header_size
The max size of allowed headers. Defaults to 8kB
http.compression
壓縮Support for compression when possible (with Accept-Encoding). Defaults to true
.http.compression_level
壓縮級別Defines the compression level to use for HTTP responses. Valid values are in the range of 1 (minimum compression) and 9 (maximum compression). Defaults to 3
.http.cors.enabled
跨域配置Enable or disable cross-origin resource sharing, i.e. whether a browser on another origin can execute requests against Elasticsearch. Set to true
to enable Elasticsearch to process pre-flight CORS requests. Elasticsearch will respond to those requests with theAccess-Control-Allow-Origin
header if theOrigin
sent in the request is permitted by thehttp.cors.allow-origin
list. Set tofalse
(the default) to make Elasticsearch ignore theOrigin
request header, effectively disabling CORS requests because Elasticsearch will never respond with theAccess-Control-Allow-Origin
response header. Note that if the client does not send a pre-flight request with anOrigin
header or it does not check the response headers from the server to validate theAccess-Control-Allow-Origin
response header, then cross-origin security is compromised. If CORS is not enabled on Elasticsearch, the only way for the client to know is to send a pre-flight request and realize the required response headers are missing.http.cors.allow-origin
Which origins to allow. Defaults to no origins allowed. If you prepend and append a /
to the value, this will be treated as a regular expression, allowing you to support HTTP and HTTPs. for example using/https?:\/\/localhost(:[0-9]+)?/
would return the request header appropriately in both cases.*
is a valid value but is considered a security risk as your Elasticsearch instance is open to cross origin requests from anywhere.http.cors.max-age
Browsers send a “preflight” OPTIONS-request to determine CORS settings. max-age
defines how long the result should be cached for. Defaults to1728000
(20 days)http.cors.allow-methods
Which methods to allow. Defaults to OPTIONS, HEAD, GET, POST, PUT, DELETE
.http.cors.allow-headers
Which headers to allow. Defaults to X-Requested-With, Content-Type, Content-Length
.http.cors.allow-credentials
Whether the Access-Control-Allow-Credentials
header should be returned. Note: This header is only returned, when the setting is set totrue
. Defaults tofalse
http.detailed_errors.enabled
Enables or disables the output of detailed error messages and stack traces in response output. Note: When set to false
and theerror_trace
request parameter is specified, an error will be returned; whenerror_trace
is not specified, a simple message will be returned. Defaults totrue
http.pipelining.max_events
The maximum number of events to be queued up in memory before an HTTP connection is closed, defaults to 10000
.http.max_warning_header_count
The maximum number of warning headers in client HTTP responses, defaults to unbounded. http.max_warning_header_size
The maximum total size of warning headers in client HTTP responses, defaults to unbounded.
transport 配置參考:
Setting Description transport.port
transport端口A bind port range. Defaults to 9300-9400
.transport.publish_port
The port that other nodes in the cluster should use when communicating with this node. Useful when a cluster node is behind a proxy or firewall and the transport.port
is not directly addressable from the outside. Defaults to the actual port assigned viatransport.port
.transport.bind_host
transport監聽的IPThe host address to bind the transport service to. Defaults to transport.host
(if set) ornetwork.bind_host
.transport.publish_host
The host address to publish for nodes in the cluster to connect to. Defaults to transport.host
(if set) ornetwork.publish_host
.transport.host
Used to set the transport.bind_host
and thetransport.publish_host
.transport.connect_timeout
The connect timeout for initiating a new connection (in time setting format). Defaults to 30s
.transport.compress
Set to true
to enable compression (DEFLATE
) between all nodes. Defaults tofalse
.transport.ping_schedule
Schedule a regular application-level ping message to ensure that transport connections between nodes are kept alive. Defaults to 5s
in the transport client and-1
(disabled) elsewhere. It is preferable to correctly configure TCP keep-alives instead of using this feature, because TCP keep-alives apply to all kinds of long-lived connections and not just to transport connections.
配置ES節點的JVM設置
默認的JVM配置文件是:$ES_HOME/config/jvm.options
# 配置內存占用最大最小都為1G。 |
注意:
生產環境,請根據實際情況進行設置。同時不同的角色需要設置不同的資源大小。
建議不要超過32GB,如果有足夠的內存建議配置在26G-30G。參考
此時的JVM也可以通過環境變量的方式設置:
$ export ES_JAVA_OPTS="-Xms1g -Xmx1g $ES_JAVA_OPTS" ./bin/elasticsearch |
了解了ES的配置,下面給出本次使用的配置樣例:
說明:
node.attr.xxx: yyy
用於設定這台node節點的屬性,比如機架,可用區,或者以后可以設置冷熱數據的分別存儲都是基於這個。- 因為我的環境中只用了一台主機,所以采用了區分端口的方式。分別配置了
http.port
,transport.tcp.port
- 我這里的服務發現使用的是自定義可解析名稱,通過在
/etc/hosts
指定解析完成的,方便后期更換IP地址。 - 我這里的三台node節點,在初次啟動時都可以競選主節點,生產環境要注意選擇合格主節點``node.master: true`
es01
$ cat es01/config/elasticsearch.yml |grep -Ev "^$|^#" |
es02
$ cat es02/config/elasticsearch.yml |grep -Ev "^$|^#" |
es03
$ cat es03/config/elasticsearch.yml |grep -Ev "^$|^#" |
配置完Elasticsearch后,下面就是啟動測試了。
啟動Elasticsearch
首先查看一下Elasticsearch的命令幫助:
$ ./es01/bin/elasticsearch --help |
分別啟動三台ES:
$ ll |
可以通過在$ES_HOME/logs/\<CLUSTER_NAME\>.log
查看日志。
測試,我們來查看一下集群中的節點:
$ curl localhost:9200/_cat/nodes?v |
查看集群的健康狀況:
分為三種狀態:
- green,綠色,代表所有數據都健康。
- yellow,黃色,代表數據部分正常,但是沒有數據丟失,可以恢復到green。
- red,紅色,代表有數據丟失,且無法恢復了。
$ curl localhost:9200 |
查看所有/_cat
接口:
$ curl localhost:9200/_cat |
查看我們之前給每台機器定義的屬性:
$ curl localhost:9200/_cat/nodeattrs |
我們發現,所有的這些API接口都是能夠直接訪問的,不需要任何的認證的,對於生產來說非常的不安全,同時任一台node節點都可以加入到集群中,這些都非常的不安全;下面介紹如果開啟auth以及node間的ssl認證。
開啟ES集群的Auth認證和Node間SSL
開啟ES集群的Auth認證
在最新版的ES中,已經開源了X-pack組件,但是開源 != 免費,但是一些基礎的安全是免費的,例如本例中的Auth以及Node間SSL就是免費的。
首先我們嘗試生成密碼:命令是$ES_HOME/bin/elasticsearch-setup-passwords
,查看一下幫助:
$ ./es01/bin/elasticsearch-setup-passwords --help |
我們查看一些ES01的日志,發現有報錯:
[2019-11-27T14:35:13,391][WARN ][r.suppressed ] [es01] path: /_security/_authenticate, params: {pretty=} |
提示說需要先開啟安全:
我們按照提示分別的三台ES節點上添加如下信息:
$ echo "xpack.security.enabled: true" >> es01/config/elasticsearch.yml |
然后重啟:
$ ps -ef|grep elasticsearch |
發現無法啟動,錯誤提示:
ERROR: [1] bootstrap checks failed |
好吧我們再添加這條配置:
$ echo "xpack.security.transport.ssl.enabled: true" >> es01/config/elasticsearch.yml |
然后再次啟動,我們又發現,在啟動第二台的時候,兩個es節點都一直報錯,如下:
[2019-11-27T14:50:58,643][WARN ][o.e.t.TcpTransport ] [es01] exception caught on transport layer [Netty4TcpChannel{localAddress=/172.17.0.87:9331, remoteAddress=/172.17.0.87:56654}], closing connection |
發現沒有配置認證的方式。好吧,我們先往下繼續配置:
配置Node間SSL
注意:這里是指配置ES集群節點間transport的SSL認證,對於ES節點的HTTP API接口並沒有配置,所以通過API訪問ES時不需要提供證書。
參考官網:
https://www.elastic.co/guide/en/elasticsearch/reference/current/ssl-tls.html
https://www.elastic.co/guide/en/elasticsearch/reference/7.4/configuring-tls.html
創建SSL/TLS證書:通過命令$ES_HOME/bin/elasticsearch-certutil
# 查看命令幫助 |
創建CA證書:
# 命令幫助: |
這個命令生成格式為PKCS#12
名稱為 elastic-stack-ca.p12
的keystore文件,包含CA證書和私鑰。
創建節點間認證用的證書:
# 命令幫助: |
這個命令生成格式為PKCS#12
名稱為 elastic-certificates.p12
的keystore文件,包含node證書、私鑰、CA證書。
這個命令生成的證書內部默認是不包含主機名信息的(他沒有任何 Subject Alternative Name 字段),所以證書可以用在任何的node節點上,但是你必須配置elasticsearch關閉主機名認證。
配置ES節點使用這個證書:
$ mkdir config/certs |
注意:config/certs 目錄中不需要拷貝CA證書文件,只拷貝cert文件即可。我這里是圖方便。
同時要注意把CA證書保存好,如果設置了CA證書密鑰也要保護放,方便后期增加ES節點使用。
xpack.security.transport.ssl.verification_mode 這里配置認證方式:參考官網
full
,認證證書是否通過信任的CA證書簽發的,同時認證server的hostname or IP address是否匹配證書中配置的。certificate
,我們這里采用的方式,只認證證書是否通過信任的CA證書簽發的none
,什么也不認證,相當於關閉了SSL/TLS 認證,僅用於你非常相信安全的環境。
配置了,然后再次啟動ES節點測試:
測試能夠正常啟動了。好了,我們再來繼續之前的生成密碼:在隨意一台節點即可。
$ ./es01/bin/elasticsearch-setup-passwords auto |
查看集群節點數量:
$ curl -u elastic localhost:9200/_cat/nodes |
注意:
這里只是配置了ES集群中node間通信啟用了證書加密,HTTP API接口是使用用戶名和密碼的方式認證的,如果你需要更安全的SSL加密,請參考:TLS HTTP。
安全配置的參數,請參考
好了,一個比較安全的Elasticsearch的集群就已經創建完畢了。
kibana的安裝配置
下面開始安裝kibana,方便通過瀏覽器訪問。
$ wget -c "https://artifacts.elastic.co/downloads/kibana/kibana-7.4.2-linux-x86_64.tar.gz" |
配置kibana:
$ cat kibana/config/kibana.yml |grep -Ev "^$|^#" |
kibana 命令幫助:
$ ./kibana/bin/kibana --help |
訪問kibana的IP:5601即可,可以看到登陸界面:
輸入上面生成的管理員elastic的用戶和密碼,就可以登陸了,我們查看一下license許可吧:
查看一下Elasticsearch集群中的節點監控信息,包括CPU,負載,JVM使用率,磁盤空間,可以根據此來修改擴容ES node節點:
一個使用永不過期的Basic許可的免費License,開啟了基本的Auth認證和集群間SSL/TLS 認證的Elasticsearch集群就創建完畢了。
等等,你有沒有想過Kibana的配置文件中使用着明文的用戶名密碼,這里只能通過LInux的權限進行控制了,有沒有更安全的方式呢,有的,就是keystore。
kibana keystore 安全配置
查看``kibana-keystore`命令幫助:
$ ./bin/kibana-keystore --help |
首先我們創建keystore:
$ bin/kibana-keystore create |
增加配置:
我們要吧kibana.yml 配置文件中的敏感信息,比如:elasticsearch.username
和 elasticsearch.password
,給隱藏掉,或者直接去掉;
所以這里我們增加兩個配置:分別是elasticsearch.password
和 elasticsearch.username
:
# 查看add的命令幫助: |
好了,我們把kibana.yml配置文件中的這兩項配置刪除即可,然后直接啟動kibana,kibana會自動已用這兩個配置的。
最終的kibana.yml配置如下:
server.port: 5601 |
這樣配置文件中就不會出現敏感信息了,達到了更高的安全性。
類似的Keystore方式不只是Kibana支持,ELK的產品都是支持的。
安裝Elasticsearch Head插件
GitHub:
https://github.com/mobz/elasticsearch-head
WebSite:
http://mobz.github.io/elasticsearch-head/
我們這里使用最簡單的方式安裝,安裝Elasticsearch Head Chrome 插件:
Chrome打開如下地址,進行安裝:
https://chrome.google.com/webstore/detail/elasticsearch-head/ffmkiejjmecolpfloofpjologoblkegm/
安裝完畢后,直接點擊ES Head圖標即可。
注意:通過這種Chrome 插件方式安裝ES Head不需要開啟ES集群的CORS跨域配置:
https://github.com/mobz/elasticsearch-head#enable-cors-in-elasticsearch
輸入ES集群的連接地址,點擊連接,然后輸入用戶名密碼即可:
到這里,文章已經基本完畢,下面是一些小技巧:
生產環境中整個集群重啟和滾動重啟的正確操作
比如我們后期可能要對整個集群的重啟,或者呢,更改一些配置,需要一台一台的重啟集群中的每個節點,因為在重啟的時候ES集群會自動復制下線節點的shart到其他的節點上,並再平衡node間的shart,會產生很大的IO的,但是這個IO操作是完全沒有必要的。
關閉shard allocation
curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d' |
關閉索引和synced flush
curl -X POST "localhost:9200/_flush/synced?pretty" |
做完上面兩步的話再關閉整個集群;待變更完配置后,重新啟動集群,然后在打開之前關閉的shard allocation:
打開shard allocation
curl -X PUT "localhost:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d' |
對於ES集群node節點輪訓重啟的操作時,在關閉每個節點之前都先執行上面兩步關閉的操作,然后關閉這個節點,做變更操作,然后在啟動該節點,然后在打開shard allocation,等待ES集群狀態變為Green后,再進行第二台,然后依次類推。