在平常运维工做中,对于系统和业务日志的处理尤其重要。尤为是分布式架构,每一个服务都会有不少节点,若是要手工一个一个的去取日志,运维怕是要累死。 java
简单介绍:node
ELK 是 elasticsearch + logstash + kibana 三款开源软件的简称。linux
elasticsearch:是个开源的分布式搜索引擎,特色是:分布式、配置简单、自动发现、索引自动分片、索引副本机制、restful风格接口,多数据源,自动搜索负载等nginx
logstash:能够对日志进行收集、滤过、并将其存储在 elasticsearch中git
kibana:能够为 elasticsearch提供友好的用户交互界面,用户能够经过 kibana来分析、搜索甚至绘图来分析数据。github
这里介绍下目前使用比较多的架构:npm
ELK + filebeat bootstrap
Filebeat 是一个轻量级开源日志文件数据收集器,能够将它安装到须要收集的节点上,它会将日志输送到 logstash 或 elasticsearchvim
有了 ELK 就能够将分布到多台的日志统一规划起来。浏览器
网络上有不少关于 ELK 的部署方案,参考了不少发现要不就是老版本的,要不就是不太完善,所以本身作下记录。
注意:在安装 ELK 的时候,这三个软件的版本必须保持支持,不然出现各类bug
实验拓扑图:
实验环境主机服务介绍:
本次实验是收集 nginx 日志,并存储在 elasticsearch中。将 elasticsearch 和 kibana 安装在同一主机上能够避免没必要要的网络IO操做,直接本机交互。
(1)初始化工做
时间同步:
[root@192.168.118.14 ~]#ntpdate tiger.sina.com.cn
修改主机名:
[root@192.168.118.14 ~]#hostnamectl set-hostname node1 修改完主机名别忘记在 /etc/hosts 中申明 192.168.118.14 node1
修改文件打开最大数:
[root@192.168.118.14 ~]#vim /etc/security/limits.conf * soft nproc 655350 * hard nproc 655350 * soft nofile 655350 * hard nofile 655350 [root@192.168.118.14 ~]#ulimit -SHn 655350
(2)配置 java 环境
[root@192.168.118.14 /usr/local/src]#tar xf jdk-8u77-linux-x64.tar.gz -C /usr/local/ 在 /etc/profile 文件中追加 JAVA_HOME=/usr/local/jdk1.8.0_77 JAVA_BIN=$JAVA_HOME/bin PATH=$PATH:$JAVA_BIN CLASSPATH=$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export JAVA_HOME JAVA_BIN PATH CLASSPATH [root@192.168.118.14 /usr/local/src]#source /etc/profile [root@192.168.118.14 /usr/local/src]#ln -vs /usr/local/jdk1.8.0_77/bin/java /usr/bin/ [root@192.168.118.14 /usr/local/src]#java -version java version "1.8.0_77" Java(TM) SE Runtime Environment (build 1.8.0_77-b03) Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)
(3)安装 elasticsearch
下载地址:https://www.elastic.co/cn/downloads/past-releases#elasticsearch
这里下载的是 6.8 的 rpm 包
直接安装:
[root@192.168.118.14 ~/ELK]#yum localinstall elasticsearch-6.8.2.rpm 修改配置文件以下: [root@192.168.118.14 ~/ELK]#egrep ^[a-z] /etc/elasticsearch/elasticsearch.yml cluster.name: super-cluster node.name: node1 path.data: /var/lib/elasticsearch path.logs: /var/log/elasticsearch bootstrap.memory_lock: true network.host: 0.0.0.0 http.port: 9200 discovery.zen.ping.unicast.hosts: ["192.168.118.14"] http.cors.enabled: true http.cors.allow-origin: "*"
启动
[root@192.168.118.14 ~/ELK]#systemctl enable elasticsearch ; systemctl start elasticsearch
首次启动可能会启动失败,查看日志:
[root@192.168.118.14 ~/ELK]#tail /var/log/elasticsearch/super-cluster.log … [1]: memory locking requested for elasticsearch process but memory is not locked …
如上报错,须要修改启动脚本:
[root@192.168.118.14 ~/ELK]#vim /lib/systemd/system/elasticsearch.service 在 [Service] 配置段添加: … LimitMEMLOCK=infinity … [root@192.168.118.14 ~/ELK]#systemctl daemon-reload [root@192.168.118.14 ~/ELK]#systemctl start elasticsearch
查看端口,若是 9200 和 9300 监听,则说明 elasticsearch启动成功。
验证:
[root@192.168.118.14 ~/ELK]#curl http://localhost:9200/ { "name" : "node1", "cluster_name" : "super-cluster", "cluster_uuid" : "1FD-KmYMTVCzWVPI9vn8zw", "version" : { "number" : "6.8.2", "build_flavor" : "default", "build_type" : "rpm", "build_hash" : "b506955", "build_date" : "2019-07-24T15:24:41.545295Z", "build_snapshot" : false, "lucene_version" : "7.7.0", "minimum_wire_compatibility_version" : "5.6.0", "minimum_index_compatibility_version" : "5.0.0" }, "tagline" : "You Know, for Search" }
到此,elasticsearch安装成功。
这里多安装一个elasticsearch-head 用来调试和查看数据是很是方便的。
程序下载地址:https://github.com/mobz/elasticsearch-head
首先安装依赖包
yum install git nodejs openssl-devel screen -y 克隆 elasticsearch-head项目 [root@192.168.118.14 ~]# git clone https://github.com/mobz/elasticsearch-head.git [root@192.168.118.14 ~]# cd elasticsearch-head/ 此时忽略phantomjs-prebuilt@2.1.16,执行命令以下 [root@node1 elasticsearch-head]# npm install phantomjs-prebuilt --ignore-scripts [root@node1 elasticsearch-head]# npm install …
这里是一个很是缓慢的过程。
启动 elasticsearch-head 服务
[root@192.168.118.14 ~]#cd elasticsearch-head/ [root@192.168.118.14 ~/elasticsearch-head]#screen [root@node1 elasticsearch-head]# npm run start Ctrl+a Ctrl+d 将进程放置到后台,这里不懂的查下 screen 命令,很好使。
查看端口 只要 9100 被监听,说明启动成功。
浏览器访问:
妥了,安装成功。经过 elasticsearch-head 能够查看 elasticsearch 中的全部数据。目前就一个 node1 索引。若是要深刻学习 elasticsearch 推荐一本书《elasticsearch-the-definitive-guide-cn》 网上有 PDF 能够下载。
接下来,安装 kibana 。Kibana 和 elasticsearch安装到同一台主机的
Kibana 下载地址:https://www.elastic.co/cn/downloads/past-releases#kibana
直接rpm包安装
[root@192.168.118.14 ~/ELK]#yum localinstall kibana-6.8.2-x86_64.rpm -y
修改配置文件:
这里注意,若是将 kibana 端口修改成 80 ,这里是须要修改kibana启动用户为 root 由于普通用户是不能启动 1024 如下端口的。
修改启动配置文件:
[root@192.168.118.14 ~]#vim /etc/systemd/system/kibana.service User=root Group=root 再次启动服务 [root@192.168.118.14 ~]#systemctl daemon-reload [root@192.168.118.14 ~]#systemctl restart kibana
查看 80 端口若是被监听就说明启动成功。
根据规划,logstash 应该被安装到一台独立的主机上,logstash安装很是简单。
Logstash 下载地址:https://www.elastic.co/cn/downloads/past-releases#logstash
和上面同样,初始化工做不要忘记,这里就再也不描述了。
安装 jdk 也和上面同样的,jdk验证:
[root@192.168.118.15 /usr/local/src]#java -version java version "1.8.0_77" Java(TM) SE Runtime Environment (build 1.8.0_77-b03) Java HotSpot(TM) 64-Bit Server VM (build 25.77-b03, mixed mode)
安装 logstash
[root@192.168.118.15 ~]#yum localinstall logstash-6.8.2.rpm -y 将 logstash 命令添加到 PATH 环境变量中 [root@192.168.118.15 /etc/logstash]#vim /etc/profile.d/logstash.sh export PATH=/usr/share/logstash/bin:$PATH
Ok, 到这里已经安装完成,是否是很简单。
验证:
[root@192.168.118.15 ~]#logstash -e 'input { stdin {} } output { stdout{} }' 只要出现 Successfully started Logstash API endpoint {:port=>9600} 就表示启动成功。 你好,中国 { "@version" => "1", "message" => "你好,中国", "host" => "logstash-node1", "@timestamp" => 2019-09-14T04:14:35.035Z
测试经过,logstash验证成功。
Filebeat 下载地址:https://www.elastic.co/cn/downloads/past-releases#filebeat
首先安装 nginx 直接yum安装
[root@192.168.118.16 ~]#yum install nginx -y 启动nginx [root@192.168.118.16 ~]#nginx
安装 filebeat
[root@192.168.118.16 ~]#yum localinstall filebeat-6.8.2-x86_64.rpm -y 开启nginx模块 [root@192.168.118.16 ~]#cd /etc/filebeat/ [root@192.168.118.16 /etc/filebeat]#filebeat modules enable nginx Enabled nginx
修改 filebeat主配置文件:
[root@192.168.118.16 ~]#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.118.16:5044"]
注意这里的 hosts 要写 logstash 主机的 IP
修改 nginx 模块配置文件:
[root@192.168.118.16 ~]#vim /etc/filebeat/modules.d/nginx.yml
启动 filebeat 服务
[root@192.168.118.16 ~]#systemctl start filebeat Filebeat 服务是没有监听端口的,只要状态是 running 就表示启动成功,能够查看 filebeat 日志 /var/log/filebeat/filebeat
到此,ELK + filebeat 已经部署完毕,接下来就能够安装需求来进行调整和收集数据,而这一块的工做都集中在 logstash,所以 ELK 编写 logstash 才是难点。Logstash 配置语法,强力建议查看官方文档,很是全面了。
这里采用按部就班的方式展开,能够先写一个简单的测试。
编写一个将数据输出到屏幕的配置文件:
[root@192.168.118.15 /etc/logstash/conf.d]#vim test.conf
Logstash 能够根据配置文件来启动,启动方式以下:
[root@192.168.118.15 /etc/logstash/conf.d]#logstash -f test.conf 出现 Successfully started Logstash API endpoint 就表示启动成功。
启动成功后,咱们尝试访问 nginx 生成日志数据。
日志文件已经传输过来了,接下来就是把这些数据写入到 elasticsearch 中。
继续修改配置文件:
经过配置文件启动 logstash
[root@192.168.118.15 /etc/logstash/conf.d]#logstash -f test.conf
尝试访问 nginx 查看 elasticsearch-head中是否有新的索引被建立出来。
如上图,一个新的索引被建立出来,能够经过 elasticsearch-head 查看该索引中的数据。
目前已经将日志数据写入到 elasticsearch中了, 而后经过 kibana 展现出来,浏览器访问上面装好的 kibana
设置完成,直接点击 Discover
多访问几回nginx,查看日志是否展现出来。
ok,到此, ELK + filebeat 获取 nginx 日志就完成了。虽然将日志展现出来了, 可是这样杂乱无章的日志数据看着仍是很难受的,这就须要进一步的规整。
下一篇再写若是更详细的经过 logstash获取nginx日志数据,而后经过kibana展现更规整的数据及绘图。