【问题标题】:Running kubectl patch --local fails due to missing config由于缺少配置,运行 kubectl patch --local 失败
【发布时间】:2021-02-10 16:51:02
【问题描述】:

我有一个 GitHub Actions 工作流程,用于替换部署清单中的值。我使用kubectl patch --local=true 来更新图像。直到现在,这曾经完美无缺。今天,工作流开始失败,出现Missing or incomplete configuration info 错误。

我正在运行带有--local 标志的kubectl,因此不需要配置。有谁知道kubectl突然开始需要配置的原因是什么?我在 Kubernetes GitHub 问题中找不到任何有用的信息,而且谷歌搜索数小时也无济于事。

GitHub Actions 工作流程中失败步骤的输出:

Run: kubectl patch --local=true -f authserver-deployment.yaml -p '{"spec":{"template":{"spec":{"containers":[{"name":"authserver","image":"test.azurecr.io/authserver:20201230-1712-d3a2ae4"}]}}}}' -o yaml > temp.yaml && mv temp.yaml authserver-deployment.yaml

error: Missing or incomplete configuration info.  Please point to an existing, complete config file:


  1. Via the command-line flag --kubeconfig
  2. Via the KUBECONFIG environment variable
  3. In your home directory as ~/.kube/config

To view or setup config directly use the 'config' command.
Error: Process completed with exit code 1.

kubectl version的输出:

Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.0", 
GitCommit:"ffd68360997854d442e2ad2f40b099f5198b6471", GitTreeState:"clean", 
BuildDate:"2020-11-18T13:35:49Z", GoVersion:"go1.15.0", Compiler:"gc", 
Platform:"linux/amd64"}

【问题讨论】:

  • 好像你的 kube 上下文没有设置。检查你得到了什么kubectl config current-context
  • 我收到了error: current-context is not set。我看到的问题是我想在本地修补文件,为此我不需要上下文集。还是我弄错了?

标签: kubernetes kubectl github-actions


【解决方案1】:

作为一种解决方法,我安装了 kind(完成这项工作确实需要更长的时间,但至少它可以工作,并且可以在以后用于 e2e 测试)。

添加了这一步:

- name: Setup kind
        run: kubectl version
        uses: engineerd/setup-kind@v0.5.0

还可以使用 --dry-run=client 作为 kubectl 命令的选项。

我确实意识到这不是正确的解决方案。

【讨论】:

    【解决方案2】:

    您仍然需要设置config to access kubernetes cluster。即使您在本地修改文件,您仍在执行必须针对集群运行的kubectl 命令。默认情况下,kubectl 在$HOME/.kube 目录中查找名为 config 的文件。

    error: current-context is not set 表示当前没有为集群设置上下文,kubectl 不能对集群执行。您可以使用 this tutorial 为服务帐户创建上下文。

    【讨论】:

      【解决方案3】:

      导出KUBERNETES_MASTER 环境变量应该可以解决问题:

      $ export KUBERNETES_MASTER=localhost:8081  # 8081 port, just to ensure it works 
      
      $ kubectl version
      
      Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.8", GitCommit:"9f2892aab98fe339f3bd70e3c470144299398ace", GitTreeState:"clean", BuildDate
      :"2020-08-13T16:12:48Z", GoVersion:"go1.13.15", Compiler:"gc", Platform:"linux/amd64"}
      The connection to the server localhost:8081 was refused - did you specify the right host or port?
      
      # Notice the port 8081 in the error message ^^^^^^
      

      现在补丁也应该像往常一样工作:

      $ kubectl patch --local=true -f testnode.yaml -p '{"metadata":{"managedFields":[]}}'    # to get the file content use  -o yaml 
      node/k8s-w1 patched
      

      或者,您可以将kubectl 更新到更高版本。 (即使没有技巧,v1.18.8 也能正常工作)

      说明部分:

      这个变化很可能是由PR #86173 stop defaulting kubeconfig to http://localhost:8080

      引入的

      Revert "stop defaulting kubeconfig to http://localhost:8080" #90243 中的更改已恢复为以后的 18.x 版本,请参阅问题 kubectl --local requires a valid kubeconfig file #90074 了解详细信息

      【讨论】:

        【解决方案4】:

        我最终使用 sed 将字符串替换为图像

          - name: Update manifests with new images
            working-directory: test/cloud
            run: |
              sed -i "s~image:.*$~image: ${{ steps.image_tags.outputs.your_new_tag }}~g" your-deployment.yaml
        

        现在就像一个魅力。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-04
          • 1970-01-01
          • 2016-05-28
          • 1970-01-01
          • 2021-10-05
          • 2020-06-02
          相关资源
          最近更新 更多