ELK + filebeat集群部署

ELK + filebeat集群部署

 

一、ELK简介

1. Elasticsearch

Elasticsearch是一个实时的分布式搜索分析引擎, 它能让你以一个之前从未有过的速度和规模,去探索你的数据。它被用作全文检索、结构化搜索、分析以及这三个功能的组合

 

2.Logstash

Logstash是一款强大的数据处理工具,它可以实现数据传输,格式处理,格式化输出,还有强大的插件功能,常用于日志处理。

 

3.Kibana

kibana是一个开源和免费的工具,它可以为Logstash和ElasticSearch提供的日志分析友好的Web界面,可以帮助您汇总、分析和搜索重要数据日志。

 

4.ELK版本信息为7.2.1,你可以从官网下,也可以直接从下面地址下载:

https://artifacts.elastic.co/downloads/logstash/logstash-7.2.1.tar.gz

https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.1-linux-x86_64.tar.gz

https://artifacts.elastic.co/downloads/kibana/kibana-7.2.1-linux-x86_64.tar.gz

官网地址:https://www.elastic.co/cn/downloads/

百度网盘地址:

链接: https://pan.baidu.com/s/1G7zfve-XnWwQcSQx6wqHuA 
提取码: 8816

 

二、环境准备

1.三台Linux服务器,系统统一

[root@ELK1 ~]# cat /etc/redhat-release

CentOS Linux release 7.2.1511 (Core)

2.角色划分

NODE

IP

节点类型

ELK1

192.168.3.181

数据、主节点(安装elasticsearch、logstash、kabana、filebeat)

ELK2

192.168.3.182

数据节点(安装elasticsearch、filebeat)

ELK3

192.168.3.183

数据节点(安装elasticsearch、filebeat)

 

 

3.安装jdk11

这里安装jdk11,如果安装9或者8会报错,具体可看官方文档:

https://www.elastic.co/cn/support/matrix
https://www.elastic.co/guide/en/elasticsearch/reference/7.2/setup.html

下载安装包

[root@ELK1 tools]# pwd

/home/tools

[root@ELK tools]# wget https://download.java.net/java/GA/jdk11/13/GPL/openjdk-11.0.1_linux-x64_bin.tar.gz
[root@ELK1 tools]# mkdir -p /home/elk

百度网盘

链接:https://pan.baidu.com/s/1QK82p1eb-NbgxaFLKsSlqg
提取码:9dx8

  

解压到指定目录

[root@ELK1 tools]# tar -xzvf jdk-11.0.4_linux-x64_bin.tar.gz -C /usr/local/jdk

  

配置环境变量

#set java environment
JAVA_HOME=/usr/local/jdk/jdk-11.0.1
CLASSPATH=$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH

使环境变量生效

[root@ELK1 tools]# source /etc/profile

查看

[root@ELK1 tools]# java -version

openjdk version "11.0.1" 2018-10-16

OpenJDK Runtime Environment 18.9 (build 11.0.1+13)

OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)

 

 三、 安装Elasticsearch(简称ES)集群

 1.下载及解压

#下载
[root@ELK1 tools]# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.1-linux-x86_64.tar.gz

#创建目录
[root@ELK1 tools]# mkdir -p /home/elk

#解压
[root@ELK1 tools]# tar -zvxf elasticsearch-7.2.1-linux-x86_64.tar.gz -C /home/elk

  

2.创建用户及授权

ElasticSerach要求以非root身份启动,在每个节点创建用户及用户组

[root@ELK1 tools]# groupadd elasticsearch
[root@ELK1 tools]# useradd elasticsearch -g elasticsearch

 

 在每个节点上创建数据data和logs目录:

[root@ELK1 tools]# mkdir -p /data/elasticsearch/{data,logs}
[root@ELK1 tools]# chown -R elasticsearch. /data/elasticsearch/
[root@ELK1 tools]# chown -R elasticsearch. /home/elk/elasticsearch-7.2.1

  

