使用fluentd来搜集Nginx日志,准备3台服务器,列表以下java
node1 | elasticsearch/kibana/td-agent |
node2 | td-agent/nginx |
node3 | td-agent/nginx |
http://packages.treasuredata.com.s3.amazonaws.com/3/redhat/7/x86_64/td-agent-3.4.1-0.el7.x86_64.rpm
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-x86_64.rpm
https://artifacts.elastic.co/downloads/kibana/kibana-7.2.0-x86_64.rpm
yum install -y elasticsearch-7.2.0-x86_64.rpm kibana-7.2.0-x86_64.rpm
yum install -y td-agent-3.4.1-0.el7.x86_64.rpm
td-agent-gem install fluent-plugin-elasticsearch
vim /etc/td-agent/td-agent.confnode
#td-agent.conf <system> workers 10 </system> @include /etc/td-agent/conf.d/*.conf
vim /etc/td-agent/conf.d/source.confnginx
<source> @type forward port 24224 bind 0.0.0.0 </source>
vim /etc/td-agent/conf.d/match.conf redis
#match.conf <match nginx.access> @type elasticsearch host localhost port 9200 #user elastic #若是有安全认证则须要设置 #password password logstash_format true logstash_prefix fluentd.${tag} # index名称 </match>
vim /etc/td-agent/td-agent.confsql
#td-agent.conf <system> workers 1 </system> @include /etc/td-agent/conf.d/*.conf
vim /etc/td-agent/conf.d/source.conf express
#source.conf <source> @type tail path /var/log/openresty/*access.log
path_key file_path tag nginx.access pos_file /var/log/td-agent/nginx-access.log.pos # parse <parse> @type regexp #根据本身的Nginx日志规则调整 expression /^(?<remote_addr>[^ ]*) (?<white>[^ ]*) (?<remote_user>[^ ]*) \[(?<time_local>[^\]]*)\] "(?<method>\S+)(?: +(?<url_path>[^\"][A-Za-z0-9$.+!*'(){},~:;=@#%&_\-\/]*)(?:\?(?<url_param>[^\"]*))? +\S*)?" (?<response_code>[^ ]*) (?<body_bytes_sent>[^ ]*)(?: "(?<http_referer>[^\"]*)" "(?<http_user_agent>[^\"]*)" "(?<http_x_forwarded_for>[^\"]*)" "(?<request_body>[^\"]*)" "(?<upstream_addr>[^\"]*)" "(?<uri>[^\"]*)" "(?<upstream_response_time>[^\"]*)" "(?<upstream_http_name>[^\"]*)" "(?<upstream_http_host>[^\"]*)" "(?<upstream_cache_status>[^\"]*)" "(?<request_time>[^\"]*)")?/ types body_bytes_sent:integer,request_time:float,upstream_response_time:float time_format %d/%b/%Y:%H:%M:%S %z </parse> </source>
vim /etc/td-agent/conf.d/filter.confvim
#filter.conf <filter nginx.access> @type record_transformer <record> hostname "#{Socket.gethostname}" tag ${tag} </record> </filter>
vim /etc/td-agent/conf.d/match.conf安全
#match.conf <match nginx.access> @type forward send_timeout 60s recover_wait 10s hard_timeout 60s <server> name td-0 host node1 port 24224 weight 60 </server>
#配置主备
#<server>
# name td-1
# host node2
# port 24224
# weight 60
#</server> </match>
X-Pack是一个Elastic Stack的扩展,将安全,警报,监视,报告和图形功能包含在一个易于安装的软件包中服务器
在6.3版本及以后,已经默认集成,无需额外安装,基础安全属于付费黄金版内容。从7 .1版本开始,基础安全免费。网络
修改/etc/elasticsearch/elasticsearch.yml开启安全认证
xpack.security.enabled: true xpack.security.transport.ssl.enabled: true
设置密码
/usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive
修改/etc/kibana/kibana.yml
elasticsearch.username: "elastic" elasticsearch.password: "yourpassword"
重启es、kibana服务后,须要认证
登陆后在management->users能够查看全部的用户
logstash和filebeat:
都具备日志收集功能,filebeat更轻量,占用资源更少,但logstash 具备filter功能,能过滤分析日志。通常结构都是filebeat采集日志,而后发送到消息队列,redis,kafaka。而后logstash去获取,利用filter功能过滤分析,而后存储到elasticsearch中。
Curator:
是elasticsearch 官方的一个索引管理工具,能够删除、建立、关闭、段合并等等功能。
Cerebro:
是一款es比较好用的开源监控软件,主要是scala写的,修改源码方便。能够经过修改源码屏蔽掉一些像DELETE等危险操做。从而给更多人包括运维人员使用。
Bigdesk:
是elasticsearch的一个集群监控工具,能够经过它来查看es集群的各类状态,如:cpu、内存使用状况,索引数据、搜索状况,http链接数等。
Metricbeat:
是一个轻量级代理,在服务器上安装,以按期从操做系统和服务器上运行的服务收集指标。Metricbeat提供多种内部模块,用于从服务中收集指标,例如 Apache、NGINX、MongoDB、MySQL、PostgreSQL、Prometheus、Redis 等等。
Packetbeat:
抓取网路包数据
自动解析网络包协议,如: ICMP DNS、HTTP、Mysql/PgSQL/MongoDB、Memcache、Thrift、TLS等。
Heartbeat:
是一个轻量级守护程序,能够安装在远程服务器上,按期检查服务状态并肯定它们是否可用。与Metricbeat不一样,Metricbeat仅肯定服务器是启动仍是关闭,Heartbeat会确认服务是否可访问。
Marvel:
工具能够帮助使用者监控elasticsearch的运行状态,不过这个插件须要收费,只有开发版是免费,咱们学习不影响咱们使用。它集成了head以及bigdesk的功能,是官方推荐产品。
Auditbeat:
是一个轻量级代理,能够在审核服务器系统上用户和进程的活动。 例如,可使用Auditbeat从Linux Audit Framework收集和集中审核事件,还可使用Auditbeat检测关键文件(如二进制文件和配置文件)的更改,并识别潜在的安全策略违规。
参考: