kubectl port forwarding does not work when called from Jenkins pipeline

5/5/2020

I am running a kubernetes cluster using minikube on a Linux machine as part of a Jenkins pipeline job.

Both the Jenkins slave and minikube run on the same machine. The job runs some golang tests that try to access services running inside the kubernetes cluster.

In my pipeline script I am forwarding ports for some pods in this manner:

stage('start kubernetes'){
sh '''
//start cluster here
kubectl port-forward pods/pod_name 9281 &
kubectl port-forward pods/datastore_name 6379 &
'''
}

stage('start tests'){
sh '''
//run tests
'''
}

I also tried

nohup kubectl port-forward pods/pod_name 9281 &
nohup kubectl port-forward pods/datastore_name 6379 &

When I run the job:

  • The tests cannot connect to the services.
  • The job's console output shows the ports are forwarded but when I log in to the machine and do ps | grep port-forward it shows nothing. Am I missing something?
-- Abhijeet Vaikar
jenkins
jenkins-pipeline
kubectl
kubernetes

0 Answers