实现原理

基于k8s的日志收集主要有两种方案,一是使用daemoset,另一种是基于sidecar。两种方式各有优缺点,目前DHorse是基于daemoset实现的。如图1所示:

图1

在每个k8s集群中启动一个daemoset组件,即Filebeat的服务,监控/var/log/containers目录下的日志文件变动,然后把日志内容推送到ELK集群。

DHorse日志配置

在DHorse的安装目录conf子目录下,可以通过filebeat-k8s.yml文件进行日志收集的相关配置,如下:

---
apiVersion: v1
kind: ConfigMap
metadata:
# 默认名称,不允许修改
name: filebeat-config
# 默认命名空间,不允许修改
namespace: dhorse-system
labels:
# 默认标签名,不允许修改
app: filebeat
data:
filebeat.yml: |-
filebeat.inputs:
- type: container
paths:
- /var/log/containers/*.log
processors:
- add_kubernetes_metadata:
host: ${NODE_NAME}
matchers:
- logs_path:
logs_path: "/var/log/containers/" # To enable hints based autodiscover, remove `filebeat.inputs` configuration and uncomment this:
#filebeat.autodiscover:
# providers:
# - type: kubernetes
# node: ${NODE_NAME}
# hints.enabled: true
# hints.default_config:
# type: container
# paths:
# - /var/log/containers/*${data.kubernetes.container.id}.log processors:
- add_cloud_metadata:
- add_host_metadata: cloud.id: ${ELASTIC_CLOUD_ID}
cloud.auth: ${ELASTIC_CLOUD_AUTH} output.elasticsearch:
hosts: ['${ELASTICSEARCH_HOST:elasticsearch}:${ELASTICSEARCH_PORT:9200}']
username: ${ELASTICSEARCH_USERNAME}
password: ${ELASTICSEARCH_PASSWORD}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
# 默认名称,不允许修改
name: filebeat
# 默认命名空间,不允许修改
namespace: dhorse-system
labels:
# 默认标签名,不允许修改
app: filebeat
spec:
selector:
matchLabels:
# 默认标签名,不允许修改
app: filebeat
template:
metadata:
labels:
# 默认标签名,不允许修改
app: filebeat
spec:
terminationGracePeriodSeconds: 30
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet
containers:
- name: filebeat
# 替换成你自己的filebeat镜像
image: docker.elastic.co/beats/filebeat:8.1.0
args: [
"-c", "/etc/filebeat.yml",
"-e",
]
#替换成你自己的es地址和账号
env:
- name: ELASTICSEARCH_HOST
value: 127.0.0.1
- name: ELASTICSEARCH_PORT
value: "9200"
- name: ELASTICSEARCH_USERNAME
value: elastic
- name: ELASTICSEARCH_PASSWORD
value: changeme
- name: ELASTIC_CLOUD_ID
value:
- name: ELASTIC_CLOUD_AUTH
value:
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
securityContext:
runAsUser: 0
# If using Red Hat OpenShift uncomment this:
#privileged: true
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 100Mi
volumeMounts:
- name: config
mountPath: /etc/filebeat.yml
readOnly: true
subPath: filebeat.yml
- name: data
mountPath: /usr/share/filebeat/data
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
- name: varlog
mountPath: /var/log
readOnly: true
- name: hosttime
mountPath: /etc/localtime
readOnly: true
volumes:
- name: config
configMap:
defaultMode: 0640
name: filebeat-config
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
- name: varlog
hostPath:
path: /var/log
- name: hosttime
hostPath:
path: /etc/localtime
# data folder stores a registry of read status for all files, so we don't send everything again on a Filebeat pod restart
- name: data
hostPath:
# When filebeat runs as non-root user, this directory needs to be writable by group (g+w).
path: /var/lib/filebeat-data
type: DirectoryOrCreate

然后,需要开启目标集群的日志开关即可,如图2所示:

图2

最新文章

  1. Erlang 虚拟机内的内存管理(Lukas Larsson演讲听写稿)
  2. CSS 概念 & 作用
  3. Play with docker 1.12
  4. 【转】Swift开源项目精选
  5. petapoco-SQLServer模型增加注释
  6. Html5 Geolocation获取地理位置信息
  7. Bootstrap页面布局14 - BS按钮群组
  8. c#读写文本文档-1-用file类
  9. NSBundle 的使用
  10. apple wwdc resource
  11. 在云服务器搭建WordPress博客(三)安装WordPress
  12. MATLAB-ginput函数问题
  13. UFLDL课程学习(二)
  14. Android - 设置adb的usb连接配置
  15. 【机器学习笔记之七】PCA 的数学原理和可视化效果
  16. 函数原型属性-JavaScript深入浅出(三)
  17. Linux下进程间通信--共享内存:最快的进程间通信方式
  18. Windows下return,exit和ExitProcess的区别和分析
  19. DataStream_操作基本类型数据的流对象
  20. SortedMap与TreeMap的一个典型应用

热门文章

  1. td-agent的v2,v3,v4版本区别
  2. 我操作MySQL的惊险一幕
  3. 从零开始学Graph Database:什么是图
  4. Centos7使用sendEmail-v1.56发送邮件
  5. Docker | redis安装及测试
  6. 基于win11安装Java11环境
  7. 在Centos7上安装JDK1.8和tomcat8.5的步骤和过程(亲测可用)
  8. python uiautomator
  9. NC 使用Nginx实现https的反向代理
  10. CC1,3,6回顾