CentOS 8上设置多节点ELK stack集群

  • CentOS 8上设置多节点ELK stack集群已关闭评论
  • 119 views
  • A+
所属分类:elasticsearch kibana

Elastic stack被广泛称为ELK stack,它是一组开源产品,如ElasticsearchLogstashKibana。Elastic Stack由Elastic公司开发和维护。使用弹性堆栈,可以将系统的日志提供给Logstash,它是一个数据收集引擎,它接受来自所有源的日志或数据并规范化日志,然后将日志转发给Elasticsearch进行分析索引搜索存储,最后使用Kibana可以代表可视化数据,使用Kibana我们还可以根据用户的查询创建交互式图形和图表。

CentOS 8上设置多节点ELK stack集群

在本文中,我们将演示如何在RHEL 8 / CentOS 8服务器上设置多节点ELK stack集群。以下是我的Elastic Stack Cluster的详细信息:

Elasticsearch:
  • 3台minimal 模式安装的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:
  • 2台minimal 模式安装的CentOS 8服务器
  • ip以及对应的域名

192.168.56.20 (logstash1.linuxtechi. local)

192.168.56.30 (logstash2.linuxtechi. local)

Kibana:
  • 1台minimal 模式安装的CentOS 8服务器
  • ip以及对应的域名

192.168.56.10(kibana.linuxtechi.local)

Filebeat:
  • 1台minimal 模式安装的CentOS 8服务器
  • ip以及对应的域名

192.168.56.70 (web-server)

 

让我们从Elasticsearch集群设置开始,

设置3节点Elasticsearch集群

正如我已经说过的,我保留了Elasticsearch集群的节点,登录到每个节点,设置主机名并配置yum / dnf存储库。

使用以下hostnamectl命令在各个节点上设置主机名,

[root@localhost ~]# hostnamectl set-hostname "elasticsearch1.linuxtechi. local"
[root@localhost ~]# exec bash
[root@elasticsearch1 ~]#
[root@localhost ~]# hostnamectl set-hostname "elasticsearch2.linuxtechi. local"
[root@localhost ~]# exec bash
[root@elasticsearch2 ~]#
[root@localhost ~]# hostnamectl set-hostname "elasticsearch3.linuxtechi. local"
[root@localhost ~]# exec bash
[root@elasticsearch3 ~]#

对于CentOS 8系统,我们不需要配置任何操作系统软件包存储库和RHEL 8服务器,如果您有有效的订阅,然后使用Red Hat订阅它以获取软件包存储库。如果您想为OS软件包配置本地yum / dnf存储库,请参考以下URL:

如何使用DVD或ISO文件在RHEL 8服务器上设置本地Yum / DNF存储库

在所有节点上配置Elasticsearch包存储库,在/etc/yum.repos.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@elasticsearch1 ~]# dnf install java-openjdk -y
[root@elasticsearch2 ~]# dnf install java-openjdk -y
[root@elasticsearch3 ~]# dnf install java-openjdk -y

在所有三个节点上使用dnf命令下方安装Elasticsearch,

[root@elasticsearch1 ~]# dnf install elasticsearch -y
[root@elasticsearch2 ~]# dnf install elasticsearch -y
[root@elasticsearch3 ~]# dnf install elasticsearch -y

注意:如果在每个Elasticsearch节点中启用并运行OS防火墙,则在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@elasticsearch1 ~]# ss -tunlp | grep 9200
tcp   LISTEN  0       128       [::ffff:192.168.56.40]:9200              *:*     users:(("java",pid=2734,fd=256))                  
[root@elasticsearch1 ~]#

使用以下curl命令验证Elasticsearch集群状态

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

上面的命令输出如下所示,

CentOS 8上设置多节点ELK stack集群

上面的输出确认我们已经成功创建了3个节点的Elasticsearch集群,集群的状态也是绿色的。

注意:如果要修改JVM堆大小,则可以编辑文件“ /etc/elasticsearch/jvm.options ”并更改适合您环境的以下参数,

  • -Xms1g
  • -Xmx1g

现在让我们转到Logstash节点,

安装和配置Logstash

在两个Logstash节点上执行以下步骤,

使用以下hostnamectl命令登录到两个节点设置主机名,

