Azure 容器服务 (AKS) kubeconfig 文件已过时

Azure Container Service (AKS) kubeconfig file outdated

我正在学习 K8s,并使用 kubectl apply 设置了一个发布管道。我已经通过 Terraform 设置了 AKS 集群,并且在第一个 运行 上一切似乎都很好。一旦我销毁了集群,我就重新运行了管道,我得到了我认为与异常中提到的 kubeconfig 文件相关的问题。我尝试了云 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\kubectlTask07504932712\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 yaml 的 repo,然后该阶段执行 kubectl apply。

"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": ""
                            }
                        }
                    ]
    ```

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

我为访问新的和重新配置的 AKS 集群所做的是:

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

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

Once I destroyed the cluster I reran the pipeline, I get issues....

如果集群被破坏。 azure devops 中的 kubernetesServiceEndpoint 仍然连接到源集群。使用 origin kubernetesServiceEndpoint 的 kubectl 任务仍在寻找旧集群。它将因上述错误而失败,因为旧集群已被破坏。

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

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

但是如果你的 kubernete 集群经常被销毁和重新创建。我建议在 Kubectl 任务中使用 Azure Resource Manager 作为服务连接类型来连接到集群。请参阅下面的屏幕截图。

通过使用azureSubscriptionEndpoint并指定azureResourceGroup,只要集群名称不变,重新创建集群多少次都无所谓。

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