<p>I got app that logs to file my_log/1.log, and then I use filebeat to collect the logs from the file</p>

<p>Now I use k8s to deploy it into some nodes, and use hostpath type Volumes to mounts my_log file to the local file syetem, /home/my_log, suddenly I found a subtle situation:</p>

<p>what will it happened if more than one pod deployed on this machine, and then they try to write the log at the same time?</p>

<p>I know that in normal situation, multi-processing try to write to a file at the same time, the system will lock the file,so these processes can write one by one, BUT I am not sure will k8s diffirent pods will not share the same lock space, if so, it will be a disaster.
I try to test this and it seems diffirent pods will still share the file lock,the log file seems normal</p>

<blockquote>
  <p>how kubernetes deal with file write locker accross multi pods when hostpath Volumes concerned</p>
</blockquote>

<p>It doesn't.</p>

<p>Operating System and File System are handling that.
As an example let's take <code>syslog</code>. It handles it by opening a socket, setting the socket to server mode, opening a log file in write mode, being notified of packages, parsing the message and finally writing it to the file.</p>

<p>Logs can also be cached, and the process can be limited to 1 thread, so you should not have many pods writing to one file. This could lead to issues like missing logs or lines being cut.</p>

<p>Your application should handle the file locking to push logs, also if you want to have many pods writing logs, you should have a separate log file for each pod.</p>
