暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

基于AWS EKS的K8S实践 - 通过 Agent收集日志

604



Hi~朋友,关注置顶防止错过消息


基于AWS EKS的K8S实践系列文章是基于企业级的实战文章,一些设置信息需要根据公司自身要求进行设置,如果大家有问题讨论或咨询可以加我微信(公众号后台回复 程序员修炼笔记 可获取联系方式)

基于SideCar的容器

基于SideCar的容器灵活程度相对较高,但每个Pod多一个容器也是对资源的消耗。

基于Agent的容器收集方案

基于Agent的日志虽然将所有配置都放在了一个ConfigMap中,可能配置会比较冗长(通过filebeat.autodiscover进行解决),但由于他是DaemoSet的形式,将会极大程度的降低资源的损耗。

配置kube-system的filebeat-config ConfigMap

修改ConfigMap,配置filebeat-config,这里相当于修改filebeat的配置文件如下:

由于我们线上开了证书认证,因此我这里还需要存储一下证书,用于DaemonSet 在连接的时候使用,另外我们这里选择的 input 是 container,可以看到在我这里我分别采集了我们 nginx 的日志(我们的 nginx 日志是个 json 格式)和 xxx-app 的日志, 如果你们的日志格式都很统一,且日志处理方式也类似,推荐使用filebeat.autodiscover,这个可能会大大减少你的配置文件的书写。

  1. apiVersion: v1

  2. kind: ConfigMap

  3. metadata:

  4. name: filebeat-config

  5. namespace: kube-system

  6. labels:

  7. k8s-app: filebeat

  8. data:

  9. elasticsearch-ca.pem: |-

  10. -----BEGIN CERTIFICATE-----

  11. XXXXXXX

  12. -----END CERTIFICATE-----

  13. filebeat.yml: |-

  14. filebeat.inputs:

  15. - type: container

  16. ignore_older: 1h

  17. paths:

  18. - /var/log/containers/ingress-nginx-controller*.log

  19. fields:

  20. project: k8s-nginx

  21. json.keys_under_root: true

  22. json.add_error_key: true

  23. json.overwrite_keys: true

  24. json.expand_keys: true


  25. - type: container

  26. paths:

  27. - "/var/log/containers/xxx-app*.log"

  28. multiline:

  29. type: pattern

  30. pattern: '^\d{4}-\d{2}-\d{2}\s\d{2}\:\d{2}\:\d{2}\.\d{3}'

  31. negate: true

  32. match: after

  33. fields:

  34. project: xxx-app

  35. pipeline: application-service-log-pipeline


  36. output.elasticsearch:

  37. hosts: ['${ELASTICSEARCH_HOST:elasticsearch}:${ELASTICSEARCH_PORT:9200}']

  38. username: ${ELASTICSEARCH_USERNAME}

  39. password: ${ELASTICSEARCH_PASSWORD}

  40. allow_older_versions: "true"

  41. protocol: "https"

  42. ssl.certificate_authorities: /etc/filebeat/certs/elasticsearch-ca.pem

  43. indices:

  44. - index: "%{[fields.project]}-%{+yyyy.MM.dd}"

修改DaemonSet

这里我主要修改了污点容忍,这是为了让我 DaemonSet 的 FileBeat 能够在有污点的节点上运行,另外还需要设置ES 的用户名、密码以及端口,以及SSL 证书,如下(下面的示例只是我这里和官网上配置不通的地方,完整的配置可以见文章末尾):

  1. apiVersion: apps/v1

  2. kind: DaemonSet

  3. metadata:

  4. name: filebeat

  5. namespace: kube-system

  6. labels:

  7. k8s-app: filebeat

  8. spec:

  9. ...

  10. spec:

  11. ...

  12. tolerations:

  13. - key: subnet-type.kubernetes.io

  14. operator: Equal

  15. value: public

  16. effect: NoSchedule

  17. containers:

  18. - name: filebeat

  19. image: docker.elastic.co/beats/filebeat:8.6.1

  20. args: [

  21. "-c", "/etc/filebeat.yml",

  22. "-e",

  23. ]

  24. env:

  25. - name: ELASTICSEARCH_HOST

  26. value: xxx

  27. - name: ELASTICSEARCH_PORT

  28. value: "9200"

  29. - name: ELASTICSEARCH_USERNAME

  30. value: xxxx

  31. - name: ELASTICSEARCH_PASSWORD

  32. value: xxxxx

  33. ...

  34. volumeMounts:

  35. - name: config

  36. mountPath: /etc/filebeat/certs/elasticsearch-ca.pem

  37. readOnly: true

  38. subPath: elasticsearch-ca.pem

  39. ...

  40. volumes:

  41. - name: config

  42. configMap:

  43. defaultMode: 0640

  44. name: filebeat-config

  45. ...

