CentOS8 上建立多節點 Elastic stack 集群

如何在 RHEL8 /CentOS8 上建立多節點 Elastic stack 集群

Elastic stack 俗稱 ELK stack,是一組包括 Elasticsearch、Logstash 和 Kibana 在內的開源產品。

-- Pradeep Kumar(作者)

Elastic stack 俗稱 ELK stack,是一組包括 Elasticsearch、Logstash 和 Kibana 在內的開源產品。Elastic Stack 由 Elastic 公司開發和維護。使用 Elastic stack,可以將系統日誌發送到 Logstash,它是一個數據收集引擎,接受來自可能任何來源的日誌或數據,並對日誌進行歸一化,然後將日誌轉發到 Elasticsearch,用於分析、索引、搜索和存儲,最後使用 Kibana 表示為可視化數據,使用 Kibana,我們還可以基於用戶的查詢創建交互式圖表。

在本文中,我們將演示如何在 RHEL 8 / CentOS 8 服務器上設置多節點 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個節點 Elasticsearch 集群

正如我已經說過的,設置 Elasticsearch 集群的節點,登錄到每個節點,設置主機名並配置 yum/dnf 庫。

使用命令 hostnamectl 設置各個節點上的主機名:

[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 系統,我們不需要配置任何操作系統包庫,對於 RHEL 8 服務器,如果你有有效訂閱,那麼用紅帽訂閱以獲得包存儲庫就可以了。如果你想為操作系統包配置本地 yum/dnf 存儲庫,請參考以下網址:

  • 如何使用 DVD 或 ISO 文件在 RHEL 8 服務器上設置本地 Yum / DNF 存儲庫

在所有節點上配置 Elasticsearch 包存儲庫,在 /etc/yum.repo.d/ 文件夾下創建一個包含以下內容的 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

保存文件並退出。

在所有三個節點上使用 rpm 命令導入 Elastic 公共簽名密鑰。

~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

在所有三個節點的 /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 命令在所有三個節點上安裝 Java:

[root@linuxtechi ~]# dnf install java-openjdk -y
[root@linuxtechi ~]# dnf install java-openjdk -y
[root@linuxtechi ~]# dnf install java-openjdk -y

使用 yum/dnf 命令在所有三個節點上安裝 Elasticsearch:

[root@linuxtechi ~]# dnf install elasticsearch -y
[root@linuxtechi ~]# dnf install elasticsearch -y
[root@linuxtechi ~]# dnf install elasticsearch -y

注意: 如果操作系統防火牆已啟用並在每個 Elasticsearch 節點中運行,則使用 firewall-cmd 命令允許以下端口開放:

~]# firewall-cmd --permanent --add-port=9300/tcp
~]# firewall-cmd --permanent --add-port=9200/tcp
~]# firewall-cmd --reload

配置 Elasticsearch, 在所有節點上編輯文件 /etc/elasticsearch/elasticsearch.yml 並加入以下內容:

~]# 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"]

注意: 在每個節點上,在 node.name 中填寫正確的主機名,在 network.host 中填寫正確的 IP 地址,其他參數保持不變。

現在使用 systemctl 命令在所有三個節點上啟動並啟用 Elasticsearch 服務:

~]# systemctl daemon-reload
~]# systemctl enable elasticsearch.service
~]# systemctl start elasticsearch.service

使用下面 ss 命令驗證 elasticsearch 節點是否開始監聽 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 群集狀態:

[root@linuxtechi ~]# curl http://elasticsearch1.linuxtechi.local:9200
[root@linuxtechi ~]# curl -X GET http://elasticsearch2.linuxtechi.local:9200/_cluster/health?pretty

命令的輸出如下所示:


如何在 RHEL8 /CentOS8 上建立多節點 Elastic stack 集群

Elasticsearch-cluster-status-rhel8


以上輸出表明我們已經成功創建了 3 節點的 Elasticsearch 集群,集群的狀態也是綠色的。

注意: 如果你想修改 JVM 堆大小,那麼你可以編輯了文件 /etc/elasticsearch/jvm.options,並根據你的環境更改以下參數:

  • -Xms1g
  • -Xmx1g

現在讓我們轉到 Logstash 節點。

安裝和配置 Logstash

在兩個 Logstash 節點上執行以下步驟。

登錄到兩個節點使用 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 節點的 /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

保存文件並退出。

在兩個節點上配置 Logstash 存儲庫,在文件夾 /ete/yum.repo.d/ 下創建一個包含以下內容的文件 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 命令在兩個節點上安裝 Java OpenJDK:

~]# dnf install java-openjdk -y

