Elastic Stack(也稱爲 ELK Stack)可用於各類用例 — 從可觀測性到安全性,從企業搜索到業務分析。java
elasticsearchnode
elasticsearch是一個分佈式搜索引擎,用於搜索、分析、存儲日誌。nginx
logstashgit
logstash用於採集、過濾日誌,將解析後的日誌輸出給elasticsearch。github
kibanaweb
kibana是一個數據可視化組件,提供豐富的web頁面。shell
filebeatjson
filebeat是一個輕量級的日誌採集器。vim
服務器規劃centos
序號 | 主機名 | 系統 |
---|---|---|
1 | elk-master | centos7-4C8G200G |
2 | elk-salve | centos7-4C8G200G |
系統優化
# 1. sysctl.conf配置 vi /etc/sysctl.conf fs.file-max = 655360 vm.max_map_count = 262144
# 2. limits配置(將4096改成20480) vi /etc/security/limits.d/20-nproc.conf * soft nproc 20480
jdk環境
# openjdk8 yum install java-1.8.0-openjdk.x86_64 -y
下載
wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/yum/7.7.1/elasticsearch-7.7.1-x86_64.rpm
安裝
rpm -ivh elasticsearch-7.7.1-x86_64.rpm
建立目錄和受權
# 建立目錄 mkdir -pv /data/elasticsearch # 受權 chown -R elasticsearch.elasticsearch /data/elasticsearch
master節點配置
vim /etc/elasticsearch/elasticsearch.yml
# 集羣名稱 cluster.name: test # 節點名稱 node.name: elk-master # 數據保存路徑 path.data: /data/elasticsearch # 日誌保存路徑 path.logs: /data/elasticsearch/log # 網絡綁定 network.host: 0.0.0.0 # 端口 http.port: 9200 # 集羣發現 elk-master:9300 elk-slave:9300 discovery.seed_hosts: ["elk-master:9300", "elk-slave:9300"] # 手動指定maste節點r cluster.initial_master_nodes: ["elk-master"]
slave節點配置
vim /etc/elasticsearch/elasticsearch.yml
# 集羣名稱 cluster.name: test # 節點名稱 node.name: elk-slave # 數據保存路徑 path.data: /data/elasticsearch # 日誌保存路徑 path.logs: /data/elasticsearch/log # 網絡綁定 network.host: 0.0.0.0 # 端口 http.port: 9200 # 集羣發現 elk-master:9300 elk-slave:9300 discovery.seed_hosts: ["elk-master:9300", "elk-slave:9300"] # 手動指定maste節點r cluster.initial_master_nodes: ["elk-master"]
jvm優化配置
vi /etc/elasticsearch/jvm.options
-Xms3g -Xmx3g
啓動
# 啓動 systemctl start elasticsearch # 開機自啓動 systemctl enable elasticsearch
驗證
# 查看集羣狀態(green) curl -XGET http://localhost:9200/_cluster/health?pretty # 查看集羣節點 curl -XGET 'http://localhost:9200/_cat/nodes?v&pretty'
中文分詞器(可選)
# 下載 wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.7.1/elasticsearch-analysis-ik-7.7.1.zip # 到es的plugins 目錄建立文件夾 mkdir /usr/share/elasticsearch/plugins/ik # 上傳文件到該目錄 elasticsearch-analysis-ik-7.7.1.zip # 解壓 unzip elasticsearch-analysis-ik-7.7.1.zip # 重啓es systemctl restart elasticsearch
下載
wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/yum/7.7.1/logstash-7.7.1.rpm
安裝
rpm -ivh logstash-7.7.1.rpm
管道文件配置
# 過濾nginx的日誌 vim /etc/logstash/conf.d/nginx.conf
input { beats { port => "5044" } } filter { if [fields][logtype] == "nginx-access" { mutate { gsub => ["message", "\\x", "\\\x"] } json { source => "message" } mutate { remove_field => [ "message" ] } if "HEAD" in [request_method] or "x.x.x.x" in [remote_addr] or "x.x.x.x" in [http_x_forwarded_for] { drop {} } useragent { source => "http_user_agent" target => "ua" } if "-" in [upstream_response_time] { mutate { replace => { "upstream_response_time" => "0" } } } mutate { convert => [ "upstream_response_time", "float" ] } mutate { gsub => [ "request_body", "\\x22", '"' ] gsub => [ "request_body", "\\x0A", "\n" ] } } } output { elasticsearch { hosts => ["http://localhost:9200"] index => "%{[fields][logsource]}-%{+YYYY.MM.dd}" } }
logstash.yml文件配置
# 數據目錄 path.data: /data/logstash/data # 日誌目錄 path.logs: /data/logstash/log
jvm優化
vim /etc/logstash/jvm.options
-Xms2g -Xmx2g
建立文件和受權
# 建立 mkdir -pv /data/logstash/{data,log} # 受權 chown -R logstash.logstash /data/logstash/
啓動
# 啓動 systemctl start logstash # 開機自啓動 systemctl enable logstash
nginx的json日誌格式配置
log_format main '{"@timestamp":"$time_iso8601",' '"remote_addr":"$remote_addr",' '"request_uri":"$request_uri",' '"request_method":"$request_method",' '"server_protocol":"$server_protocol",' '"request_time":$request_time,' '"upstream_response_time":"$upstream_response_time",' '"upstream_addr":"$upstream_addr",' '"host":"$host",' '"hostname":"$hostname",' '"http_host":"$http_host",' '"uri":"$uri",' '"http_x_forwarded_for":"$http_x_forwarded_for",' '"http_user_agent":"$http_user_agent",' '"request_body":"$request_body",' '"status":"$status"}';
微服務管道配置
vim /etc/logstash/conf.d/app.conf
input { beats { port => "5045" } } filter { grok { match => { "message" => "%{TIMESTAMP_ISO8601:time}\s+%{LOGLEVEL:loglevel}\s+%{NOTSPACE:service}\s+%{NUMBER:pid}\s+%{NOTSPACE:thread}\s+%{DATA:class}:\s+%{GREEDYDATA:message}"} overwrite => ["message"] } if "|" in [message] { grok { match => { "message" => "%{NOTSPACE:caller}\|+%{NOTSPACE:user}\|+%{NOTSPACE:method}\|%{GREEDYDATA:message}"} overwrite => ["message"] } } } output { elasticsearch { hosts => ["http://localhost:9200"] index => "%{[fields][logsource]}-%{+YYYY.MM.dd}" } }
其餘
下載
wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/yum/7.7.1/filebeat-7.7.1-x86_64.rpm
安裝
rpm -ivh filebeat-7.7.1-x86_64.rpm
配置
vim /etc/filebeat/filebeat.yml
# nginx日誌收集 t.config_dir: /usr/share/filebeat/module filebeat.inputs: - input_type: log enabled: true paths: - /var/log/nginx/*.log document_type: test-nginx-log fields: logtype: nginx-access logsource: test-nginx-log multiline.pattern: '[0-9]{4}-[0-9]{2}-[0-9]{2}' multiline.negate: true multiline.match: after name: ip # 收集服務器ip地址 output.logstash: hosts: ["elk-master:5044"]
# java微服務日誌收集 t.config_dir: /usr/share/filebeat/module filebeat.inputs: - input_type: log enabled: true paths: - /data/Log/**/*.log document_type: test-app-log fields: logsource: test-app-log multiline.pattern: '[0-9]{4}-[0-9]{2}-[0-9]{2}' multiline.negate: true multiline.match: after name: ip # 收集服務器ip地址 output.logstash: hosts: ["elk-master:5045"]
參數說明
# input 輸入日誌 # 日誌路徑 paths # 文檔類型 document_type # 屬性配置 fields # 多行日誌配置 multiline # output 輸出日誌 # 輸出到logstash output.logstash # logstash服務器地址 hosts
啓動
# 啓動 systemctl start filebeat # 開機自啓動 systemctl enable filebeat
下載
wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/7.x/yum/7.7.1/kibana-7.7.1-x86_64.rpm
安裝
rpm -ivh kibana-7.7.1-x86_64.rpm
配置
vim /etc/kibana/kibana.yml
# 端口 server.port: 5601 # 地址 server.host: "localhost" # es服務器地址 elasticsearch.hosts: ["http://localhost:9200"] # 中文 i18n.locale: "zh-CN"
啓動
# 啓動 systemctl start kibana # 自啓動 systemctl enable kibana
訪問web
curl http://localhost:5601
索引管理
# 訪問地址 http://localhost:5601/app/kibana#/management/elasticsearch/index_management/indices # 操做 刪除、查詢索引
索引模式
# 訪問地址 http://localhost:5601/app/kibana#/management/kibana/index_patterns?_g=() # 建立索引 # 1. 定義索引模式 使用通配符 # 2. 配置設置 時間篩選字段 @timestamp
可視化
# 訪問地址 http://localhost:5601/app/kibana#/visualize
儀表盤
# 訪問地址 http://localhost:5601/app/kibana#/dashboards