3.修改elasticsearch.yml配置文件

[root@ELK1 elk]# vim /home/elk/elasticsearch-7.2.1/config/elasticsearch.yml

path.data: /data/elasticsearch/data #数据
path.logs: /data/elasticsearch/logs #日志
cluster.name: ELK1 # 集群中的名称
cluster.initial_master_nodes: ["192.168.3.181","192.168.3.182","192.168.3.183"] #主节点
node.name: ELK1 # 该节点名称,与前面配置hosts保持一致
node.master: true # 意思是该节点是否可选举为主节点
node.data: true # 表示这不是数据节点
network.host: 0.0.0.0 # 监听全部ip,在实际环境中应为一个安全的ip
http.port: 9200 # es服务的端口号
http.cors.enabled: true
http.cors.allow-origin: "*"
discovery.zen.ping.unicast.hosts: ["192.168.3.181", "192.168.3.182", "192.168.3.183"] # 配置自动发现
discovery.zen.minimum_master_nodes: 2 #防止集群“脑裂”,需要配置集群最少主节点数目,通常为 (主节点数目/2) + 1

  

4.修改elasticsearch的JVM内存

elasticesearch在实际生产中非常消耗cpu,需要将初始申请的JVM内存调高,默认是1G

[root@ELK1 elk]# vim /home/elk/elasticsearch-7.2.1/config/jvm.options

#修改这两行
-Xms4g #设置最小堆的值为4g
-Xmx4g #设置组大堆的值为4g

  

5.启动elasticsearch

其它两个节点也类似配置,只需要修改下node.name:即可,在配置好相应的节点后,首先启动主节点,然后在启动相应节点

[root@ELK1 elk]# su - elasticsearch
Last login: Mon Aug 12 09:58:23 CST 2019 on pts/1

[elasticsearch@ELK1 ~]$ cd /home/elk/elasticsearch-7.2.1/bin/
[elasticsearch@ELK1 bin]$ ./elasticsearch -d

  

查看端口号,分别为9200和9300

[root@ELK1 tools]# netstat -lntup|grep java
tcp6 0 0 192.168.3.181:9200 :::* LISTEN 9721/java tcp6 0 0 192.168.3.181:9300 :::* LISTEN 9721/java

 

集群基本操作

#查看集群的健康信息
curl '192.168.3.181:9200/_cluster/health?pretty'

#查看集群的详细信息
curl '192.168.3.181:9200/_cluster/state?pretty'

#查询索引列表
curl -XGET http://192.168.3.181:9200/_cat/indices?v

#创建索引
curl -XPUT http://192.168.3.181:9200/customer?pretty

#查询索引
curl -XGET http://192.168.3.181:9200/customer/external/1?pretty

#删除索引
curl -XDELETE http://192.168.3.181:9200/customer?pretty

#删除指定索引
curl -XDELETE 192.168.3.181:9200/nginx-log-2019.08

#删除多个索引
curl -XDELETE 192.168.3.181:9200/system-log-2019.0606,system-log-2019.0607

#删除所有索引
curl -XDELETE 192.168.3.181:9200/_all

#在删除数据时,通常不建议使用通配符,误删后果会很严重,所有的index都可能被删除,为了安全起见需要禁止通配符,可以在elasticsearch.yml配置文件中设置禁用_all和*通配符
action.destructive_requires_name: true

  

四、安装logstash

在主节点上进行部署

1.下载解压

#下载
[root@ELK1 tools]# wget https://artifacts.elastic.co/downloads/logstash/logstash-7.2.1.tar.gz

#解压
[root@ELK1 tools]# tar -zvxf logstash-7.2.1.tar.gz -C /home/elk

#创建数据及日志目录
[root@ELK1 tools]# mkdir -p /data/logstash/{logs,data}

  

2.修改logstash配置文件

[root@ELK1 tools]# vim /home/elk/logstash-7.2.1/config/logstash.yml

