filebeat->redis->logstash->elasticsearch->kibana

總體流程

  1. filebeat收集openresty應用日誌傳輸到Redis集羣中
  2. Logstash從Redis集羣中拉取數據,並傳輸到Elasticsearch集羣
  3. 使用Kibana可視化索引
  4. 使用Elasticsearch-head管理lasticsearch集羣

注:Logstash不支持集羣模式html

環境

均爲CentOS 7.4 x64系統java

openresty 192.168.0.10 1.15.8版本 filebeat 192.168.0.10 7.3.0版本 Redis集羣 192.168.0.11 6381-6386端口(暫採用僞集羣的方式) 5.0.5版本 Logstash 192.168.0.12 7.3.0版本 Elasticsearch集羣 192.168.0.13-15 7.3.0版本 Kibana 192.168.0.16 7.3.0版本 Elasticsearch-head 192.168.0.17linux

軟件下載地址 filebeat|Logstash|Elasticsearch|Kibana:https://www.elastic.co/cn/downloads/past-releases#elasticsearch Elasticsearch-head:https://github.com/mobz/elasticsearch-head Redis:https://redis.io/downloadnginx

Redis集羣開啓外部訪問,設置訪問密碼,防火牆放行端口git

配置

filebeat配置

安裝

軟件採用經過下載rpm包的方式進行安裝github

rpm -ivh filebeat-7.3.0-x86_64.rpm
systemctl daemon-reload
systemctl enable filebeat.service
systemctl start filebeat.service

修改filebeat.yml配置文件

openresty日誌文件路徑: access:/usr/local/openresty/nginx/logs/host.access.log error:/usr/local/openresty/nginx/logs/error.logweb

文件路徑:/etc/filebeat/filebeat.ymlredis

註銷其餘的output輸出shell

filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /usr/local/openresty/nginx/logs/host.access.log
  fields:
    log_source: access

- type: log
  enabled: true
  paths:
    - /usr/local/openresty/nginx/logs/error.log
  fields:
    log_source: error

output.redis:
  hosts: ["192.168.0.11:6381","192.168.0.11:6382","192.168.0.11:6383","192.168.0.11:6384","192.168.0.11:6385","192.168.0.11:6386"]
  password: foobar2000
  db: 0
  key: "log_list"

還有一些其餘的優化參數,後期須要再添加數據庫

測試

重啓filebeat後,使用瀏覽器訪問網址,確保先生成的有access和error日誌,而後在Redis集羣的第0庫查看 會發現有一個key是log_list,值是list的數據,裏面一行一行的數據就是收集過來的日誌,這些日誌加的有一些元數據 具體能夠查看本博客的另外一篇文章《使用filebeat收集日誌傳輸到redis的各類效果展現》,這裏面有詳細的講解。

Logstash配置

安裝

  1. 配置java環境
cd /usr/local/src
tar -zxv -f openjdk-12_linux-x64_bin.tar.gz  -C /usr/local/
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/local/jdk-12
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

source /etc/profile.d/java.sh
  1. 安裝logstash

軟件採用經過下載rpm包的方式進行安裝

rpm -ivh logstash-7.3.0.rpm
systemctl daemon-reload
systemctl enable logstash.service
systemctl start logstash.service

修改啓動文件,確保能找到java環境變量 文件路徑:/usr/share/logstash/bin/logstash.lib.sh 在行首加入以下信息

export JAVA_HOME=/usr/local/jdk-12
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

msg.conf配置文件修改

logstash默認的配置文件時在/etc/logstash/conf.d/目錄下建立以conf結尾的文件,本例中建立的是msg.conf 文件路徑:/etc/logstash/conf.d/msg.conf 該文件內容以下

input {
  
  redis {
  	# 若報錯該redis不能訪問,則不使用Redis集羣,只使用一臺redis,須要修改filebeat.yml文件
    host => "192.168.0.11"
    port => 6381
    password => foobar2000
    data_type => "list"
    key => "log_list" 
    db => 0 
  }
  
}

output {
  # 根據filebeat.yml中設置的不一樣日誌來源進行區分,從而建立不一樣的索引
  if [fields][log_source] == 'access' {
    elasticsearch {
      hosts => ["http://192.168.0.13:9200","http://192.168.0.14:9200","http://192.168.0.15:9200"]
      index => "filebeat-access-%{+YYYY.MM.dd}"
      id => "access_id"
    }
  } 

  if [fields][log_source] == 'error' {
    elasticsearch {
      hosts => ["http://192.168.0.13:9200","http://192.168.0.14:9200","http://192.168.0.15:9200"]
      index => "filebeat-error-%{+YYYY.MM.dd}"
      id => "error_id"
    }
  } 
}

