暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

ELK环境部署

孤岛鱼夫 2020-08-18
271

ELK环境部署

【历史库存文档】

之前做的ELK的环境,只用了logstash作为收集日志的agent,而logstash消耗系统资源很大,在生产环境下将logstash作为agent和服务跑在一起并不理想,今天有空闲时间,在自己本地对原来的结构做了调整,收集日志的agent采用轻量的filebeat,并且中间使用redis进行数据写入的缓冲,整体架构如下图

主机规划:

  • 192.168.2.141 :部署应用 nginx, filebeat

  • 192.168.2.142 :部署redis

  • 192.168.2.143 :部署logstash

  • 192.168.2.144 :部署elasticsearch

  • 192.168.2.145 :部署kibana

部署nginx与filebeat

  • 安装nginx

  1. [root@nginx-filebeat ~]# yum install nginx -y

  • 安装filebeat

  1. [root@nginx-filebeat sowft]# yum install filebeat-5.6.10-x86_64.rpm -y

  • 查看filebeat安装后的相关路径

  1. [root@nginx-filebeat sowft]# rpm -ql filebeat

  • 配置filebeat,将日志输出到redis

/etc/filebeat/filebeat.full.yml
文件中有对redis配置的内容,将里面的对应内容复制添加到 /etc/filebeat/filebeat.yml
中,如下配置

  1. #------------------------------- Redis output ----------------------------------

  2. output.redis:

  3. # Boolean flag to enable or disable the output module.

  4. enabled: true


  5. # The list of Redis servers to connect to. If load balancing is enabled, the

  6. # events are distributed to the servers in the list. If one server becomes

  7. # unreachable, the events are distributed to the reachable servers only.

  8. hosts: ["192.168.2.142:6379"]


  9. # The Redis port to use if hosts does not contain a port number. The default

  10. # is 6379.

  11. port: 6379


  12. # The name of the Redis list or channel the events are published to. The

  13. # default is filebeat.

  14. key: filebeat


  15. # The password to authenticate with. The default is no authentication.

  16. #password:


  17. # The Redis database number where the events are published. The default is 0.

  18. db: 0


  19. # The Redis data type to use for publishing events. If the data type is list,

  20. # the Redis RPUSH command is used. If the data type is channel, the Redis

  21. # PUBLISH command is used. The default value is list.

  22. datatype: list

部署redis

  1. [root@redis-server ~]# yum install reids -y

  • 修改监听端口,其他设置可以暂不修改

  1. bind 0.0.0.0

安装logstash

  • 安装jdk

  1. yum install java-1.8.0-openjdk-devel.x86_64 -y

  • 安装logstash

  1. [root@logstash logstash]# yum localinstall logstash-5.6.10.rpm -y

  • 将logstash指令加入环境变量

  1. [root@logstash ~]# vim /etc/profile.d/logstash.sh



  2. export PATH=/usr/share/logstash/bin:$PATH

  1. [root@logstash ~]# source /etc/profile.d/logstash.sh

  • 修改vim /etc/logstash/jvm.options文件

  1. -Xms1g

  2. -Xmx1g

  3. #两个值保持一样,如果不一样logstash无法传输文档到ESserver

  • 测试logstash正确性

  1. [root@logstash ~]# cd /etc/logstash/conf.d

  2. [root@logstash conf.d]# vim test.conf


  3. input {

  4. stdin{}


  5. }


  6. output {

  7. stdout{}

  8. }

  • 执行测试指令

  1. [root@logstash conf.d]# logstash -f ./test.conf -t


  2. Configuration OK

logstash上的配置文件暂时可以不配置,等部署完elasticsearch后在配置logstash的过滤规则,这里只做正确性测试即可

部署elasticsearch

  • 安装jdk

  1. [root@elastic ~]# yum install java-1.8.0-openjdk-devel.x86_64 -y

  • 安装elasticsearch

  1. [root@elastic opt]# yum install elasticsearch-5.6.10.rpm -y

  • 修改配置

  1. [root@elastic ~]# vim /etc/elasticsearch/elasticsearch.yml


  2. cluster.name: my-application

  3. node.name: node-1

  4. path.data: /elk/data

  5. path.logs: /elk/logs

  6. network.host: 192.168.2.144

  7. http.port: 9200

  • 创建日志和数据目录

  1. [root@elastic ~]# mkdir -p /elk/{data,logs}

  • 将目录属主改为elasticsearch用户

  1. [root@elastic ~]# chown -R elasticsearch.elasticsearch /elk/

  • 启动服务

  1. [root@elastic ~]# systemctl start elasticsearch

在nginx应创建测试数据访问

  • 创建测试页面

  1. [root@nginx-filebeat html]# for i in {1..150};do echo test$i > test$i.html;done

  • 模拟不同地址访问生成日志

  1. [root@nginx-filebeat ~]# while true; do ip=$[$RANDOM%223+1]; curl --header "X-Forwarded-For: $ip.33.22.100" http://192.168.2.141/test$ip.html; sleep 2;done

以上的配置可以自动测试访问nginx,并生成不同的ip地址段

