ELK+FileBeat 开源日志分析系统搭建-Centos7.8

本文所用到的tar包下载地址(不包含JDK、elasticsearch-head)

ELK:也就是elasticsearch  logstash  kibana三款开源软件的简称

FileBeat:一个轻量级开源日志文件数据收集器

也是上班闲着没事研究一下了。网上的文档也看了多,emmm好多文档有些地方写的不够详细,自己也踩了很多坑,整理了下记录来,方便下次使用

1.修改一些系统参数,并创建一个用户,用于elasticsearch的启动

vim /etc/sysctl.conf
vm.max_map_count = 262144

如果不加入这个参数在启动的时候就会报错,提示如下

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

1.1修改sysctl.conf配置用户打开最大文件数

*               soft    nproc       655350
*               hard    nproc       655350
*               soft    nofile       655350
*               hard    nofile       655350
elk soft memlock unlimited
elk hard memlock unlimited

注意:后面两行必须加上,否则会出现如下错误提示,上面四行不加同样会报错,具体错误忘记了

memory locking requested for elasticsearch process but memory is not locked

添加完成后运行如下命令

sysctl -p
ulimit -SHn 65535

1.3配置java环境,由于我这里用的elasticsearch7.3.0的版本,所以jdk必须要在11以上,否则无法分配内存

vim /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/local/jdk-11
export PATH=.:$PATH:$JAVA_HOME/bin
export CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

[root@oracle ~]# source /etc/profile.d/jdk.sh
[root@oracle ~]# java -version
java version "11" 2018-09-25
Java(TM) SE Runtime Environment 18.9 (build 11+28)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11+28, mixed mode)

2.安装elasticsearch,可用rpm或tar包进行安装,根据自己情况选择,我这里用的tar包

tar xf elasticsearch-7.3.0-linux-x86_64.tar.gz
mv elasticsearch-7.3.0-linux-x86_64 ../elasticsearch-7.3.0

#修改elasticsearch配置文件,具体参数如下,其中http参数是为了跨域测试使用,如果不加访问时会被拦截
cluster.name: my-elk
node.name: node1
path.data: /usr/local/elasticsearch-7.3.0/data
path.data: /usr/local/elasticsearch-7.3.0/logs
bootstrap.memory_lock: true
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["121.37.158.40"]
cluster.initial_master_nodes: ["node1"]
http.cors.enabled: true
http.cors.allow-origin: "*"
xpack.license.self_generated.type: basic
xpack.security.enabled: false
node.data: true
node.ingest: true
node.master: true

2.1创建用户,在这个/usr/local/elasticsearch-7.3.0目录下执行以下命令

groupadd elk
useradd -g elk elk
chown -R elk:elk .

2.2启动并测试elasticsearch

#启动并测试elasticsearch
./bin/elasticsearch
curl http://localhost:9200/
#结果如下
{
  "name" : "node1",
  "cluster_name" : "my-elk",
  "cluster_uuid" : "yjN4KxYJQwG5-V_4cBOlxA",
  "version" : {
    "number" : "7.3.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "de777fa",
    "build_date" : "2019-07-24T18:30:11.767338Z",
    "build_snapshot" : false,
    "lucene_version" : "8.1.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

3.安装elasticsearch-head

cd /usr/local/
#安装依赖包
yum install git nodejs openssl-devel screen -y
#克隆 elasticsearch-head项目
git clone  https://github.com/mobz/elasticsearch-head.git
#完成后执行如下命令,时间较长请耐心等待
npm install phantomjs-prebuilt --ignore-scripts
npm install

安装完成后利用screen,ctrl+a ctlr +d 命令在后台将elasticsearch-head运行起来,

screen
npm run start

4.安装Kibana

tar xf kibana-7.3.0-linux-x86_64.tar.gz
mv kibana-7.3.0-linux-x86_64 ../kibana-7.3.0

修改kibana配置文件,如果你将elasticsearch、kibana部署在不同的服务器上,请将localhost改为服务器ip。另外kibana的版本不能比elasticsearch版本高,否则启动的时候会报错

vim config/kibana.yml
server.port: 8080
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://localhost:9200"]
i18n.locale: "zh-CN"

启动kibana,并查看端口是否被监听,端口被监听则表示成功。由于我是用root用户运行的,所以需要加--allow-root这个参数

./bin/kibana --allow-root
lsof -i:8080

5.安装logstash,logstash同样需要java环境,前面我们已经配置过了,这里就不在赘述了

tar xf logstash-7.9.3.tar.gz
mv logstash-7.9.3 ../
#将logstash加入到变量中
vim ~/.bash_profile
export PATH=$PATH:$HOME/bin:$M2_HOME/bin:/u01/mysql/bin:/usr/local/logstash-7.9.3/bin
source ~/.bash_profile
#验证logstash,只要出现 Successfully startfilebeat-7.9.3-linux-x86_64.tar.gzed Logstash API endpoint {:port=>9600} 就表示启动成功。
logstash -e 'input { stdin {} } output { stdout{} }'

6.安装nginx和filebeat,nginx就用yum装的,测试使用

yum install nginx -y
tar xf filebeat-7.9.3-linux-x86_64.tar.gz
mv filebeat-7.9.3-linux-x86_64 ../filebeat-7.9.3

6.1修改filebeat主配置文件

vim /etc/filebeat/filebeat.yml
#注释掉输出到 elasticsearch
#output.elasticsearch:
# Array of hosts to connect to.
#hosts: ["localhost:9200"]
开启输出到 logstash
output.logstash:
  # The Logstash hosts
  hosts: ["192.168.11.16:5044"]   --主机填写logstash所在主机

6.2修改 nginx 模块配置文件

vim /etc/filebeat/modules.d/nginx.yml
# Module: nginx
# Docs: https://www.elastic.co/guide/en/beats/filebeat/7.9/filebeat-module-nginx.html

- module: nginx
  # Access logs
  access:
    enabled: true

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    var.paths: ["/var/log/nginx/access.log"]

  # Error logs
  error:
    enabled: true

    # Set custom paths for the log files. If left empty,
    # Filebeat will choose the paths depending on your OS.
    var.paths: ["/var/log/nginx/error.log"]

启动filebeat,注意看日志,如果有错误,请及时排查问题

./bin/filebeat -e

7.编写测试配置文件,放在logstash配置文件目录下

vim nginx.conf
input {
    beats {
        ports => "5044"
    }
}


#output {
#    stdout {
#        index => "rubydebug"
#    }
#}


output {
    elasticsearch {
        hosts => ["127.0.0.1:9200"]
        index => "nginx-%{+YYYY.MM.dd}"
    }
}

通过配置文件启动logstash

#修改配置文件自动重载模式
logstash -f nginx.conf --config.reload.automatic

8.kibana部分效果图

 9.elasticsearch-head效果图

 后续就是编写获取日志文件的的规则。

资料来源

原文地址:https://www.cnblogs.com/Roobbin/p/13936943.html