Jenkins on Kubernetes - Get logs of containers from kubectl

Viewed 2049

I start this question with the doubt if this belongs here or to StackOverflow; I'll give a try here ,the problem is the following:

I'm working with Jenkins on Kubernetes and I'm not able to retrieve the logs of containers used on the pipelines.
For example using the following pipeline:

pipeline {
  agent {
    kubernetes {
      containerTemplate { name 'alpine' image 'alpine' command 'tail -f /dev/null'}
    }
  }
  stages { 
      stage('sample'){
          steps{
              container('alpine'){
                echo "Hello world"
                sleep 600
              }
          }
      }
  }
}

everything works fine and I can see "Hello world" printed on Jenkins console, but I'd also like to get it as output from the command kubectl get logs pod-name -c alpine. I suppose that kubectl logs display what is written on stdout/stderr by the command used as entrypoint in the container ( tail -f /dev/null in this case ) and that that's the reason a I don't get "Hello world", but I don't get how to achieve what I want.

I also saw a workaround suggested on Docker documentation :

The official nginx image creates a symbolic link from /var/log/nginx/access.log to /dev/stdout, and creates another symbolic link from /var/log/nginx/error.log to /dev/stderr, overwriting the log files and causing logs to be sent to the relevant special device instead. See the Dockerfile.

but I don't think that solution fits my problem.

1 Answers
Related