I'm running a Kubernetes Job from a Jenkins pipeline and want to stream its logs until completion.
I currently use this pattern in a Bash script:
job_status_cmd_complete="kubectl get job ${kube_job_name} --namespace=${kube_namespace} -o jsonpath={..status.conditions[?(@.type=='Complete')].status}"
job_status_cmd_failed="kubectl get job ${kube_job_name} --namespace=${kube_namespace} -o jsonpath={..status.conditions[?(@.type=='Failed')].status}"
function jobIsFinished {
if [[ $($job_status_cmd_complete) == 'True' || $($job_status_cmd_failed) == 'True' ]]; then
echo 'True'
else
echo 'False'
fi
}
# First log stream — immediately after pod is ready
kubectl logs --tail=-1 --follow --selector=job-name=$job_name --namespace=$namespace -c $job_name
# Loop to check for completion
while [[ $(jobIsFinished) != 'True' ]]; do
# Second log stream to catch more output in case the job is still running
kubectl logs --tail=100 --follow --selector=job-name=$job_name --namespace=$namespace -c $job_name
done
Why I do this:
- The first kubectl logs --follow is to get logs as soon as the pod is ready.
- The second one in the loop is a safeguard to continue watching if the job is long-running or restarts.
- This was intended to ensure I don't miss logs if the first stream gets disconnected or if the pod is slow to start logging.
Issue:
This approach sometimes causes duplicate logs, especially in Jenkins output.
It's not clear whether --follow maintains log offset or restarts from the beginning in each call.
Is kubectl logs --follow stateless and prone to duplication if used multiple times?
kubectl logs; see Logging Architecture in the Kubernetes documentation. Configuring this is more of a system-administration question than a programming one. Yes,kubectl logsis stateless and prone to repeating content.