Elastic stack 俗稱 ELK stack,是一組包括 Elasticsearch、Logstash 和 Kibana 在內(nèi)的開源產(chǎn)品。Elastic Stack 由 Elastic 公司開發(fā)和維護(hù)。使用 Elastic stack,可以將系統(tǒng)日志發(fā)送到 Logstash,它是一個(gè)數(shù)據(jù)收集引擎,接受來自可能任何來源的日志或數(shù)據(jù),并對(duì)日志進(jìn)行歸一化,然后將日志轉(zhuǎn)發(fā)到 Elasticsearch,用于分析、索引、搜索和存儲(chǔ),最后使用 Kibana 表示為可視化數(shù)據(jù),使用 Kibana,我們還可以基于用戶的查詢創(chuàng)建交互式圖表。
在本文中,我們將演示如何在 RHEL 8 / CentOS 8 服務(wù)器上設(shè)置多節(jié)點(diǎn) elastic stack 集群。以下是我的 Elastic Stack 集群的詳細(xì)信息:
Elasticsearch:
- 三臺(tái)服務(wù)器,最小化安裝 RHEL 8 / CentOS 8
- IP & 主機(jī)名 – 192.168.56.40(
elasticsearch1.linuxtechi.local
)、192.168.56.50 (elasticsearch2.linuxtechi.local
)、192.168.56.60(elasticsearch3.linuxtechi.local`)
Logstash:**
- 兩臺(tái)服務(wù)器,最小化安裝 RHEL 8 / CentOS 8
- IP & 主機(jī) – 192.168.56.20(
logstash1.linuxtechi.local
)、192.168.56.30(logstash2.linuxtechi.local
)
Kibana:
一臺(tái)服務(wù)器,最小化安裝 RHEL 8 / CentOS 8IP & 主機(jī)名 – 192.168.56.10(kibana.linuxtechi.local
)
Filebeat:
- 一臺(tái)服務(wù)器,最小化安裝 CentOS 7
- IP & 主機(jī)名 – 192.168.56.70(
web-server
)
讓我們從設(shè)置 Elasticsearch 集群開始,
設(shè)置3個(gè)節(jié)點(diǎn) Elasticsearch 集群
正如我已經(jīng)說過的,設(shè)置 Elasticsearch 集群的節(jié)點(diǎn),登錄到每個(gè)節(jié)點(diǎn),設(shè)置主機(jī)名并配置 yum/dnf 庫(kù)
使用命令 hostnamectl
設(shè)置各個(gè)節(jié)點(diǎn)上的主機(jī)名:
[root@linuxtechi ~]# hostnamectl set-hostname "elasticsearch1.linuxtechi. local"
[root@linuxtechi ~]# exec bash
[root@linuxtechi ~]#
[root@linuxtechi ~]# hostnamectl set-hostname "elasticsearch2.linuxtechi. local"
[root@linuxtechi ~]# exec bash
[root@linuxtechi ~]#
[root@linuxtechi ~]# hostnamectl set-hostname "elasticsearch3.linuxtechi. local"
[root@linuxtechi ~]# exec bash
[root@linuxtechi ~]#
對(duì)于 CentOS 8 系統(tǒng),我們不需要配置任何操作系統(tǒng)包庫(kù),對(duì)于 RHEL 8 服務(wù)器,如果你有有效訂閱,那么用紅帽訂閱以獲得包存儲(chǔ)庫(kù)就可以了。如果你想為操作系統(tǒng)包配置本地 yum/dnf 存儲(chǔ)庫(kù),請(qǐng)參考以下網(wǎng)址:
如何使用 DVD 或 ISO 文件在 RHEL 8 服務(wù)器上設(shè)置本地 Yum / DNF 存儲(chǔ)庫(kù)
在所有節(jié)點(diǎn)上配置 Elasticsearch 包存儲(chǔ)庫(kù),在 /etc/yum.repo.d/
文件夾下創(chuàng)建一個(gè)包含以下內(nèi)容的 elastic.repo
文件:
~]# vi /etc/yum.repos.d/elastic.repo
[elasticsearch-7.x]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
保存文件并退出。
在所有三個(gè)節(jié)點(diǎn)上使用 rpm
命令導(dǎo)入 Elastic 公共簽名密鑰。
~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
在所有三個(gè)節(jié)點(diǎn)的 /etc/hosts
文件中添加以下行:
192.168.56.40 elasticsearch1.linuxtechi.local
192.168.56.50 elasticsearch2.linuxtechi.local
192.168.56.60 elasticsearch3.linuxtechi.local
使用 yum
/dnf
命令在所有三個(gè)節(jié)點(diǎn)上安裝 Java:
[root@linuxtechi ~]# dnf install java-openjdk -y
[root@linuxtechi ~]# dnf install java-openjdk -y
[root@linuxtechi ~]# dnf install java-openjdk -y
使用 yum
/dnf
命令在所有三個(gè)節(jié)點(diǎn)上安裝 Elasticsearch:
root@linuxtechi ~]# dnf install elasticsearch -y
[root@linuxtechi ~]# dnf install elasticsearch -y
[root@linuxtechi ~]# dnf install elasticsearch -y
注意: 如果操作系統(tǒng)防火墻已啟用并在每個(gè) Elasticsearch 節(jié)點(diǎn)中運(yùn)行,則使用 firewall-cmd
命令允許以下端口開放:
~]# firewall-cmd --permanent --add-port=9300/tcp
~]# firewall-cmd --permanent --add-port=9200/tcp
~]# firewall-cmd --reload
配置 Elasticsearch, 在所有節(jié)點(diǎn)上編輯文件 /etc/elasticsearch/elasticsearch.yml
并加入以下內(nèi)容:
~]# vim /etc/elasticsearch/elasticsearch.yml
cluster.name: opn-cluster
node.name: elasticsearch1.linuxtechi.local
network.host: 192.168.56.40
http.port: 9200
discovery.seed_hosts: ["elasticsearch1.linuxtechi.local", "elasticsearch2.linuxtechi.local", "elasticsearch3.linuxtechi.local"]
cluster.initial_master_nodes: ["elasticsearch1.linuxtechi.local", "elasticsearch2.linuxtechi.local", "elasticsearch3.linuxtechi.local"]
注意: 在每個(gè)節(jié)點(diǎn)上,在 node.name
中填寫正確的主機(jī)名,在 network.host
中填寫正確的 IP 地址,其他參數(shù)保持不變。
現(xiàn)在使用 systemctl
命令在所有三個(gè)節(jié)點(diǎn)上啟動(dòng)并啟用 Elasticsearch 服務(wù):
~]# systemctl daemon-reload
~]# systemctl enable elasticsearch.service
~]# systemctl start elasticsearch.service
使用下面 ss
命令驗(yàn)證 elasticsearch 節(jié)點(diǎn)是否開始監(jiān)聽 9200 端口:
[root@linuxtechi ~]# ss -tunlp | grep 9200
tcp LISTEN 0 128 [::ffff:192.168.56.40]:9200 *:* users:(("java",pid=2734,fd=256))
[root@linuxtechi ~]#
使用以下 curl
命令驗(yàn)證 Elasticsearch 群集狀態(tài):
[root@linuxtechi ~]# curl http://elasticsearch1.linuxtechi.local:9200
[root@linuxtechi ~]# curl -X GET http://elasticsearch2.linuxtechi.local:9200/_cluster/health?pretty
命令的輸出如下所示:

以上輸出表明我們已經(jīng)成功創(chuàng)建了 3 節(jié)點(diǎn)的 Elasticsearch 集群,集群的狀態(tài)也是綠色的。
注意: 如果你想修改 JVM 堆大小,那么你可以編輯了文件 /etc/elasticsearch/jvm.options
,并根據(jù)你的環(huán)境更改以下參數(shù)
現(xiàn)在讓我們轉(zhuǎn)到 Logstash 節(jié)點(diǎn)。
安裝和配置 Logstash
在兩個(gè) Logstash 節(jié)點(diǎn)上執(zhí)行以下步驟。
登錄到兩個(gè)節(jié)點(diǎn)使用 hostnamectl
命令設(shè)置主機(jī)名:
[root@linuxtechi ~]# hostnamectl set-hostname "logstash1.linuxtechi.local"
[root@linuxtechi ~]# exec bash
[root@linuxtechi ~]#
[root@linuxtechi ~]# hostnamectl set-hostname "logstash2.linuxtechi.local"
[root@linuxtechi ~]# exec bash
[root@linuxtechi ~]#
在兩個(gè) logstash 節(jié)點(diǎn)的 /etc/hosts
文件中添加以下條目:
~]# vi /etc/hosts
192.168.56.40 elasticsearch1.linuxtechi.local
192.168.56.50 elasticsearch2.linuxtechi.local
192.168.56.60 elasticsearch3.linuxtechi.local
保存文件并退出。
在兩個(gè)節(jié)點(diǎn)上配置 Logstash 存儲(chǔ)庫(kù),在文件夾 /ete/yum.repo.d/
下創(chuàng)建一個(gè)包含以下內(nèi)容的文件 logstash.repo
:
~]# vi /etc/yum.repos.d/logstash.repo
[elasticsearch-7.x]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
保存并退出文件,運(yùn)行 rpm
命令導(dǎo)入簽名密鑰:
~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
使用 yum
/dnf
命令在兩個(gè)節(jié)點(diǎn)上安裝 Java OpenJDK:
~]# dnf install java-openjdk -y
從兩個(gè)節(jié)點(diǎn)運(yùn)行 yum
/dnf
命令來安裝 logstash:
[root@linuxtechi ~]# dnf install logstash -y
[root@linuxtechi ~]# dnf install logstash -y
現(xiàn)在配置 logstash,在兩個(gè) logstash 節(jié)點(diǎn)上執(zhí)行以下步驟,創(chuàng)建一個(gè) logstash 配置文件,首先我們?cè)?/etc/logstash/conf.d/
下復(fù)制 logstash 示例文件:
# cd /etc/logstash/
# cp logstash-sample.conf conf.d/logstash.conf
編輯配置文件并更新以下內(nèi)容:
# vi conf.d/logstash.conf
input {
beats {
port => 5044
}
}
output {
elasticsearch {
hosts => ["http://elasticsearch1.linuxtechi.local:9200", "http://elasticsearch2.linuxtechi.local:9200", "http://elasticsearch3.linuxtechi.local:9200"]
index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
#user => "elastic"
#password => "changeme"
}
}
在 output
部分之下,在 hosts
參數(shù)中指定所有三個(gè) Elasticsearch 節(jié)點(diǎn)的 FQDN,其他參數(shù)保持不變。
使用 firewall-cmd
命令在操作系統(tǒng)防火墻中允許 logstash 端口 “5044”:
~ # firewall-cmd --permanent --add-port=5044/tcp
~ # firewall-cmd –reload
現(xiàn)在,在每個(gè)節(jié)點(diǎn)上運(yùn)行以下 systemctl
命令,啟動(dòng)并啟用 Logstash 服務(wù):
~]# systemctl start logstash
~]# systemctl eanble logstash
使用 ss
命令驗(yàn)證 logstash 服務(wù)是否開始監(jiān)聽 5044 端口:
[root@linuxtechi ~]# ss -tunlp | grep 5044
tcp LISTEN 0 128 *:5044 *:* users:(("java",pid=2416,fd=96))
[root@linuxtechi ~]#
以上輸出表明 logstash 已成功安裝和配置。讓我們轉(zhuǎn)到 Kibana 安裝。
安裝和配置 Kibana
登錄 Kibana 節(jié)點(diǎn),使用 hostnamectl
命令設(shè)置主機(jī)名:
[root@linuxtechi ~]# hostnamectl set-hostname "kibana.linuxtechi.local"
[root@linuxtechi ~]# exec bash
[root@linuxtechi ~]#
編輯 /etc/hosts
文件并添加以下行:
192.168.56.40 elasticsearch1.linuxtechi.local
192.168.56.50 elasticsearch2.linuxtechi.local
192.168.56.60 elasticsearch3.linuxtechi.local
使用以下命令設(shè)置 Kibana 存儲(chǔ)庫(kù):
[root@linuxtechi ~]# vi /etc/yum.repos.d/kibana.repo
[elasticsearch-7.x]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
[root@linuxtechi ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
執(zhí)行 yum
/dnf
命令安裝 kibana:
[root@linuxtechi ~]# yum install kibana -y
通過編輯 /etc/kibana/kibana.yml
文件,配置 Kibana:
[root@linuxtechi ~]# vim /etc/kibana/kibana.yml
…………
server.host: "kibana.linuxtechi.local"
server.name: "kibana.linuxtechi.local"
elasticsearch.hosts: ["http://elasticsearch1.linuxtechi.local:9200", "http://elasticsearch2.linuxtechi.local:9200", "http://elasticsearch3.linuxtechi.local:9200"]
…………
啟用并啟動(dòng) kibana 服務(wù):
root@linuxtechi ~]# systemctl start kibana
[root@linuxtechi ~]# systemctl enable kibana
在系統(tǒng)防火墻上允許 Kibana 端口 “5601”:
[root@linuxtechi ~]# firewall-cmd --permanent --add-port=5601/tcp
success
[root@linuxtechi ~]# firewall-cmd --reload
success
[root@linuxtechi ~]#
使用以下 URL 訪問 Kibana 界面:http://kibana.linuxtechi.local:5601

從面板上,我們可以檢查 Elastic Stack 集群的狀態(tài)。

這證明我們已經(jīng)在 RHEL 8 /CentOS 8 上成功地安裝并設(shè)置了多節(jié)點(diǎn) Elastic Stack 集群。
現(xiàn)在讓我們通過 filebeat
從其他 Linux 服務(wù)器發(fā)送一些日志到 logstash 節(jié)點(diǎn)中,在我的例子中,我有一個(gè) CentOS 7服務(wù)器,我將通過 filebeat
將該服務(wù)器的所有重要日志推送到 logstash。
登錄到 CentOS 7 服務(wù)器使用 yum/rpm 命令安裝 filebeat 包:
[root@linuxtechi ~]# rpm -ivh https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.3.1-x86_64.rpm
Retrieving https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.3.1-x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:filebeat-7.3.1-1 ################################# [100%]
[root@linuxtechi ~]#
編輯 /etc/hosts
文件并添加以下內(nèi)容:
192.168.56.20 logstash1.linuxtechi.local
192.168.56.30 logstash2.linuxtechi.local
現(xiàn)在配置 filebeat
,以便它可以使用負(fù)載平衡技術(shù)向 logstash 節(jié)點(diǎn)發(fā)送日志,編輯文件 /etc/filebeat/filebeat.yml
,并添加以下參數(shù):
在 filebeat.inputs:
部分將 enabled: false
更改為 enabled: true
,并在 paths
參數(shù)下指定我們可以發(fā)送到 logstash 的日志文件的位置;注釋掉 output.elasticsearch
和 host
參數(shù);刪除 output.logstash:
和 hosts:
的注釋,并在 hosts
參數(shù)添加兩個(gè) logstash 節(jié)點(diǎn),以及設(shè)置 loadbalance: true
。
[root@linuxtechi ~]# vi /etc/filebeat/filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/messages
- /var/log/dmesg
- /var/log/maillog
- /var/log/boot.log
#output.elasticsearch:
# hosts: ["localhost:9200"]
output.logstash:
hosts: ["logstash1.linuxtechi.local:5044", "logstash2.linuxtechi.local:5044"]
loadbalance: true
使用下面的 2 個(gè) systemctl
命令 啟動(dòng)并啟用 filebeat
服務(wù):
[root@linuxtechi ~]# systemctl start filebeat
[root@linuxtechi ~]# systemctl enable filebeat
現(xiàn)在轉(zhuǎn)到 Kibana 用戶界面,驗(yàn)證新索引是否可見。
從左側(cè)欄中選擇管理選項(xiàng),然后單擊 Elasticsearch 下的索引管理:

正如我們上面看到的,索引現(xiàn)在是可見的,讓我們現(xiàn)在創(chuàng)建索引模型。
點(diǎn)擊 Kibana 部分的 “Index Patterns”,它將提示我們創(chuàng)建一個(gè)新模型,點(diǎn)擊 “Create Index Pattern” ,并將模式名稱指定為 “filebeat”:

點(diǎn)擊下一步。
選擇 “Timestamp” 作為索引模型的時(shí)間過濾器,然后單擊 “Create index pattern”:


現(xiàn)在單擊查看實(shí)時(shí) filebeat 索引模型:

這表明 Filebeat 代理已配置成功,我們能夠在 Kibana 儀表盤上看到實(shí)時(shí)日志。
以上就是本文的全部?jī)?nèi)容,對(duì)這些幫助你在 RHEL 8 / CentOS 8 系統(tǒng)上設(shè)置 Elastic Stack 集群的步驟,請(qǐng)不要猶豫分享你的反饋和意見。
via: https://www.linuxtechi.com/setup-multinode-elastic-stack-cluster-rhel8-centos8/
總結(jié)
以上所述是小編給大家介紹的在 RHEL8 /CentOS8 上建立多節(jié)點(diǎn) Elastic stack 集群的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!