如何在RHEL8/CentOS8上建立多節(jié)點Elastic stack集群
Elastic stack 俗稱 ELK stack,是一組包括 Elasticsearch、Logstash 和 Kibana 在內(nèi)的開源產(chǎn)品。Elastic Stack 由 Elastic 公司開發(fā)和維護。使用 Elastic stack,可以將系統(tǒng)日志發(fā)送到 Logstash,它是一個數(shù)據(jù)收集引擎,接受來自可能任何來源的日志或數(shù)據(jù),并對日志進行歸一化,然后將日志轉(zhuǎn)發(fā)到 Elasticsearch,用于分析、索引、搜索和存儲,使用 Kibana 表示為可視化數(shù)據(jù),使用 Kibana,我們還可以基于用戶的查詢創(chuàng)建交互式圖表。
在本文中,我們將演示如何在 RHEL 8 / CentOS 8 服務器上設置多節(jié)點 elastic stack 集群。以下是我的 Elastic Stack 集群的詳細信息:
Elasticsearch:
- 三臺服務器,最小化安裝 RHEL 8 / CentOS 8
- IP & 主機名 – 192.168.56.40(
elasticsearch1.linuxtechi.local
)、192.168.56.50 (elasticsearch2.linuxtechi.local
)、192.168.56.60(elasticsearch3.linuxtechi.local`)
Logstash:
- 兩臺服務器,最小化安裝 RHEL 8 / CentOS 8
- IP & 主機 – 192.168.56.20(
logstash1.linuxtechi.local
)、192.168.56.30(logstash2.linuxtechi.local
)
Kibana:
- 一臺服務器,最小化安裝 RHEL 8 / CentOS 8
- IP & 主機名 – 192.168.56.10(
kibana.linuxtechi.local
)
Filebeat:
- 一臺服務器,最小化安裝 CentOS 7
- IP & 主機名 – 192.168.56.70(
web-server
)
讓我們從設置 Elasticsearch 集群開始,
設置3個節(jié)點 Elasticsearch 集群
正如我已經(jīng)說過的,設置 Elasticsearch 集群的節(jié)點,登錄到每個節(jié)點,設置主機名并配置 yum/dnf 庫。
使用命令 hostnamectl
設置各個節(jié)點上的主機名:
[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 ~]#
對于 CentOS 8 系統(tǒng),我們不需要配置任何操作系統(tǒng)包庫,對于 RHEL 8 服務器,如果你有有效訂閱,那么用紅帽訂閱以獲得包存儲庫就可以了。如果你想為操作系統(tǒng)包配置本地 yum/dnf 存儲庫,請參考以下網(wǎng)址:
在所有節(jié)點上配置 Elasticsearch 包存儲庫,在 /etc/yum.repo.d/
文件夾下創(chuàng)建一個包含以下內(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
保存文件并退出。
在所有三個節(jié)點上使用 rpm
命令導入 Elastic 公共簽名密鑰。
~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
在所有三個節(jié)點的 /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
命令在所有三個節(jié)點上安裝 Java:
[root@linuxtechi ~]# dnf install java-openjdk -y
[root@linuxtechi ~]# dnf install java-openjdk -y
[root@linuxtechi ~]# dnf install java-openjdk -y
使用 yum
/dnf
命令在所有三個節(jié)點上安裝 Elasticsearch:
[root@linuxtechi ~]# dnf install elasticsearch -y
[root@linuxtechi ~]# dnf install elasticsearch -y
[root@linuxtechi ~]# dnf install elasticsearch -y
注意: 如果操作系統(tǒng)防火墻已啟用并在每個 Elasticsearch 節(jié)點中運行,則使用 firewall-cmd
命令允許以下端口開放:
~]# firewall-cmd --permanent --add-port=9300/tcp
~]# firewall-cmd --permanent --add-port=9200/tcp
~]# firewall-cmd --reload
配置 Elasticsearch, 在所有節(jié)點上編輯文件 /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"]
注意: 在每個節(jié)點上,在 node.name
中填寫正確的主機名,在 network.host
中填寫正確的 IP 地址,其他參數(shù)保持不變。
現(xiàn)在使用 systemctl
命令在所有三個節(jié)點上啟動并啟用 Elasticsearch 服務:
~]# systemctl daemon-reload
~]# systemctl enable elasticsearch.service
~]# systemctl start elasticsearch.service
使用下面 ss
命令驗證 elasticsearch 節(jié)點是否開始監(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
命令驗證 Elasticsearch 群集狀態(tài):
[root@linuxtechi ~]# curl http://elasticsearch1.linuxtechi.local:9200
[root@linuxtechi ~]# curl -X GET http://elasticsearch2.linuxtechi.local:9200/_cluster/health?pretty
命令的輸出如下所示:
Elasticsearch-cluster-status-rhel8
以上輸出表明我們已經(jīng)成功創(chuàng)建了 3 節(jié)點的 Elasticsearch 集群,集群的狀態(tài)也是綠色的。
注意: 如果你想修改 JVM 堆大小,那么你可以編輯了文件 /etc/elasticsearch/jvm.options
,并根據(jù)你的環(huán)境更改以下參數(shù):
-Xms1g
-Xmx1g
現(xiàn)在讓我們轉(zhuǎn)到 Logstash 節(jié)點。
安裝和配置 Logstash
在兩個 Logstash 節(jié)點上執(zhí)行以下步驟。
登錄到兩個節(jié)點使用 hostnamectl
命令設置主機名:
[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 ~]#
在兩個 logstash 節(jié)點的 /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
保存文件并退出。
在兩個節(jié)點上配置 Logstash 存儲庫,在文件夾 /ete/yum.repo.d/
下創(chuàng)建一個包含以下內(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
保存并退出文件,運行 rpm
命令導入簽名密鑰:
~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
使用 yum
/dnf
命令在兩個節(jié)點上安裝 Java OpenJDK:
~]# dnf install java-openjdk -y
從兩個節(jié)點運行 yum
/dnf
命令來安裝 logstash:
[root@linuxtechi ~]# dnf install logstash -y
[root@linuxtechi ~]# dnf install logstash -y
現(xiàn)在配置 logstash,在兩個 logstash 節(jié)點上執(zhí)行以下步驟,創(chuàng)建一個 logstash 配置文件,首先我們在 /etc/logstash/conf.d/
下復制 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ù)中指定所有三個 Elasticsearch 節(jié)點的 FQDN,其他參數(shù)保持不變。
使用 firewall-cmd
命令在操作系統(tǒng)防火墻中允許 logstash 端口 “5044”:
~ # firewall-cmd --permanent --add-port=5044/tcp
~ # firewall-cmd –reload
現(xiàn)在,在每個節(jié)點上運行以下 systemctl
命令,啟動并啟用 Logstash 服務:
~]# systemctl start logstash
~]# systemctl eanble logstash
使用 ss
命令驗證 logstash 服務是否開始監(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é)點,使用 hostnamectl
命令設置主機名:
[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
使用以下命令設置 Kibana 存儲庫:
[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"]
…………
啟用并啟動 kibana 服務:
[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
Kibana-Dashboard-rhel8
從面板上,我們可以檢查 Elastic Stack 集群的狀態(tài)。
Stack-Monitoring-Overview-RHEL8
這證明我們已經(jīng)在 RHEL 8 /CentOS 8 上成功地安裝并設置了多節(jié)點 Elastic Stack 集群。
現(xiàn)在讓我們通過 filebeat
從其他 Linux 服務器發(fā)送一些日志到 logstash 節(jié)點中,在我的例子中,我有一個 CentOS 7服務器,我將通過 filebeat
將該服務器的所有重要日志推送到 logstash。
登錄到 CentOS 7 服務器使用 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
,以便它可以使用負載平衡技術向 logstash 節(jié)點發(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ù)添加兩個 logstash 節(jié)點,以及設置 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 個 systemctl
命令 啟動并啟用 filebeat
服務:
[root@linuxtechi ~]# systemctl start filebeat
[root@linuxtechi ~]# systemctl enable filebeat
現(xiàn)在轉(zhuǎn)到 Kibana 用戶界面,驗證新索引是否可見。
從左側(cè)欄中選擇管理選項,然后單擊 Elasticsearch 下的索引管理:
Elasticsearch-index-management-Kibana
正如我們上面看到的,索引現(xiàn)在是可見的,讓我們現(xiàn)在創(chuàng)建索引模型。
點擊 Kibana 部分的 “Index Patterns”,它將提示我們創(chuàng)建一個新模型,點擊 “Create Index Pattern” ,并將模式名稱指定為 “filebeat”:
Define-Index-Pattern-Kibana-RHEL8
點擊下一步。
選擇 “Timestamp” 作為索引模型的時間過濾器,然后單擊 “Create index pattern”:
Time-Filter-Index-Pattern-Kibana-RHEL8
filebeat-index-pattern-overview-Kibana
現(xiàn)在單擊查看實時 filebeat 索引模型:
Discover-Kibana-REHL8
這表明 Filebeat 代理已配置成功,我們能夠在 Kibana 儀表盤上看到實時日志。
以上就是本文的全部內(nèi)容,對這些幫助你在 RHEL 8 / CentOS 8 系統(tǒng)上設置 Elastic Stack 集群的步驟,請不要猶豫分享你的反饋和意見。