How to Delete/Remove Kubernetes – Azure Arc using CLI without kubeconfig or connected to cluster?


microsoft-azure How to Delete/Remove Kubernetes - Azure Arc using CLI without kubeconfig or connected to cluster? cloud Kubernetes Microsoft Azure

microsoft-azure

Kubernetes – Azure Arc is a layer ontop the auzre aks cluster. Sometimes the cluster can be no longer accessible/deleted or we don’t have the correct kubeconfig anymore. In such case, when we try to delete the cluster using the az connectedk8s delete command:

1
az connectedk8s delete --name clusterName --resource-group resourceGroup  --yes --no-wait
az connectedk8s delete --name clusterName --resource-group resourceGroup  --yes --no-wait

Then, most likely wewill have this error if the cluster is not setup properly:

1
ERROR: Problem loading the kubeconfig file. Invalid kube-config file. No configuration found.
ERROR: Problem loading the kubeconfig file. Invalid kube-config file. No configuration found.

Or, we don’t have access to the cluster anymore:

1
2
The current context in the kubeconfig file does not correspond to the connected cluster resource specified. Agents installed on this cluster correspond to the resource group name 'resourceGroup' and resource name 'resourceName'.
ERROR: Deployment failed. Correlation ID: XXXXXXXXXXXXXXXXX. The underlying Arc Resource : /subscriptions/subscriptionId/resourcegroups/resourcegroups/providers/microsoft.kubernetes/connectedclusters/resourceName, is no longer connected to Azure. Check that Azure Arc Agents are running and in a healthy state.
The current context in the kubeconfig file does not correspond to the connected cluster resource specified. Agents installed on this cluster correspond to the resource group name 'resourceGroup' and resource name 'resourceName'.
ERROR: Deployment failed. Correlation ID: XXXXXXXXXXXXXXXXX. The underlying Arc Resource : /subscriptions/subscriptionId/resourcegroups/resourcegroups/providers/microsoft.kubernetes/connectedclusters/resourceName, is no longer connected to Azure. Check that Azure Arc Agents are running and in a healthy state.

Certainly, we can remove these unwanted resources in Azure Portal, but how about using the azure-cli (eg. the az command)?

The “az connectedk8s delete” command expects you to have correct kubeconfig. If we’re looking to delete the connected cluster alone (ARM resource) without using portal, we can use armclient to do that: Do an “armclient login” followed by “armclient delete [ARM ID of connected cluster]?api-version=2021-10-01”.

The armclient is a Open Source project but it is not an official library for Microsoft Azure.

What’s been done underneath is to send a DELETE request to the azure API endpoint:

1
URL="https://management.azure.com/subscriptions/$(SUBSCRIPTION_ID)/resourceGroups/$(RESOURCE_GROUP_NAME)/providers/Microsoft.Kubernetes/connectedClusters/$(RESOURCE_NAME)?api-version=2020-01-01-preview
URL="https://management.azure.com/subscriptions/$(SUBSCRIPTION_ID)/resourceGroups/$(RESOURCE_GROUP_NAME)/providers/Microsoft.Kubernetes/connectedClusters/$(RESOURCE_NAME)?api-version=2020-01-01-preview

Of course, we need to authenticate with the token – we can get the access token via the “az account get-access-token” after we login using account or service principal name:

1
ACCESS_TOKEN=`az account get-access-token --query 'accessToken' -o tsv`
ACCESS_TOKEN=`az account get-access-token --query 'accessToken' -o tsv`

Then, we can use the cURL CLI tool to send the DELETE request with the Authorization header (containing the Bearer token).

1
curl -X DELETE $URL -H "Accept: application/json" -H "Authorization: Bearer $ACCESS_TOKEN"
curl -X DELETE $URL -H "Accept: application/json" -H "Authorization: Bearer $ACCESS_TOKEN"

It will return 200 and body “null” on the first request, and the subsequent DELETE request will return 204 (No Content).

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
527 words
Last Post: Teaching Kids Programming - Floyd Warshall Multi-source/All Pairs Shortest Path Algorithm (Sum of Costs in a Directed Unweighted Graph)
Next Post: Teaching Kids Programming - Single Source Shortest Path Algorithm in a Directed Unweighted Graph using Breadth First Search

The Permanent URL is: How to Delete/Remove Kubernetes – Azure Arc using CLI without kubeconfig or connected to cluster?

Leave a Reply