Tail logfiles with a webhook
The tailer-webhook is a different approach for the same problem: parsing legacy application’s log file. As an alternative to using a host file tailer service, you can use a file tailer webhook service.
While the containers of the host file tailers run in a separated pod, file tailer webhook uses a different approach: if a pod has a specific annotation, the webhook injects a sidecar container for every tailed file into the pod.

The tailer-webhook behaves differently compared to the host-tailer:
Pros:
- A simple annotation on the pod initiates the file tailing.
 - There is no need to use 
mounted volumes, Logging operator will manage the volumes and mounts between your containers. 
Cons:
- Required to start the Logging operator with webhooks service enabled. This requires additional configuration, especially on certificates since webhook services are allowed over TLS only.
 - Possibly uses more resources, since every tailed file attaches a new sidecar container to the pod.
 
Enable webhooks in Logging operator
We recommend using
cert-managerto manage your certificates. Below is a really simple command that bootstraps generates the required resources for thetailer-webhook.
Issuing certificates using cert-manager
Follow the official installation guide.
Once installed the following commands should allow you to create the required certificate for the webhook.
You will require the following things:
- a valid client certificate,
 - a CA certificate, and
 - a custom value.yaml file for your helm chart.
 
The following example refers to a Kubernetes secret named webhook-tls which is a self-signed certificate generated by cert-manager.
Add the following lines to your custom values.yaml or create a new file if needed:
This will:
- Set 
ENABLE_WEBHOOKSenvironment variable totrue. This is the official way to enable webhooks in Logging operator. - Create a volume from the 
webhook-tlsKubernetes secret. - Mount the 
webhook-tlssecret volume to the/tmp/k8s-webhook-server/serving-certspath where Logging operator will search for it. 
Now you are ready to install Logging operator with the new custom values:
Alternatively, instead of using the values.yaml file, you can run the installation from command line also by passing the values with the set and set-string parameters:
You also need a service which points to the webhook port (9443) of Logging operator, and where the mutatingwebhookconfiuration will point to. Running the following command in shell will create the required service:
Furthermore, you need to tell Kubernetes to send admission requests to our webhook service. To do that, create a mutatingwebhookconfiguration Kubernetes resource, and:
- Set the configuration to call 
/tailer-webhookpath on yourlogging-webhooksservice whenv1.Podis created. - Set 
failurePolicytoignore, which means that the original pod will be created on webhook errors. - Set 
sideEffectstonone, because we won’t cause any out-of-band changes in Kubernetes. 
Unfortunately, mutatingwebhookconfiguration requires the caBundle field to be filled because we used a self-signed certificate, and the certificate cannot be validated through the system trust roots. If your certificate was generated with a system trust root CA, remove the caBundle line, because the certificate will be validated automatically.
There are more sophisticated ways to load the CA into this field, but this solution requires no further components.
For example: you can inject the CA with a simple cert-manager
cert-manager.io/inject-ca-from: logging/webhook-tlsannotation on themutatingwebhookconfigurationresource.
Triggering the webhook
CAUTION:
To use the webhook, you must first enable webhooks in the Logging operator.File tailer webhook is based on a Mutating Admission Webhook. It is called every time when a pod starts.
To trigger the webhook, add the following annotation to the pod metadata:
- 
Annotation key:
sidecar.logging-extensions.banzaicloud.io/tail - 
Value of the annotation: the filename (including path, and optionally the container) you want to tail, for example:
 - 
To tail multiple files, add only one annotation, and separate the filenames with commas, for example:
 - 
If the pod contains multiple containers, see Multi-container pods.
 
Note: If the pod with the sidecar annotation is in the
defaultnamespace, Logging operator handlestailer-webhookannotations clusterwide. To restrict the webhook callbacks to the current namespace, change thescopeof themutatingwebhookconfigurationtonamespaced.
File tailer example
The following example creates a pod that is running a shell in infinite loop that appends the date command’s output to a file every second. The annotation sidecar.logging-extensions.banzaicloud.io/tail notifies Logging operator to attach a sidecar container to the pod. The sidecar tails the /var/log/date file and sends its output to the stdout.
After you have created the pod with the required annotation, make sure that the test-pod contains two containers by running kubectl get pod
Expected output:
Check the container names in the pod to see that the Logging operator has created the sidecar container called legacy-logs-date-log. The sidecar containers’ name is always built from the path and name of the tailed file. Run the following command:
Expected output:
Check the logs of the test container. Since it writes the logs into a file, it does not produce any logs on stdout.
Expected output:
Check the logs of the legacy-logs-date-log container. This container exposes the logs of the test container on its stdout.
Expected output:
Multi-container pods
In some cases you have multiple containers in your pod and you want to distinguish which file annotation belongs to which container. You can order every file annotations to particular container by prefixing the annotation with a ${ContainerName}: container key. For example:
CAUTION:
- Annotations without containername prefix: the file gets tailed on the default container (container 0)
 - Annotations with invalid containername: file tailer annotation gets discarded
 
| Annotation | Explanation | 
|---|---|
| sample-container:/var/log/date | tails file /var/log/date in sample-container | 
| sample-container2:/var/log/anotherfile | tails file /var/log/anotherfile in sample-container2 | 
| /var/log/mycustomfile | tails file /var/log/mycustomfile in default container (sample-container) | 
| foobarbaz:/foo/bar/baz | will be discarded due to non-existing container name |