Yugandhar Reddy
Yugandhar Reddy

Reputation: 19

kubectl copy logs from pod when terminating

We are trying to get the logs of pods after multiple restarts but we dont want to use any external solution like efk.

i tried below config but its not working. does the below cmd run on the pod or it will run on node level

lifecycle:
  preStop:
  exec:
       command: ["/bin/sh", "-c", "kubectl logs appworks-0 > /container-stoped.txt"]

Upvotes: 1

Views: 226

Answers (1)

Harsh Manvar
Harsh Manvar

Reputation: 30113

i tried below config but its not working. does the below cmd run on the pod or it will run on node level

it will run on the POD level, not on Node level

You can use the Hostpath in POD configuration

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:  
  - image: alpine
    name: test-container
    command: ["tail"]
    args: ["-f", "/dev/null"] 
    volumeMounts:
    - mountPath: /host
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      path: /
      type: Directory

Hostpath will directly will create one Dir at the Node level and save logs over there, if you don't want this solution you can add your solution of lifecycle hook also however when you can directly write app logs to Host don't add lifecycle hook extra.

Note : Make sure if your Node goes down hostpath or emptyDir logs you will miss.

Upvotes: 1

Related Questions