gpt4 book ai didi

由于上下文,Kubernetes 部署失败

转载 作者:行者123 更新时间:2023-12-02 11:42:58 25 4
gpt4 key购买 nike

我已经为本地部署创建了上下文。

root@jenkins-linux-vm:/usr/lib# kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
K8sCluster-CC-pre-release kubernetes kubernetes-admin K8sCluster-CC-pre-release
PR kubernetes kubernetes-admin PR
* kubernetes-admin@kubernetes kubernetes kubernetes-admin

当我的 jenkins 运行构建时,我遇到了以下错误,即使我已经创建了上下文及其指向 (*)。

+ kubectl '--kubeconfig=****' '--context=K8sCluster-CC-pre-release' apply -f ./environment/pre-release '-n=pre-release'
error: context "K8sCluster-CC-pre-release" does not exist
tage('deployment') {
container('kubectl') {
withCredentials([kubeconfigFile(credentialsId: 'KUBERNETES_CLUSTER_CONFIG', variable: 'KUBECONFIG')]) {
def kubectl
echo 'Deployment Start'
if(gitBranch == "future-master-fix") {
kubectl = "kubectl --kubeconfig=${KUBECONFIG} --context=K8sCluster-CC-pre-release"
echo 'deploy to PRERELEASE!'
sh "${kubectl} apply -f ./environment/pre-release -n=pre-release"
echo 'Deployment End'
}
}
}
}

最佳答案

当您配置 Jenkins 时,您可能使用了基于更流行的 Kubeconfig 的配置。

基于 Kubernetes CLI

The plugin generates a kubeconfig file based on the parameters that were provided in the build. This file is stored in a temporary folder on the Jenkins executor and the exact path can be found in the KUBECONFIG environment variable. kubectl automatically picks up the path from this environment variable. Once the build is finished (or the pipeline block is exited), the temporary kubeconfig file is automatically removed.

当您使用 kubectl config get-contexts 时,您使用了来自 ${HOME}/.kube/config 的本地配置。

如果你检查 kubectl config --help 你会得到:

  1. If the --kubeconfig flag is set, then only that file is loaded. The flag may only be set once and no merging takes place.
  2. If $KUBECONFIG environment variable is set, then it is used as a list of paths (normal path delimiting rules for your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the last file in the list.
  3. Otherwise, ${HOME}/.kube/config is used and no merging takes place.

这意味着如果您在本地使用 Kubernetes,则您使用的是选项 3
Jenkins 中,您使用的是选项 1,因此每次您使用不同的配置运行 kubectl,其中不包含所需的 context.

在这个命令中:kubectl = "kubectl --kubeconfig=${KUBECONFIG} --context=K8sCluster-CC-pre-release" 你正在覆盖 kubeconfig,所以它找不到这个context

请从代码中删除 --kubeconfig=${KUBECONFIG} 并重试。它应该可以工作。

关于由于上下文,Kubernetes 部署失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59732944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com