Answers: <p>Maybe it is strange solution, but I would like to provide it to you:</p>
  
  <p>You can get actual kubeconfig file from your Kubernetes cluster or locally and change it to base64 format like in example below:</p>
  
  <pre><code>- cat /root/.kube/config | base 64
  </code></pre>
  
  <p>Then use your base64 configuration file in pipeline:</p>
  
  <pre><code>- echo $your_variable | base64 -d | tee /root/.kube/config
  </code></pre>
  
  <p>In such case you will be having actual kubeconfig file on runner machine.</p>
  
  <p>In case of DNS problem - just need to specify IP address of your runner machine in /etc/resolv.conf file.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-02-12T09:51:19.843Z
Answers: 2020-02-12T09:51:19.843Z
Answers: Darii Nurgaleev
Body: <p>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 </p>
  
  <p>here is what my <strong>Jenkinsfile</strong> looks like</p>
  
  <pre><code>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
                  '''
  
              }
          }
      }
  }
  </code></pre>
  
  <p>but it fails at the <code>helm ls</code> line because it can not connect to the kubernetes cluster.
  How do i connect this pipeline to use the <strong>Kubernetes configuration(kubeconfig)</strong> i have setup</p>
  
  <p>here is error am getting</p>
  
  <pre><code>+ helm ls
  Error: Kubernetes cluster unreachable
  </code></pre>
  
  <p>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?</p>
  
  <blockquote>
    <p><strong>UPDATE</strong></p>
  </blockquote>
  
  <p>i also tried this</p>
  
  <pre><code>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
                  '''
                }
  
              }
          }
      }
  }
  </code></pre>
  
  <p>and got this error</p>
  
  <pre><code>+ helm ls
  Error: Kubernetes cluster unreachable
  </code></pre>
  
  <p>I could not find any documentation on how to connect docker pipeline to connect to the kubernetes cluster</p>
  
  <p>What do i add to the Jenkinsfile to be able to connect?</p>
  
  <p>so seems the docker container is unable to resolve the dns of the kubernetes cluster</p>
  
  <p>how do i resolve this?</p>
  
Comments: CC BY-SA 4.0
Comments: 2020-01-21T18:42:37.257Z
Comments: Where do you have the kubeconfig?
Comments: papanito
Comments: CC BY-SA 4.0
Comments: 2020-01-21T18:53:03.130Z
Comments: 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 https://wiki.jenkins.io/display/JENKINS/Kubernetes+Continuous+Deploy+Plugin
Comments: uberrebu
Comments: CC BY-SA 4.0
Comments: 2020-01-21T18:55:30.870Z
Comments: 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
Comments: uberrebu
Tags: <docker><jenkins><kubernetes><cicd>
Title: How do i connect docker pipeline on jenkins to kubernetes cluster
UserDisplayName: uberrebu
ThreadText: # How do i connect docker pipeline on jenkins to kubernetes cluster
  
  (Asked by: username_0 on 2020-01-21)
  
  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?
  
  **Tags:** docker, jenkins, kubernetes, cicd
  
  > **Comments:**
  > **username_1**: Where do you have the kubeconfig?  
  > **username_0**: 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 https://wiki.jenkins.io/display/JENKINS/Kubernetes+Continuous+Deploy+Plugin  
  > **username_0**: 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  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2020-02-12. Score: 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.
  
  
  ---
  
  