创建 Filebeat 服务账户和 ClusterRole
apiVersion: v1
kind: Namespace
metadata:
name: logging
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: filebeat
namespace: logging
labels:
k8s-app: filebeat
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: filebeat
namespace: logging
labels:
k8s-app: filebeat
rules:
- apiGroups: [""]
resources:
- namespaces
- pods
- nodes
verbs:
- get
- watch
- list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: filebeat
namespace: logging
subjects:
- kind: ServiceAccount
name: filebeat
namespace: logging
roleRef:
kind: ClusterRole
name: filebeat
apiGroup: rbac.authorization.k8s.io
2. 创建 Filebeat ConfigMap
kind: ConfigMap
apiVersion: v1
metadata:
name: filebeat-config
namespace: logging
labels:
k8s-app: filebeat
data:
filebeat.yml: |-
filebeat.inputs:
- type: filestream
enabled: true
paths:
- /home/leve/logs/*/*.log
- /var/log/leve/test/*/*.log
tags: ["test-log"]
fields:
source: test-log
multiline.pattern: '^\d{4}-\d{2}-\d{2}'
multiline.negate: true
multiline.match: after
index: filebeat-service-test
- type: log
enabled: true
paths:
- /var/log/leve/*/*.log
- /var/log/leve/dev/*/*.log
tags: ["dev-log"]
fields:
source: dev-log
multiline.pattern: '^\d{4}-\d{2}-\d{2}'
multiline.negate: true
multiline.match: after
index: filebeat-service-dev
processors:
- add_host_metadata:
- script:
lang: javascript
source: >
function process(event) {
try {
var message = event.Get("message")
var arr = message.match(/^(\d{4}-\d{2}-\d{2}T\S+)\s+([a-zA-Z]+)\s+\[([a-zA-Z]+)(.*)/)
var date = arr[1]
var level = arr[2]
var service = arr[3]
var content = arr[4]
var fields = event.Get("fields")
fields["date"] = date
fields["level"] = level
fields["service"] = service
fields["content"] = content
event.Put("fields", fields)
} catch(err) {
var fields = event.Get("fields")
fields["js_err"] = err.message
event.Put("fields", fields)
}
}
filebeat.config.modules:
path: ${path.config}/modules.d/*.yml
reload.enabled: false
output.elasticsearch:
hosts: ["es1.leve.com:9202", "es2.leve.com:9202", "es3.leve.com:9202"]
username: "elastic"
password: "123456"
3. 创建filebeat DaemonSet
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: filebeat
namespace: logging
labels:
k8s-app: filebeat
spec:
selector:
matchLabels:
k8s-app: filebeat
template:
metadata:
labels:
k8s-app: filebeat
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: zone
operator: Exists
serviceAccountName: filebeat
terminationGracePeriodSeconds: 30
hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet
tolerations:
- effect: NoSchedule
key: node-role.kubernetes.io/master
containers:
- name: filebeat
image: elastic/filebeat:8.12.2
args: [
"-c", "/root/filebeat.yml",
"-e",
]
env:
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
securityContext:
runAsUser: 0
resources:
limits:
memory: 300Mi
requests:
cpu: 100m
memory: 100Mi
volumeMounts:
- name: config
mountPath: /root/filebeat.yml
readOnly: true
subPath: filebeat.yml
- name: data
mountPath: /usr/share/filebeat/data
- name: dev-log
mountPath: /var/log/leve
- name: test-log
mountPath: /home/leve/logs
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
volumes:
- name: config
configMap:
defaultMode: 0600
name: filebeat-config
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
- name: dev-log
hostPath:
path: /var/log/leve
- name: test-log
hostPath:
path: /home/leve/logs
- name: data
hostPath:
path: /var/lib/filebeat-data
type: DirectoryOrCreate
---