Home > database >  Multiple pods writes to same logging file
Multiple pods writes to same logging file

Time:08-03

Consider there is one rollout in K8s setting and it creates 2 replicaSets, each replicaSet has a single pod. Each pod include the same application that has a volume to directory with same value (i.e. /dir/logs/)

Rollout
  |_____ReplicaSet1
            |_______Pod1
                |________Volume (/logs/ mount to /dir/logs/)
  |_____ReplicaSet2
            |_______Pod2
                |________Volume (/logs/ mount to /dir/logs/)

If application in Pod1 will output log to file

 /logs/application.log

And application in Pod2 has the same definition

 /logs/application.log

is it going to be an locking issue, so that application in pod1 can write data in /logs/application.log that application in pod2 can't access the file because it is being written by pod1? How to configure K8s so that multiple pods won't facing such issue?

CodePudding user response:

Include the pod name as an environment variable and then use this as part of your log path:

...
          env:
            - name: PODNAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
          command:
            - sh
            - -c
            - |
              exec myapplication --logfile /logs/$PODNAME.log
...
  • Related