使用fluentd來搜集Nginx日志,准備3台服務器,列表如下
node1 | elasticsearch/kibana/td-agent |
node2 | td-agent/nginx |
node3 | td-agent/nginx |
1.下載相關軟件
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
2.在node1安裝elasticsearch和kibana並啟動
yum install -y elasticsearch-7.2.0-x86_64.rpm kibana-7.2.0-x86_64.rpm
3.在每台機器上安裝td-agent
yum install -y td-agent-3.4.1-0.el7.x86_64.rpm
4.在node1安裝elasticsearch插件
td-agent-gem install fluent-plugin-elasticsearch
5.在node1修改td-agent配置並啟動
vim /etc/td-agent/td-agent.conf
#td-agent.conf <system> workers 10 </system> @include /etc/td-agent/conf.d/*.conf
vim /etc/td-agent/conf.d/source.conf
<source> @type forward port 24224 bind 0.0.0.0 </source>
vim /etc/td-agent/conf.d/match.conf
#match.conf <match nginx.access> @type elasticsearch host localhost port 9200 #user elastic #如果有安全認證則需要設置 #password password logstash_format true logstash_prefix fluentd.${tag} # index名稱 </match>
6.在其他兩個節點修改td-agent配置並啟動
vim /etc/td-agent/td-agent.conf
#td-agent.conf <system> workers 1 </system> @include /etc/td-agent/conf.d/*.conf
vim /etc/td-agent/conf.d/source.conf
#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.conf
#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>
7.安裝X-PACK實現安全認證
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可以查看所有的用戶
8.其他插件介紹
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檢測關鍵文件(如二進制文件和配置文件)的更改,並識別潛在的安全策略違規。
參考: