Centos7下搭建ELK日志分析系统

 

  • ELK简介

ELK是Elasticsearch、Logstash、Kibana的简称,这三者是核心套件,但并非全部。

Elasticsearch是实时全文搜索和分析引擎,提供搜集、分析、存储数据三大功能;是一套开放REST和JAVA API等结构提供高效搜索功能,可扩展的分布式系统。它构建于Apache Lucene搜索引擎库之上。

Logstash是一个用来搜集、分析、过滤日志的工具。它支持几乎任何类型的日志,包括系统日志、错误日志和自定义应用程序日志。它可以从许多来源接收日志,这些来源包括 syslog、消息传递(例如 RabbitMQ)和JMX,它能够以多种方式输出数据,包括电子邮件、websockets和Elasticsearch。

Kibana是一个基于Web的图形界面,用于搜索、分析和可视化存储在 Elasticsearch指标中的日志数据。它利用Elasticsearch的REST接口来检索数据,不仅允许用户创建他们自己的数据的定制仪表板视图,还允许他们以特殊的方式查询和过滤数据。

redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

  • 环境介绍
  1. 软件版本

操作系统:CentOS Linux release 7.6.1810 (Core)

Elasticsearch:7.9.3
elasticsearch-head:grunt-cli v1.3.2
logstash:7.9.3
kibana:7.9.3
redis: 6.0.6
  1. 下载链接
  • Elasticsearch 、logstash、kibana下载链接如下,使用终端浏览器打开后下载

https://www.elastic.co/cn/downloads/elasticsearch

https://www.elastic.co/cn/downloads/logstash

https://www.elastic.co/cn/downloads/kibana

centos系统查看系统日志看内存报错 centos7日志查看_服务器

 

  • elasticsearch-head下载链接及方式
git clone git://github.com/mobz/elasticsearch-head.git
• redis下载链接及方式
# wget http://download.redis.io/releases/redis-6.0.6.tar.gz
1. 安装路径
Elasticsearch 、elasticsearch-head、logstash、kibana、redis全部安装到/usr/local路径下
• 安装步骤
将Elasticsearch 、elasticsearch-head、logstash、kibana软件下载后上传至/usr/local目录下。
1. elasticsearch安装步骤
• 解压下载的安装包
# tar –zxvf elasticsearch-7.9.3-linux-x86_64.tar.gz
• 创建软连接
# ln -s elasticsearch-7.9.3 elasticsearch
# useradd elk
# passwd elk
# chown -R elk.elk elasticsearch elasticsearch-7.9.3
• 编辑配置文件
# cd elasticsearch
# vim config/elasticsearch.yml
• 将如下配置添加到配置文件中
cluster.name: logcenter
node.name: s101
path.data: /home/elk/data
path.logs: /home/elk/logs
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: [s101]
http.cors.enabled: true
http.cors.allow-origin: "*"
• 启动elasticsearch
#nohup /usr/local/elasticsearch/bin/elasticsearch 2>&1 &
• 浏览器访问elasticsearch URL
 
1. elasticsearch-head安装步骤
• 下载程序包
#git clone git://github.com/mobz/elasticsearch-head.git
• 安装grunt
# cd /usr/local/elasticsearch-head/
# npm install -g grunt-cli
• 检查grunt是否安装成功
# grunt -version

centos系统查看系统日志看内存报错 centos7日志查看_运维_02

  • 修改elasticsearch-head配置文件
# vim /usr/local/elasticsearch-head/Gruntfile.js
99 keepalive: true,
 100 hostname: “*”# vim /usr/local/elasticsearch-head/_site/app.js
this.base_uri = this.config.base_uri || this.prefs.get(“app-base_uri”) || “http://192.168.100.10:9200”;
• 安装npm
# cd /usr/local/elasticsearch-head/
# npm install
• 启动服务
# grunt server&
• 重新启动elasticsearch服务
#su - elk
# pgrep -f elasticsearch | xargs kill
#nohup /usr/local/elasticsearch/bin/elasticsearch 2>&1 &
  1. Redis安装步骤
  • 解压程序包