完整 FileBeat DaemonSet 的部署 Yaml

下面的 Yaml把上面我提到的地方按照你们自己的配置修改以后,直接运行就可以将 FileBeat 进行部署,部署成功以后如果有新的日志采集需要加入,只需要修改 ConfigMap,然后重新部署 DaemonSet 即可。

  1. apiVersion: v1

  2. kind: ServiceAccount

  3. metadata:

  4. name: filebeat

  5. namespace: kube-system

  6. labels:

  7. k8s-app: filebeat


  8. ---


  9. apiVersion: rbac.authorization.k8s.io/v1

  10. kind: ClusterRole

  11. metadata:

  12. name: filebeat

  13. labels:

  14. k8s-app: filebeat

  15. rules:

  16. - apiGroups: [""] # "" indicates the core API group

  17. resources:

  18. - namespaces

  19. - pods

  20. - nodes

  21. verbs:

  22. - get

  23. - watch

  24. - list

  25. - apiGroups: ["apps"]

  26. resources:

  27. - replicasets

  28. verbs: ["get", "list", "watch"]

  29. - apiGroups: ["batch"]

  30. resources:

  31. - jobs

  32. verbs: ["get", "list", "watch"]


  33. ---

  34. apiVersion: rbac.authorization.k8s.io/v1

  35. kind: Role

  36. metadata:

  37. name: filebeat

  38. # should be the namespace where filebeat is running

  39. namespace: kube-system

  40. labels:

  41. k8s-app: filebeat

  42. rules:

  43. - apiGroups:

  44. - coordination.k8s.io

  45. resources:

  46. - leases

  47. verbs: ["get", "create", "update"]


  48. ---

  49. apiVersion: rbac.authorization.k8s.io/v1

  50. kind: Role

  51. metadata:

  52. name: filebeat-kubeadm-config

  53. namespace: kube-system

  54. labels:

  55. k8s-app: filebeat

  56. rules:

  57. - apiGroups: [""]

  58. resources:

  59. - configmaps

  60. resourceNames:

  61. - kubeadm-config

  62. verbs: ["get"]


  63. ---

  64. apiVersion: rbac.authorization.k8s.io/v1

  65. kind: ClusterRoleBinding

  66. metadata:

  67. name: filebeat

  68. subjects:

  69. - kind: ServiceAccount

  70. name: filebeat

  71. namespace: kube-system

  72. roleRef:

  73. kind: ClusterRole

  74. name: filebeat

  75. apiGroup: rbac.authorization.k8s.io


  76. ---

  77. apiVersion: rbac.authorization.k8s.io/v1

  78. kind: RoleBinding

  79. metadata:

  80. name: filebeat

  81. namespace: kube-system

  82. subjects:

  83. - kind: ServiceAccount

  84. name: filebeat

  85. namespace: kube-system

  86. roleRef:

  87. kind: Role

  88. name: filebeat

  89. apiGroup: rbac.authorization.k8s.io


  90. ---

  91. apiVersion: rbac.authorization.k8s.io/v1

  92. kind: RoleBinding

  93. metadata:

  94. name: filebeat-kubeadm-config

  95. namespace: kube-system

  96. subjects:

  97. - kind: ServiceAccount

  98. name: filebeat

  99. namespace: kube-system

  100. roleRef:

  101. kind: Role

  102. name: filebeat-kubeadm-config

  103. apiGroup: rbac.authorization.k8s.io


  104. ---

  105. apiVersion: v1

  106. kind: ConfigMap

  107. metadata:

  108. name: filebeat-config

  109. namespace: kube-system

  110. labels:

  111. k8s-app: filebeat

  112. data:

  113. elasticsearch-ca.pem: |-

  114. -----BEGIN CERTIFICATE-----

  115. XXXXXXX

  116. -----END CERTIFICATE-----

  117. filebeat.yml: |-

  118. filebeat.inputs:

  119. - type: container

  120. ignore_older: 1h

  121. paths:

  122. - /var/log/containers/ingress-nginx-controller*.log

  123. fields:

  124. project: k8s-nginx

  125. json.keys_under_root: true

  126. json.add_error_key: true

  127. json.overwrite_keys: true

  128. json.expand_keys: true


  129. - type: container

  130. paths:

  131. - "/var/log/containers/xxx-app*.log"

  132. multiline:

  133. type: pattern

  134. pattern: '^\d{4}-\d{2}-\d{2}\s\d{2}\:\d{2}\:\d{2}\.\d{3}'

  135. negate: true

  136. match: after

  137. fields:

  138. project: xxx-app

  139. pipeline: application-service-log-pipeline


  140. output.elasticsearch:

  141. hosts: ['${ELASTICSEARCH_HOST:elasticsearch}:${ELASTICSEARCH_PORT:9200}']

  142. username: ${ELASTICSEARCH_USERNAME}

  143. password: ${ELASTICSEARCH_PASSWORD}

  144. allow_older_versions: "true"

  145. protocol: "https"

  146. ssl.certificate_authorities: /etc/filebeat/certs/elasticsearch-ca.pem

  147. indices:

  148. - index: "%{[fields.project]}-%{+yyyy.MM.dd}"


  149. ---

  150. apiVersion: apps/v1

  151. kind: DaemonSet

  152. metadata:

  153. name: filebeat

  154. namespace: kube-system

  155. labels:

  156. k8s-app: filebeat

  157. spec:

  158. selector:

  159. matchLabels:

  160. k8s-app: filebeat

  161. template:

  162. metadata:

  163. labels:

  164. k8s-app: filebeat

  165. spec:

  166. serviceAccountName: filebeat

  167. terminationGracePeriodSeconds: 30

  168. hostNetwork: true

  169. dnsPolicy: ClusterFirstWithHostNet

  170. tolerations:

  171. - key: subnet-type.kubernetes.io

  172. operator: Equal

  173. value: public

  174. effect: NoSchedule

  175. containers:

  176. - name: filebeat

  177. image: docker.elastic.co/beats/filebeat:8.6.1

  178. args: [

  179. "-c", "/etc/filebeat.yml",

  180. "-e",

  181. ]

  182. env:

  183. - name: ELASTICSEARCH_HOST

  184. value: xxx

  185. - name: ELASTICSEARCH_PORT

  186. value: "9200"

  187. - name: ELASTICSEARCH_USERNAME

  188. value: xxxx

  189. - name: ELASTICSEARCH_PASSWORD

  190. value: xxxxx

  191. - name: NODE_NAME

  192. valueFrom:

  193. fieldRef:

  194. fieldPath: spec.nodeName

  195. securityContext:

  196. runAsUser: 0

  197. # If using Red Hat OpenShift uncomment this:

  198. #privileged: true

  199. resources:

  200. limits:

  201. memory: 200Mi

  202. requests:

  203. cpu: 100m

  204. memory: 100Mi

  205. volumeMounts:

  206. - name: config

  207. mountPath: /etc/filebeat.yml

  208. readOnly: true

  209. subPath: filebeat.yml

  210. - name: config

  211. mountPath: /etc/filebeat/certs/elasticsearch-ca.pem

  212. readOnly: true

  213. subPath: elasticsearch-ca.pem

  214. - name: data

  215. mountPath: /usr/share/filebeat/data

  216. - name: varlibdockercontainers

  217. mountPath: /var/lib/docker/containers

  218. readOnly: true

  219. - name: varlog

  220. mountPath: /var/log

  221. readOnly: true

  222. volumes:

  223. - name: config

  224. configMap:

  225. defaultMode: 0640

  226. name: filebeat-config

  227. - name: varlibdockercontainers

  228. hostPath:

  229. path: /var/lib/docker/containers

  230. - name: varlog

  231. hostPath:

  232. path: /var/log

  233. # data folder stores a registry of read status for all files, so we don't send everything again on a Filebeat pod restart

  234. - name: data

  235. hostPath:

  236. # When filebeat runs as non-root user, this directory needs to be writable by group (g+w).

  237. path: /var/lib/filebeat-data

  238. type: DirectoryOrCreate



【知识星球招募】


  知识星球正在初期搭建中,在星球中可以互相提问,分享经验,我也会在星球中分享基础技术架构的全流程搭建,包括但不仅限于DevOps,监控、Flink开发,Java开发(JVM、多线程、MySQL数据库)等相关技术类文章,期待大家的加入




文章转载自程序员修炼笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论