kubectl and kubelogin: troubleshooting authentication errors
Clear the kubelogin cache, check identity, contexts and port-forward permission with kubectl.
When kubectl or a graphical client such as OpenLens fails to authenticate against a Kubernetes cluster that uses kubelogin, a stale cached token is a frequent cause. This article covers resetting the cache and the checks to run afterwards.
Clear the kubelogin cache
Delete the kubelogin file from the .kube cache folder of the user profile:
C:\Users\<user>\.kube\cache\kubelogin
Then check the identity the cluster sees:
kubectl auth whoami
Check the environment
az account show --query user.name -o tsv
kubectl version --client
Get-Command kubectl, kubelogin | Select-Object Name, Source
The last command confirms which kubectl and kubelogin executables are really used (useful when several versions exist in the PATH).
Check the configuration and contexts
kubectl config get-contexts
kubectl config current-context
Test the port-forward permission
kubectl auth can-i verifies, per context and namespace, that the account may open a port-forward:
kubectl --context cluster-weu -n my-namespace auth can-i create pods/portforward
kubectl --context cluster-neu -n my-namespace auth can-i create pods/portforward
Expected answer: yes.
Notes
- A "dry run" script that only checks this permission does not prove that a port-forward really opens: only a real end-to-end test, on a single cluster and a single target, validates the whole chain.
- Any write against a production environment should remain a deliberate operator decision.