how kubernetes deal with file write locker accross multi pods when hostpath Volumes concerned

8/28/2018

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

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:

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?

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

-- Tarjintor
kubernetes

1 Answer

8/29/2018

how kubernetes deal with file write locker accross multi pods when hostpath Volumes concerned

It doesn't.

Operating System and File System are handling that. As an example let's take syslog. 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.

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.

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.

-- Crou
Source: StackOverflow