配置logstash过滤规则

  1. input {


  2. # file {

  3. #

  4. # start_position => "end"

  5. # path => ["/var/log/nginx/access.log"]

  6. #

  7. # }

  8. # 以上file段配置是针对logstash作为agent端收集日志的场景使用,从本地文件输入


  9. redis {

  10. host => "192.168.2.142"

  11. port => "6379"

  12. key => "filebeat" #这里的key要与filebeat.yml中定义的key相同

  13. data_type => "list"

  14. # threads => "5"

  15. db => "0"

  16. }


  17. }


  18. filter {

  19. grok {

  20. match => { "message" => "%{HTTPD_COMBINEDLOG} \"%{DATA:realclient}\"" }

  21. remove_field => "message"


  22. }

  23. # \"%{DATA:realclient}\" 可以记录nginx的真实客户端地址


  24. date {

  25. match => [ "timestamp","dd/MMM/YYYY:H:m:s Z" ]

  26. remove_field => "timestamp"

  27. }



  28. }


  29. output {

  30. # elasticsearch { #输出到elasticsearch服务器,先输出到本地终端查看,没有问题后输出到elasticsearch

  31. # hosts => "192.168.2.144:9200"

  32. # index => "logstash-%{+YYYY.MM.dd}"

  33. # document_type => "nginx_logs"

  34. # }


  35. stdout { #输出到屏幕测试

  36. codec => rubydebug

  37. }

  38. }

  • 以上的logstash的日志输出格式如下:

  1. {

  2. "request" => "/test1.html",

  3. "agent" => "\"curl/7.29.0\"",

  4. "offset" => 11699,

  5. "auth" => "-",

  6. "ident" => "-",

  7. "input_type" => "log",

  8. "verb" => "GET",

  9. "source" => "/var/log/nginx/access.log",

  10. "type" => "log",

  11. "referrer" => "\"-\"",

  12. "@timestamp" => 2018-09-27T08:35:35.000Z,

  13. "realclient" => "1.33.22.100",

  14. "response" => "200",

  15. "bytes" => "6",

  16. "clientip" => "192.168.2.141",

  17. "beat" => {

  18. "name" => "nginx-filebeat",

  19. "hostname" => "nginx-filebeat",

  20. "version" => "5.6.10"

  21. },

  22. "@version" => "1",

  23. "httpversion" => "1.1"

  24. }

  • 把配置文件中的配置output输出对象改为输出到elasticsearch

  1. output {

  2. elasticsearch {

  3. hosts => "192.168.2.144:9200"

  4. index => "logstash-%{+YYYY.MM.dd}"

  5. document_type => "nginx_logs"

  6. }


  7. # stdout { #输出到屏幕测试

  8. # codec => rubydebug

  9. # }

  10. }

注意:/etc/logstash/conf.d/ 目录下的所有文件都会被加载,不仅仅是以.conf结尾的文件,所以正常情况下该目录只存在一个确定使用的配置文件,其他文件都不要放在这

  • 启动logstash服务

  1. systemctl start logstash

  • 查看elasticsearch服务生成的数据文件,实时访问服务,看看数据文件大小是否变化

  1. [root@logstash ~]# curl -XGET 192.168.2.144:9200/_cat/indices

  2. yellow open logstash-2018.09.27 vOhS-Hq7RG-oAYTUJng91Q 5 1 93 0 560.7kb 560.7kb


  3. [root@logstash ~]# curl -XGET 192.168.2.144:9200/_cat/indices

  4. yellow open logstash-2018.09.27 vOhS-Hq7RG-oAYTUJng91Q 5 1 106 0 651.2kb 651.2kb


  5. #最开头以yellow开头说明配置项有点小问题。

  6. #这个地方以红黄蓝颜色标记数据传输状态。

  7. #黄色是缺失索引文件副本,ESserver是以集群的方式工作的,但这里是单节点

部署配置kibana

  1. [root@kibana opt]# yum localinstall kibana-5.6.1-x86_64.rpm -y

  • 配置

  1. server.port: 5601

  2. server.host: "0.0.0.0"

  3. server.name: "kibana"

  4. elasticsearch.url: "http://192.168.2.144:9200"

  • 启动

  1. [root@kibana ~]# systemctl start kibana

登录Kibana只需在浏览器数据kibana服务器地址+5601端口即可,经过访问测试,以上配置登录kibana调取日志访问成功


配置用户的ip归属地

ip地址的归属地查询需要使用互联网上的地图的解析库,地图的解析库会定期地更新,下载到服务器本地,解压,在logstash的配置文件中指定解压后的 .mmdb
后缀的文件路径

地图解析库下载地址:https://dev.maxmind.com/geoip/geoip2/geolite2/

  • logstash的配置文件添加 geoip段,最终配置段如下:

  1. redis {

  2. host => "192.168.2.142"

  3. port => "6379"

  4. key => "filebeat"

  5. data_type => "list"

  6. # threads => "5"

  7. db => "0"

  8. }


  9. }


  10. filter {

  11. grok {

  12. match => {

  13. "message" => "%{HTTPD_COMBINEDLOG} \"%{DATA:realclient}\""


  14. }


  15. remove_field => "message"


  16. }


  17. date {

  18. match => [ "timestamp","dd/MMM/YYYY:H:m:s Z" ]

  19. remove_field => "timestamp"

  20. }



  21. geoip { #ip地址归属配置段


  22. source => "realclient"

  23. target => "geoip"

  24. database => "/home/city/GeoLite2-City_20180925/GeoLite2-City.mmdb"

  25. }


  26. }


  27. output {

  28. elasticsearch {

  29. hosts => "192.168.2.144:9200"

  30. index => "logstash-%{+YYYY.MM.dd}"

  31. document_type => "nginx_logs"

  32. }


  33. # 注意:

  34. # 1、输出的日志文件名必须以“logstash-”开头,方可将geoip.location的type自动设定为"geo_point";

  35. # 2、target => "geoip"


  36. # stdout { #输出到屏幕调试

  37. # codec => rubydebug

  38. # }

  39. }


至此,基于filebeat+redis+logstash+elasticsearch+kibana的日志收集系统部署完成


文章转载自孤岛鱼夫,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论