goodstudy 2020-01-02
上一篇:ELK 架构之 Elasticsearch 和 Kibana 安装配置
阅读目录:
上一篇主要说的是 Elasticsearch 和 Kibana 安装配置,以及服务追踪数据的处理和展示,日志数据采集使用的 Spring Cloud Sleuth Zipkin + Stream/RabbitMQ 中间件(Service 端配置),然后 Zipkin Server 从队列中获取日志数据,再使用 HTTP 的请求的方式,传输并存储到 Elasticsearch 中,最后 Kibana 进行日志数据展示。
在 ELK Stack 中,日志数据采集有单独的工具,就是 Logstash 和 Beats。
目前 Beats 包含六种工具:
本篇文章目的:
第三种方案的实现架构图:
服务器环境:Centos 7.0(单机版)
Elasticsearch 和 Logstash 需要 Java,Elasticsearch 推荐的版本为 Java 8,安装教程:确定稳定的 Spring Cloud 相关环境版本
另外,我们需要修改下服务器主机信息:
[ ~]# vi /etc/hostname node1 [ ~]# vi /etc/hosts 192.168.0.11 node1 127.0.0.1 node1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 node1 localhost localhost.localdomain localhost6 localhost6.localdomain6
运行以下命令将 Elasticsearch 公共 GPG 密钥导入 rpm:
[ ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
在/etc/yum.repos.d/
目录中,创建一个名为logstash.repo
的文件,添加下面配置:
[logstash-5.x] name=Logstash repository for 5.x packages baseurl=https://artifacts.elastic.co/packages/5.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md
然后安装 Logstash:
[ ~]# yum makecache && yum install logstash -y 已加载插件:fastestmirror base | 3.6 kB 00:00:00 elasticsearch-5.x | 1.3 kB 00:00:00 extras | 3.4 kB 00:00:00 kibana-5.x | 1.3 kB 00:00:00 logstash-5.x | 1.3 kB 00:00:00 rabbitmq-erlang | 1.3 kB 00:00:00 updates | 3.4 kB 00:00:00 Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.163.com 元数据缓存已建立 已加载插件:fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.163.com * extras: mirrors.163.com * updates: mirrors.163.com 正在解决依赖关系 --> 正在检查事务 ---> 软件包 logstash.noarch.1.5.6.9-1 将被 安装 --> 解决依赖关系完成 依赖关系解决 ===================================================================================================== Package 架构 版本 源 大小 ===================================================================================================== 正在安装: logstash noarch 1:5.6.9-1 elasticsearch-5.x 98 M 事务概要 ===================================================================================================== 安装 1 软件包 总下载量:98 M 安装大小:188 M Downloading packages: logstash-5.6.9.rpm | 98 MB 00:06:38 Running transaction check Running transaction test Transaction test succeeded Running transaction 正在安装 : 1:logstash-5.6.9-1.noarch 1/1 Using provided startup.options file: /etc/logstash/startup.options /usr/share/logstash/vendor/jruby/bin/jruby:行388: /usr/bin/java: 没有那个文件或目录 Unable to install system startup script for Logstash. 验证中 : 1:logstash-5.6.9-1.noarch 1/1 已安装: logstash.noarch 1:5.6.9-1 完毕!
安装报错,具体问题:/usr/share/logstash/vendor/jruby/bin/jruby:行388: /usr/bin/java: 没有那个文件或目录
。
解决方案:安装 Logstash 提示 /usr/bin/java: No such file or directory
对 Java 配置软链接,链接到/usr/bin/java
:
[ ~]# ln -s /usr/local/java/bin/java /usr/bin/java
然后卸载 Logstash:
[ ~]# yum -y remove logstash
重新安装:
[ ~]# yum makecache && yum install logstash -y
设置开机启动,以及启动系统服务:
[ ~]# sudo /bin/systemctl daemon-reload [ ~]# sudo /bin/systemctl enable logstash.service
先不急启动 Logstash,需要先配置下 Logstash。
采集日志数据,需要有个数据源,这里我们使用 rsyslog 进行测试。
Linux 日志机制的核心是 rsyslog 守护进程,该服务负责监听 Linux下 的日志信息,并把日志信息追加到对应的日志文件中,一般在 /var/log 目录下。 它还可以把日志信息通过网络协议发送到另一台 Linux 服务器上,或者将日志存储在 MySQL 或 Oracle 等数据库中。
修改 rsyslog 配置:
[ ~]# vi /etc/rsyslog.conf # remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional *.* @@192.168.1.40:10514
重启 rsyslog:
[ ~]# systemctl restart rsyslog
增加配置文件:
[ ~]# vi /etc/logstash/conf.d/syslog.conf input { syslog { type => "system-syslog" port => 10514 } } output { elasticsearch { hosts => ["node1:9200"] # 定义es服务器的ip index => "system-syslog-%{+YYYY.MM}" # 定义索引 } }
验证配置文件,是否有效:
[ ~]# cd /usr/share/logstash/bin [ bin]# ./logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/syslog.conf --config.test_and_exit Sending Logstash‘s logs to /var/log/logstash which is now configured via log4j2.properties Configuration OK
启动 Logstash:
[ ~]# systemctl start logstash
查看启动日志:
[ ~]# cat /var/log/logstash/logstash-plain.log [2018-05-15T08:19:05,578][FATAL][logstash.runner ] An unexpected error occurred! {:error=>#<ArgumentError: Path "/var/lib/logstash/queue" must be a writable directory. It is not writable.>, :backtrace=>["/usr/share/logstash/logstash-core/lib/logstash/settings.rb:439:in `validate‘", "/usr/share/logstash/logstash-core/lib/logstash/settings.rb:222:in `validate_value‘", "/usr/share/logstash/logstash-core/lib/logstash/settings.rb:138:in `validate_all‘", "org/jruby/RubyHash.java:1342:in `each‘", "/usr/share/logstash/logstash-core/lib/logstash/settings.rb:137:in `validate_all‘", "/usr/share/logstash/logstash-core/lib/logstash/runner.rb:243:in `execute‘", "/usr/share/logstash/vendor/bundle/jruby/1.9/gems/clamp-0.6.5/lib/clamp/command.rb:67:in `run‘", "/usr/share/logstash/logstash-core/lib/logstash/runner.rb:204:in `run‘", "/usr/share/logstash/vendor/bundle/jruby/1.9/gems/clamp-0.6.5/lib/clamp/command.rb:132:in `run‘", "/usr/share/logstash/lib/bootstrap/environment.rb:71:in `(root)‘"]}
具体错误:Path "/var/lib/logstash/queue" must be a writable directory. It is not writable
解决方案:http://www.cnblogs.com/szk5043/articles/8652677.html
执行命令:
[ ~]# chown -R logstash /var/log/logstash /var/lib/logstash
重新启动 Logstash:
[ ~]# systemctl restart logstash
查看 Logstash 启动状态:
[ ~]# systemctl status logstash logstash.service - logstash Loaded: loaded (/etc/systemd/system/logstash.service; enabled) Active: active (running) since 二 2018-05-15 08:23:37 CEST; 1min 4s ago Main PID: 10855 (java) CGroup: /system.slice/logstash.service └─10855 /usr/bin/java -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupan... 5月 15 08:23:37 node1 systemd[1]: Starting logstash... 5月 15 08:23:37 node1 systemd[1]: Started logstash. 5月 15 08:23:46 node1 logstash[10855]: Sending Logstash‘s logs to /var/log/logstash which is ...ties Hint: Some lines were ellipsized, use -l to show in full.
查看端口监听(是否正常):
[ ~]# netstat -lntp |grep 9600 tcp6 0 0 127.0.0.1:9600 :::* LISTEN 10855/java [ ~]# netstat -lntp |grep 10514 tcp6 0 0 :::10514 :::* LISTEN 10855/java
Logstash 的监听 IP 是127.0.0.1
这个本地 IP,本地 IP 无法远程通信,所以需要修改一下配置文件,配置一下监听的 IP(需要设置 IP 地址,不能设置 Hostname):
[ ~]# vi /etc/logstash/logstash.yml http.host: "192.168.1.40"
重新启动 Logstash:
[ ~]# systemctl restart logstash
查看端口监听(是否正常):
[ ~]# netstat -lntp |grep 9600 tcp6 0 0 192.168.1.40:9600 :::* LISTEN 11214/java
查看 Elasticsearch 索引列表,可以看到生成了system-syslog-*
类型的日志数据:
[ ~]# curl http://node1:9200/_cat/indices?v health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open twitter k1KnzWyYRDeckjt7GASh8w 5 1 1 0 5.2kb 5.2kb yellow open .kibana 8zJGQkq8TwC4s3JJLMX44g 1 1 5 0 27.1kb 27.1kb yellow open system-syslog-2018.05 0aKfJeOmTZ6yen9eSlCOig 5 1 32 0 201.9kb 201.9kb yellow open zipkin:span-2018-05-15 8vnm9SJvSlK-cAtakK8VeQ 3 1 9 0 47.3kb 47.3kb yellow open zipkin:span-2018-05-14 TZ-MmJpoSs-NwjHjxEvhFQ 3 1 219 0 127.1kb 127.1kb yellow open student iZPqPcwrQbifGOfE9DQYvg 5 1 0 0 955b 955b
然后在 Kibana 上面创建system-syslog-*
索引:
创建好之后,就可以看到整个日志数据了:
其实这里显示的日志数据就是/var/log/messages
文件里的数据,因为 Logstash 里配置的就是收集messages
文件里的数据。
以上这就是如何使用 Logstash 收集系统日志,输出到 Elasticsearch 服务器上,并在 Kibana 的页面上进行查看。
运行以下命令将 Elasticsearch 公共 GPG 密钥导入 rpm:
[ ~]# rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
在/etc/yum.repos.d/
目录中,创建一个名为elastic.repo
的文件,添加下面配置:
[elastic-5.x] name=Elastic repository for 5.x packages baseurl=https://artifacts.elastic.co/packages/5.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md
安装 Filebeat:
[ ~]# yum makecache && yum install filebeat -y
设置开机启动,以及启动系统服务:
[ ~]# sudo /bin/systemctl daemon-reload [ ~]# sudo /bin/systemctl enable filebeat.service
编辑配置文件:
[ ~]# vi /etc/filebeat/filebeat.yml filebeat.prospectors: - type: log paths: - /var/log/messages # 指定需要收集的日志文件的路径 output.elasticsearch: # Array of hosts to connect to. hosts: ["node:9200"] # 配置 Elasticsearch 服务器的 IP 地址
启动 Filebeat 服务:
[ ~]# systemctl start filebeat
查看 Filebeat 服务状态:
[ ~]# systemctl status filebeat filebeat.service - filebeat Loaded: loaded (/usr/lib/systemd/system/filebeat.service; enabled) Active: active (running) since 二 2018-05-15 09:29:44 CEST; 1min 12s ago Docs: https://www.elastic.co/guide/en/beats/filebeat/current/index.html Main PID: 12332 (filebeat) CGroup: /system.slice/filebeat.service └─12332 /usr/share/filebeat/bin/filebeat -c /etc/filebeat/filebeat.yml -path.home /usr/... 5月 15 09:29:44 node1 systemd[1]: Started filebeat.
查看 Elasticsearch 索引列表,可以看到生成了filebeat-*
类型的日志数据:
[ ~]# curl http://node1:9200/_cat/indices?v health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open twitter k1KnzWyYRDeckjt7GASh8w 5 1 1 0 5.2kb 5.2kb yellow open .kibana 8zJGQkq8TwC4s3JJLMX44g 1 1 6 0 33.3kb 33.3kb yellow open system-syslog-2018.05 0aKfJeOmTZ6yen9eSlCOig 5 1 689 0 652.3kb 652.3kb yellow open filebeat-2018.05.15 K852lKlgSaKG6-hE_GNB4w 5 1 27417 0 6.7mb 6.7mb yellow open zipkin:span-2018-05-15 8vnm9SJvSlK-cAtakK8VeQ 3 1 9 0 47.3kb 47.3kb yellow open zipkin:span-2018-05-14 TZ-MmJpoSs-NwjHjxEvhFQ 3 1 219 0 127.1kb 127.1kb yellow open student iZPqPcwrQbifGOfE9DQYvg 5 1 0 0 955b 955b
然后在 Kibana 上面创建filebeat-*
索引:
创建好之后,就可以看到整个日志数据了:
和上面 Logstash 收集 rsyslog 日志数据一样,Filebeat 配置的,也是收集的/var/log/messages
日志数据。
可以看到,system-syslog
和filebeat-
索引对应的日志数据,是同步更新的(如果没效果,重启下 Logstash 和 Filebeat)。
先停止 Logstash 和 Filebeat:
[ ~]# systemctl stop logstash && systemctl stop filebeat
然后删除上面产生的索引日志数据:
[ ~]# curl -XDELETE http://node1:9200/system-syslog-2018.05 && curl -XDELETE http://node1:9200/filebeat-2018.05.15
创建新的 Logstash 配置文件:
[ ~]# vi /etc/logstash/conf.d/logstash-filebeat-syslog.conf input { beats { port => 10515 } } filter { if [type] == "syslog" { grok { match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" } add_field => [ "received_at", "%{@timestamp}" ] add_field => [ "received_from", "%{host}" ] } syslog_pri { } date { match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ] } } } output { elasticsearch { hosts => [ "node1:9200" ] index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}" } }
验证 Logstash 配置文件,是否有效:
[ ~]# cd /usr/share/logstash/bin [ bin]# ./logstash --path.settings /etc/logstash/ -f /etc/logstash/conf.d/logstash-filebeat-syslog.conf --config.test_and_exit Sending Logstash‘s logs to /var/log/logstash which is now configured via log4j2.properties Configuration OK
编辑 Filebeat 配置文件(注释掉output.elasticsearch
):
[ ~]# vi /etc/filebeat/filebeat.yml filebeat.prospectors: - type: log paths: - /var/log/messages # 指定需要收集的日志文件的路径 fields: log_type: syslog output.logstash: # The Logstash hosts hosts: ["node1:10515"]
重新启动 Logstash 和 Filebeat:
[ ~]# systemctl restart logstash && systemctl restart filebeat
查看 Logstash 是否正常监听:
[ conf.d]# netstat -lnp|grep 10515 tcp6 0 0 :::10515 :::* LISTEN 22384/java
可以看到生成了filebeat-*
类型的日志数据(system-syslog-2018.05
索引日志数据,是由 Logstash 的syslog.conf
配置文件采集的):
[ ~]# curl http://node1:9200/_cat/indices?v health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open twitter k1KnzWyYRDeckjt7GASh8w 5 1 1 0 5.2kb 5.2kb yellow open .kibana 8zJGQkq8TwC4s3JJLMX44g 1 1 7 1 57.4kb 57.4kb yellow open system-syslog-2018.05 dzRXf3eFQ_-hKqlGILTSJg 5 1 223 0 511.9kb 511.9kb yellow open zipkin:span-2018-05-15 8vnm9SJvSlK-cAtakK8VeQ 3 1 49 0 142.6kb 142.6kb yellow open filebeat-2018.05.15 eEuumW8pTdeXa7Ny1gLIPQ 5 1 177 0 300.2kb 300.2kb yellow open zipkin:span-2018-05-14 TZ-MmJpoSs-NwjHjxEvhFQ 3 1 219 0 127.1kb 127.1kb yellow open student iZPqPcwrQbifGOfE9DQYvg 5 1 0 0 955b 955b
因为之前创建了filebeat-*
和system-syslog-*
索引,这边我们可以直接查看日志数据了:
以上是使用 ELK Logstash 和 Filebeat 采集日志数据的过程(使用 rsyslog 日志数据源),后面需要将 ELK 集成到 Spring Cloud/Boot 中,用作整个微服务的集中日志采集处理中心。
参考资料: