简体   繁体   English

Kubernetes 的 Jenkins 插件的容器问题

[英]Containers issue with Jenkins plugin for Kubernetes

I'm using kubernetes plugin in jenkins.我在 jenkins 中使用 kubernetes 插件。 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.作业正在运行,但仅在名称为jnlp-slave:3.35-5-alpine容器中,我没有在任何地方定义。

Even if define the image in the pipeline nothing happanig and still running in this jnlp-slave:3.35-5-alpine image.即使在管道中定义图像,也不会发生任何事情并且仍在此jnlp-slave:3.35-5-alpine图像中运行。 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?我的错误在哪里?

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:在我的jenkinsfile示例中,我正在测试使用 diffrend docker 图像,如下所示:

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
                    '''
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM