重要
在 Kubernetes 环境中,日志收集是一个常见的需求。通过使用 Filebeat 作为 sidecar 容器,可以方便地收集容器的日志并发送到日志存储系统(如 Elasticsearch)。这种方式可以确保日志收集与主应用容器的解耦,同时保持日志收集的灵活性和高效性。
配置
以下是一个示例,helm-chart配置Filebeat的Deployment模板, 展示如何在 Kubernetes Deployment 中配置 Filebeat 作为 sidecar 容器来收集日志。
Deployment 配置示例:
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 | apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "common.name" . }}
  labels:
    app: {{ template "common.name" . }}
spec:
  selector:
    matchLabels:
      app: {{ template "common.name" . }}
  replicas: {{ .Values.deploymentReplicas }}
  template:
    metadata:
      name: {{ template "common.name" . }}
      annotations:
        container.security.alpha.kubernetes.io/{{- template "common.name" . -}}: "runtime/default"
      labels:
        app: {{ template "common.name" . }}
        release: "{{ .Release.Name }}"
    spec:
      containers:
      {{- if .Values.filebeatSidecar }}
      - name: filebeat
        image: docker.elastic.co/beats/filebeat:7.10.1
        env:
          - name: ELASTICSEARCH_HOST
            value: "elasticsearch"
          - name: ELASTICSEARCH_PORT
            value: "9200"
        volumeMounts:
          - name: log-data
            mountPath: /var/logs/app
          - name: filebeat-config
              mountPath: /usr/share/filebeat/filebeat.yml
              subPath: filebeat.yml
      {{- end }}
      - name: {{ template "common.containers.name" . }}
        image: "{{ .Values.global.imageRepositoryName }}/{{ .Values.imageRepository }}:{{.Chart.AppVersion}}"
        imagePullPolicy: {{ .Values.imagePullPolicy | quote }}
        volumeMounts:
          - name: log-data
            mountPath: /var/log/app
        {{- end }}
      volumes:
        - name: log-data
          emptyDir: {}
        {{- if .Values.filebeatSidecar }}
        - name: filebeat-config
          configMap:
            name: filebeat-config
        {{- end }}
 | 
说明
- 主应用容器:- 主应用容器的 volumeMounts配置了一个名为log-data的卷,挂载到/var/log/app目录。这使得主应用容器可以将日志写入该目录。
 
- Filebeat 容器:- Filebeat 容器使用 {{ $.Values.filebeat.image | quote }}镜像。
- Filebeat 容器的 volumeMounts配置了log-data卷,用于共享主应用容器的日志目录。
- 如果启用了 configMapEnable,Filebeat 容器还会挂载configMapList中定义的配置文件。
 
- 配置卷:- log-data卷是一个- emptyDir,用于在 Pod 内共享日志文件。
- 如果启用了 configMapEnable,会根据configMapList配置文件创建额外的卷。
 
- 条件渲染:- 使用 Helm 模板的条件渲染功能,根据 .Values.filebeatSidecar和.Values.configMapEnable的值动态生成配置。
 
Filebeat 配置文件示例:
| 1
2
3
4
5
6
7
8
 | filebeat.inputs:
- type: log
  enabled: true
  paths:
    - /var/logs/app/*.log
output.elasticsearch:
  hosts: ["${ELASTICSEARCH_HOST}:${ELASTICSEARCH_PORT}"]
 | 
说明
- 输入配置:- filebeat.inputs配置了日志文件的路径,这里指定为- /var/logs/app/*.log。
 
- 输出配置:- output.elasticsearch配置了 Elasticsearch 的地址和端口,使用环境变量- ${ELASTICSEARCH_HOST}和- ${ELASTICSEARCH_PORT}。
 
使用步骤
- 创建 ConfigMap:- 将 Filebeat 的配置文件 filebeat.yml保存到一个 ConfigMap 中。
- 示例命令:| 1
 | kubectl create configmap filebeat-config --from-file=filebeat.yml
 |  
 
 
- 部署应用:- 使用上述 Deployment 配置文件部署应用。
- 示例命令:| 1
 | kubectl apply -f deployment.yaml
 |  
 
 
- 验证日志收集:- 检查 Elasticsearch 中是否收集到了日志。
 
注意事项
- 权限问题:确保 Filebeat 容器有足够的权限访问日志文件。
- 日志路径:根据实际应用的日志路径调整 Filebeat 的配置文件中的 paths。
- Elasticsearch 地址:根据实际部署的 Elasticsearch 地址和端口调整环境变量。
- Helm 模板:确保 Helm 模板中的变量和条件渲染逻辑正确无误。