Search code examples
jenkinskubernetesjenkins-pipelinejenkins-pluginsdevops

Containers issue with Jenkins plugin for Kubernetes


I'm using kubernetes plugin in jenkins. I'm trying to use it with Pipeline and Freestyle jobs. The jobs are running, but only in container with name jnlp-slave:3.35-5-alpine which I did't define anywhere.

Even if define the image in the pipeline nothing happanig and still running in this jnlp-slave:3.35-5-alpine image. Here is example of my pipeline:

pipeline {
    agent none
    stages {
        stage ('Stage as dsl') {
            agent {
                kubernetes {
                    cloud 'kubernetes'
                    label 'jnlp-slave'
                    containerTemplate {
                        name 'jnlp-slave'
                        image 'jnlp-slave'
                    }
                }
            }
            steps {
                sh '''
                    pwd; \
                    whoami; \
                    uname -a
                '''
            }
        }
    }
}

Where is my mistake?


Solution

  • The solution that I find is to NOT define any label for the cluster.

    In my jenkinsfile example I'm testing to use diffrend docker images like this:

    Working Solution

    def podTemplate = """
    apiVersion: v1
    kind: Pod
    spec:
      containers:
      - name: java-slave
        image: myrepo/java-slave:latest
        command:
        - sleep
        args:
        - infinity
      - name: jenkins-slave-dind
        image: myrepo/jenkins-slave-dind:latest
        command:
        - sleep
        args:
        - infinity
        volumeMounts:
        - name: dockersock
          mountPath: /var/run/docker.sock
      volumes:
      - name: dockersock
        hostPath:
          path: /var/run/docker.sock
    """
    pipeline {
        agent {
            kubernetes {
                yaml podTemplate
                defaultContainer 'java-slave'
            }
        }
        stages {
            stage('Main') {
                steps {
                    sh 'curl google.com'
                }
            }
            stage ('Stage as dsl 4'){
                steps {
                    container('jenkins-slave-dind') {
                        sh '''
                            docker ps -a
                        '''
                    }
                }
            }
        }
    }