3

I am trying to setup a pipeline on jenkins that uses docker containers to execute tasks like for example deploy an helm charts using a docker image with helm

here is what my Jenkinsfile looks like

pipeline {
    agent none
    stages {
        stage('Helm') {
            agent {
                docker { image 'dtzar/helm-kubectl' }
            }
            steps {
                sh '''
                helm version
                kubectl version
                helm ls
                kubectl get all -n test
                '''

            }
        }
    }
}

but it fails at the helm ls line because it can not connect to the kubernetes cluster. How do i connect this pipeline to use the Kubernetes configuration(kubeconfig) i have setup

here is error am getting

+ helm ls
Error: Kubernetes cluster unreachable

I could not find any documentation on how to connect docker pipeline to connect to the kubernetes cluster What do i add to the Jenkinsfile to be able to connect?

UPDATE

i also tried this

pipeline {
    agent none
    stages {
        stage('Helm') {
            agent {
                docker { image 'dtzar/helm-kubectl' }
            }
            steps {
              withCredentials([kubeconfigContent(credentialsId: 'k8s-cluster', variable: 'KUBECONFIG_CONTENT')]) {
                sh '''
                helm version
                helm ls
                kubectl get all -n test
                '''
              }

            }
        }
    }
}

and got this error

+ helm ls
Error: Kubernetes cluster unreachable

I could not find any documentation on how to connect docker pipeline to connect to the kubernetes cluster

What do i add to the Jenkinsfile to be able to connect?

so seems the docker container is unable to resolve the dns of the kubernetes cluster

how do i resolve this?

3
  • Where do you have the kubeconfig?
    – papanito
    Jan 21, 2020 at 18:42
  • as credentials in jenkins..i am able to use it with kubernetesDeploy with reference of the crdentials as kubeconfigId here is link to the plugin wiki.jenkins.io/display/JENKINS/…
    – uberrebu
    Jan 21, 2020 at 18:53
  • issue now is how do i reference that in docker pipeline in jenkinsfile so when i run something like helm or kubectl it is able to use that kubeconfig credential to connect to the kubernetes cluster
    – uberrebu
    Jan 21, 2020 at 18:55

1 Answer 1

1

Maybe it is strange solution, but I would like to provide it to you:

You can get actual kubeconfig file from your Kubernetes cluster or locally and change it to base64 format like in example below:

- cat /root/.kube/config | base 64

Then use your base64 configuration file in pipeline:

- echo $your_variable | base64 -d | tee /root/.kube/config

In such case you will be having actual kubeconfig file on runner machine.

In case of DNS problem - just need to specify IP address of your runner machine in /etc/resolv.conf file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.