经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Kubernetes » 查看文章
DHorse日志收集原理
来源:cnblogs  作者:tiandizhiguai  时间:2022/12/12 13:07:08  对本文有异议

实现原理

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

图1

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

DHorse日志配置

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

  1. ---
  2. apiVersion: v1
  3. kind: ConfigMap
  4. metadata:
  5. # 默认名称,不允许修改
  6. name: filebeat-config
  7. # 默认命名空间,不允许修改
  8. namespace: dhorse-system
  9. labels:
  10. # 默认标签名,不允许修改
  11. app: filebeat
  12. data:
  13. filebeat.yml: |-
  14. filebeat.inputs:
  15. - type: container
  16. paths:
  17. - /var/log/containers/*.log
  18. processors:
  19. - add_kubernetes_metadata:
  20. host: ${NODE_NAME}
  21. matchers:
  22. - logs_path:
  23. logs_path: "/var/log/containers/"
  24. # To enable hints based autodiscover, remove `filebeat.inputs` configuration and uncomment this:
  25. #filebeat.autodiscover:
  26. # providers:
  27. # - type: kubernetes
  28. # node: ${NODE_NAME}
  29. # hints.enabled: true
  30. # hints.default_config:
  31. # type: container
  32. # paths:
  33. # - /var/log/containers/*${data.kubernetes.container.id}.log
  34. processors:
  35. - add_cloud_metadata:
  36. - add_host_metadata:
  37. cloud.id: ${ELASTIC_CLOUD_ID}
  38. cloud.auth: ${ELASTIC_CLOUD_AUTH}
  39. output.elasticsearch:
  40. hosts: ['${ELASTICSEARCH_HOST:elasticsearch}:${ELASTICSEARCH_PORT:9200}']
  41. username: ${ELASTICSEARCH_USERNAME}
  42. password: ${ELASTICSEARCH_PASSWORD}
  43. ---
  44. apiVersion: apps/v1
  45. kind: DaemonSet
  46. metadata:
  47. # 默认名称,不允许修改
  48. name: filebeat
  49. # 默认命名空间,不允许修改
  50. namespace: dhorse-system
  51. labels:
  52. # 默认标签名,不允许修改
  53. app: filebeat
  54. spec:
  55. selector:
  56. matchLabels:
  57. # 默认标签名,不允许修改
  58. app: filebeat
  59. template:
  60. metadata:
  61. labels:
  62. # 默认标签名,不允许修改
  63. app: filebeat
  64. spec:
  65. terminationGracePeriodSeconds: 30
  66. hostNetwork: true
  67. dnsPolicy: ClusterFirstWithHostNet
  68. containers:
  69. - name: filebeat
  70. # 替换成你自己的filebeat镜像
  71. image: docker.elastic.co/beats/filebeat:8.1.0
  72. args: [
  73. "-c", "/etc/filebeat.yml",
  74. "-e",
  75. ]
  76. #替换成你自己的es地址和账号
  77. env:
  78. - name: ELASTICSEARCH_HOST
  79. value: 127.0.0.1
  80. - name: ELASTICSEARCH_PORT
  81. value: "9200"
  82. - name: ELASTICSEARCH_USERNAME
  83. value: elastic
  84. - name: ELASTICSEARCH_PASSWORD
  85. value: changeme
  86. - name: ELASTIC_CLOUD_ID
  87. value:
  88. - name: ELASTIC_CLOUD_AUTH
  89. value:
  90. - name: NODE_NAME
  91. valueFrom:
  92. fieldRef:
  93. fieldPath: spec.nodeName
  94. securityContext:
  95. runAsUser: 0
  96. # If using Red Hat OpenShift uncomment this:
  97. #privileged: true
  98. resources:
  99. limits:
  100. memory: 200Mi
  101. requests:
  102. cpu: 100m
  103. memory: 100Mi
  104. volumeMounts:
  105. - name: config
  106. mountPath: /etc/filebeat.yml
  107. readOnly: true
  108. subPath: filebeat.yml
  109. - name: data
  110. mountPath: /usr/share/filebeat/data
  111. - name: varlibdockercontainers
  112. mountPath: /var/lib/docker/containers
  113. readOnly: true
  114. - name: varlog
  115. mountPath: /var/log
  116. readOnly: true
  117. - name: hosttime
  118. mountPath: /etc/localtime
  119. readOnly: true
  120. volumes:
  121. - name: config
  122. configMap:
  123. defaultMode: 0640
  124. name: filebeat-config
  125. - name: varlibdockercontainers
  126. hostPath:
  127. path: /var/lib/docker/containers
  128. - name: varlog
  129. hostPath:
  130. path: /var/log
  131. - name: hosttime
  132. hostPath:
  133. path: /etc/localtime
  134. # data folder stores a registry of read status for all files, so we don't send everything again on a Filebeat pod restart
  135. - name: data
  136. hostPath:
  137. # When filebeat runs as non-root user, this directory needs to be writable by group (g+w).
  138. path: /var/lib/filebeat-data
  139. type: DirectoryOrCreate

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

图2

原文链接:https://www.cnblogs.com/tiandizhiguai/p/16975766.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号