【问题标题】:Store Kubernetes Cluster Credentials in Jenkins and use in declarative pipeline在 Jenkins 中存储 Kubernetes 集群凭证并在声明式管道中使用
【发布时间】:2020-05-03 07:27:27
【问题描述】:

我正在尝试使用 Helm 3 和 jenkins 部署 k8s 集群。 Jenkins 和 k8s 在不同的服务器上运行。我合并了 kubeconfig 文件,所有信息都在一个配置文件 ./kube 目录中。我想根据 GIT_BRANCH 值将我的应用程序部署到相关环境和命名空间。我对以下脚本有两个问题。

1.我应该存储 k8s 集群凭据并将在管道中使用的最佳方式是什么。我看到了一些插件,例如 Kubernetes CLI,但我不确定它是否能满足我的要求。如果我使用这个插件,我应该手动将 k8s 文件存储到 Jenkins 机器中还是这个插件已经通过上传配置文件来处理这个问题。

2.我应该更改以下脚本中的任何内容以遵循最佳实践吗?

         stage('Deploy to dev'){
         script{
             steps{
                 if(env.GIT_BRANCH.contains("dev")){

                        def namespace="dev"
                        def ENV="development"

                        withCredentials([file(credentialsId: ...)]) {
                        // change context with related namespace
                        sh "kubectl config set-context $(kubectl config current-context) --namespace=${namespace}"

                        //Deploy with Helm
                        echo "Deploying"
                        sh "helm upgrade --install road-dashboard -f values.${ENV}.yaml --set tag=$TAG --namespace ${namespace}"    
                 }
             }
         }
     }

    stage('Deploy to Test'){
        script{
            steps{
                 if(env.GIT_BRANCH.contains("test")){

                        def namespace="test"
                        def ENV="test"

                        withCredentials([file(credentialsId: ...)]) {
                        // change context with related namespace
                        sh "kubectl config set-context $(kubectl config current-context) --namespace=${namespace}"


                        //Deploy with Helm
                        echo "Deploying"
                        sh "helm upgrade --install road-dashboard -f values.${ENV}.yaml --set tag=$TAG --namespace ${namespace}"
                    }
                }
            }
        }
    }

    stage ('Deploy to Production'){

        when {
            anyOf{
                environment name: 'DEPLOY_TO_PROD' , value: 'true'
            }
        }

        steps{
            script{
                DEPLOY_PROD = false
                def namespace = "production"

                withCredentials([file(credentialsId: 'kube-config', variable: 'kubecfg')]){
                    //Change context with related namespace
                    sh "kubectl config set-context $(kubectl config current-context) --namespace=${namespace}"

                    //Deploy with Helm
                    echo "Deploying to production"
                    sh "helm upgrade --install road-dashboard -f values.${ENV}.yaml --set tag=$TAG --namespace ${namespace}"
                }
            }
        }
    }

【问题讨论】:

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


    【解决方案1】:

    我从未尝试过,但理论上凭据变量可用作环境变量。尝试使用KUBECONFIG作为变量名

    withCredentials([file(credentialsId: 'secret', variable: 'KUBECONFIG')]) {
    
      // change context with related namespace
      sh "kubectl config set-context $(kubectl config current-context) --namespace=${namespace}"
    
      //Deploy with Helm
      echo "Deploying"
      sh "helm upgrade --install road-dashboard -f values.${ENV}.yaml --set tag=$TAG --namespace ${namespace}"
    }
    

    【讨论】:

    • 有什么特殊的方法可以在Jenkins系统中存储凭证文件吗?
    【解决方案2】:

    对我有用的解决方法:

    withCredentials([file(credentialsId: 'k8s-dk-staging', variable: 'KUBECRED')]) {
       sh 'cat $KUBECRED > ~/.kube/config'
       sh './deploy-app.sh'
    }
    

    我不喜欢这样做,理想情况下我想使用 KUBECONFIG 但现在这对我有用。

    【讨论】:

      猜你喜欢
      • 2017-08-12
      • 2021-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 1970-01-01
      相关资源
      最近更新 更多