How to connect on premise kubernetes cluster using Jenkins File

7/30/2018

I am trying to deploy application on kubernetes cluster by using jenkins multi branch pipeline and "Jenkins file" but unable to make connection between Jenkins and Kubernetes. From code side I can't share more details here.

I just want to know if there is any way to make this connection (Jenkins and Kubernetes) using Jenkins file so that I will use it to deploy the application on Kubernetes.

Following is the technology stack that might clear my issue:

  1. Jenkins file is kept at root location of project in git hub.

  2. Separate jenkins server where pipeline is created to deploy the application on Kubernetes.

  3. On premise kubernetes cluster.

-- Rahul
jenkins-pipeline
kubernetes

1 Answer

7/30/2018

You need credentials to talk to Kubernetes. When you have automation like Jenkins running jobs, it's best to create a service account for Jenkins, look here for some documentation. Once you create the Jenkins service account, you can extract an authentication token for that account, which you put into Jenkins. What I would recommend doing, since your Jenkins is not a pod inside your Kubernetes cluster is to upload a working kubectl config as a secret file in the Jenkins credential manager.

Then, in your Jenkins job configuration, you can use that secret. Jenkins can put the file somewhere for your job to access, then in your Jenkinsfile, you can run commands with "kubectl --kubeconfig= ...".

-- Marcin Romaszewicz
Source: StackOverflow