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


微服務日志打印。

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

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

都知道,微服務第一個特點就是,多,不僅項目多,而且往往單台主機當中也會有多個應用,因此多個日志文件情況下,如何處理才更加快速便捷呢,這里使用了filebeat來作為日志轉發組件。

架構如圖:

1,配置filebeat。

主機規划如下圖簡示:

主機 組件
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。

  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。

新版本配置實例

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實例匯總到一起即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM