<p>I am learning about K8s and did setup a release pipeline with a kubectl apply. I've setup the AKS cluster via Terraform and on the first run all seemed fine. Once I destroyed the cluster I reran the pipeline, I get issues which I believe are related to the kubeconfig file mentioned in the exception. I tried the cloud shell etc. to get to the file or reset it but I wasn't succesful. How can I get back to a clean state?</p>
<pre><code>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://learn.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
</code></pre>
<p>Update, workflow tasks of the release pipeline:</p>
<p>Initially I get the artifact, clone of the repo containing the k8s yamls, then the stage does a kubectl apply.</p>
<pre><code>&quot;workflowTasks&quot;: [
                        {
                            &quot;environment&quot;: {},
                            &quot;taskId&quot;: &quot;cbc316a2-586f-4def-be79-488a1f503564&quot;,
                            &quot;version&quot;: &quot;0.*&quot;,
                            &quot;name&quot;: &quot;kubectl apply&quot;,
                            &quot;refName&quot;: &quot;&quot;,
                            &quot;enabled&quot;: true,
                            &quot;alwaysRun&quot;: false,
                            &quot;continueOnError&quot;: false,
                            &quot;timeoutInMinutes&quot;: 0,
                            &quot;definitionType&quot;: null,
                            &quot;overrideInputs&quot;: {},
                            &quot;condition&quot;: &quot;succeeded()&quot;,
                            &quot;inputs&quot;: {
                                &quot;kubernetesServiceEndpoint&quot;: &quot;82e5971b-9ac6-42c6-ac43-211d2f6b60e4&quot;,
                                &quot;namespace&quot;: &quot;&quot;,
                                &quot;command&quot;: &quot;apply&quot;,
                                &quot;useConfigurationFile&quot;: &quot;false&quot;,
                                &quot;configuration&quot;: &quot;&quot;,
                                &quot;arguments&quot;: &quot;-f $(System.DefaultWorkingDirectory)/medquality-cordapp/k8s&quot;,
                                &quot;secretType&quot;: &quot;dockerRegistry&quot;,
                                &quot;secretArguments&quot;: &quot;&quot;,
                                &quot;containerRegistryType&quot;: &quot;Azure Container Registry&quot;,
                                &quot;dockerRegistryEndpoint&quot;: &quot;&quot;,
                                &quot;azureSubscriptionEndpoint&quot;: &quot;&quot;,
                                &quot;azureContainerRegistry&quot;: &quot;&quot;,
                                &quot;secretName&quot;: &quot;&quot;,
                                &quot;forceUpdate&quot;: &quot;true&quot;,
                                &quot;configMapName&quot;: &quot;&quot;,
                                &quot;forceUpdateConfigMap&quot;: &quot;false&quot;,
                                &quot;useConfigMapFile&quot;: &quot;false&quot;,
                                &quot;configMapFile&quot;: &quot;&quot;,
                                &quot;configMapArguments&quot;: &quot;&quot;,
                                &quot;versionOrLocation&quot;: &quot;version&quot;,
                                &quot;versionSpec&quot;: &quot;1.7.0&quot;,
                                &quot;checkLatest&quot;: &quot;false&quot;,
                                &quot;specifyLocation&quot;: &quot;&quot;,
                                &quot;cwd&quot;: &quot;$(System.DefaultWorkingDirectory)&quot;,
                                &quot;outputFormat&quot;: &quot;json&quot;,
                                &quot;kubectlOutput&quot;: &quot;&quot;
                            }
                        }
                    ]
    ```
</code></pre>

1174
<p>I can see you are using <code>kubernetesServiceEndpoint</code> as the Service connection type in Kubectl task.</p>
<blockquote>
<p>Once I destroyed the cluster I reran the pipeline, I get issues....</p>
</blockquote>
<p>If the cluster was destroyed. The <code>kubernetesServiceEndpoint</code> in azure devops is still connected to the origin cluster. Kubectl task which using the origin <code>kubernetesServiceEndpoint</code> is still looking for the old cluster. And it will fail with above error, since the old cluster was destroyed.</p>
<p>You can fix this issue by updating the <a href="https://learn.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&amp;tabs=yaml#sep-kuber" rel="nofollow noreferrer">kubernetesServiceEndpoint</a> in azure devops with the newly created cluster:</p>
<p>Go to Azure devops <strong>Project settings</strong>--&gt;<strong>Service connections</strong>--&gt; Find your Kubernetes Service connection--&gt;Click <strong>Edit</strong> to update the configuration.</p>
<p>But if your kubernete cluster gets destroyed and recreated frequently. I would suggest using <strong>Azure Resource Manager</strong> as the Service connection type to connect to the cluster in Kubectl task. See below screenshot.</p>
<p>By using <code>azureSubscriptionEndpoint</code> and specifying <code>azureResourceGroup</code>, if only the cluster's name doesnot change, It doesnot matter how many times the cluster is recreated.</p>
<p><a href="https://i.stack.imgur.com/dOuL8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dOuL8.png" alt="enter image description here" /></a></p>
<p>See document to create an <a href="https://learn.microsoft.com/en-us/azure/devops/pipelines/library/connect-to-azure?view=azure-devops" rel="nofollow noreferrer">Azure Resource Manager service connection</a></p>
