Filebeat+Kafka+Logstash+ElasticSearch+Kibana+elasticsearch-head搭建
ELK Stack這套東西是什麼,看這篇文章的估計都有所瞭解,我這裏也不介紹了,這裏我只是把我在實際項目中搭建這套環境的過程和筆記分享給大家。如果有什麼問題可以留言交流
安裝包下載
安裝包自己到官網下載,我使用的版本是6.4.0
filebeat-6.4.0-linux-x86_64.tar.gz
logstash-6.4.0.tar.gz
elasticsearch-6.4.0.tar.gz
kibana-6.4.0-linux-x86_64.tar.gz
節點部署
192.168.0.94 filebeat,elasticsearch,kibana
192.168.0.131 elasticsearch-head,logstash
安裝
上傳安裝包
scp *.tar.gz [email protected]:/home/soft/elk/6.4.0
登錄0.94主機並解壓所有安裝包
elasticsearch
創建elastic用戶組及elastic用戶
elasticsearch不能使用root啓動,不然會報錯
groupadd elastic
useradd elastic -g elastic -p 123456
更改elk文件夾及內部文件的所屬用戶及組爲elastic:elastic
chown -R elastic:elastic /home/soft/elk
切換到elastic用戶再啓動
su elastic
./bin/elasticsearch
停止es
jps
kill -9 {jps出來的進程編碼}
遇到的問題與解決方法
- 問題一,啓動報錯
[2019-04-04T15:38:50,451][ERROR][o.e.b.Bootstrap ] [Y_O2LI0] node validation exception
[2] bootstrap checks failed
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[2]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
虛擬內存數量超標,Linux限制一個進程可訪問的VMA(虛擬內存)數量。虛擬內存區域是一個連續的虛擬地址空間區域。在進程的生命週期中,每當程序嘗試在內存中映射文件,鏈接到共享內存段,或者分配堆空間的時候,這些區域將被創建。調優這個值將限制進程可擁有VMA的數量。
使用root用戶修改/etc/sysctl.conf文件
vim /etc/sysctl.conf
添加內容
vm.max_map_count = 262144
檢查查看
sysctl -p
- 問題二,不能訪問9200端口
修改es配置文件config/elasticsearch.yml
vim config/elasticsearch.yml
添加以下配置(屬於gateway屬性)
http.cors.enabled: true
http.cors.allow-origin: "*"
ip:9200
即可訪問
- 問題三,低版本linux不支持Seccomp
修改es配置文件config/elasticsearch.yml
vim config/elasticsearch.yml
添加以下配置(屬於Memory屬性)
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
elasticsearch.yml
配置如下
cluster.name: appes
node.name: app-es-node-1
path.data: /home/soft/elk/6.4.0/elasticsearch-6.4.0/data
path.logs: /home/soft/elk/6.4.0/elasticsearch-6.4.0/logs
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
network.host: 0.0.0.0
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
cat config/elasticsearch.yml |grep -v '^#'
使用此命令查看
kibana
解壓並修改配置文件
tar -zxvf kibana-6.4.0-linux-x86_64.tar.gz
cd kibana-6.4.0-linux-x86_64
vim config/kibana.yml
修改內容如下
server.port: 5601
server.host: "192.168.0.94"
server.name: "kibana94"
elasticsearch.url: "http://192.168.0.94:9200"
啓動
cd bin
nohup ./kibana &
訪問與驗證:ip:5601
elasticsearch-head
需要準備Node.js環境與Grunt環境
1. install node
下載,解壓,設置環境變量,生效。(0.94已經有node v6.11.5,此處未安裝,經測試有問題,最好使用最新版本)
wget http://cdn.npm.taobao.org/dist/node/v10.15.3/node-v10.15.3-linux-x64.tar.xz
tar -xvf node-v10.15.3-linux-x64.tar.xz
創建軟連接
ln -s /home/soft/node-v10.15.3-linux-x64/bin/node /usr/bin/node
ln -s /home/soft/node-v10.15.3-linux-x64/bin/npm /usr/bin/npm
node -v查看版本
2. install grunt
npm install -g grunt-cli
grunt -version
查看版本grunt-cli v1.3.2
3. install elasticsearch-head
cd /home/soft/elasticsearch-head
wget https://github.com/mobz/elasticsearch-head/archive/master.zip
unzip master.zip
cd elasticsearch-head-master
npm install -g cnpm --registry=https://registry.npm.taobao.org
修改配置文件Gruntfile.js,使head可以訪問elasticsearch
vim Gruntfile.js
connect: {
server: {
options: {
hostname: '0.0.0.0',
port: 9100,
base: '.',
keepalive: true
}
}
}
啓動
grunt server
報錯
>> Local Npm module "grunt-contrib-clean" not found. Is it installed?
>> Local Npm module "grunt-contrib-watch" not found. Is it installed?
>> Local Npm module "grunt-contrib-connect" not found. Is it installed?
>> Local Npm module "grunt-contrib-jasmine" not found. Is it installed?
Warning: Task "connect:server" not found. Use --force to continue.
Aborted due to warnings.
升級node
cd ~
npm install -g n
n 10.15.3 stable
npm install grunt-contrib-clean
npm install grunt-contrib-watch
npm install grunt-contrib-connect
npm install grunt-contrib-jasmine
啓動OK
後臺啓動
nohup grunt server &
可以修改
_site/app.js
文件中的localhost:9200
,改爲主機ip:9200
用於初始化head連接es
http://ip:9100/
logstash
tar -xvf logstash-6.4.0.tar.gz
cd logstash-6.4.0
cp logstash-sample.conf first-pipeline.conf
修改first-pipeline.cof
input {
kafka {
bootstrap_servers => ["192.168.0.131:9092,192.168.0.136:9092,192.168.0.137:9092"]
topics => "filebeat"
group_id => "es"
codec => json
}
}
filter {
grok {
match => {
"message" => "^\[(?<createtime>%{YEAR}[./-]%{MONTHNUM}[./-]%{MONTHDAY}[- ]%{TIME}?)\]\|(?<appname>[-%{WORD}]+?)\|(?<thread>[\w-]+?)\|%{LOGLEVEL:loglevel}"
}
}
date {
match => ["createtime", "yyyy-MM-dd HH:mm:ss"]
target => "@timestamp"
}
mutate{
remove_field => ["beat.name"]
}
}
output {
elasticsearch {
hosts => ["http://192.168.0.94:9200"]
document_type => "APP-LOG"
#host => "%{[@host][beat]}"
#index => "%{[@metadata][beat]}-%{[@metadata][version]}-%{+YYYY.MM.dd}"
#user => "elastic"
#password => "changeme"
}
}
codec => json 由於beat傳輸數據給kafka集羣的時候,會附加很多tag,默認情況下,logstash就會將這串tag也認爲是message的一部分。這樣不利於後期的數據處理。所有需要添加codec處理。得到原本的message數據。
啓動腳本start.sh
#!/bin/bash
nohup ./bin/logstash -f config/first-pipeline.conf --config.reload.automatic &
–path.data=/home/softs/elk/logstash-6.4.0是指存放數據的路徑,不加的話不知道是否默認這裏
filebeat
tar -xvf filebeat-6.4.0-linux-x86_64.tar.gz
cd filebeat-6.4.0-linux-x86_64
cp filebeat.yml filebeat-kafka.yml
修改filebeat-kafka.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /home/logs/app-*-info.log
# 向輸出的每一條日誌添加額外的信息,比如“level:debug”,方便後續對日誌進行分組統計。
# 默認情況下,會在輸出信息的fields子目錄下以指定的新增fields建立子目錄,例如fields.level
# 這個得意思就是會在es中多添加一個字段,格式爲 "filelds":{"level":"debug"}
fields:
host: 192.168.0.94
# 每個filebeat對應的機器的ip,區分日誌機器來源
multiline.pattern: ^\[
multiline.negate: true
multiline.match: after
# 上面配置的意思是:不以[開頭的行都合併到上一行的末尾
#pattern:正則表達式
#negate:true 或 false;默認是false,匹配pattern的行合併到上一行;true,不匹配pattern的行合併到上一行
#match:after 或 before,合併到上一行的末尾或開頭
#-------------------------- Kafka output ------------------------------
output.kafka:
hosts: ["192.168.0.131:9092","192.168.0.136:9092","192.168.0.137:9092"]
topic: filebeat
required_acks: 1
# 其他output都註釋掉
啓動腳本start.sh
#!/bin/bash
nohup ./filebeat -e -c filebeat-kafka.yml -d "publish" > /dev/null 2>&1 &