測試

查看redis的key值對應的list數據還有沒,沒有的話說明已經把日誌傳輸給logstash了。

Elasticsearch集羣配置

安裝

詳細參考本博客另外一篇文章《ElasticSearch集羣》

elasticsearch.yml配置文件修改

文件路徑:/etc/elasticsearch/elasticsearch.yml 詳細參考本博客另外一篇文章《ElasticSearch集羣》

測試

詳細參考本博客另外一篇文章《ElasticSearch集羣》

Elasticsearch-head配置

yum -y install epel-release
yum -y install git npm
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
npm run start

啓動後會佔用當前窗口,改用腳本啓動方式 在當前目錄下建立一個start.sh文件

#!/bin/bash

nohup npm run start > /tmp/head.log 2>&1 &

默認訪問地址是http://localhost:9100

這時能夠經過使用nginx代理的方式進行訪問 能夠配置一個域名,同時設置一個nginx網站訪問密碼,具體參考本博客的另外一篇文章《nginx配置訪問密碼,輸入用戶名和密碼才能訪問》

location /head {
    proxy_pass  http://127.0.0.1:9100;
    #Proxy Settings
    proxy_redirect     off;
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_max_temp_file_size 0;
    proxy_connect_timeout      90;
    proxy_send_timeout         90;
    proxy_read_timeout         90;
    proxy_buffer_size          4k;
    proxy_buffers              4 32k;
    proxy_busy_buffers_size    64k;
    proxy_temp_file_write_size 64k;
}

修改elasticsearch.yml配置文件,開啓跨域訪問 在每一個集羣節點的elasticsearch.yml配置文件末尾加上以下兩行數據

http.cors.enabled: true
http.cors.allow-origin: "*"

Kibana配置

安裝

軟件採用經過下載rpm包的方式進行安裝

rpm -Uvh kibana-7.3.0-x86_64.rpm
systemctl daemon-reload
systemctl enable kibana.service
systemctl start kibana.service

kibana.yml配置文件修改

文件路徑:/etc/kibana/kibana.yml 這個也採用nginx代理的方式進行訪問,同時設置nginx的web訪問密碼 具體參考本博客的另外一篇文章《nginx配置訪問密碼,輸入用戶名和密碼才能訪問》

後期能夠結合X-pack插件進行用戶角色和權限管理等功能

server.port: 5601
server.host: "127.0.0.1"
server.basePath: "/kibana"  # 跟nginx那個路徑對應,不然沒法加載靜態資源
elasticsearch.hosts: ["http://192.168.0.13:9200","http://192.168.0.14:9200","http://192.168.0.15:9200"]
i18n.locale: "zh-CN"

nginx配置文件內容

location /kibana/ {
	proxy_pass http://127.0.0.1:5601;
	proxy_redirect off;
	proxy_set_header Host $host;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	rewrite ^/kibana/(.*)$ /$1 break;
}

使用http://ip/kibana進行訪問,查看索引信息等

或者採用另外一種配置 nginx配置文件

server {
    listen 80;  
    server_name xxx.xxx.com;
    root   /usr/share/nginx/html;
    expires -1;
   
    client_max_body_size    10m;  
    location / {
        proxy_http_version 1.1;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-Port $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass  http://172.17.107.187:5600;
    }
}

kibana.yml配置文件

server.port: 5601
server.host: "127.0.0.1"
elasticsearch.hosts: ["http://192.168.0.13:9200","http://192.168.0.14:9200","http://192.168.0.15:9200"]
i18n.locale: "zh-CN"

訪問的話直接使用域名的方式,不加後綴kibana

注意這兩種不一樣方式的配置

測試

打開瀏覽器進行訪問,查看索引,日誌文件等

後期擴展

  1. 各軟件之間的數據傳輸採用加密的方式進行
  2. 結合X-pack給kibana的訪問設置用戶角色和權限控制等
  3. filebeat模塊的功能還有待研究
  4. 其餘beats產品有待研究
  5. kibana的web界面使用還有待研究
  6. 讀取MySQL數據庫數據到Elasticsearch功能還有待研究
  7. 在Logstash中過濾數據還有待研究
  8. 其餘未了解的也都待研究
相關文章
相關標籤/搜索