[root@localhost ~]# hostnamectl set-hostname "logstash1.linuxtechi.local"
[root@localhost ~]# exec bash
[root@logstash1 ~]#
[root@localhost ~]# hostnamectl set-hostname "logstash2.linuxtechi.local"
[root@localhost ~]# exec bash
[root@logstash2 ~]#

在两个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.repos.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

使用以下dnf命令在两个节点上安装Java OpenJDK,

~]# dnf install java-openjdk -y

从两个节点运行以下dnf命令以安装logstash,

[root@logstash1 ~]# dnf install logstash -y
[root@logstash2 ~]# dnf install logstash -y

现在配置logstash,在两个logstash节点上执行以下步骤,

创建一个logstash conf文件,首先我们在'/etc/logstash/conf.d/'下复制样本logstash文件。

# cd /etc/logstash/
# cp logstash-sample.conf conf.d/logstash.conf

编辑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"
  }
}

在输出部分下,在hosts参数中指定所有三个Elasticsearch节点的FQDN,其他参数保持不变。

使用以下firewall-cmd命令在OS防火墙中允许logstash端口“5044”,

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

现在启动并启用Logstash服务,在两个节点上运行以下systemctl命令

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

使用下面的ss命令验证logstash服务是否开始在5044上监听,

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

以上输出确认已成功安装和配置了logstash。我们转到Kibana安装。

安装和配置Kibana

登录Kibana节点,使用hostnamectl命令设置主机

[root@localhost ~]# hostnamectl set-hostname "kibana.linuxtechi.local"
[root@localhost ~]# exec bash
[root@kibana ~]#

编辑/ 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@kibana ~]# 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@kibana ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

执行以下dnf命令安装kibana,

[root@kibana ~]# yum install kibana -y

通过编辑文件“ /etc/kibana/kibana.yml ” 配置Kibana

[root@kibana ~]# 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@kibana ~]# systemctl start kibana
[root@kibana ~]# systemctl enable kibana

在操作系统防火墙中允许Kibana端口'5601',

[root@kibana ~]# firewall-cmd --permanent --add-port=5601/tcp
success
[root@kibana ~]# firewall-cmd --reload
success
[root@kibana ~]#

使用以下URL访问Kibana门户/ GUI:

HTTP://kibana.linuxtechi.local:5601

CentOS 8上设置多节点ELK stack集群

从仪表板,我们还可以检查我们的弹性堆栈群集状态

CentOS 8上设置多节点ELK stack集群

这证实我们已在CentOS 8上成功设置了多节点弹性ELK集群。

现在让我们通过来自其他Linux服务器的filebeat将一些日志发送到logstash节点。在我的情况下,我有一个CentOS 7服务器,我将通过filebeat将此服务器的所有重要日志推送到logstash。

使用以下rpm命令登录CentOS 7服务器并安装filebeat包,

[root@web-server ~]# 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@web-server ~]#

编辑/ 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的位置日志文件,在输出Elasticsearch部分注释掉“ output.elasticsearch” “和主机参数。在Logstash输出部分中,删除“ output.logstash: ”和“ hosts: ” 的注释,并在hosts参数中添加两个logstash节点,并添加“ loadbalance:true”。

[root@web-server ~]# 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
………………………………………

使用systemctl命令下启动并启用filebeat服务,

[root@web-server ~]# systemctl start filebeat
[root@web-server ~]# systemctl enable  filebeat

现在转到Kibana GUI,验证新索引是否可见,

从左侧栏中选择管理选项,然后单击Elasticsearch下的索引管理,

CentOS 8上设置多节点ELK stack集群

正如我们上面所看到的,索引现在是可见的,让我们创建索引模式,

单击Kibana Section中的“Index Patterns”,它将提示我们创建一个新模式,单击“ Create Index Pattern ”并将模式名称指定为“ filebeat ”

CentOS 8上设置多节点ELK stack集群

单击下一步

选择“ timestamp”作为索引模式的时间过滤器,然后单击“创建索引模式”

CentOS 8上设置多节点ELK stack集群

CentOS 8上设置多节点ELK stack集群

现在点击Discover查看实时filebeat索引模式,

CentOS 8上设置多节点ELK stack集群

这确认Filebeat代理已成功配置,我们可以在Kibana仪表板上查看实时日志。

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

  • 安卓客户端下载
  • 微信扫一扫
  • weinxin
  • 微信公众号
  • 微信公众号扫一扫
  • weinxin
avatar