Inject Log Configuration with DataKit Operator¶
To collect logs directly with DataKit on the node, configure the Operator to write the datakit/logs annotation on new Pods. File logs use directory mounts from the rule. Standard output needs only the annotation.
Install the Operator and enable container log collection in DataKit. To forward file logs through a sidecar, use logfwd.
Operator Configuration¶
Configure a rule in admission_mutate.loggings in the Operator. Both the namespace and label must match. This rule uses the existing app=direct-logs label on Pods in middleware. The workload YAML needs no additional label or annotation.
{
"admission_mutate": {
"loggings": [
{
"namespace_selectors": ["^middleware$"],
"label_selectors": ["app=direct-logs"],
"config": "[{\"disable\":false,\"type\":\"file\",\"path\":\"/var/log/app/*.log\",\"source\":\"logging-demo\"}]"
}
]
}
}
| Field | Description |
|---|---|
namespace_selectors |
Array of Namespace regular expressions; at least one matching entry is required |
label_selectors |
Array of Pod Label Selectors; at least one matching entry is required |
config |
JSON array string written to datakit/logs |
One match per selector list is sufficient. If several rules match, the first in configuration order applies.
Set config to a JSON array encoded as a string. This example collects /var/log/app/*.log with the source logging-demo.
Injection Result¶
For a Pod matching the preceding example, the Operator adds:
metadata:
annotations:
datakit/logs: '[{"disable":false,"type":"file","path":"/var/log/app/*.log","source":"logging-demo"}]'
spec:
containers:
- name: app
volumeMounts:
- name: datakit-logs-volume-0
mountPath: /var/log/app
volumes:
- name: datakit-logs-volume-0
emptyDir: {}
The Operator reuses or creates emptyDir mounts in regular application containers. If the Pod already has datakit/logs, it preserves the existing configuration and mounts.
Deployment Example¶
Run this Deployment in the middleware namespace. The container writes to /var/log/app/app.log every five seconds. The rule above adds its collection configuration and mount.
apiVersion: apps/v1
kind: Deployment
metadata:
name: direct-logs
namespace: middleware
spec:
replicas: 1
selector:
matchLabels:
app: direct-logs
template:
metadata:
labels:
app: direct-logs
spec:
containers:
- name: app
image: busybox:1.36
command: ["sh", "-c"]
args:
- mkdir -p /var/log/app; while true; do date -u '+%Y-%m-%dT%H:%M:%SZ sample log' >> /var/log/app/app.log; sleep 5; done
After editing the Operator ConfigMap directly, restart the Operator and create or recreate the target Pods. Check the new Pod’s annotation and the file written by the application.
kubectl -n middleware get pod -l app=direct-logs -o yaml
kubectl -n middleware exec deployment/direct-logs -c app -- tail -n 1 /var/log/app/app.log
The new Pod should have datakit/logs and a /var/log/app mount. In the log explorer, filter by source=logging-demo to find the sample log line.