elasticsearch+kibana+fluentd 日誌蒐集集羣搭建

使用fluentd來蒐集Nginx日誌,準備3臺服務器,列表以下java

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.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>

6.在其餘兩個節點修改td-agent配置並啓動

 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>

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檢測關鍵文件(如二進制文件和配置文件)的更改,並識別潛在的安全策略違規。

 

參考:

https://yq.aliyun.com/articles/484403

https://blog.csdn.net/hxpjava1/article/details/80338844

相關文章
相關標籤/搜索