[kubernetes]-filebeat以sidecar模式收集pod日志

本是古典 何须时尚 2022-12-01 11:58 286阅读 0赞

目的: 收集测试环境日志到kibana

定义日志收集相关配置的一个通配configmap

test-filebeat-config.yaml

  1. #定义日志收集相关配置的一个通配configmap
  2. ---
  3. apiVersion: v1
  4. kind: ConfigMap
  5. metadata:
  6. name: test-filebeat-config
  7. namespace: test
  8. labels:
  9. config: test-filebeat-config
  10. data:
  11. filebeat.yml: |-
  12. filebeat.prospectors:
  13. - type: log
  14. paths:
  15. - /data/tomcat-haozhuo-ec/json.info.*.log
  16. json.keys_under_root: true
  17. json.overwrite_keys: true
  18. tags: ["info","ec"]
  19. - type: log
  20. enabled: true
  21. paths:
  22. - /data/tomcat-haozhuo-ec/json.error.*.log
  23. json.keys_under_root: true
  24. json.overwrite_keys: true
  25. tags: ["error","ec"]
  26. - type: log
  27. paths:
  28. - /data/tomcat-haozhuo-basic/json.info.*.log
  29. json.keys_under_root: true
  30. json.overwrite_keys: true
  31. tags: ["info","basic"]
  32. - type: log
  33. enabled: true
  34. paths:
  35. - /data/tomcat-haozhuo-basic/json.error.*.log
  36. json.keys_under_root: true
  37. json.overwrite_keys: true
  38. tags: ["error","basic"]
  39. - type: log
  40. paths:
  41. - /data/tomcat-haozhuo-deep/json.info.*.log
  42. json.keys_under_root: true
  43. json.overwrite_keys: true
  44. tags: ["info","deep"]
  45. - type: log
  46. enabled: true
  47. paths:
  48. - /data/tomcat-haozhuo-deep/json.error.*.log
  49. json.keys_under_root: true
  50. json.overwrite_keys: true
  51. tags: ["error","deep"]
  52. - type: log
  53. paths:
  54. - /data/tomcat-haozhuo-video/json.info.*.log
  55. json.keys_under_root: true
  56. json.overwrite_keys: true
  57. tags: ["info","video"]
  58. - type: log
  59. enabled: true
  60. paths:
  61. - /data/tomcat-haozhuo-video/json.error.*.log
  62. json.keys_under_root: true
  63. json.overwrite_keys: true
  64. tags: ["error","video"]
  65. - type: log
  66. paths:
  67. - /data/tomcat-haozhuo-wx/json.info.*.log
  68. json.keys_under_root: true
  69. json.overwrite_keys: true
  70. tags: ["info","wx"]
  71. - type: log
  72. enabled: true
  73. paths:
  74. - /data/tomcat-haozhuo-wx/json.error.*.log
  75. json.keys_under_root: true
  76. json.overwrite_keys: true
  77. tags: ["error","wx"]
  78. - type: log
  79. paths:
  80. - /data/tomcat-haozhuo-system/json.info.*.log
  81. json.keys_under_root: true
  82. json.overwrite_keys: true
  83. tags: ["info","system"]
  84. - type: log
  85. enabled: true
  86. paths:
  87. - /data/tomcat-haozhuo-system/json.error.*.log
  88. json.keys_under_root: true
  89. json.overwrite_keys: true
  90. tags: ["error","system"]
  91. - type: log
  92. paths:
  93. - /data/tomcat-haozhuo-shoppingmall/json.info.*.log
  94. json.keys_under_root: true
  95. json.overwrite_keys: true
  96. tags: ["info","shoppingmall"]
  97. - type: log
  98. enabled: true
  99. paths:
  100. - /data/tomcat-haozhuo-shoppingmall/json.error.*.log
  101. json.keys_under_root: true
  102. json.overwrite_keys: true
  103. tags: ["error","shoppingmall"]
  104. - type: log
  105. paths:
  106. - /data/tomcat-haozhuo-reportContent/json.info.*.log
  107. json.keys_under_root: true
  108. json.overwrite_keys: true
  109. tags: ["info","reportContent"]
  110. - type: log
  111. enabled: true
  112. paths:
  113. - /data/tomcat-haozhuo-reportContent/json.error.*.log
  114. json.keys_under_root: true
  115. json.overwrite_keys: true
  116. tags: ["error","reportContent"]
  117. - type: log
  118. paths:
  119. - /data/tomcat-haozhuo-point/json.info.*.log
  120. json.keys_under_root: true
  121. json.overwrite_keys: true
  122. tags: ["info","point"]
  123. - type: log
  124. enabled: true
  125. paths:
  126. - /data/tomcat-haozhuo-point/json.error.*.log
  127. json.keys_under_root: true
  128. json.overwrite_keys: true
  129. tags: ["error","point"]
  130. - type: log
  131. paths:
  132. - /data/tomcat-haozhuo-openapi/json.info.*.log
  133. json.keys_under_root: true
  134. json.overwrite_keys: true
  135. tags: ["info","openapi"]
  136. - type: log
  137. enabled: true
  138. paths:
  139. - /data/tomcat-haozhuo-openapi/json.error.*.log
  140. json.keys_under_root: true
  141. json.overwrite_keys: true
  142. tags: ["error","openapi"]
  143. - type: log
  144. paths:
  145. - /data/tomcat-haozhuo-job/json.info.*.log
  146. json.keys_under_root: true
  147. json.overwrite_keys: true
  148. tags: ["info","job"]
  149. - type: log
  150. enabled: true
  151. paths:
  152. - /data/tomcat-haozhuo-job/json.error.*.log
  153. json.keys_under_root: true
  154. json.overwrite_keys: true
  155. tags: ["error","job"]
  156. - type: log
  157. paths:
  158. - /data/tomcat-haozhuo-info/json.info.*.log
  159. json.keys_under_root: true
  160. json.overwrite_keys: true
  161. tags: ["info","info"]
  162. - type: log
  163. enabled: true
  164. paths:
  165. - /data/tomcat-haozhuo-info/json.error.*.log
  166. json.keys_under_root: true
  167. json.overwrite_keys: true
  168. tags: ["error","info"]
  169. - type: log
  170. paths:
  171. - /data/tomcat-haozhuo-hm/json.info.*.log
  172. json.keys_under_root: true
  173. json.overwrite_keys: true
  174. tags: ["info","hm"]
  175. - type: log
  176. enabled: true
  177. paths:
  178. - /data/tomcat-haozhuo-hm/json.error.*.log
  179. json.keys_under_root: true
  180. json.overwrite_keys: true
  181. tags: ["error","hm"]
  182. - type: log
  183. paths:
  184. - /data/tomcat-haozhuo-health/json.info.*.log
  185. json.keys_under_root: true
  186. json.overwrite_keys: true
  187. tags: ["info","health"]
  188. - type: log
  189. enabled: true
  190. paths:
  191. - /data/tomcat-haozhuo-health/json.error.*.log
  192. json.keys_under_root: true
  193. json.overwrite_keys: true
  194. tags: ["error","health"]
  195. - type: log
  196. paths:
  197. - /data/tomcat-haozhuo-family/json.info.*.log
  198. json.keys_under_root: true
  199. json.overwrite_keys: true
  200. tags: ["info","family"]
  201. - type: log
  202. enabled: true
  203. paths:
  204. - /data/tomcat-haozhuo-family/json.error.*.log
  205. json.keys_under_root: true
  206. json.overwrite_keys: true
  207. tags: ["error","family"]
  208. - type: log
  209. paths:
  210. - /data/tomcat-haozhuo-disc/json.info.*.log
  211. json.keys_under_root: true
  212. json.overwrite_keys: true
  213. tags: ["info","disc"]
  214. - type: log
  215. enabled: true
  216. paths:
  217. - /data/tomcat-haozhuo-disc/json.error.*.log
  218. json.keys_under_root: true
  219. json.overwrite_keys: true
  220. tags: ["error","disc"]
  221. - type: log
  222. paths:
  223. - /data/tomcat-haozhuo-check/json.info.*.log
  224. json.keys_under_root: true
  225. json.overwrite_keys: true
  226. tags: ["info","check"]
  227. - type: log
  228. enabled: true
  229. paths:
  230. - /data/tomcat-haozhuo-check/json.error.*.log
  231. json.keys_under_root: true
  232. json.overwrite_keys: true
  233. tags: ["error","check"]
  234. - type: log
  235. paths:
  236. - /data/tomcat-haozhuo-card/json.info.*.log
  237. json.keys_under_root: true
  238. json.overwrite_keys: true
  239. tags: ["info","card"]
  240. - type: log
  241. enabled: true
  242. paths:
  243. - /data/tomcat-haozhuo-card/json.error.*.log
  244. json.keys_under_root: true
  245. json.overwrite_keys: true
  246. tags: ["error","card"]
  247. - type: log
  248. paths:
  249. - /data/tomcat-haozhuo-appServer/json.info.*.log
  250. json.keys_under_root: true
  251. json.overwrite_keys: true
  252. tags: ["info","appServer"]
  253. - type: log
  254. enabled: true
  255. paths:
  256. - /data/tomcat-haozhuo-appServer/json.error.*.log
  257. json.keys_under_root: true
  258. json.overwrite_keys: true
  259. tags: ["error","appServer"]
  260. - type: log
  261. paths:
  262. - /data/tomcat-haozhuo-admin/json.info.*.log
  263. json.keys_under_root: true
  264. json.overwrite_keys: true
  265. tags: ["info","admin"]
  266. - type: log
  267. enabled: true
  268. paths:
  269. - /data/tomcat-haozhuo-admin/json.error.*.log
  270. json.keys_under_root: true
  271. json.overwrite_keys: true
  272. tags: ["error","admin"]
  273. - type: log
  274. paths:
  275. - /data/tomcat-haozhuo-account/json.info.*.log
  276. json.keys_under_root: true
  277. json.overwrite_keys: true
  278. tags: ["info","account"]
  279. - type: log
  280. enabled: true
  281. paths:
  282. - /data/tomcat-haozhuo-account/json.error.*.log
  283. json.keys_under_root: true
  284. json.overwrite_keys: true
  285. tags: ["error","account"]
  286. - type: log
  287. paths:
  288. - /data/tomcat-haozhuo-hmp/json.info.*.log
  289. json.keys_under_root: true
  290. json.overwrite_keys: true
  291. tags: ["info","hmp"]
  292. - type: log
  293. enabled: true
  294. paths:
  295. - /data/tomcat-haozhuo-hmp/json.error.*.log
  296. json.keys_under_root: true
  297. json.overwrite_keys: true
  298. tags: ["error","hmp"]
  299. - type: log
  300. paths:
  301. - /data/tomcat-haozhuo-search/json.info.*.log
  302. json.keys_under_root: true
  303. json.overwrite_keys: true
  304. tags: ["info","search"]
  305. - type: log
  306. enabled: true
  307. paths:
  308. - /data/tomcat-haozhuo-search/json.error.*.log
  309. json.keys_under_root: true
  310. json.overwrite_keys: true
  311. tags: ["error","search"]
  312. - type: log
  313. paths:
  314. - /data/tomcat-haozhuo-service_center/info.*.log
  315. json.keys_under_root: true
  316. json.overwrite_keys: true
  317. tags: ["info","service_center"]
  318. - type: log
  319. enabled: true
  320. paths:
  321. - /data/tomcat-haozhuo-service_center/error.*.log
  322. json.keys_under_root: true
  323. json.overwrite_keys: true
  324. tags: ["error","service_center"]
  325. - type: log
  326. paths:
  327. - /data/tomcat-haozhuo-appoint_center/info.*.log
  328. json.keys_under_root: true
  329. json.overwrite_keys: true
  330. tags: ["info","appoint_center"]
  331. - type: log
  332. enabled: true
  333. paths:
  334. - /data/tomcat-haozhuo-appoint_center/error.*.log
  335. json.keys_under_root: true
  336. json.overwrite_keys: true
  337. tags: ["error","appoint_center"]
  338. filebeat.config.modules:
  339. path: ${ path.config}/modules.d/*.yml
  340. reload.enabled: false
  341. setup.template.enabled: false
  342. setup.template.settings:
  343. index.number_of_shards: 3
  344. setup.template.name: "filebeat"
  345. setup.template.fields: "fields.yml"
  346. setup.template.overwrite: false
  347. setup.kibana:
  348. output.elasticsearch:
  349. hosts: ["192.168.1.165:9200"]
  350. index: "jsonlog-%{+yyyy.MM.dd}"

创建一个statefulset部署应用服务。filebeat以sidecar形式一起部署 StatefulSet.yaml

  1. apiVersion: apps/v1
  2. kind: StatefulSet
  3. metadata:
  4. # deployment名字 和svc 和ingress绑定没关系
  5. name: haozhuo-account-dp
  6. namespace: test
  7. spec:
  8. serviceName: haozhuo-account
  9. replicas: 1
  10. # 在定义模板的时候必须定义labels,因为Deployment.spec.selector是必须字段,而他又必须和template.labels对应
  11. selector:
  12. matchLabels:
  13. app: haozhuo-account
  14. # template里面定义的内容会应用到下面所有的副本集里面(例如depolyment下的pod),在template.spec.containers里面不能定义labels标签。可以kubectl get pods --show-labels查看
  15. template:
  16. metadata:
  17. labels:
  18. app: haozhuo-account
  19. env: test
  20. spec:
  21. containers:
  22. # 20200822
  23. - image: registry.cn-shanghai.aliyuncs.com/yjk-datag/filebeat:6 # 提前下载下来到私有镜像库的镜像(官方的可能会被墙)
  24. name: filebeat
  25. args: [
  26. "-c", "/srv/filebeat/filebeat.yml",
  27. "-e",
  28. ]
  29. env:
  30. - name: POD_IP
  31. valueFrom:
  32. fieldRef:
  33. apiVersion: v1
  34. fieldPath: status.podIP
  35. - name: pod_name
  36. valueFrom:
  37. fieldRef:
  38. apiVersion: v1
  39. fieldPath: metadata.name
  40. - name: POD_IP
  41. valueFrom:
  42. fieldRef:
  43. apiVersion: v1
  44. fieldPath: status.podIP
  45. - name: pod_name
  46. valueFrom:
  47. fieldRef:
  48. apiVersion: v1
  49. fieldPath: metadata.name
  50. # securityContext:
  51. # runAsUser: 0
  52. resources:
  53. limits:
  54. memory: 200Mi
  55. requests:
  56. cpu: 200m
  57. memory: 200Mi
  58. volumeMounts:
  59. - name: config # 将configmap的内容放到容器本地目录
  60. mountPath: /srv/filebeat/
  61. - name: data
  62. mountPath: /usr/share/filebeat/data
  63. - name: logdata # 同一个pod内的两个应用共享目录logdata, 一个写一个读
  64. mountPath: /data/tomcat-haozhuo-account/
  65. # 20200822
  66. # containers名字 和svc 和ingress绑定没关系
  67. - name: haozhuo-account
  68. image: registry.cn-shanghai.aliyuncs.com/yjk-datag/k8s-haozhuo-account:v28
  69. #image: registry.cn-shanghai.aliyuncs.com/yjk-datag/haozhuo-video:vtest06
  70. env:
  71. - name: DUBBO_IP_TO_REGISTRY
  72. valueFrom:
  73. fieldRef:
  74. fieldPath: status.hostIP
  75. - name: DUBBO_PORT_TO_REGISTRY
  76. value: "20901"
  77. resources:
  78. limits:
  79. cpu: 2000m
  80. memory: 2048Mi
  81. requests:
  82. cpu: 1000m
  83. memory: 1024Mi
  84. ports:
  85. - name: jacoco-port
  86. containerPort: 6309
  87. - name: liveness-port
  88. containerPort: 8080
  89. # 使用了 hostPort 的容器只能调度到端口不冲突的 Node 上,除非有必要(比如运行一些系统级的 daemon 服务),不建议使用端口映射功能。
  90. # 跟 hostPort 一样的原因,避免使用 hostNetwork。
  91. # hostPorts:
  92. # - min: 8000
  93. # max: 8080
  94. hostPort: 8801
  95. - name: readiness-port
  96. containerPort: 20901
  97. hostPort: 20901
  98. # 后续需要jacoco端口也暴露一下
  99. livenessProbe:
  100. tcpSocket:
  101. port: liveness-port
  102. initialDelaySeconds: 20
  103. periodSeconds: 3
  104. readinessProbe:
  105. tcpSocket:
  106. port: readiness-port
  107. initialDelaySeconds: 30
  108. periodSeconds: 10
  109. # 20200822
  110. volumeMounts:
  111. - name: logdata # 指定挂在目录到logdata 同一个pod内的两个应用共享目录logdata, 一个写一个读
  112. # 我这里要挂载应用容器haozhuo-account中/srv/tomcat/tomcat-haozhuo-account/yjklogs/里面的*.log到filebeat容器的/data/tomcat-haozhuo-account/下
  113. mountPath: /srv/tomcat/tomcat-haozhuo-account/yjklogs/
  114. volumes:
  115. - name: data
  116. emptyDir: { }
  117. - name: logdata #定义logdata为EmptyDir类型挂载目录
  118. emptyDir: { }
  119. - name: config
  120. configMap:
  121. name: test-filebeat-config #使用前面定义的configmap
  122. items:
  123. - key: filebeat.yml
  124. path: filebeat.yml
  125. # 20200822
  126. imagePullSecrets:
  127. - name: aliyun-ns-test

导入并上传镜像

  1. docker load < filebeat6.tar.gz
  2. docker tag prima/filebeat:6 registry.cn-shanghai.aliyuncs.com/yjk-datag/filebeat:6
  3. docker push registry.cn-shanghai.aliyuncs.com/yjk-datag/filebeat:6

format_png

应用yaml

  1. kubectl apply -f StatefulSet.yaml

等到容器启动完成

进入容器需要 -c 指定容器name

  1. # 进入应用容器
  2. kubectl exec -it haozhuo-account-dp-0 -c haozhuo-account /bin/bash -n test
  3. # 进入filebeat容器
  4. kubectl exec -it haozhuo-account-dp-0 -c filebeat /bin/bash -n test
  5. # 如果不使用-c的话会进入sidecar容器

format_png 1

format_png 2

手动输出error到error日志或者info日志

  1. echo '{"@timestamp":"2020-08-22T15:59:10.014+08:00","@version":1,"message":"请求url:http://192.168.1.164:8030/dingtalk/order/saveOrderDataRecord 入参:","logger_name":"com.haozhuo.common.sys.http.HttpRequest","thread_name":"account-updateOrderBizData_Worker-1","level":"INFO","level_value":20000,"HOSTNAME":"haozhuo-account-dp-0.haozhuo-account.test.svc.cluster.local","tid":"6494f345-b18e-40ac-b4fc-baf851cab191"} ' >> /data/tomcat-haozhuo-account/json.info.2020-08-22.log

观察filebeat 发现采集成功

format_png 3

  1. kubectl logs -f haozhuo-account-dp-0 filebeat -n test
  2. kubectl logs -f haozhuo-account-dp-0 haozhuo-account -n test

format_png 4

发表评论

表情:
评论列表 (有 0 条评论,286人围观)

还没有评论,来说两句吧...

相关阅读