首页 > 其他分享 >k8s通过sidecar模式收集pod的容器日志至ELK

k8s通过sidecar模式收集pod的容器日志至ELK

时间:2023-11-09 13:58:04浏览次数:39  
标签:ELK filebeat log 10.0 clavaplus test pod k8s consume

架构:


已完成的部署
1、ES集群及kibana部署

 https://blog.51cto.com/yht1990/6080981

2、kafaka+zookeeper集群

 https://blog.51cto.com/yht1990/6081518

准备sidecar镜像(filebeat)
找一台服务器打镜像

[root@yw-test filebeat]# cat Dockerfile
FROM docker.elastic.co/beats/filebeat:7.9.0
COPY filebeat.yml /usr/share/filebeat/filebeat.yml
USER root
RUN chown root:filebeat /usr/share/filebeat/filebeat.yml
USER filebeat
[root@yw-test filebeat]#
[root@yw-test filebeat]#
[root@yw-test filebeat]# cat filebeat.yml
filebeat.inputs:
- input_type: log
paths:
- /data/logic/log/*.log
fields:
log_topic: "${TOPIC_ID}"
#tail_files: true
clean_inactive: 48h
ignore_older: 24h
close_inactive: 1m

output.kafka:
hosts: "${KAFKA_SERVER}"
topic: '%{[fields.log_topic]}'
partition.round_robin:
reachable_only: true
required_acks: 1
compression: gzip
max_message_bytes: 1000000

logging.level: error

[root@yw-test filebeat]# docker build . -t 10.0.7.12/k8s/filebeat/sidecar:7.9.0
[root@yw-test filebeat]# docker push 10.0.7.12/k8s/filebeat/sidecar:7.9.0
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
扩展filebeat直接发送logstash
sh-4.2$ cat filebeat.yml
filebeat.inputs:
- input_type: log
paths:
- /var/log/pods/test-app*/*/*.log
- /var/log/pods/uat-app*/*/*.log
- /var/log/pods/pro-app*/*/*.log
clean_inactive: 48h
ignore_older: 24h
close_inactive: 1m

output.logstash:
hosts: "${LOGSTASH_SERVER}"

logging.level: error
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
创建deployment
[root@k8s-test-manager src]# cat test-clavaplus-consume-sidecar-filebeat.yml
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
app: test-clavaplus-consume-deployment-label
name: test-clavaplus-consume-deployment
namespace: test-app
spec:
replicas: 2
selector:
matchLabels:
app: test-clavaplus-consume-selector
template:
metadata:
labels:
app: test-clavaplus-consume-selector
spec:
imagePullSecrets:
- name: myregistrykey
containers:
- name: test-clavaplus-consume
image: registry.cn-shenzhen.aliyuncs.com/test/swoole:test-clavaplus-consume-518b279-2023-0227-1833
imagePullPolicy: IfNotPresent
#imagePullPolicy: Always
ports:
- containerPort: 9501
protocol: TCP
name: http
resources:
limits:
cpu: 1
memory: "512Mi"
requests:
cpu: 200m
memory: "512Mi"
volumeMounts:
- name: applogs
mountPath: /data/logic/log
- name: sidecar-filebeat
image: 10.0.7.12/k8s/filebeat/sidecar:7.9.0
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always
env:
- name: "TOPIC_ID"
value: "test-clavaplus-consume"
- name: "KAFKA_SERVER"
value: '["10.0.7.53:9092", "10.0.7.54:9092", "10.0.7.55:9092"]'
volumeMounts:
- name: applogs
mountPath: /data/logic/log
volumes:
- name: applogs #定义通过emptyDir实现业务容器与sidecar容器的日志共享,以让sidecar收集业务容器中的日志
emptyDir: {}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
运行deployement
kubectl apply -f test-clavaplus-consume-sidecar-filebeat.yml
1.
kafka工具检查有无数据
可以看到目前已有数据

 

部署logstash
yum -y install java-1.8.0-openjdk
yum -y install https://mirror.tuna.tsinghua.edu.cn/elasticstack/7.x/yum/7.9.0/logstash-7.9.0.rpm
systemctl start logstash
systemctl enable logstash
tail -f /var/log/logstash/logstash-plain.log
1.
2.
3.
4.
5.
创建logstash配置文件
# 将收集过来的json日志做json解析

    codec => json {

       charset => "UTF-8" # 支持中文搜索

   }

 

# 将message消息赋值给source字段

filter {

 json {

   source => "message"

 }

 mutate {    

  # 删除没用的字段

   remove_field => ["message","@version","agent","ecs"]

 }

}

input {
kafka {
bootstrap_servers => "10.0.7.53:9092,10.0.7.54:9092,10.0.7.55:9092"
topics => ["test-clavaplus-consume"]
codec => json {
charset => "UTF-8"
}
}
}

filter {
json {
source => "message"
}
if [fields][log_topic] == "test-clavaplus-consume" {
mutate {
remove_field => ["message","@version","agent","ecs"]
}
}
}

output {
if [fields][log_topic] == "test-clavaplus-consume" {
elasticsearch {
hosts => ["10.0.7.46:9200","10.0.7.47:9200","10.0.7.48:9200"]
index => "test-clavaplus-consume-%{+YYYY.MM.dd}"
}
}
}
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
启动logstash
systemctl restart logstash && tail -f /var/log/logstash/logstash-plain.log
1.
登陆kibana创建索引并查看数据

 

 

 


查询语法:
 https://www.elastic.co/guide/en/kibana/7.9/kuery-query.html#kuery-query
-----------------------------------
©著作权归作者所有:来自51CTO博客作者小杨同学THY的原创作品,请联系作者获取转载授权,否则将追究法律责任
k8s通过sidecar模式收集pod的容器日志至ELK
https://blog.51cto.com/yht1990/6089320

标签:ELK,filebeat,log,10.0,clavaplus,test,pod,k8s,consume
From: https://www.cnblogs.com/gaoyanbing/p/17819544.html

相关文章

  • 运行k8s
      这个总算全部都run起来了,记录下要点,如果flannel一直显示crashloopback: ......
  • DHorse(K8S的CICD平台)的实现原理
    综述首先,本篇文章所介绍的内容,已经有完整的实现,可以参考这里。在微服务、DevOps和云平台流行的当下,使用一个高效的持续集成工具也是一个非常重要的事情。虽然市面上目前已经存在了比较成熟的自动化构建工具,比如jekines,还有一些商业公司推出的自动化构建工具,但他们都不能够很好的......
  • 升级k8s版本废弃docker引擎
    1、废弃docker引擎systemctlenablekubeletsystemctlenablecontainerdcat<<EOF|sudotee/etc/modules-load.d/k8s.confoverlaybr_netfilterEOFcat/etc/modules-load.d/k8s.confmodprobeoverlaymodprobebr_netfiltermv/etc/sysctl.d/k8s.conf/etc......
  • elasticview elk查看工具安装
    ElasticView是一款用来监控ElasticSearch状态和操作ElasticSearch索引的web可视化工具。它由golang开发而成,具有部署方便,占用内存小等优点,官网地址:http://www.elastic-view.cnElasticSearch连接树管理(更方便的切换测试/生产环境)支持权限管理支持sql转换成dsl语法更方便的重......
  • 一文带你认识「Docker」与「k8s」
    转载:https://juejin.cn/post/7015729458959089701随着k8s作为容器编排解决方案变得越来越流行,有些人开始拿Docker和k8s进行对比,不禁问道:Docker不香吗?k8s是kubernetes的缩写,'8'代表中间的八个字符。其实Docker和k8s并非直接的竞争对手,它俩相互依存。Docker是......
  • k8s的使用
     apiVersion:v1              #必选,版本号,例如v1kind:Pod                #必选,Podmetadata:                #必选,元数据 name:string           ......
  • k8s 外部ip定义
    apiVersion:v1kind:Servicemetadata:name:xinxi-azure-svcnamespace:jituan-xinxi-bespec:ports:-name:xinxi-azure-portport:443protocol:TCPtargetPort:38455type:ClusterIP---apiVersion:v1kind:Endpointsmeta......
  • k8s-服务网格实战-配置 Mesh(灰度发布)
    在上一篇k8s-服务网格实战-入门Istio中分享了如何安装部署Istio,同时可以利用Istio实现gRPC的负载均衡。今天我们更进一步,深入了解使用Istio的功能。从Istio的流量模型中可以看出:Istio支持管理集群的出入口请求(gateway),同时也支持管理集群内的mesh流量,也就是集群内......
  • 一、minikube部署单机k8s环境
    一、minikube部署单机k8s环境注意:此处为方便测试,使用minikube部署单机k8s环境,生产环境请根据实际情况操作1.安装docker-ce依赖安装:yuminstall-yyum-utilsdevice-mapper-persistent-datalvm2wget添加docker软件源:yum-config-manager--add-repohttps://mirrors.aliyun.co......
  • 华为云ubuntu1804安装rancherk8s
    apt-getinstalldocker.iodockerversionClient:Version:20.10.21APIversion:1.41Goversion:go1.18.1Gitcommit:20.10.21-0ubuntu1~18.04.3Built:ThuApr2705:50:212023OS/Arch:linux/amd6......