<p>Currently, I have the following architecuture in kubernetes: </p>

<ul>
<li>In a pod, a service and a sidecar container (called <code>logger</code>) is running. </li>
<li>The services writes to a file, the sidecar container reads that file and writes it to stdout.</li>
<li>A fluentd daemonset is configured to read the output (which is collected in a file in <code>/var/log/containers/*_logger-*.log</code>, which is a link to another file (the latest file since the last file rotation, to the older files, no link points).</li>
<li>Always 3 log messages belong together (some same fields)</li>
</ul>

<p>This configration works as expected for thousands of messages.</p>

<p>However, here is the problem:</p>

<p>I noticed that fluentd sometimes only forwards logmessage 1 or 2 of the 3 messages that belong together, although all 3 messages are written by the service and the sidecar container. </p>

<p>For the explaination, assume 1 is forwarded, 2 and 3 not. After some research, I found out, that in such cases, message 1 is the last message before the log rotates, message 2 and 3 are in another file (where the symbolic link points to since the rotation, and therefore should be read).</p>

<p>Therefore, it looks like fluentd skips some lines before continue reading at the new file after the kubernetes log rotation.</p>

<ul>
<li>Is this a known problem?</li>
<li>Why are fluentd and kubernetes behaving like this?</li>
<li>And the main question: <strong>What can I do to prevent this behavior, in order to receive all log messages?</strong></li>
</ul>

<p>I am using the docker-image <code>fluent/fluentd-kubernetes-daemonset:v0.12.33-elasticsearch</code></p>

<p>If more information is required, please let me know.</p>

<p><strong>TLDR</strong>:</p>

<p>In theory this should work with the latest version of <code>fluentd-kubernetes-daemonset</code>. If it's not the default value of <code>rotate_wait</code> will probably need to be overwritten for the <code>in_tail_container_logs</code> configuration because of timing issues.</p>

<p>To do so you'll need to create a custom docker image that will overwrite the <code>kubernetes.conf</code> file, or use a config map with your custom config, mount it in the container and set <code>FLUENT_CONF</code> to the main config file in the mounted directory.</p>

<p><strong>Explanation</strong>:</p>

<p>The docker process is reading from both stdout and stderr of a container.
While flushing the streams to the logfile it will also keep track of the set limits. When a limit has been reached it will start the log rotation.</p>

<p>At the same time fluentd is watching the symlink. When the symlink changes fluentd's file watcher will get triggered to update its internal pointer to the actual log file and reset the position in the pos file because the newly created log file is empty.</p>

<p>Using the config parameter <code>rotate_wait</code> we're telling fluentd to wait for the set amount of seconds (defaults to 5) so the last log lines that have been flushed to the file (or are soon to be) can be picked up before we're continuing with the newly created log file. This will also make sure that the log lines are processed in the correct order.</p>
