Home > OS >  kubectl copy logs from pod when terminating
kubectl copy logs from pod when terminating

Time:12-04

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"]

CodePudding user response:

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.

CodePudding user response:

there is an issue with the command you are using in the preStop hook. The kubectl logs command is used to print the logs of a container, but it is not a command that can be run inside a container. Instead, you can use the logs command of the busybox image, which is a small Linux utility image that includes many common Unix command line utilities.

For example, you could use the following preStop hook to print the logs of the appworks-0 container to the file /container-stopped.txt before the container is terminated:

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

You will also need to make sure that the appworks-0 container has the /container-stopped.txt file mounted as a volume, so that it is accessible to the preStop hook.

It's important to note that the preStop hook will only run if the container is terminated. If the container is restarted, the preStop hook will not be executed again.

  • Related