http.host: "ELK1"
path.data: /data/logstash/data
path.logs: /data/logstash/logs
xpack.monitoring.enabled: true #kibana监控插件中启动监控logstash
xpack.monitoring.elasticsearch.hosts: ["192.168.3.181:9200","192.168.3.182:9200","192.168.3.183:9200"]

  

3.创建配置文件

[root@ELK1 tools]# vim /home/elk/logstash-7.2.1/config/logstash.conf 

input {
  beats {
    port => 5044
  }
}
output {
  stdout {
    codec => rubydebug
  }
  elasticsearch {
    hosts => ["192.168.3.181:9200","192.168.3.182:9200","192.168.3.183"]
  }
}

  

4.启动服务

[root@ELK1 tools]# cd /home/elk/logstash-7.2.1/
[root@ELK1 logstash-7.2.1]# nohup bin/logstash -f config/logstash.conf &

  

五、安装kibana

1.下载及解压

#进入工具目录
[root@ELK1 tools] cd /home/tools

#下载
[root@ELK1 tools] wget https://artifacts.elastic.co/downloads/kibana/kibana-7.2.1-linux-x86_64.tar.gz

#解压
[root@ELK1 tools] tar xf kibana-7.2.1-linux-x86_64.tar.gz -C /home/elk

#创建日志目录
mkdir -p /data/kibana/logs/

  

2.修改kibana配置文件

[root@ELK1 tools]# vim /home/elk/kibana-7.2.1-linux-x86_64/config/kibana.yml

server.port: 5601 # 配置kibana的端口
server.host: 192.168.3.181 # 配置监听ip(设置本地ip使用nginx认证登录)
elasticsearch.hosts: ["http://192.168.3.181:9200","http://192.168.3.182:9200","http://192.168.3.183:9200"] # 配置es服务器的ip
logging.dest: /data/kibana/logs/kibana.log # 配置kibana的日志文件路径,默认messages
i18n.locale: "zh-CN" #配置中文语言

  

3.启动服务

[root@ELK1 tools]# cd /home/elk/kibana-7.2.1-linux-x86_64/
[root@ELK1 kibana-7.2.1-linux-x86_64]# nohup bin/kibana --allow-root &

  

六、安装filebeat

1.下载及解压

#进入工具目录
[root@ELK1 kibana-7.2.1-linux-x86_64]# cd /home/tools/

#下载
[root@ELK1 tools]# 
https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-7.2.1-linux-x86_64.tar.gz

#解压
[root@ELK1 tools]# tar -zvxf filebeat-7.2.1-linux-x86_64.tar.gz -C /home/elk/

  

2.修改filebeat配置文件

[root@ELK1 tools]# vim /home/elk/filebeat-7.3.0-linux-x86_64/filebeat.yml

#=========================== Filebeat inputs =============================
filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/log/nginx/access.log
  fields:
    document_type: nginx

#============================= Filebeat modules =============================== filebeat.config.modules: path: ${path.config}/modules.d/*.yml reload.enabled: false #==================== Elasticsearch template setting ========================== setup.template.settings: index.number_of_shards: 1
#============================== Kibana ===================================== setup.kibana: host: "192.168.3.181:5601" #----------------------------- Logstash output -------------------------------- output.logstash: hosts: ["192.168.3.181:5044"]
#================================ Processors ===================================== processors: - add_host_metadata: ~ - add_cloud_metadata: ~

  

如果需要添加多个日志,只需要添加

- type: log
  enabled: true
  paths:
    - /var/log/*.log

  

3.启动filebeat

[root@ELK1 tools]# cd /home/elk/filebeat-7.3.0-linux-x86_64/
[root@ELK1 filebeat-7.3.0-linux-x86_64]# nohup ./filebeat -e -c filebeat.yml &

通过tail -f nohup.out 可以看到我们的服务已经启动!

 

 

 

 

 

 

 

 

  

 

posted @ 2019-08-13 20:26  何宇泽  阅读(3968)  评论(0编辑  收藏  举报