從兩個節點運行 yum/dnf 命令來安裝 logstash:

[root@linuxtechi ~]# dnf install logstash -y
[root@linuxtechi ~]# dnf install logstash -y

現在配置 logstash,在兩個 logstash 節點上執行以下步驟,創建一個 logstash 配置文件,首先我們在 /etc/logstash/conf.d/ 下複製 logstash 示例文件:

# cd /etc/logstash/
# cp logstash-sample.conf conf.d/logstash.conf
# 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 參數中指定所有三個 Elasticsearch 節點的 FQDN,其他參數保持不變。

使用 firewall-cmd 命令在操作系統防火牆中允許 logstash 端口 “5044”:

~ # firewall-cmd --permanent --add-port=5044/tcp
~ # firewall-cmd –reload

現在,在每個節點上運行以下 systemctl 命令,啟動並啟用 Logstash 服務:

~]# systemctl start logstash
~]# systemctl eanble logstash

使用 ss 命令驗證 logstash 服務是否開始監聽 5044 端口:

[root@linuxtechi ~]# ss -tunlp | grep 5044
tcp LISTEN 0 128 *:5044 *:* users:(("java",pid=2416,fd=96))
[root@linuxtechi ~]#

以上輸出表明 logstash 已成功安裝和配置。讓我們轉到 Kibana 安裝。

安裝和配置 Kibana

登錄 Kibana 節點,使用 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

執行 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

在系統防火牆上允許 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


如何在 RHEL8 /CentOS8 上建立多節點 Elastic stack 集群

Kibana-Dashboard-rhel8


從面板上,我們可以檢查 Elastic Stack 集群的狀態。


如何在 RHEL8 /CentOS8 上建立多節點 Elastic stack 集群

Stack-Monitoring-Overview-RHEL8


這證明我們已經在 RHEL 8 /CentOS 8 上成功地安裝並設置了多節點 Elastic Stack 集群。

現在讓我們通過 filebeat 從其他 Linux 服務器發送一些日誌到 logstash 節點中,在我的例子中,我有一個 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 文件並添加以下內容:

192.168.56.20 logstash1.linuxtechi.local
192.168.56.30 logstash2.linuxtechi.local

現在配置 filebeat,以便它可以使用負載平衡技術向 logstash 節點發送日誌,編輯文件 /etc/filebeat/filebeat.yml,並添加以下參數:

在 filebeat.inputs: 部分將 enabled: false 更改為 enabled: true,並在 paths 參數下指定我們可以發送到 logstash 的日誌文件的位置;註釋掉 output.elasticsearch 和 host 參數;刪除 output.logstash: 和 hosts: 的註釋,並在 hosts 參數添加兩個 logstash 節點,以及設置 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

現在轉到 Kibana 用戶界面,驗證新索引是否可見。

從左側欄中選擇管理選項,然後單擊 Elasticsearch 下的索引管理:


如何在 RHEL8 /CentOS8 上建立多節點 Elastic stack 集群

Elasticsearch-index-management-Kibana


正如我們上面看到的,索引現在是可見的,讓我們現在創建索引模型。

點擊 Kibana 部分的 “Index Patterns”,它將提示我們創建一個新模型,點擊 “Create Index Pattern” ,並將模式名稱指定為 “filebeat”:


如何在 RHEL8 /CentOS8 上建立多節點 Elastic stack 集群

Define-Index-Pattern-Kibana-RHEL8


點擊下一步。

選擇 “Timestamp” 作為索引模型的時間過濾器,然後單擊 “Create index pattern”:


如何在 RHEL8 /CentOS8 上建立多節點 Elastic stack 集群

Time-Filter-Index-Pattern-Kibana-RHEL8


如何在 RHEL8 /CentOS8 上建立多節點 Elastic stack 集群

filebeat-index-pattern-overview-Kibana


現在單擊查看實時 filebeat 索引模型:


如何在 RHEL8 /CentOS8 上建立多節點 Elastic stack 集群

Discover-Kibana-REHL8


這表明 Filebeat 代理已配置成功,我們能夠在 Kibana 儀表盤上看到實時日誌。

以上就是本文的全部內容,對這些幫助你在 RHEL 8 / CentOS 8 系統上設置 Elastic Stack 集群的步驟,請不要猶豫分享你的反饋和意見。


via: https://www.linuxtechi.com/setup-multinode-elastic-stack-cluster-rhel8-centos8/

作者: Pradeep Kumar 選題: lujun9972 譯者: heguangzhi 校對: wxy

本文由 LCTT 原創編譯, Linux中國 榮譽推出


分享到:


相關文章: