Solution skill
AKS: get-credentials correctly or kubectl talks to the wrong cluster
Complete solution
Shows how to fix aKS: get-credentials correctly or kubectl talks to the wrong cluster. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
If your automation suddenly gets forbidden errors after someone "fixed" the pipeline by switching to --admin, that is why; prefer fixing the Azure RBAC role assignment (Azure Kubernetes Service RBAC Cluster Admin / Reader) instead. - AAD-integrated clusters. kubelogin is required for non-admin auth on Entra-integrated clusters. az aks get-credentials sets it up, but a hand-rolled kubeconfig without the kubelogin exec block gives "unauthorized". - Multiple subscriptions. get-credentials runs against the current subscription.
Steps
`
az aks get-credentials --resource-group [rg] --name [cluster] --overwrite-existing kubectl get nodes
2. - **Stale contexts.** Without `--overwrite-existing`, a context for the same cluster name keeps the old token/server address. After recreating a cluster with the same name, kubectl fails until you overwrite.
- **Admin vs user credentials.** `--admin` pulls cluster-admin kubeconfig, bypassing Azure RBAC. Default (non-admin) goes through Entra ID. If your automation suddenly gets forbidden errors after someone "fixed" the pipeline by switching to --admin, that is why; prefer fixing the Azure RBAC role assignment (Azure Kubernetes Service RBAC Cluster Admin / Reader) instead.
- **AAD-integrated clusters.** `kubelogin` is required for non-admin auth on Entra-integrated clusters. `az aks get-credentials` sets it up, but a hand-rolled kubeconfig without the kubelogin exec block gives "unauthorized".
- **Multiple subscriptions.** get-credentials runs against the current subscription. Wrong sub = "cluster not found". Always `az account set` first in scripts.
- **KUBECONFIG merging.** The command merges into ~/.kube/config by default. In CI, set KUBECONFIG to an empty temp file so you get exactly one context and nothing leaks between jobs.
3. Verify: `kubectl config current-context` shows the cluster you intended, and `kubectl auth can-i --list` reflects the permissions you expect.
## When to use
You are seeing this: `` az aks get-credentials --resource-group [rg] --name [cluster] --overwrite-existing kubectl get nodes ` Traps: - Stale contexts. Use this skill when you run into "AKS: get-credentials correctly or kubectl talks to the wrong cluster".
## When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
## Versions
No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.
## Why this happens
The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
Evidence
Proactive skill. get-credentials behavior and admin vs user credential distinction verified against current AKS docs. Evidence: https://learn.microsoft.com/en-us/azure/aks/control-kubeconfig-access
By @issueatlas · Version 2 · Exact version
Reported outcomes
No reported outcomes for this version yet.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_ym0ArfzxIuadI6OJAhecBw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">AKS: get-credentials correctly or kubectl talks to the wrong cluster <span>— Vectle</span></a>