2

I've containerized server app running as a DaemonSet component in k8s cluster. And there is a containerized client running as a deploy component in the same cluster.

I'm using same logging mechanism for both client and server.

client code.

err := os.MkdirAll("/root/mahesh/logs/", 0777)
    if err != nil {
        log.Fatalf("error creating directory file: %v", err)
    }

    filename := filepath.Join("/root/mahesh/logs/", "client.log")

    f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
    if err != nil {
        log.Fatalf("error opening file: %v", err)
    }
    defer f.Close()

    log.SetOutput(f)
    log.Println("LOG INITIALIZED and writing in file")

server code

err := os.MkdirAll("/root/mahesh/logs/", 0777)
if err != nil {
    log.Fatalf("error creating directory file: %v", err)
}

filename := filepath.Join("/root/mahesh/logs/", "server.log")

f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
    log.Fatalf("error opening file: %v", err)
}
defer f.Close()

log.SetOutput(f)

log.Println("server/server:InitializeServer() LOG initialized")

client Persistent volume configuration,

spec:
  containers:
  - name: client
    image: <client-image-name>:<image-tag>
    volumeMounts:
    - mountPath: /root/mahesh/logs
      name: mydir
    ports:
    - containerPort: 50051
    envFrom:
      - configMapRef:
           name: client-config
  volumes:
  - name: mydir
    hostPath:
      # Ensure the file directory is created.
      path: /root/mahesh/logs
      type: DirectoryOrCreate
  nodeSelector:
    node: ubuntu22

server daemonset file configuration for volume,

 containers:
  - name: server
    image: <server-image-name>:<image-tag>
    ports:
    - containerPort: 50051
      hostPort: 50051
    envFrom:
      - configMapRef:
           name: server-config
    volumeMounts:
      - mountPath: /root/mahesh/logs
        name: server-log
    securityContext:
      privileged: true
  volumes:
    - name: server-log
      hostPath:
        # Ensure the file directory is created.
        path: /root/mahesh/logs
        type: DirectoryOrCreate

When ever client restarted I can see after restart logs are properly appending to existing file as expected.

But in server, due to heavy load server gets restarted many times I expect the logs should append in the same file in log folder. But it didn't. After every restart log file also gets restarted, it seems.

So I couldn't debug further. How to resolve this issue?

imaheshwaran s
  • 167
  • 1
  • 11

0 Answers0