get logs for init container post job completion
Asked Answered
C

2

6

We have a multi pod container which consists of 2 pods. init container runs first and the actual container run. recently our init container job are failing due to which our deployment is getting failed. we need to get the init container logs post completion of the init container

is there way to do that ??

i know we can get the logs when init container is running but we need logs even after the init container job is comeplete

any solution , we are using jenkins for build and deploy

Codie answered 25/4, 2023 at 8:49 Comment(2)
Hello everyone there is a small correction from myside, we need the init logs to be printed in the jenkins console if possible during the time or failure or once the init container job is done success/failureCodie
Kindly raise a different question for that.Jankell
H
8

You can run the cmd:

kubectl logs <pod_name> -c <init_container_name>

This will fetch the logs of the init container even after is finished running.

Just an example, for the below pod spec the init-container runs first (to completion) and just prints "Hello world".

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - name: main-container
    image: busybox
    command: ["/bin/sh", "-ec", "while :; do echo '.'; sleep 5 ; done"]
  initContainers:
  - name: init-container
    image: busybox
    command: ['sh', '-c', 'echo "Hello world"'] 

If I run this command:

kubectl logs test-pod -c init-container

I get the output:

Hello world

To get logs of all containers within a pod, you could use:

kubectl logs <pod_name> --all-containers

Hope it helps!

Heteronomous answered 25/4, 2023 at 9:15 Comment(0)
B
1

Even if your init container is completed you can get the logs

This is what i tried

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      initContainers:
        - name: init-myservice
          image: busybox
          command: ['sh', '-c', "echo 'Hello'"]
      containers:
        - name: nginx
          image: nginx
          ports:
          - containerPort: 80

enter image description here

You can run the

kubectl logs <pod-name> -c <init-container-name>

Or else you can write the logs file to Host path for retrieve it whenever required.

apiVersion: v1
kind: Pod
metadata:
  name: test-logs-mount
spec:
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', "Your command/Write logs to >> /work-dir/logs.txt"]
  volumeMounts:
  - name: host
    mountPath: "/"
  containers:
  - image: image
    name: test-container
    volumeMounts:
    - mountPath: /
      name: host
  volumes:
   - name: host
     hostPath:
       path: /data
Boll answered 25/4, 2023 at 9:36 Comment(5)
(The hostPath approach requires figuring out which node the pod had run on, and then directly logging into the node to retrieve the log file; in many standard Kubernetes environments you won't be able to do that.)Illiquid
Yes, David, you are right and i do agree fully with you. With managed node case would be hard although, i did the same way with GKE SSH private node once to debug when logs are not getting pushed/stored to any central logging. Please do share any suggestion if you have any would love to hear.Boll
I'm not sure how's this any different (including the update) from the answer posted earlier by me.Jankell
Hi there, thanks for reading the answer..! update i shared to prove complete init logs available already however sorry i think maybe you missed the last part suggestion of Hostpath to store log in worst case to debug that is different from the answer you posted earlier.Boll
Sleeping init-container will delay the main container to start or extra commit to remove the sleep condition after debugging and deploying again as he is looking for post-completion logs.Boll

© 2022 - 2024 — McMap. All rights reserved.