【问题标题】:Azure Container Service (AKS) kubeconfig file outdatedAzure 容器服务 (AKS) kubeconfig 文件已过时
【发布时间】:2021-03-20 15:56:48
【问题描述】:

我正在学习 K8s,并使用 kubectl apply 设置了发布管道。我已经通过 Terraform 设置了 AKS 集群,在第一次运行时一切似乎都很好。一旦我破坏了集群,我重新运行了管道,我得到了我认为与异常中提到的 kubeconfig 文件有关的问题。我尝试了 cloud shell 等来获取文件或重置它,但我没有成功。我怎样才能回到干净的状态?

2020-12-09T09:08:51.7047177Z ##[section]Starting: kubectl apply
2020-12-09T09:08:51.7482440Z ==============================================================================
2020-12-09T09:08:51.7483217Z Task         : Kubectl
2020-12-09T09:08:51.7483729Z Description  : Deploy, configure, update a Kubernetes cluster in Azure Container Service by running kubectl commands
2020-12-09T09:08:51.7484058Z Version      : 0.177.0
2020-12-09T09:08:51.7484996Z Author       : Microsoft Corporation
2020-12-09T09:08:51.7485587Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/deploy/kubernetes
2020-12-09T09:08:51.7485955Z ==============================================================================
2020-12-09T09:08:52.7640528Z [command]C:\ProgramData\Chocolatey\bin\kubectl.exe --kubeconfig D:\a\_temp\kubectlTask\1607504932712\config apply -f D:\a\r1\a/medquality-cordapp/k8s
2020-12-09T09:08:54.1555570Z Unable to connect to the server: dial tcp: lookup mq-k8s-dfee38f6.hcp.switzerlandnorth.azmk8s.io: no such host
2020-12-09T09:08:54.1798118Z ##[error]The process 'C:\ProgramData\Chocolatey\bin\kubectl.exe' failed with exit code 1
2020-12-09T09:08:54.1853710Z ##[section]Finishing: kubectl apply

发布管道的更新、工作流任务:

最初我得到了工件,克隆了包含 k8s yamls 的 repo,然后这个阶段应用了 kubectl。

"workflowTasks": [
                        {
                            "environment": {},
                            "taskId": "cbc316a2-586f-4def-be79-488a1f503564",
                            "version": "0.*",
                            "name": "kubectl apply",
                            "refName": "",
                            "enabled": true,
                            "alwaysRun": false,
                            "continueOnError": false,
                            "timeoutInMinutes": 0,
                            "definitionType": null,
                            "overrideInputs": {},
                            "condition": "succeeded()",
                            "inputs": {
                                "kubernetesServiceEndpoint": "82e5971b-9ac6-42c6-ac43-211d2f6b60e4",
                                "namespace": "",
                                "command": "apply",
                                "useConfigurationFile": "false",
                                "configuration": "",
                                "arguments": "-f $(System.DefaultWorkingDirectory)/medquality-cordapp/k8s",
                                "secretType": "dockerRegistry",
                                "secretArguments": "",
                                "containerRegistryType": "Azure Container Registry",
                                "dockerRegistryEndpoint": "",
                                "azureSubscriptionEndpoint": "",
                                "azureContainerRegistry": "",
                                "secretName": "",
                                "forceUpdate": "true",
                                "configMapName": "",
                                "forceUpdateConfigMap": "false",
                                "useConfigMapFile": "false",
                                "configMapFile": "",
                                "configMapArguments": "",
                                "versionOrLocation": "version",
                                "versionSpec": "1.7.0",
                                "checkLatest": "false",
                                "specifyLocation": "",
                                "cwd": "$(System.DefaultWorkingDirectory)",
                                "outputFormat": "json",
                                "kubectlOutput": ""
                            }
                        }
                    ]
    ```

【问题讨论】:

  • 您能否分享管道步骤并提供更多详细信息。您是否在管道本身中为新创建的集群获取 kubeconfig?
  • 我已经更新了 OP,我没有获取 kubeconfig。
  • Kubectl 将需要信息来连接到您的新集群。您将需要获取集群信息并使用集群的详细信息更新您的 kubeconfig 文件。其中一种方法是,您可以在管道中调用 kubectl apply 之前使用 Az CLI 任务 (az aks get-credentials) 更新 kubeconfig。
  • 我在 kubectl apply 任务之前使用了 Radek 在任务中给出的命令,但同样的问题。一定还是做错了什么。 2020-12-09T13:09:44.8926956Z Merged "medquality-aks1-admin" as current context in /home/vsts/.kube/config 2020-12-09T13:09:45.2265525Z [command]/usr/bin/az account clear
  • 您是否仍然收到“无法连接到服务器:拨号 tcp:查找 mq-k8s-dfee38f6.hcp.switzerlandnorth.azmk8s.io”?如果是新的错误信息,可以在这里添加吗?

标签: azure kubernetes azure-devops terraform-provider-azure


【解决方案1】:

我可以看到您在 Kubectl 任务中使用 kubernetesServiceEndpoint 作为服务连接类型。

一旦我破坏了集群,我就重新运行了管道,我遇到了问题......

如果集群被破坏。 azure devops 中的 kubernetesServiceEndpoint 仍连接到源集群。使用原点kubernetesServiceEndpoint 的 Kubectl 任务仍在寻找旧集群。由于旧集群被破坏,它将失败并出现上述错误。

您可以通过使用新创建的集群更新 azure devops 中的 kubernetesServiceEndpoint 来解决此问题:

转到 Azure devops 项目设置-->服务连接-->找到您的 Kubernetes 服务连接-->点击编辑进行更新配置。

但是如果您的 kubernete 集群经常被破坏并重新创建。我建议使用 Azure 资源管理器 作为服务连接类型来连接到 Kubectl 任务中的集群。请看下面的截图。

通过使用azureSubscriptionEndpoint并指定azureResourceGroup,如果只是集群的名称没有改变,那么集群重新创建多少次都没有关系。

查看文档以创建Azure Resource Manager service connection

【讨论】:

  • 非常感谢李维!就是这样,对我来说,服务连接可能指向旧的 fqdn 并不明显。也非常感谢您指出这种情况的战略解决方案。非常感谢。
【解决方案2】:

当您销毁和重新配置 AKS 集群时,kube API URL 和其他一些东西会发生变化,但正如您发现的那样,在您配置的客户端上没有任何内容会自动更新。

我要访问新的和重新配置的 AKS 群集的方法是:

az aks get-credentials --subscription <sub> -g <rg> -n <aksname> -a --overwrite

【讨论】:

  • 它不在客户端。我也在本地执行了 aks get-credentials,但问题发生在我没有提供 kubeconfig 的发布管道上。
  • 对不起,现在更好理解了,在 kubectl apply 任务之前添加了它,但同样的问题。 2020-12-09T13:09:44.8926956Z Merged "medquality-aks1-admin" as current context in /home/vsts/.kube/config 2020-12-09T13:09:45.2265525Z [command]/usr/bin/az account clear
猜你喜欢
  • 2023-01-17
  • 2021-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多