转载来源 :ELK+filebeat+redis 日志分析平台 :https://www.jianshu.com/p/bf2793e85d18
ELK Stack是软件集合Elasticsearch、Logstash、Kibana的简称,由这三个软件及其相关的组件可以打造大规模日志实时处理系统。
其中,Elasticsearch 是一个基于 Lucene 的、支持全文索引的分布式存储和索引引擎,主要负责将日志索引并存储起来,方便业务方检索查询。
Logstash是一个日志收集、过滤、转发的中间件,主要负责将各条业务线的各类日志统一收集、过滤后,转发给 Elasticsearch 进行下一步处理。
Kibana是一个可视化工具,主要负责查询 Elasticsearch 的数据并以可视化的方式展现给业务方,比如各类饼图、直方图、区域图等。
所谓“大规模”,指的是 ELK Stack 组成的系统以一种水平扩展的方式支持每天收集、过滤、索引和存储 TB 规模以上的各类日志。
通常,各类文本形式的日志都在处理范围,包括但不限于 Web 访问日志,如 Nginx/Apache Access Log 。
基于对日志的实时分析,可以随时掌握服务的运行状况、统计 PV/UV、发现异常流量、分析用户行为、查看热门站内搜索关键词等。 上图是ELK Stack实际应用中典型的一种架构,其中:
1)filebeat:部署在具体的业务机器上,通过定时监控的方式获取增量的日志,并转发到Kafka消息系统暂存。
2)Kafka:以高吞吐量的特征,作为一个消息系统的角色,接收从filebeat收集转发过来的日志,通常以集群的形式提供服务。
3)logstash:然后,Logstash从Kafka中获取日志,并通过Input-Filter-Output三个阶段的处理,更改或过滤日志,最终输出我们感兴趣的数据。通常,根据Kafka集群上分区(Partition)的数量,1:1确定Logstash实例的数量,组成Consumer Group进行日志消费。
4)elasticsearch:最后,Elasticsearch存储并索引Logstash转发过来的数据,并通过Kibana查询和可视化展示,达到实时分析日志的目的。
Elasticsearch/Kibana还可以通过安装x-pack插件实现扩展功能,比如监控Elasticsearch集群状态、数据访问授权等。
我们一步步安装部署Elastic Stack系统的各个组件,然后以网站访问日志为例进行数据实时分析。
首先,到ELK 官网下载需要用到的Filebeat/Logstash/Elasticsearch/Kibana软件安装包。(推荐下载编译好的二进制可执行文件,直接解压执行就可以部署)
1…下载软件包:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.1.tar.gz wget https://artifacts.elastic.co/downloads/kibana/kibana-5.4.1-linux-x86_64.tar.gz wget https://artifacts.elastic.co/downloads/logstash/logstash-5.4.1.tar.gz wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.1-linux-x86_64.tar.gz wget http://download.redis.io/releases/redis-3.0.5.tar.gz yum install java-1.8.0-openjdk2.准备 1)下面的命令实现永久关闭SELinux
[root@elk ~]# sed -i 's/^SELINUX=.*/#&/;s/^SELINUXTYPE=.*/#&/;/SELINUX=.*/a SELINUX=disabled' /etc/sysconfig/selinux #永久修改下主机名,需要重启系统之后生效 #下面的命令实现临时关闭SELinux [root@elk ~]# setenforce 0 setenforce: SELinux is disabled2) 修改主机名
#临时修改 [root@elk ~]#hostname elk.server.com #永久修改 [root@elk ~]# vi /etc/sysconfig/network localhost.localdomain ------》把这行修改成下面的 elk.server.com #修改成你自己的主机名 #添加域名 [root@elk ~]#cat /etc/hosts 192.168.10.243 elk.server.com3)关闭防火墙
#临时关闭 [root@elk ~]# iptables -F 或者 [root@elk ~]# service iptables stop 同步时间 ntpdate -u ntp.api.bz修改以下几项:
daemonize yes #后台模式运行
pidfile /data/application/redis-3.0.5/run/redis.pid #redis的pid
bind 0.0.0.0 #这里根据自己的ip填写
port 6379#端口
logfile “/data/application/redis-3.0.5/log/redis.log” #log存放位置
dir /data/application/redis-3.0.5
#启动redis 执行下面的命令 [root@elk~]#/ data/application/redis-3.0.5/bin/redis-server /data/application/redis-3.0.5/etc/redis.conf #查看是否启动成功 [root@elk ~]# lsof -i:6379 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME redis-ser 2736 root 4u IPv4 26198 0t0 TCP 192.168.10.243:6379 (LISTEN) #测试redis [root@localhost ~]# /data/application/redis-3.0.5/bin/redis-cli -h 192.168.10.243 192.168.10.243:6379> ping PONG #出现PONG,证明可以使用注意:
es(elasticsearch)版本2.x以上需要JDK 1.8以上
运行es不能使用root用来来运行
es目录必须指定一个普通用户和组(授权)
es对内存和CPU的消耗比较高
es使用的端口看开放iptables:9200,9300等
es配置其他插件实现资源等可视化监控
es的版本和插件之间版本要匹配
es集群配置,第一节点配置好scp到其他节点即可(修改配置文件)
#创建elk用户 [root@localhost application]# adduser -s /bin/bash -c 'elk' -m -d /home/elk elk 注: 从2.0开始不能用root用户启动需要elk用户启动 #解压 [root@elk elk_pack]# tar zxvf elasticsearch-6.01.tar.gz -C /data/application/ 注: Elasticsearch是不需要编译的,解压就可以使用 备份配置文件 [root@elk ~]# cp /data/application/elasticsearch-6.01/config/elasticsearch.yml{,.ori} #找到以下几行修改 [root@elk config]# vi elasticsearch.yml path.data: /data/shuju ----》存放数据路径 path.logs: /data/logs -----》日志路径 network.host: 0.0.0.0 -----》根据自己的ip修改 http.port: 9200 bootstrap.memory_lock: false bootstrap.system_call_filter: false #创建los,shuju [root@elk config]#mkdir /data/{shuju,logs} #修改elasticsearch权限 [root@elk ~]#chown -R elk.elk /data/application/elasticsearch-6.0.1./ [root@elk ~]#chown -R elk.elk /data/{shuju,logs} [root@elk ~]# su – elk #在前台显示下效果 [elk@elk ~]$/data/application/elasticsearch-6.0.1/bin/elasticsearch #测试是否成功 [root@elk ~]# curl 192.168.10.243:9200 { "name" : "z8htm2J", "cluster_name" : "elasticsearch", "cluster_uuid" : "wEbF7BwgSe-0vFyHb1titQ", "version" : { "number" : "6.0.1", "build_hash" : "3adb13b", "build_date" : "2017-03-23T03:31:50.652Z", "build_snapshot" : false, "lucene_version" : "6.4.1" }, "tagline" : "You Know, for Search" } 启动elasticsearch出现如下错误 1.问题:最大线程数,打开的太低,需要增加线程数 max number of threads [1024] for user [elasticsearch] likely toolow, increase to at least [2048] 解决: vi /etc/security/limits.d/90-nproc.conf * soft nproc 2048 2.问题:打开虚拟内存的个数太少需要增加 max virtual memory areas vm.max_map_count [65530] likely toolow, increase to at least [262144] 解决: [root@elk ~]#vi /etc/sysctl.conf vm.max_map_count=655360 [root@elk ~]#sysctl -p 注: vm.max_map_count文件允许max_map_count限制虚拟内存的数量 3.max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536] #临时修改 [root@elk ~]# ulimit -SHn 65536 注: -S 设置软件资源限制 -H 设置硬件资源限制 -n 设置内核可以同时可以打开文件描述符 [root@elk ~]# ulimit -n 65536 注: 修改这个原因,启动elasticsearch 会出现这个情况too many open files,导致启动失败 #永久修改 #在文件最后添加 [root@elk ~]# vi /etc/security/limits.conf * soft nofile 65536 * hard nofile 131072 * soft nproc 2048 * hard nproc 4096 注: 文件格式:username|@groupname type resource limit 分为3中类型type(有 soft,hard 和 -) soft是指当前系统生效的设置值 hard 系统设置的最大值 [if !supportLists]- [endif]同时设置了soft和hard的值 nofile - 打开文件的最大数目 noproc - 进程的最大数目 soft<=hard soft的限制不能比hard限制高 #需要重启系统才会生效#解压
[root@elk elk_pack]# tar zxvf kibana-6.0.1-linux-x86_64.tar.gz -C /data/application/注:
Kibana是不需要编译的,解压就可以使用
#修改配置kibana.yml文件
#cd kibana这个目录 [root@elk ~]# cd /data/application/kibana-6.0.1/config/#找到以下几行修改
[root@elk config]# egrep -v "^$|^[#]" kibana.yml server.port: 5601 #kibana的端口 server.host: "0.0.0.0" #访问kibana的ip地址 elasticsearch.url: "http://192.168.10.243:9200" #elasticsearch的ip地址 kibana.index: ".kibana" #创建索引#测试是否启动成功
[root@192 ~]# /data/application/kibana-6.0.1/bin/kibana log [06:22:02.940] [info][status][plugin:kibana@5.2.0] Status changed from uninitialized to green - Ready log [06:22:03.106] [info][status][plugin:elasticsearch@5.2.0] Status changed from uninitialized to yellow - Waiting for Elasticsearch log [06:22:03.145] [info][status][plugin:console@5.2.0] Status changed from uninitialized to green - Ready log [06:22:03.193] [warning] You're running Kibana 5.2.0 with some different versions of Elasticsearch. Update Kibana or Elasticsearch to the same version to prevent compatibility issues: v5.3.0 @ 192.168.10.243:9200 (192.168.201.135) log [06:22:05.728] [info][status][plugin:timelion@5.2.0] Status changed from uninitialized to green - Ready log [06:22:05.744] [info][listening] Server running at http://192.168.10.243:5601 log [06:22:05.746] [info][status][ui settings] Status changed from uninitialized to yellow - Elasticsearch plugin is yellow log [06:22:08.263] [info][status][plugin:elasticsearch@5.2.0] Status changed from yellow to yellow - No existing Kibana index found log [06:22:09.446] [info][status][plugin:elasticsearch@5.2.0] Status changed from yellow to green - Kibana index ready log [06:22:09.447] [info][status][ui settings] Status changed from yellow to green – Ready#证明启动成功
#查看port
[root@elk shuju]# lsof -i:5601 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME node 4690 root 13u IPv4 40663 0t0 TCP 192.168.10.243:esmagent (LISTEN)通过web访问
http://192.168.10.243:5601
1)安装nginx
安装依赖包 [root@www ~]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel 下载nginx的源码包:http://nginx.org/download [root@www ~]# tar zxf nginx-1.10.2.tar.gz [root@www ~]# cd nginx-1.10.2/ [root@www ~]# groupadd www#添加www组 [root@www ~]# useradd -g www www -s /sbin/nologin#创建nginx运行账户www并加入到www组,不允许www用户直接登录系统 [root@www nginx-1.10.2]# ./configure --prefix=/usr/local/nginx1.10 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre --with-http_ssl_module --with-http_gzip_static_module --user=www --group=www [root@www nginx-1.10.2]# make&& make install 修改日志类型为json [root@rocketmq-nameserver2 soft]# vim nginx/conf/nginx.conf #添加如下内容 log_format json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"url":"$uri",' '"referer":"$http_referer",' '"agent":"$http_user_agent",' '"status":"$status"}'; access_log logs/access.log json;** 2)安装部署Filebeat**
tar xf filebeat-6.0.1-linux-x86_64.tar.gz cd filebeat-6.0.1-linux-x86_64 编写收集文件: filebeat.prospectors: - input_type: log paths: - /var/log/*.log - input_type: log paths: - /aebiz/soft/nginx/logs/*.log encoding: utf-8 document_type: my-nginx-log scan_frequency: 10s harvester_buffer_size: 16384 max_bytes: 10485760 tail_files: true output.redis: enabled: true hosts: ["192.168.10.243"] port: 6379 key: filebeat db: 0 worker: 1 timeout: 5s max_retries: 3 启动 [root@~ filebeat-6.0.1-linux-x86_64]# ./filebeat -c filebeat2.yml 后台启动 [root@~ filebeat-6.0.1-linux-x86_64]# nohup ./filebeat -c filebeat2.yml &