kubectl 与 GKE 集群的连接可通过虚拟机进行工作,但无法通过同一虚拟机的 Jenkins 管道进行连接

发布于 2025-01-11 19:28:55 字数 429 浏览 0 评论 0原文

我有一个 Linux 虚拟机,可以毫无问题地运行 kubectl get pods 命令。

我发现,当我

gcloud container clusters get-credentials --cluster <cluster-name>

从该虚拟机运行:命令时,它会按预期将 kubeconfig 文件存储在 $HOME/.kube/config 中。

但是,当我在同一虚拟机中通过 jenkins 管道运行相同的步骤时,它会出现与 GKE 集群的连接超时问题。另外,kubeconfig 文件的内容相同,但这次的路径不同。它是在管道的当前目录中以 .kube 名称创建的。我也尝试过在管道中传递 --kubeconfig $HOME/.kube/config 但又出现了同样的问题。

I have a linux VM from where I am able to run kubectl get pods command without any issue.

I have found out that when I run:

gcloud container clusters get-credentials --cluster <cluster-name>

command from this VM it stores the kubeconfig file in $HOME/.kube/config as expected.

But when I am running same steps through a jenkins pipeline in same VM, its getting a connection timeout issue to GKE cluster. Also the kubeconfig file's content is same but its path is different this time. It's being created in the current directory of the pipeline under the name .kube simply. I have also tried passing --kubeconfig $HOME/.kube/config in pipeline but its the same issue again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2025-01-18 19:28:55

我也尝试在管道中传递 --kubeconfig $HOME/.kube/config 但又出现同样的问题。

通过在 jenkins 管道中指定 $HOME 变量,您可以从 jenkins 用户 的主目录请求 kube 配置文件,例如 /var/lib/jenkins。
和:

它是在管道的当前目录中以 .kube 名称创建的

,因此,解决方案是在 jenkins 作业中设置 KUBECONFIG 环境变量或设置 --kubeconfig标志并将它们指向现有的工作 kube 配置文件(检查 kube 配置路径)。查看所有可用的 jenkins 环境变量
yourJenkinsUrl:port/env-vars.html

编辑:

有一个可能有用的解决方法 - 适用于 Jenkins 的 Kubernetes CLI 插件

允许您在作业中配置 kubectl 以与 Kubernetes 集群交互。然后可以在管道中使用基于 kubectl 构建的任何工具来执行部署,例如 Shopify/krane。

// Example when used in a pipeline
node {
  stage('Apply Kubernetes files') {
    withKubeConfig([credentialsId: 'user1', serverUrl: 'https://api.k8s.my-company.com']) {
      sh 'kubectl apply -f my-kubernetes-directory'
    }
  }
}

它是如何运作的?

该插件根据构建中提供的参数生成 kubeconfig 文件。该文件存储在构建工作空间内的临时文件中,确切的路径可以在 KUBECONFIG 环境变量中找到。 kubectl 会自动从此环境变量中获取路径。一旦构建完成(或退出管道块),临时 kubeconfig 文件将自动删除。

I have also tried passing --kubeconfig $HOME/.kube/config in pipeline but its the same issue again.

By specifying $HOME variable in a jenkins pipeline you are requesting kube config file from the home dir of a jenkins user, for example /var/lib/jenkins.
And:

Its being created in the current directory of the pipeline under the name .kube

So, the solution is either setting the KUBECONFIG environment variable in a jenkins job or setting the --kubeconfig flag and pointing them to the existing working kube config file (check the kube config path). See all available jenkins environment variables at
yourJenkinsUrl:port/env-vars.html

EDIT:

There is a workaround that might be helpful - Kubernetes CLI plugin for Jenkins.

Allows you to configure kubectl in your job to interact with Kubernetes clusters. Any tool built on top of kubectl can then be used from your pipelines to perform deployments, e.g. Shopify/krane.

// Example when used in a pipeline
node {
  stage('Apply Kubernetes files') {
    withKubeConfig([credentialsId: 'user1', serverUrl: 'https://api.k8s.my-company.com']) {
      sh 'kubectl apply -f my-kubernetes-directory'
    }
  }
}

How it works?

The plugin generates a kubeconfig file based on the parameters that were provided in the build. This file is stored in a temporary file inside the build workspace 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文