一、架构流程图
声明:
kafka底层存储使用的是rook-ceph,也可以指定其他存储的storageclass
二、Kafka集群安装使用
1.kafka数据流向原理
- 生产者将消息发送到Kafka后 就会立即通知Zookeeper做watch,当watch到数据变化后就通知消费者去消费消息
- 消费者是主动pull kafka中的消息,这样可以降低Broker的压力,因为Broker中的消息是无状态的,Broker也不知道哪个消息是可以消费的
- 当消费者消费了一条消息后,也必须要去通知ZooKeeper。zookeeper会记录下消费的数据,这样但系统出现问题后就可以还原,可以知道哪些消息已经被消费了
2.集群安装
1 | helm install --name kafka --namespace efk \ |
三、Fluent 安装
1.创建serviceAccount 并绑定
1 | echo " |
2.创建fluent配置文件configMap(注意kafka配置地址)
1 | apiVersion: v1 |
3.创建fluent-daemonset
1 | kind: DaemonSet |
查看kafka topics
- 创建kafka工具
1 | echo ' |
- 查看fluent创建的topics
1 | kubectl -n efk exec testclient -- kafka-topics --zookeeper kafka-zookeeper:2181 --list |
查看fluent生产的k8s集群日志数据
1 | kubectl -n efk exec -ti testclient -- kafka-console-consumer --bootstrap-server kafka:9092 --topic ops.kube-logs-fluentbit.stream.json.001 --from-beginning |
四、安装elasticsearch 7.2集群
1 | apiVersion: v1 |
五、安装logstash
- 下载logstash helm charts
1 | helm fetch stable/logstash |
- 修改value.yaml kafka 配置
1 |
|
- 安装logstash
1 | helm install --name logstash --set persistence.storageClass=rook-ceph-block --namespace efk . |
六、安装Kibana
1 | echo ' |
七、Kibana配置
- 使用kubectl port-foward 代理Kibana 的service
1 | kubectl port-forward service/kibana-logging 5601:5601 -n efk |
- 创建Kibana索引
- 查看日志数据
八、其他
- logstash 时间戳转换
logstash的date过滤插件,支持从字段里分析日期格式,然后放入@timestamp字段里。
1 | filter { |
match
第一个参数是字段名。
第二个参数是格式化模式
第三个参数是要转换的时间格式,如下表
格式 | 示例 | 说明 |
---|---|---|
ISO8601 | 2011-04-19T03:44:01.103Z | 类似 “2018-06-17T03:44:01.103Z” 这样的格式 |
UNIX | 1326149001.132或1326149001 | UNIX 时间戳格式,记录的是从 1970 年起始至今的总秒数 |
TAI64N | @4000000052f88ea32489532c | qmail 会用这个格式 |
UNIX_MS | 64位时间戳 | UNIX_MS这个时间戳则是从 1970 年起始至今的总毫秒数 |
- Kafka 配置Topic过期时间
kafka 默认存放7天数据,如果遇到数据量比较大,空间比较小可以缩短设置数据过期时间
全局设置
1
2log.retention.hours=72
log.cleanup.policy=delete单独对某个Topic设置过期时间
retention.ms=172800000 为二天,单位是毫秒1
kafka-configs --zookeeper kafka-zookeeper:2181 --alter --entity-name ops.kube-logs-fluentbit.stream.json.001 --entity-type topics --add-config retention.ms=172800000
查看某个Topic的过期时间
1
2kafka-configs --zookeeper kafka-zookeeper:2181 --describe --entity-name ops.kube-logs-fluentbit.stream.json.001 --entity-type topics
Configs for topic 'ops.kube-logs-fluentbit.stream.json.001' are retention.ms=172800000立即删除某个Topic下的数据
1
kafka-topics --zookeeper kafka-zookeeper:2181 --alter --topic ops.kube-logs-fluentbit.stream.json.001 --config cleanup.policy=delete
- 本文作者: ChuLinx
- 本文链接: http://yoursite.com/2019/12/26/Kubernetes 日志收集方案部署实践/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!