前言
近期在研究日志系統的設計,感覺現在公司的子系統和接口太多了,日志看不過來,就想着有沒有一種方法可以把各個程序的日志組合到一起。於是乎就搜到了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:\Logs\TopShelf\*\*\*.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: "****:5601"
#允許自定義索引名稱
setup.ilm.enabled: auto
#滾動更新別名,和pattern最終拼在一起,滾動更新是00001會自增
setup.ilm.rollover_alias: "filebeat-testing"
setup.ilm.pattern: "{now/d}-000001"
#允許自動生成模板
setup.template.enabled
#可以覆蓋模板
setup.template.overwrite: true
#索引模板名稱
setup.template.name: "filebeat-testing"
#索引模板匹配策略
setup.template.pattern: "filebeat-testing-%{[agent.version]}-*"
output.elasticsearch:
hosts: ["*.*.*.*:9200"]
# Optional protocol and basic auth credentials.
#protocol: "https"
username: "elastic"
password: "123456"
processors:
- add_host_metadata: ~
- add_cloud_metadata: ~
windows啟動
powershell跳轉至下載目錄,執行命令
.\filebeat -e -c filebeat.yml
Docker配置###
version: '3.0'
services:
elasticsearch:
image: 'docker.elastic.co/elasticsearch/elasticsearch:7.5.1'
container_name: elasticsearch01
ports:
- "9200:9200"
- "9300:9300"
volumes:
- /etc/timezone:/etc/timezone
- /etc/localtime:/etc/localtime
- esdata01:/usr/share/elasticsearch/data
environment:
- node.name=es01
- cluster.initial_master_nodes=es01
- xpack.security.enabled=true
- xpack.security.transport.ssl.enabled=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
kibana:
image: 'docker.elastic.co/kibana/kibana:7.5.1'
ports:
- "8005:5601"
volumes:
- /etc/timezone:/etc/timezone
- /etc/localtime:/etc/localtime
environment:
SERVER_NAME: kibana
ELASTICSEARCH_HOSTS: http://elasticsearch01:9200
ELASTICSEARCH_USERNAME: "elastic"
ELASTICSEARCH_PASSWORD: "backer2019"
I18N_LOCALE: zh-CN
depends_on:
- elasticsearch
volumes:
esdata01:
driver: local
Docker-filebeat###
version: "3"
services:
filebeat:
image: myfilebeat
# image: docker.elastic.co/beats/filebeat:7.5.1
restart: always
container_name: filebeat
volumes:
- /etc/timezone:/etc/timezone
- /etc/localtime:/etc/localtime
- filebeatdata:/usr/share/filebeat/data
- /mnt/data/filebeat/filebeat.docker.yml:/usr/share/filebeat/filebeat.yml:ro
- /var/run/docker.sock:/var/run/docker.sock:ro
- /var/lib/docker/containers:/var/lib/docker/containers:ro
- /mnt/data:/usr/share/filebeat/logs
environment:
- output.elasticsearch.hosts=["elasticsearch內網url:9200"]
- strict.perms=false
volumes:
filebeatdata:
myfileeat-Dockerfile###
FROM docker.elastic.co/beats/filebeat:7.5.1
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8