kubectl get nodes returns the following error: "error: You must be logged in to the server (Unauthorized)"tanzu cluster kubeconfig get <cluster-name> expire after approximately 10 hours.tanzu cluster kubeconfig get <cluster-name> --admin expire unexpectedly after several months or immediately following a cluster upgrade.TKG 2.5.2
This is an expected security behavior of the authentication mechanisms in TKG.
Depending on your access requirements, use one of the following methods to resolve the issue:
Restore Interactive User Access
For standard interactive access, you must re-authenticate to generate a new short-lived session token:Run the following command to log in and refresh your context: tanzu cluster kubeconfig get <cluster-name>
Restore Administrator Access
If your --admin certificate expired due to time or a cluster upgrade, you must download the newly generated certificate from the management cluster.Run the following command to retrieve the updated administrator certificate: tanzu cluster kubeconfig get <cluster-name> --admin
Create a Non-Expiring Token for Automation (CI/CD)
If you require persistent access that bypasses the 10-hour timeout and survives cluster upgrades, you must create a dedicated Kubernetes ServiceAccount.
Log in to the cluster using a valid administrator context.
Create a ServiceAccount and bind it to the required role (e.g., cluster-admin): kubectl create serviceaccount automation-sa -n kube-system kubectl create clusterrolebinding automation-sa-rb --serviceaccount=kube-system:automation-sa --clusterrole=cluster-admin
Create a long-lived secret for the ServiceAccount. Create a file named sa-secret.yaml: