一狐史、說(shuō)明
這篇文章是2020年6月份寫(xiě)在自己博客的,組件版本應(yīng)該都比較低了坯钦。
二预皇、組件
filebeat
kafka
zookeeper
elasticsearch
kibana
三、參考鏈接:
# 1 json在線解析
https://www.json.cn/
# 2 grok debug
http://grokdebug.herokuapp.com/
http://grokdebug.herokuapp.com/patterns
# 3 template接口
https://www.elastic.co/guide/en/elasticsearch/reference/6.8/indices-templates.html
# 4 elk官網(wǎng)指南
https://www.elastic.co/guide/index.html
https://www.elastic.co/
四婉刀、服務(wù)器
172.16.1.30 kc-log-01 日志所在服務(wù)器
172.16.1.94 kc-online-log-platform-001 elasticsearch kafka zookeeper
172.16.1.93 kc-online-log-platform-002 elasticsearch kafka zookeeper
172.16.1.92 kc-online-log-platform-003 elasticsearch kafka zookeeper kibana logstash
五吟温、JAVA JDK安裝
# 1 獲取java jdk安裝包
[root@kc-online-log-platform-001 ~]# ls /opt/
elk jdk1.8.0_211 kafka
# 2 配置環(huán)境變量
[root@kc-online-log-platform-001 ~]# cat /etc/profile.d/java.sh
export JAVA_HOME=/opt/jdk1.8.0_211
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$PATH:$JAVA_HOME/bin
# 3 應(yīng)用環(huán)境變量
[root@kc-online-log-platform-001 ~]# source /etc/profile.d/java.sh
六、目錄結(jié)構(gòu)
[root@kc-online-log-platform-001 elk]# ls /data/
elk kafka zk
[root@kc-online-log-platform-001 elk]# ls /data/elk/elasticsearch/
config data
[root@kc-online-log-platform-001 elk]# ls /data/zk/
zkdatalog zookeeper
七突颊、elasticsearch集群搭建
# kc-online-log-platform-001
[root@kc-online-log-platform-001 elk]# cat docker-compose.yaml
version: '3'
services:
kclog-online-node03:
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.0
container_name: kclog-online-node03
restart: always
environment:
- node.name=es-node-03
- network.publish_host=172.16.1.94
- network.host=0.0.0.0
- discovery.seed_hosts=172.16.1.92,172.16.1.93,172.16.1.94
- cluster.name=kc-log-center
- cluster.initial_master_nodes=172.16.1.92,172.16.1.93,172.16.1.94
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms2048m -Xmx2048m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /data/elk/elasticsearch/data:/usr/share/elasticsearch/data
- /data/elk/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
ports:
- 9200:9200
- 9300:9300
# kc-online-log-platform-002
[root@kc-online-log-platform-002 ~]# cat /opt/elk/docker-compose.yaml
version: '3'
services:
kclog-online-node02:
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.0
container_name: kclog-online-node02
restart: always
environment:
- node.name=es-node-02
- network.publish_host=172.16.1.93
- network.host=0.0.0.0
- discovery.seed_hosts=172.16.1.92,172.16.1.93,172.16.1.94
- cluster.name=kc-log-center
- cluster.initial_master_nodes=172.16.1.92,172.16.1.93,172.16.1.94
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms2048m -Xmx2048m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /data/elk/elasticsearch/data:/usr/share/elasticsearch/data
- /data/elk/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
ports:
- 9200:9200
- 9300:9300
# kc-online-log-platform-003
[root@kc-online-log-platform-003 ~]# cat /opt/elk/docker-compose.yaml
version: '3'
services:
kclog-online-node01:
image: docker.elastic.co/elasticsearch/elasticsearch:7.8.0
container_name: kclog-online-node01
restart: always
environment:
- node.name=es-node-01
- network.publish_host=172.16.1.92
- network.host=0.0.0.0
- discovery.seed_hosts=172.16.1.92,172.16.1.93,172.16.1.94
- cluster.name=kc-log-center
- cluster.initial_master_nodes=172.16.1.92,172.16.1.93,172.16.1.94
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms2048m -Xmx2048m"
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /data/elk/elasticsearch/data:/usr/share/elasticsearch/data
- /data/elk/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
ports:
- 9200:9200
- 9300:9300
# 上述三臺(tái)服務(wù)器執(zhí)行一下操作:
cd /opt/elk/
docker-compose up -d
八鲁豪、常用api
# elasticsearch api
# 1 獲取索引
curl 'localhost:9200/_cat/indices?v'
curl -ulogstash:jflogstash123 '172.18.238.3:9200/_cat/indices?' 帶密碼查詢(xún)
# 2 創(chuàng)建索引
curl -XPUT 'localhost:9200/customer’
# 3 插入數(shù)據(jù)
curl -XPUT 'localhost:9200/customer/external/1'-d ' { "name": "John Doe”}'
# 4 查詢(xún)數(shù)據(jù)
curl 'localhost:9200/customer/external/1'
# 5 刪除索引
curl -XDELETE 'localhost:9200/customer'
# 6 查看已安裝的插件
curl http://127.0.0.1:9200/_cat/plugins
# 7 查看索引信息
curl -XGET "http://localhost:9200/kc-online-log-2020.06.21/_mapping"
# 8 創(chuàng)建template 其中:kc_online_log_template為創(chuàng)建的template
curl -XPUT -H "Content-Type: application/json" "http://localhost:9200/_template/kc_online_log_template" -d ''
# 9 集群狀態(tài)查看
curl 'localhost:9200/_cat/health?v
# 10 查看集群node
curl 'localhost:9200/_cat/nodes?v'
九、kafka+zookeeper集群
9.1律秃、kc-online-log-platform-001
9.1.1爬橡、zookeeper
[root@kc-online-log-platform-001 config]# grep -v "^#\|^$" /opt/kafka/config/zookeeper.properties
tickTime=2000
initLimit=5
syncLimit=2
dataDir=/data/zk/zookeeper/
dataLogDir=/data/zk/zkdatalog/
clientPort=2181
maxClientCnxns=0
server.1=172.16.1.94:2888:3888
server.2=172.16.1.93:2888:3888
server.3=172.16.1.92:2888:3888
[root@kc-online-log-platform-001 config]# cat /data/zk/zookeeper/myid
1
9.1.2、kafka
[root@kc-online-log-platform-001 config]# cat /opt/kafka/config/server.properties
broker.id=0
listeners=PLAINTEXT://172.16.1.94:9092
host.name=172.16.1.94
port=9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/data/kafka/kafka-logs/
num.partitions=3
num.recovery.threads.per.data.dir=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=172.16.1.94:2181,172.16.1.93:2181,172.16.1.92:2181
zookeeper.connection.timeout.ms=6000
9.2棒动、kc-online-log-platform-001
9.2.1糙申、zookeeper
[root@kc-online-log-platform-002 ~]# grep -v "^#\|^$" /opt/kafka/config/zookeeper.properties
tickTime=2000
initLimit=5
syncLimit=2
dataDir=/data/zk/zookeeper/
dataLogDir=/data/zk/zkdatalog/
clientPort=2181
maxClientCnxns=0
server.1=172.16.1.94:2888:3888
server.2=172.16.1.93:2888:3888
server.3=172.16.1.92:2888:3888
[root@kc-online-log-platform-002 ~]# cat /data/zk/zookeeper/myid
2
9.2.2、kafka
[root@kc-online-log-platform-002 ~]# cat /opt/kafka/config/server.properties
broker.id=1
listeners=PLAINTEXT://172.16.1.93:9092
host.name=172.16.1.93
port=9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/data/kafka/kafka-logs/
num.partitions=3
num.recovery.threads.per.data.dir=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=172.16.1.94:2181,172.16.1.93:2181,172.16.1.92:2181
zookeeper.connection.timeout.ms=6000
9.3船惨、kc-online-log-platform-001
9.3.1柜裸、zookeeper
[root@kc-online-log-platform-003 kafka]# grep -v "^#\|^$" /opt/kafka/config/zookeeper.properties
tickTime=2000
initLimit=5
syncLimit=2
dataDir=/data/zk/zookeeper/
dataLogDir=/data/zk/zkdatalog/
clientPort=2181
maxClientCnxns=0
server.1=172.16.1.94:2888:3888
server.2=172.16.1.93:2888:3888
server.3=172.16.1.92:2888:3888
[root@kc-online-log-platform-003 kafka]# cat /data/zk/zookeeper/myid
3
9.3.2、kafka
[root@kc-online-log-platform-003 kafka]# cat /opt/kafka/config/server.properties
broker.id=2
listeners=PLAINTEXT://172.16.1.92:9092
host.name=172.16.1.92
port=9092
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/data/kafka/kafka-logs/
num.partitions=3
num.recovery.threads.per.data.dir=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
zookeeper.connect=172.16.1.94:2181,172.16.1.93:2181,172.16.1.92:2181
zookeeper.connection.timeout.ms=6000
十粱锐、啟動(dòng)
# 三臺(tái)服務(wù)器執(zhí)行如下命令:
nohup /opt/kafka/bin/zookeeper-server-start.sh /opt/kafka/config/zookeeper.properties &
nohup /opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties &
十一疙挺、測(cè)試集群有效性
# 創(chuàng)建一個(gè)topic:fzh
kafka-topics.sh --create --zookeeper inte-zk-node-1:2181 --replication-factor 3 --partitions 1 --topic fzh
# 其它節(jié)點(diǎn)查看該topic是否存在:
kafka-topics.sh --describe --zookeeper kc-online-log-platform-001:2181 --topic fzh
kafka-topics.sh --describe --zookeeper kc-online-log-platform-002:2181 --topic fzh
kafka-topics.sh --describe --zookeeper kc-online-log-platform-003:2181 --topic fzh
創(chuàng)建一個(gè)topic,發(fā)現(xiàn)都能查到該topic怜浅,證明:集群成功铐然。
# 查看topic信息:
./kafka-console-consumer.sh --bootstrap-server 172.16.1.94:9092 --topic kc-online-log --from-beginning
十二、filebeat
12.1恶座、filebeat配置
[root@online-k8s2-master filebeat]# cat filebeat.yml
filebeat.inputs:
- type: log
enabled: true
paths:
- /mnt/online-data-4/thirdhub*/logs/app.log
- /mnt/online-data-4/obc-stock-log/logs/app.log
multiline:
pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
negate: true
match: after
output.kafka:
hosts: ["172.16.1.94:9092"]
topic: "kc-online-log"
12.2搀暑、filebeat啟動(dòng)
./filebeat -c filebeat.yml
十三、logstash
13.1奥裸、logstash配置
[root@kc-online-log-platform-003 kafka]# cat /opt/logstash/config/logstash.conf
input {
kafka {
bootstrap_servers => "172.16.1.93:9092"
topics => ["kc-online-log"]
codec => json
}
}
filter {
grok {
match => { "message" => "%{USERNAME:day} %{NOTSPACE:time} %{NOTSPACE:threadid} %{LOGLEVEL:level} %{GREEDYDATA:msg}" }
}
grok {
match => { "[log][file][path]" => ".*/%{USERNAME:app_name}/logs" }
}
}
output {
elasticsearch {
hosts => "http://172.16.1.92:9200"
index => "kc-online-log-%{+YYYY.MM.dd}"
}
}
13.2险掀、啟動(dòng)
[root@kc-online-log-platform-003 logstash]# ./bin/logstash -f config/logstash.conf
十四、kibana
[root@kc-online-log-platform-003 kibana]# cat docker-compose.yaml
version: '3'
services:
kclog-kibana:
image: docker.elastic.co/kibana/kibana:7.8.0
container_name: kclog-kibana
restart: always
environment:
elasticsearch.hosts: "http://172.16.1.93:9200"
server.host: "0.0.0.0"
volumes:
- /data/elk/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml
ports:
- "5601:5601"
[root@kc-online-log-platform-003 kibana]# cat /data/elk/kibana/config/kibana.yml
server.name: kibana
server.host: "0.0.0.0"
elasticsearch.hosts: [ "http://172.16.1.92:9200" ]
十五湾宙、elasticsearch索引配置
在kibana上進(jìn)行詞語(yǔ)搜索時(shí)樟氢,經(jīng)常會(huì)發(fā)現(xiàn)搜索的內(nèi)容不準(zhǔn)確冈绊,eg:搜索thread:"app-app1-app2"的時(shí)候,有app或app1或app2的thread都會(huì)被檢索到埠啃,而我們實(shí)際想要的是只有"app-app1-app2"的thread信息死宣。
查看索引mapping信息
curl -XGET "http://localhost:9200/kc-online-log-2020.06.21/_mapping"
用在線json分析工具分析10.1得到的結(jié)果:
curl -XPUT -H "Content-Type: application/json" "http://localhost:9200/_template/kc_online_log_template" -d '
{
"index_patterns": ["kc-online-log-*"],
"mappings": {
"properties": {
"app_name": {
"type": "keyword"
},
"agent": {
"properties": {
"hostname": {
"type": "keyword" # 這個(gè)地方是解決問(wèn)題的關(guān)鍵,至于template的結(jié)構(gòu)書(shū)寫(xiě)碴开,可以直接復(fù)制黏貼10.1得到的結(jié)果毅该,根據(jù)實(shí)際情況來(lái)。
}
}
}
}
}
}'