ELK-6.5.3學習筆記–使用filebeat管理微服務日誌

微服務日誌打印。

轉載於http://www.eryajf.net/2369.htmlhtml

上邊是輸出了nginx日誌,從而進行展現,以及各類繪圖分析,而如今的需求是,要將微服務當中的日誌彙總到elk當中以便開發查詢日誌定位問題。nginx

都知道,微服務第一個特色就是,多,不只項目多,並且每每單臺主機當中也會有多個應用,所以多個日誌文件狀況下,如何處理才更加快速便捷呢,這裏使用了filebeat來做爲日誌轉發組件。spring

架構如圖:json

1,配置filebeat。

主機規劃以下圖簡示:tomcat

主機 組件
192.168.100.21 spring-cloud,filebeat-6.5.3
192.168.100.21 spring-cloud,filebeat-6.5.3
192.168.10.10 logstash-6.5.3,elk

像剛剛那樣,配置好yun源,而後直接安裝。架構

  1. yum -y install filebeat

而後來配置filebeat。elasticsearch

  1. cat > /etc/filebeat/filebeat.yml << EOF
  2. filebeat.inputs:
  3. - input_type: log
  4. paths:
  5. - /home/ishangjie/ishangjie-config-server/normal/*.log
  6. type: "wf1-config"
  7. fields:
  8. logsource: 192.168.100.21
  9. logtype: wf1-config
  10. - input_type: log
  11. paths:
  12. - /home/ishangjie/ishangjie-eureka-server/normal/*.log
  13. type: "wf1-eureka"
  14. fields:
  15. logsource: 192.168.100.21
  16. logtype: wf1-eureka
  17. - input_type: log
  18. paths:
  19. - /home/ishangjie/ishangjie-gateway-server/normal/*.log
  20. type: "wf1-gateway"
  21. fields:
  22. logsource: 192.168.100.21
  23. logtype: wf1-gateway
  24. output.logstash:
  25. hosts: ["192.168.10.10:5044"]
  26. EOF
  • 多個input定義多個應用日誌路徑,且能夠用*.log進行匹配,默認讀取目錄下最新的日誌。
  • 每一個裏邊都定義一個type類型,從而便於上下文銜接。
  • 最後定義日誌輸出到elk的logstash的5044端口。

再去配置一下另一臺主機。微服務

  1. cat > /etc/filebeat/filebeat.yml << EOF
  2. filebeat.inputs:
  3. - input_type: log
  4. paths:
  5. - /home/ishangjie/ishangjie-activity-service/normal/*.log
  6. type: "wf5-activity"
  7. fields:
  8. logsource: 192.168.100.25
  9. logtype: wf5-activity
  10. - input_type: log
  11. paths:
  12. - /home/ishangjie/ishangjie-order-service/normal/*.log
  13. type: "wf5-order"
  14. fields:
  15. logsource: 192.168.100.25
  16. logtype: wf5-order
  17. - input_type: log
  18. paths:
  19. - /home/ishangjie/ishangjie-user-service/normal/*.log
  20. type: "wf5-user"
  21. fields:
  22. logsource: 192.168.100.25
  23. logtype: wf5-user
  24. - input_type: log
  25. paths:
  26. - /home/ishangjie/ishangjie-thirdparty-service/normal/*.log
  27. type: "wf5-thirdparty"
  28. fields:
  29. logsource: 192.168.100.25
  30. logtype: wf5-thirdparty
  31. output.logstash:
  32. hosts: ["192.168.10.10:5045"]
  33. EOF
  • 基本上配置與上邊差很少,須要注意的一個地方就是output的logstash的端口,與上臺主機不要一致,由於咱們要啓動多個實例進行管理的。

啓動filebeat。ui

新版本配置實例url

filebeat.prospectors:
- type: log
paths:
- /data/tomcat/tomcat1/logs/*.log
fields:
logsource: 172.18.45.88
logtype: tomcatlog
- type: log
paths:
- /var/log/secure
fields:
logsource: 172.18.45.80
logtype: systemlog
- type: log
paths:
- /data/log/nginx/t.log*
fields:
logsource: 172.18.45.99
logtype: nginx_acclog
- type: log
paths:
- /data/log/nginx/error_t.log*
fields:
logsource: 172.18.45.85
logtype: nginx_errlog
output.kafka:
enabled: true
hosts: ["172.18.45.76:9092","172.18.45.75:9092","172.18.45.88:9092"]
topic: kafka_run_log

-----------------------

filebeat.prospectors:
- input_type: log
enabled: true
paths:
- /data/log/nginx/t.log*
fields:
log_topics: nginxlog
json.keys_under_root: true
json.overwrite_keys: true

output.kafka:
enabled: true
hosts: ["172.18.45.79:9092","172.18.45.78:9092","172.18.45.80:9092"]
topic: '%{[fields][log_topics]}'
partition.round_robin:
reachable_only: false
compression: gzip
max_message_bytes: 1000000
required_acks: 1

  1. systemctl enable filebeat
  2. systemctl start filebeat
  3. systemctl status filebeat

2,配置logstash。

針對上邊兩個主機轉過來的日誌,在elk主機上添加相對應的配置進行接收。

A:

  1. cat > /etc/logstash/conf.d/wf1.conf << EOF
  2. input {
  3. beats {
  4. port => "5044"
  5. host => "192.168.100.21"
  6. }
  7. }
  8. filter {
  9. if [fields][logtype] == "wf1-config" {
  10. json {
  11. source => "message"
  12. target => "data"
  13. }
  14. }
  15. if [fields][logtype] == "wf1-eureka" {
  16. json {
  17. source => "message"
  18. target => "data"
  19. }
  20. }
  21. if [fields][logtype] == "wf1-gateway" {
  22. json {
  23. source => "message"
  24. target => "data"
  25. }
  26. }
  27. }
  28. output {
  29. if [fields][logtype] == "wf1-config" {
  30. elasticsearch {
  31. hosts => ["127.0.0.1:9200"]
  32. index => "wf1-config-%{+YYYY.MM.dd}"
  33. }
  34. }
  35. if [fields][logtype] == "wf1-eureka" {
  36. elasticsearch {
  37. hosts => ["127.0.0.1:9200"]
  38. index => "wf1-eureka-%{+YYYY.MM.dd}"
  39. }
  40. }
  41. if [fields][logtype] == "wf1-gateway" {
  42. elasticsearch {
  43. hosts => ["127.0.0.1:9200"]
  44. index => "wf1-gateway-%{+YYYY.MM.dd}"
  45. }
  46. }
  47. }
  48. EOF

B

  1. cat > /etc/logstash/conf.d/wf5.conf << EOF
  2. input {
  3. beats {
  4. port => 5052
  5. host => "192.168.100.25"
  6. }
  7. }
  8. filter {
  9. if [fields][logtype] == "wf5-activity" {
  10. json {
  11. source => "message"
  12. target => "data"
  13. }
  14. }
  15. if [fields][logtype] == "wf5-order" {
  16. json {
  17. source => "message"
  18. target => "data"
  19. }
  20. }
  21. if [fields][logtype] == "wf5-user" {
  22. json {
  23. source => "message"
  24. target => "data"
  25. }
  26. }
  27. if [fields][logtype] == "wf5-thirdparty" {
  28. json {
  29. source => "message"
  30. target => "data"
  31. }
  32. }
  33. }
  34. output {
  35. if [fields][logtype] == "wf5-activity" {
  36. elasticsearch {
  37. hosts => ["127.0.0.1:9200"]
  38. index => "wf5-activity-%{+YYYY.MM.dd}"
  39. }
  40. }
  41. if [fields][logtype] == "wf5-order" {
  42. elasticsearch {
  43. hosts => ["127.0.0.1:9200"]
  44. index => "wf5-order-%{+YYYY.MM.dd}"
  45. }
  46. }
  47. if [fields][logtype] == "wf5-user" {
  48. elasticsearch {
  49. hosts => ["127.0.0.1:9200"]
  50. index => "wf5-user-%{+YYYY.MM.dd}"
  51. }
  52. }
  53. if [fields][logtype] == "wf5-thirdparty" {
  54. elasticsearch {
  55. hosts => ["127.0.0.1:9200"]
  56. index => "wf5-thirdparty-%{+YYYY.MM.dd}"
  57. }
  58. }
  59. }
  60. EOF
  • 這裏經過端口做爲豁口,讓彼此成爲鏈接,注意要一一對應。
  • 照單全收日誌,而後轉手發給本機的es同窗。

啓動這兩個實例。

  1. nohup /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/wf1.conf --path.data=/usr/share/logstash/data5 &> /logs/logstash_nohup/wf1.out &
  2. nohup /usr/share/logstash/bin/logstash -f /etc/logstash/conf.d/wf5.conf --path.data=/usr/share/logstash/data9 &> /logs/logstash_nohup/wf5.out &

啓動以後能夠按上邊演示過的步驟,在kibana當中添加索引,而後查看日誌。

3,合理規劃。

  • 關於索引。
    • 上邊的方式是一個服務配置了一個索引,衆所周知,微服務第一大特色就是多,兩個環境下來,發現按這種方式分配索引的話,會致使es裏邊集聚不少的索引。這是其一。
  • 關於端口。
    • 按上邊的思路,基本上是外部的一臺主機,就對應啓動了一個端口,這樣很容易端口浪費,因此能夠進行一下合理規劃與配置。
  • 解決上邊兩個問題。
    • 索引的話,我這邊規劃的是一臺主機一個索引,而非一個服務一個索引。如此算下來,能夠從原來二三十個索引縮減到十個之內。固然還能夠從其餘維度來進行區分。具體操做的辦法很是簡單,那就是在配置logstash實例的時候,在output處索引歸攏便可。
    • 端口方面,個人規劃是一類環境公用一個端口,原來預發線上一共十臺服務用了十個端口,如今預發用一個,線上用一個。具體操做就是filebeat客戶端端口統一,而後logstash實例彙總到一塊兒便可。
相關文章
相關標籤/搜索