【问题标题】:Containers issue with Jenkins plugin for Kubernetes用于 Kubernetes 的 Jenkins 插件的容器问题
【发布时间】:2020-04-03 21:33:32
【问题描述】:

我在 jenkins 中使用 kubernetes 插件。我正在尝试将它与 Pipeline 和 Freestyle 作业一起使用。 作业正在运行,但仅在名称为 jnlp-slave:3.35-5-alpine 的容器中运行,我没有在任何地方定义。

即使在管道中定义图像也没有任何问题,并且仍然在这个jnlp-slave:3.35-5-alpine 图像中运行。这是我的管道示例:

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

我的错误在哪里?

【问题讨论】:

  • 也许标签是你需要改变的东西?如詹金斯文档label ->The label of the pod. Can be set to a unique value to avoid conflicts across builds 中所述。因此,如果我理解正确,现在它应该只适用于带有标签 k8s-jnlp-slave 和最新图像的 pod。

标签: jenkins kubernetes jenkins-pipeline jenkins-plugins devops


【解决方案1】:

我找到的解决方案是为集群定义任何标签。

在我的 jenkinsfile 示例中,我正在测试使用 diffrend docker 图像,如下所示:

工作解决方案

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

【讨论】:

    猜你喜欢
    • 2020-12-01
    • 1970-01-01
    • 2019-05-15
    • 1970-01-01
    • 2020-08-01
    • 2020-07-29
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    相关资源
    最近更新 更多