Jenkins部署到K8集群

时间:2020-01-07 12:46:00

标签: jenkins kubernetes

我正在尝试第一次将docker映像部署到kubernetes集群中,我有两个节点,主节点和工作节点都处于运行状态。

在jenkins构建过程中,我已经创建了namesapace预发布环境以将更改部署到该环境中,而我遇到了错误。

+ kubectl '--kubeconfig=****' '--context=K8sCluster-CC-pre-release' apply -f ./environment/pre-release '-n=pre-release'
error: context "K8sCluster-CC-pre-release" does not exist

詹金斯阶段

stage('deployment') {
        container('kubectl') {

            withCredentials([kubeconfigFile(credentialsId: 'KUBERNETES_CLUSTER_CONFIG', variable: 'KUBECONFIG')]) {
            def kubectl
                if(gitBranch == "future-master-fix") {
                kubectl = "kubectl --kubeconfig=${KUBECONFIG} --context=K8sCluster-CC-pre-release"
                sh "${kubectl} apply -f ./environment/pre-release -n=pre-release"
                }   
            }
        }
      }
kubectl config set-context K8sCluster-CC-pre-release --namespace=K8sCluster-CC-pre-release --user=kubernetes-admin --cluster=kubernetes
Context "K8sCluster-CC-pre-release" created.

CURRENT   NAME                          CLUSTER      AUTHINFO           NAMESPACE
*         K8sCluster-CC-pre-release     kubernetes   kubernetes-admin   K8sCluster-CC-pre-release

您能请人帮我吗?

1 个答案:

答案 0 :(得分:1)

原因是您的kubeconfig文件中没有K8sCluster-CC-pre-release上下文。您可以运行kubectl config view -o jsonpath='{.current-context}'来检查当前上下文并使用该上下文。