HiveBrain v1.2.0
Get Started
← Back to all entries
snippetdockerMinor

How do i connect docker pipeline on jenkins to kubernetes cluster

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
connectdockerjenkinskuberneteshowpipelinecluster

Problem

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?

Solution

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.

Code Snippets

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

Context

StackExchange DevOps Q#10540, answer score: 1

Revisions (0)

No revisions yet.