Injecting logfwd with DataKit Operator¶
Use logfwd to forward application log files through a sidecar. The Operator injects datakit-logfwd, which reads the shared log directory and sends records to the DataKit logfwdserver input. For direct collection by DataKit on the node, see logging configuration injection.
Use an Operator rule to select Pods for injection and a ClusterLoggingConfig to specify the files to collect. The dynamic configuration example below requires Operator v1.7.2 or later.
sequenceDiagram
participant App as Application container
participant Sidecar as logfwd Sidecar
participant Operator as DataKit Operator
participant CRD as ClusterLoggingConfig
participant DataKit as DataKit logfwdserver
Operator->>Sidecar: Inject when creating the Pod
Operator->>CRD: Watch and cache configurations
Sidecar->>Operator: Periodically fetch matching configurations
Sidecar->>App: Read logs from the shared directory
Sidecar->>DataKit: Send logs
Prerequisites¶
- Install DataKit Operator and enable
logfwdserverin DataKit. - The DataKit Service exposes
9533, and application Pods can access DataKit. - For dynamic configuration, the
logging.datakits.io/v1alpha1 ClusterLoggingConfigCRD is installed in the cluster and the Operator ServiceAccount hasget,list, andwatchpermissions.
For older deployments, see logfwd on v1.6.x and earlier.
Operator Configuration¶
Configure admission_inject_v2.logfwds in the Operator. The default rule selects the default namespace. To limit injection there to particular applications, add a label selector to that rule. This example selects Pods in middleware with the existing label app=logging and shares /var/log/app. Adapt the namespace, labels, and log directory in the rule for your application.
{
"admission_inject_v2": {
"logfwds": [
{
"name": "logfwd-app",
"namespace_selectors": ["^middleware$"],
"label_selectors": ["app=logging"],
"check_annotation": false,
"image": "pubrepo.guance.com/datakit/logfwd:2.20.0",
"envs": {
"LOGFWD_DATAKIT_HOST": "datakit-service.datakit.svc",
"LOGFWD_DATAKIT_PORT": "9533",
"LOGFWD_DATAKIT_OPERATOR_ENDPOINT": "datakit-operator.datakit.svc:443",
"LOGFWD_GLOBAL_SERVICE": "{fieldRef:metadata.labels['app']}",
"LOGFWD_POD_NAME": "{fieldRef:metadata.name}",
"LOGFWD_POD_NAMESPACE": "{fieldRef:metadata.namespace}",
"LOGFWD_POD_IP": "{fieldRef:status.podIP}"
},
"log_configs": "",
"log_volume_paths": ["/var/log/app"]
}
]
}
}
| Field | Description |
|---|---|
name |
Rule name, recommended for locating relevant logs |
namespace_selectors |
Array of Namespace regular expressions |
label_selectors |
Array of Pod Label Selectors |
check_annotation |
Legacy compatibility switch; when true, admission.datakit/logfwd.instances is also required |
image |
logfwd Sidecar image |
envs |
Sidecar environment variables |
log_configs |
Optional static log configuration JSON string |
log_volume_paths |
Log directories shared between application containers and the Sidecar |
resources |
Sidecar resource configuration; defaults are used when missing or invalid |
The Operator selects the first rule whose namespace and label selectors match. With check_annotation: false, injection is automatic and requires no workload YAML changes. With true, the selected rule also requires admission.datakit/logfwd.instances on the Pod template. Without it, injection is skipped. Set admission.datakit/logfwd.enabled: "false" on a Pod template to disable logfwd for that workload.
Environment Variables¶
| Environment variable | Description |
|---|---|
LOGFWD_DATAKIT_HOST |
DataKit hostname or IP reachable from application Pods. The example uses Service DNS. |
LOGFWD_DATAKIT_PORT |
DataKit logfwdserver port; defaults to 9533 |
LOGFWD_DATAKIT_OPERATOR_ENDPOINT |
Operator address for dynamically fetching CRD configurations; https:// is used automatically when the protocol is omitted |
LOGFWD_GLOBAL_SOURCE |
Overrides source in all log configurations |
LOGFWD_GLOBAL_SERVICE |
Global value used when an individual configuration has no service |
LOGFWD_GLOBAL_STORAGE_INDEX |
Overrides storage_index in all log configurations |
LOGFWD_GLOBAL_FROM_BEGINNING_THRESHOLD_SIZE |
Global threshold for collecting from the beginning of a file, in bytes |
LOGFWD_POD_NAME |
Written to the pod_name tag |
LOGFWD_POD_NAMESPACE |
Written to the namespace tag |
LOGFWD_POD_IP |
Written to the pod_ip tag |
Configuration Sources¶
Choose where to maintain the collection tasks.
- Central management (recommended). Set file paths and sources in the
ClusterLoggingConfigexample. KeepLOGFWD_DATAKIT_OPERATOR_ENDPOINTand leavelog_configsempty. The sidecar polls every 60 seconds. - Fixed tasks. Set
log_configsin the Operator rule to a JSON array encoded as a string. Omit the Operator endpoint when using static configuration alone. - Existing deployments. Pod templates can continue to supply tasks in
admission.datakit/logfwd.instances. A nonemptylog_configstakes precedence for static tasks.
Example static log_configs:
{
"log_configs": "[{\"type\":\"file\",\"path\":\"/var/log/app/*.log\",\"source\":\"app\",\"service\":\"checkout\"}]"
}
Log Directories¶
log_volume_paths specifies the directories that the Sidecar must read. For example:
- If an application container already mounts an EmptyDir at the path, the Operator mounts the same volume read-only in the Sidecar.
- If the path has no corresponding mount, the Operator creates an EmptyDir and mounts it in all regular application containers and the Sidecar.
- If the same path uses a volume other than EmptyDir, the Operator logs a conflict and skips the path.
- Avoid configuring both a parent directory and its child directory, which can cause mount conflicts.
The sidecar picks up CRD changes for files in directories it already shares. To collect from another directory, add it to log_volume_paths so the sidecar can read it when the Pod is recreated.
ClusterLoggingConfig¶
Save this resource as app-logs.yaml and run kubectl apply -f app-logs.yaml. The Operator rule selects Pods for injection. This resource selects the files their sidecars read, using the same namespace and labels to target the Pods.
apiVersion: logging.datakits.io/v1alpha1
kind: ClusterLoggingConfig
metadata:
name: app-logs
spec:
selector:
namespaceRegex: "^middleware$"
podLabelSelector: "app=logging"
podTargetLabels:
- app
configs:
- type: file
source: app
service: checkout
path: /var/log/app/*.log
The Operator repository does not install this CRD. For all fields and CRD installation instructions, see Kubernetes container log CRD configuration.
Log output categories¶
Starting with Operator v1.10.1, use spec.configs[].output_categories to select which categories logfwd writes to. Before enabling the field, apply the updated CRD and upgrade the logfwd sidecar and receiving DataKit to 2.20.0. If you use a custom injection rule, update the logfwd image in the rule and recreate the application Pods.
- Omit the field or set it to
["logging"]to write ordinary logs only. - Set it to
["security_logging"]to write security logs only. - Set it to
["logging", "security_logging"]to write to both categories. - The updated CRD rejects an explicit empty array
[].
Deployment Example¶
apiVersion: apps/v1
kind: Deployment
metadata:
name: logging-demo
namespace: middleware
spec:
replicas: 1
selector:
matchLabels:
app: logging
template:
metadata:
labels:
app: logging
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
Create the middleware namespace for this example. After editing the Operator ConfigMap directly, restart the Operator, then create or recreate the application Pods. The example writes a line to /var/log/app/app.log every five seconds.
kubectl -n middleware get pods -l app=logging -o custom-columns='POD:.metadata.name,CONTAINERS:.spec.containers[*].name'
kubectl -n middleware exec deployment/logging-demo -c app -- tail -n 1 /var/log/app/app.log
kubectl -n middleware logs deployment/logging-demo -c datakit-logfwd
Check that the new Pod has a datakit-logfwd container and that the application file contains sample log. In the log explorer, filter by source=app to find the record. If it is missing, check connectivity to DataKit on port 9533 and the ClusterLoggingConfig selectors.