# tar -zxvf redis-6.0.6.tar.gz
• 编译并安装(如果报错安装报错提示处理即可)
#make
#make install
• 查看安装成功后的redis-server版本
• 启动redis-server,可以根据需求修改bind和port等参数
# redis-server /usr/local/redis/redis.conf
1. logstash安装步骤
• 解压程序包
# tar –zxvf logstash-7.9.3.tar.gz
• 创建软连接并修改目录属主
# ln -s logstash-7.9.3 logstash
# chown -R root.root logstash logstash-7.9.3
• 编辑配置文件
# vim logstash.yml  添加如下内容
http.host: 0.0.0.0
http.port: 9600
# vim redis.conf  添加如下内容
input {
  redis {
    port => "6379"
    host => "172.16.9.101"
    data_type => "list"
    type => "log"
    key => "yhxx-log"
  }
}
output {
  elasticsearch {
     hosts => "172.16.9.101:9200"
     index => "logstash1-%{+YYYY.MM.dd}"
  }
}
  • 启动logstash程序
# nohup ./bin/logstash -f /usr/local/logstash/config/redis.conf &
1. kibana安装步骤
• 解压程序包
# tar –zxvf kibana-7.9.3-linux-x86_64.tar.gz
• 创建软连接并修改应用属主
# ln -s kibana-7.9.3-linux-x86_64 kibana
# chown -R elk.elk kibana kibana-7.9.3-linux-x86_64
• 修改config/kibana.yml配置文件
# cd kibana
# vim config/kibana.yml server.port: 5601
server.host: "172.16.9.101"
elasticsearch.hosts: ["http://172.16.9.101:9200/"]
kibana.index: ".kibana"
• 启动程序(新版本要求使用非root账户启动)
# nohup /usr/local/kibana/bin/kibana 2>&1 &
  • QA
  1. 安装完成elasticsearch-head后连接不上elasticsearch节点

centos系统查看系统日志看内存报错 centos7日志查看_centos_03

原因:elasticsearch配置文件未允许跨域访问

解决方案:


//编辑配置文件
# vim /usr/local/elasticsearch/elasticsearch.yml
 
//增加下面两项
http.cors.enabled: true
http.cors.allow-origin: "*"
 
//重启elasticsearch


刷新页面,最新效果见下图:

centos系统查看系统日志看内存报错 centos7日志查看_centos系统查看系统日志看内存报错_04

 

  1. npm 安装报错 rollbackFailedOptional verb npm-session

原因:镜像源无法访问

解决方案:修改镜像源

#npm config set registry http://registry.npm.taobao.org

  1. Elasticsearch启动的时候报错can not run elasticsearch as root

原因:如错误提示,Elasticsearch不能使用root账户启动运行

解决方案:切换至普通用户启动即可。

centos系统查看系统日志看内存报错 centos7日志查看_运维_05

  1. 安装redis6时报错server.c:3318:16: error: ‘struct redisServer‘ has no member named ‘loading‘

原因:redis6要求编译安装要求gcc版本5以上

解决方案:升级gcc版本
# yum -y install centos-release-scl
#yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
# scl enable devtoolset-9 bash

centos系统查看系统日志看内存报错 centos7日志查看_centos系统查看系统日志看内存报错_06

  1. Redis安装运行make test时报错:“You need tcl 8.5 or newer in order to run the Redis test”问题解决

原因:tcl软件版本低于要求

解决方案:升级tcl版本至8.6

# wget http://downloads.sourceforge.net/tcl/tcl8.6.1-src.tar.gz  
 # sudo tar xzvf tcl8.6.1-src.tar.gz  -C /usr/local/  
 # cd  /usr/local/tcl8.6.1/unix/  
 # sudo ./configure  
 # sudo make  
 # sudo make install 1. 安装redis时运行make test时报错tail: cannot open `+?` for reading错误处理
原因:新版本POSIX用’tail +2’命令不能正确显示文件前两行内容,必须用’tail –n +2’命令才行
解决方案:临时修改_POSIX2_VERSION参数值
设置用法:
 #export _POSIX2_VERSION=199209
 取消设置:
 #unset _POSIX2_VERSION1. 安装kibana 时报错FATAL  Error: [config validation of [elasticsearch].url]: definition for this key is missing
原因:新版本elasticsearch.url关键字改成了elasticsearch.hosts
解决方案:修改参数配置
elasticsearch.url: "http://172.16.9.101:9200"
变为
elasticsearch.hosts: ["http://172.16.9.101:9200/"]