本文共 9176 字,大约阅读时间需要 30 分钟。
日志分析是运维工程师解决系统故障,发现问题的主要手段。日志主要包括系统日志、应用程序日志和安全日志。系统运维和开发人员可以通过日志了解服务器软硬件信息、检查配置过程中的错误及错误发生的原因,经常分析日志可以了解服务器的负荷,性能安全性,从而及时采取措施纠正错误。
日志是一个非常庞大的数据,并且常常被分散在不同的设备上,这样排查问题的时候找日志就非常繁琐困难。 这时,一个专门处理日志的系统就非常必要,这里介绍其中的一种,ELK日志分析系统(ELasticsearch+Logstash+Kibana)日志服务器
提高安全性、集中存放日志、缺陷:对日志分析困难 ELK日志分析系统的日志处理步骤Elasticsearch的概述
提供了一个分布式多用户能力的全文搜索引擎
Elasticsearch核心概念
Logstash介绍
Kibana介绍
一个针对Elasticsearch的开源分析及可视化平台 搜索、查看存储在Elasticsearch索引中的数据 通过各种图表进行高级数据分析及展示 Kibana主要功能一台主机部署Apache服务,并部署logstash收
一台主机部署elasticsearch和kibana 另一台部署elasticsearch 两台elasticsearch构成ELS群集需求:
两台主机修改主机名为node1,node2,并添加IP与主机名的映射,再配置elasticsearch,以node1为例
安装elasticsearch
#准备yum源,yum安装rpm --import https://packages.elastic.co/GPG-KEY-elasticsearchcd /etc/yum.repo.d/vim elasticsearch.repo[elasticsearch-2.x]name=Elasticsearch repository for 2.x packagesbaseurl=http://packages.elastic.co/elasticsearch/2.x/centosgpgcheck=1gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearchenable=1yum install elasticsearch -yyum install java -y (1.8版本) #安装Java,因为有JVM,日志收集最好的开发语言就是java
修改配置文件
[root@node1 ~]# cd /etc/elasticsearch/[root@node1 elasticsearch]# lselasticsearch.yml log4j2.properties jvm.options scripts[root@node1 elasticsearch]# cp -p elasticsearch.yml elasticsearch.yml.bak ## 先把原配置文件做一下备份,再修改配置文件[root@node1 elasticsearch]# vim elasticsearch.yml17: cluster.name: elk-cluster ## 集群名称23: node.name: node1 ## 当前节点名33: path.data: /data/elk-data ## 数据存储的位置(目录不存在,需要创建)37: path.logs: /var/log/elasticsearch/ ## 日志文件存放的位置43: bootstrap.memory_lock: false ## true:允许内存中的数据交还给SWAP,flase:不允许内存中的数据交还给SWAP。 选择false,因为swap分区实在硬盘上建立的,如果内存不够,数据溢出,分到硬盘上的话,会影响速度55: network.host: 0.0.0.0 ## 监听地址,0.0.0.0表示所有网段59: http.port: 9200 ## ES端口号,外部通信的端口号 PS:9300是集群内部通信端口68: discovery.zen.ping.unicast.hosts: ["node1", "node2"] ## 群集中包含的节点名[root@node1 elasticsearch]# grep -v "^#" /etc/elasticsearch/elasticsearch.yml cluster.name: elk_clusternode.name: node1path.data: /data/elk_datapath.logs: /var/log/elasticsearchbootstrap.memory_lock: flasenetwork.host: 0.0.0.0http.port: 9200discovery.zen.ping.unicast.hosts: ["node1", "node2"]
java#添加映射[root@node1 ~]# vi /etc/hosts192.168.158.10 node1192.168.158.20 node2[root@node2 ~]# vi /etc/hosts192.168.158.10 node1192.168.158.20 node2
开启服务
[root@node1 elasticsearch]# mkdir -p /data/elk-data # 创建数据存放目录[root@node1 elasticsearch]# id elasticsearch ## ES的程序用户,安装的时候自动创建的用户uid=990(elasticsearch) gid=985(elasticsearch) 组=985(elasticsearch)[root@node1 elasticsearch]# chown elasticsearch.elasticsearch /data/elk-data/ ## 授权,交给用户 elasticsearch去管理[root@node1 elasticsearch]# systemctl start elasticsearch.service ## 开启服务[root@node1 elasticsearch]# netstat -anpt | grep 9200 # 过滤9200端口(外部访问集群端口)tcp6 0 0 :::9200 :::* LISTEN 15726/java
检查集群信息
在真机浏览器192.168.100.1 打开 http://192.168.158.10:9200/_cluster/health?pretty
http://192.168.158.20:9200/_cluster/health?pretty
2、检查群集状态信息 输入http://192.168.158.10:9200/_cluster/state?pretty 安装elasticsearch-head插件,可视化工具,更方便查看集群信息,并管理集群,以node1为例[root@node1 ~]# /usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head 安装位置/usr/share/elasticsearch/plugins/head
创建一个索引信息,创建索引为index-demo,类型为test,可以看到成功创建,这里的数据会被存储到ES集群中。
[root@node1 ~]# curl -XPUT 'localhost:9200/index-demo/test/1?pretty&pretty' -H 'content-Type: application/json' -d '{"user":"zhangsan","mesg":"hello world"}'
刷新一下,可以看到存储的分片处理与备份,暂时不存在节点间的主备关系。加粗的是主文件的,细框的是备份文件,不论哪个节点宕机,存储都不会丢失,很安全,一个节点有五个分片,两个节点共10个分片
利用组件的复合查询进行索引的创建、查询、删除
创建索引index-demo2 提交后结果可看到分片存储情况
查询 elasticsearch的监控组件安装 http://192.168.158.10:9200/_plugin/kopf/#!/cluster 在生产环境中--------------生产环境中必须要修改(注意)------------------ulimit -aopen files (-n) 1024方法:/etc/security/limits.conf* soft nofile 65535 * hard nofile 65535//重启生效--------------------------------------------------
解决node-01和node-02日志提示不让锁内存
less /var/log/elasticsearch/abner.log......vim /etc/security/limits.conf//末尾插入# allow user 'elasticsearch' mlockallelasticsearch soft memlock unlimitedelasticsearch hard memlock unlimitedsystemctl stop elasticsearch.servicesystemctl start elasticsearch.service
yum简易安装Apache
[root@server3 ~]# yum -y install httpd[root@server3 ~]# systemctl start httpd[root@server3 ~]# ll /etc/httpd/logs/总用量 4-rw-r--r--. 1 root root 0 11月 21 20:27 access_log-rw-r--r--. 1 root root 812 11月 21 20:27 error_log
安装logstash
# 准备yum源[root@client ~]# rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch[root@client ~]# cd /etc/yum.repos.d/[root@client yum.repos.d]# vim logstash.repo[logstash-2.1]name=Logstash repository for 2.1.x packagesbaseurl=http://packages.elastic.co/logstash/2.1/centosgpgcheck=1gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearchenable=1[root@client yum.repos.d]# yum install logstash -y[root@client yum.repos.d]# ln -s /opt/logstash/bin/logstash /usr/local/bin/[root@client yum.repos.d]# systemctl start logstash.service
查看java版本
[root@client yum.repos.d]# java -versionopenjdk version "1.8.0_131"OpenJDK Runtime Environment (build 1.8.0_131-b12)OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)
logstash(Apache)与elasticsearch (node)功能是否正常,做对接测试
logstash字段描述解释:-f通过这个选项可以指定logstash的配置文件,根据配置文件配置logstash -e后面跟着字符串该字符串可以被当做logstash的配置(如果是””,则默认使用stdin做为输入、stdout作为输出)-t测试配置文件是否正确,然后退出
验证输出格式化,使用 rubydebug 显示详细信息输出,code为一种编解码
[root@server3 yum.repos.d]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { stdout{codec => rubydebug} }'Settings: Default filter workers: 1Logstash startup completedsuxi123123 { "message" => "suxi123123", "@version" => "1", "@timestamp" => "2020-11-21T15:18:48.827Z", "host" => "server3"}
[root@server3 yum.repos.d]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.158.10:9200"] } }'Settings: Default filter workers: 1Logstash startup completedwww.baidu.comaaa123bbb123
写入ES和同时生成文本
[root@server3 yum.repos.d]# /opt/logstash/bin/logstash -e 'input { stdin{} } output { elasticsearch { hosts => ["192.168.158.10:9200"] } stdout { codec => rubydebug } }'Settings: Default filter workers: 1Logstash startup completedsuxi111{ "message" => "suxi111", "@version" => "1", "@timestamp" => "2020-11-21T15:50:24.202Z", "host" => "server3"}
[root@client bin]# vi /etc/logstash/conf.d/system.conf #添加文件input { file{ path => "/var/log/messages" type => "system" #系统类型 start_position => "beginning" } }output { elasticsearch { hosts => ["192.168.158.10:9200"] index => "system-%{+YYYY.MM.dd}" } }[root@client bin]# logstash -f /etc/logstash/conf.d/system.conf #指定logstash配置文件,输出系统日志#node1上重启elasticsearch[root@node1 elasticsearch-head]# systemctl restart elasticsearch.service查看索引切片情况
[root@node1 ~]# mkdir -p /usr/local/kibana[root@node1 ~]# tar xzf kibana-4.3.1-linux-x64.tar.gz -C /usr/local/kibana#修改配置文件[root@node1 ~]# vi /usr/local/kibana/kibana-4.3.1-linux-x64/config/kibana.yml//2行 server.port: 5601 #服务端口安徽//5行server.host: "0.0.0.0"//12行 ES地址elasticsearch.url: "http://192.168.158.10:9200“ #和ES建立联系//20行 kibana.index: ".kibana" #建立索引[root@node1 ~]# yum install screen -y[root@node1 ~]# /usr/local/kibana/kibana-4.3.1-linux-x64/bin/kibana #启动kibana监听#ctrl+a+d 进行丢入后台Discover里查看日志,图表式显示
#在logstash上输入Apache日志,输出到elasticsearch[root@client ~]# vi /etc/logstash/conf.d/apache-log.confinput { #输入Apache日志 file{ path => "/etc/httpd/logs/access_log" type => "access" start_position => "beginning" } file{ path => "/etc/httpd/logs/error_log" type => "error" start_position => "beginning" } } output { #输出到ES if [type] == "access" { elasticsearch { hosts => ["192.168.158.10:9200"] index => "apache_access.%{+YYYY.MM.dd}" } } if [type] == "error" { elasticsearch { hosts => ["192.168.158.10:9200"] index => "apache_error.%{+YYYY.MM.dd}" } } }[root@client ~]# /usr/share/logstash/bin/logstash -f apache_log.confhistory /etc/logstash/conf.d/apache-log.conf #启动脚本,进行输出
刷新ES网页,查看索引,建议先访问apache页面,否则可能刷新不出来
在kibana创建Apache索引图表 如果对您有用的话,欢迎点赞评论呀!!!转载地址:http://dfdaf.baihongyu.com/