使用ElasticSearch集群搭建步驟
一、前言
Elasticsearch 是一個開源的分布式搜索和分析引擎,用于全文搜索、結(jié)構(gòu)化搜索、分析和可視化大規(guī)模數(shù)據(jù)。
它被設(shè)計成一個可擴展的實時搜索引擎,能夠處理大規(guī)模數(shù)據(jù),并提供快速的搜索和分析功能。
以下是一些 Elasticsearch 的特性和用途:
- 全文搜索:Elasticsearch可以存儲大量的數(shù)據(jù),并提供基于全文搜索的查詢功能,支持各種復雜的查詢操作。
- 實時性:Elasticsearch能夠?qū)崟r索引數(shù)據(jù),并且查詢的響應時間非常快,適用于需要快速查詢的場景。
- 分布式架構(gòu):Elasticsearch 是一個分布式系統(tǒng),數(shù)據(jù)被分散存儲在多個節(jié)點上,可以水平擴展以處理大規(guī)模數(shù)據(jù)。
- 多種數(shù)據(jù)類型支持:Elasticsearch 支持多種數(shù)據(jù)類型的索引和查詢,包括文本、數(shù)字、日期等。
- 強大的查詢語言:Elasticsearch 提供了豐富而靈活的查詢語言,可以滿足各種復雜的查詢需求。
- 實時數(shù)據(jù)分析:除了搜索功能,Elasticsearch 還提供了強大的數(shù)據(jù)分析功能,可以對數(shù)據(jù)進行聚合、統(tǒng)計和可視化分析。
- 易于部署和管理:Elasticsearch 提供了簡單易用的API和管理工具,可以方便地部署、監(jiān)控和管理集群。
- 與其他工具集成:Elasticsearch 與許多工具和技術(shù)(如Logstash、Kibana、Beats等)集成,構(gòu)建了強大的日志管理和數(shù)據(jù)分析解決方案。
總的來說,Elasticsearch是一個功能強大且靈活的搜索和分析引擎,適用于各種場景,包括日志分析、實時監(jiān)控、全文搜索等。
本例將在3臺機器上搭建 Elasticsearch 集群:
192.168.72.151 node-1 192.168.72.152 node-2 192.168.72.153 node-3
二、使用 RPM 安裝 Elasticsearch
導入 Elasticsearch GPG 密鑰
下載并安裝公共簽名密鑰
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
從 RPM 存儲庫安裝
在 /etc/yum.repos.d/ 創(chuàng)建 elasticsearch.repo
[elasticsearch] name=Elasticsearch repository for 8.x packages baseurl=https://artifacts.elastic.co/packages/8.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=0 autorefresh=1 type=rpm-md
執(zhí)行 yum install 命令
yum install --enablerepo=elasticsearch elasticsearch -y
默認生成的目錄
- 安裝目錄:/usr/share/elasticsearch
- 配置文件目錄:/etc/elasticsearch
啟動 Elasticsearch 命令:
systemctl enable elasticsearch.service systemctl start elasticsearch.service
三、設(shè)置基本安全性
首次啟動 Elasticsearch 時,會為用戶生成密碼,并自動為用戶配置 TLS ,可以隨時調(diào)整 TLS 配置,更新節(jié)點證書
生成證書
1、在任意節(jié)點上進入 Elasticsearch 的安裝目錄,使用 elasticsearch-certutil 為集群生成 CA 。
bin/elasticsearch-certutil ca
- 設(shè)置 CA 文件名(默認即可)
- 為 CA 設(shè)置密碼
2、利用上一步的 elastic-stack-ca.p12 為集群生成證書和私鑰。
bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
- 輸入CA的密碼
- 為證書創(chuàng)建密碼,并設(shè)置文件名(默認即可)
3、將證書拷貝到其他節(jié)點上。
使用TLS加密節(jié)點間通信
1、進入 Elasticsearch 配置目錄,編輯 elasticsearch.conf 文件:
cluster.name: my-cluster node.name: node-1 xpack.security.enabled: true xpack.security.transport.ssl.enabled: true xpack.security.transport.ssl.verification_mode: certificate xpack.security.transport.ssl.client_authentication: required xpack.security.transport.ssl.keystore.path: elastic-certificates.p12 xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
2、執(zhí)行以下命令將證書密碼保存到 Elasticsearch 的 keystore
bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
3、為集群中的每個節(jié)點完成前面的步驟。
4、在集群的每個節(jié)點上,重新啟動 Elasticsearch 。
四、為 Elasticsearch 加密 HTTP 客戶端通信
1、 在任意單個節(jié)點上,從安裝 Elasticsearch 的目錄中,運行Elasticsearch HTTP 證書工具生成證書簽名請求(CSR)。
bin/elasticsearch-certutil http
- 是否生成 CSR ,輸入 n 。
- 是否使用現(xiàn)有 CA ,輸入 y 。
- 輸入 CA 的路徑。這是 elastic-stack-ca.p12 的絕對路徑。
- 輸入 CA 的密碼。
- 輸入證書的有效期。
- 是否為每個節(jié)點生成一個證書,輸入 y 。
- 輸入每個節(jié)點的名稱(node.name)。
- 輸入所有節(jié)點的主機名、IP地址。
- 輸入私鑰密碼。
該命令生成一個 .zip 文件,其中包含 Elasticsearch 和 Kibana 使用的證書和密鑰。每個文件夾都包含一個 README.txt ,說明如何使用這些文件。
2、解壓縮生成的 elasticsearch-ssl-http.zip 文件。這個壓縮文件包含用于Elasticsearch和Kibana的目錄。
/elasticsearch |_ README.txt |_ http.p12 |_ sample-elasticsearch.yml
/kibana |_ README.txt |_ elasticsearch-ca.pem |_ sample-kibana.yml
3、在集群中的每個節(jié)點上,完成以下步驟:
復制上面 elasticsearch 文件夾中的 http.p12 到 Elasticsearch 的配置目錄下。
編輯 elasticsearch.yml ,啟用 HTTPS 安全性,并指定 http.12 文件的位置。
xpack.security.http.ssl.enabled: true xpack.security.http.ssl.keystore.path: http.p12
將私鑰密碼添加到 Elasticsearch 的安全設(shè)置中。
bin/elasticsearch-keystore add xpack.security.http.ssl.keystore.secure_password
4、重新啟動 Elasticsearch
五、配置集群
編輯 elasticsearch.yml
3個節(jié)點的配置文件除了ip、節(jié)點名之外,都一樣
cluster.name: my-clusters node.name: node-1 path.data: /var/lib/elasticsearch path.logs: /var/log/elasticsearch indices.memory.index_buffer_size: 20% indices.memory.min_index_buffer_size: 96mb thread_pool: search: size: 32 analyze: size: 30 queue_size: 1000 indices.requests.cache.size: 2% indices.queries.cache.size: 10% indices.fielddata.cache.size: 20% indices.breaker.fielddata.limit: 40% network.host: 0.0.0.0 http.port: 9200 transport.port: 9300 transport.compress: true http.max_content_length: 100mb discovery.seed_hosts: ["192.168.72.151", "192.168.72.152","192.168.72.153"] cluster.initial_master_nodes: ["node-1", "node-2","node-3"] cluster.fault_detection.leader_check.interval: 5s discovery.cluster_formation_warning_timeout: 10s cluster.publish.timeout: 30s cluster.routing.allocation.node_initial_primaries_recoveries: 16 cluster.routing.allocation.node_concurrent_recoveries: 8 indices.recovery.max_bytes_per_sec: 125mb
集群默認是通過9300端口連接的,記得防火墻還要開通9300
啟動集群
依次啟動各個節(jié)點
service elasticsearch start
設(shè)置密碼
bin/elasticsearch-setup-passwords interactive
檢查集群狀態(tài)
curl -XGET --user elastic:password http://192.168.72.151:9200/_cluster/health?pretty
也可以在瀏覽器直接訪問
請注意,上述步驟中的IP地址、端口和目錄路徑應根據(jù)你的實際環(huán)境進行調(diào)整。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Windows 和 Linux 上Redis的安裝守護進程配置方法
​ Redis是目前最常用的非關(guān)系型數(shù)據(jù)庫(NOSql)之一,常以Key-Value的形式存儲。這篇文章主要介紹了Windows 和 Linux 上Redis的安裝守護進程配置 ,需要的朋友可以參考下2019-06-06Linux系統(tǒng)下移植busybox中mkfs.vfat命令
為了延長磁盤壽命來存儲音視頻文件,打發(fā)在格式化磁盤過程中將簇的大小設(shè)置大點。這接下來通過本文給大家介紹移植busybox中mkfs.vfat命令,需要的朋友可以參考下2019-07-07