ELK基礎配置

  • 2019 年 11 月 11 日
  • 筆記

前言

近期在研究日誌系統的設計,感覺現在公司的子系統和接口太多了,日誌看不過來,就想着有沒有一種方法可以把各個程序的日誌組合到一起。於是乎就搜到了ELK。開始對ELK的概念完全搞不懂,就照着各個平台文檔一頓安裝和研究。終於搞明白了ELK這套系統的大致流程。

ELK即:Elasticsearch、Logstash、Kibana的簡稱。

簡單介紹來說:Elasticsearch用來存儲日誌,Logstash用來搜集和過濾日誌,Kibana用來展示日誌。

為什麼用Elasticsearch存儲日誌呢,它是個搜索引擎,可以存儲海量數據,可以各種查詢並且速度很快。

Logstash可以搜集和分析日誌,但是它占的內存和cpu過大,所以我最終選擇了研究FileBeat替代Logstash。日誌搜集工具的工作流程就是在各個產生日誌的服務器上安裝該工具,然後它負責從數據庫文件系統或者mq等地方搜集日誌並通過http發送到ElasticSearch

ELK裏面涉及到的每個工具的功能都相當豐富和強大,遠不止日誌記錄這一功能。後面還要繼續學習

關於ElK的基本安裝和使用本文就不做介紹了,因為網上很多。記錄一下基礎的配置和常見的問題防止以後忘記,也留給需要的人希望給你們一些幫助。後面遇到新的坑和問題會繼續完善該博客

ElasticSearch

配置修改

配置文件路徑:/config/elasticsearch.yml    #開啟外網訪問  network.host: 0.0.0.0    node.name: node-1    cluster.initial_master_nodes: ["node-1"]    http.port:9200    #啟用密碼驗證  xpack.security.enabled: true  xpack.security.transport.ssl.enabled: true

設置密碼:

bin/elasticsearch-setup-passwords interactive    接下來系統將會提示為幾個用戶挨個設置密碼    執行該命令的前提是配置文件啟用了密碼驗證

後台啟動

啟動:/elasticsearch -p /tmp/elasticsearch-pid -d    關閉:cat /tmp/elasticsearch-pid  展示:pid  kill -9 pid

報錯

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

原因:elasticsearch用戶擁有的內存權限太小,至少需要262144:  解決方案:    執行命令:  sysctl -w vm.max_map_count=262144    查看結果:  sysctl -a|grep vm.max_map_count  顯示:vm.max_map_count = 262144    /etc/sysctl.conf文件最後添加一行 vm.max_map_count=262144 。否則重啟服務器配置將失效

Kibana

配置修改

配置文件路徑:config/kibana.yml  server.host:"0.0.0.0" #用於外網訪問    #配置elasticsearch的地址  elasticsearch.hosts: ["http://localhost:9200"]    #es開啟授權後配置es的用戶名和密碼  elasticsearch.username: "elastic"  elasticsearch.password: "123456"    #中文支持  i18n.locale: "zh-CN" 

後台啟動

啟動(加上&):  kibana-4.5.2-linux-x64/bin/kibana &    退出:   ps -ef|grep kibana   kill -9 pid

展示

在IndexManage  中 Create IndexPattern 這一步是為了將日誌的Index展示到Discover中去  在Discover中查看系統日誌

FileBeat

配置文件

filebeat.inputs:      enabled: true      # 解決中文亂碼      encoding: GB2312      paths:        D:LogsTopShelfAllBizReportConsumerEmqJob**.txt    # 正則表達式 The regexp Pattern that has to be matched. The example pattern matches all lines starting with [  multiline.pattern: '^時間'  # true 或 false;默認是false,匹配pattern的行合併到上一行;true,不匹配pattern的行合併到上一行  multiline.negate: true  # Note: After is the equivalent to previous and before is the equivalent to to next in Logstash  multiline.match: after  #匹配結尾  #multiline.flush_pattern: 'End event'  #合併最大行,默認500  multiline.max_lines: 50  #一次合併事件的超時時間,默認5s,防止合併消耗太多時間甚至卡死  multiline.timeout: 5s    setup.kibana:      host: "122.51.251.177:5601"    output.elasticsearch:      hosts: ["122.51.251.177:9200"]      # Optional protocol and basic auth credentials.      #protocol: "https"      username: "elastic"      password: "123456"    processors:    - add_host_metadata: ~    - add_cloud_metadata: ~