Cyberithub

Solved: "Kubectl error: You must logged in to the Server (Unauthorized)"

Advertisements

In this article, we will see how to solve Kubectl error: You must logged in to the Server (Unauthorized). It is not very uncommon that whenever you try to switch your context or namespace and use kubectl get pods command to check the status of pods then all you get on the output is this Kubectl error: You must logged in to the Server (Unauthorized).

Although you might not be expecting this error but this is all you see on the output and not able to check the pods status. The next question that automatically comes to your mind that from where this error came and how to get rid of this error. Here we will see all the probable causes along with the solution to solve this error.

Advertisements

 

Solved: "Kubectl error: You must logged in to the Server (Unauthorized)"

Solved: "Kubectl error: You must logged in to the Server (Unauthorized)"

Also Read: How to Rollback Deployment in Kubernetes Using 4 Easy Steps

Before going into this error further, I would quickly give you a glimpse of the situation that I faced which lead me to this error. As you probably already know, kubectl requires you to have a kubeconfig file from where it will detect all the cluster context and namespaces to switch and use valid user credentials to connect and authenticate to the k8s Clusters.

Advertisements

If you don't provide the correct credentials, you will end up having this Kubectl error: You must logged in to the Server (Unauthorized). When we say credentials then it usually means user name and access token to authenticate. The error could occur due to problem in either of these. We shall see problem in both of the cases.

 

Case 1: Due to Incorrect User Name

This was exactly the reason for the error in my case. While initially I was using hcm user to login and connect to the cluster but when I changed the user, I forgot to update the same in my kubeconfig file and hence ended up with Kubectl error: You must logged in to the Server (Unauthorized). So initially my kubeconfig file was looking like below.

Advertisements
[cyberithub@node1]$ kubectl config view
.....................
- context:
    cluster: rtl01
    namespace: ua-fscm-prod
    user: hcm
  name: app-fscm-prod
.......................

But after identifying the problem when I updated the user from hcm to fscm then my kubeconfig file looks like below.

[cyberithub@node1]$ kubectl config view
.....................
- context:
    cluster: rtl01
    namespace: ua-fscm-prod
    user: fscm
  name: app-fscm-prod
.......................

Now when I tried to check the pods in app-fscm-prod context by switching to the context and running kubectl get pods command then this time I am able to see the status of the pods as you can also see below.

[cyberithub@node1]$ kubectl get pods
NAME                             READY  STATUS   RESTARTS  AGE
fscm-sample-66b6c48dd5-8n9sh     1/1    Running  0         2h
fscm-example74d6a52ee7-q72cf     1/1    Running  0         13d
fscm-app-51g9d36ab6-z8tgd        1/1    Running  0         41d

 

Case 2: Due to Incorrect or Expired Access Tokens

Sometimes, it is also possible that you are using correct user name but still you are getting same Kubectl error: You must logged in to the Server (Unauthorized). This could be due to either you are using incorrect or expired access token. So if you run kubectl config view command to check your current kubeconfig file, your access tokens should look like below.

[cyberithub@node1]$ kubectl config view
........................................................
-name: fscm
 user:
    auth-provider:
       config:
         access-token: eyEJF83OEBFD2FJNFASFVBXBOFF94FJOWDBWPFJFEKB2DCBVKJXKAAFEBVBBOV
.........................................................

You need to check and verify your access token if in case it is incorrect or expired. As soon as you place the valid access token and run kubectl get pods again then you will be able to query your cluster and get the pods status as expected.

Hope above solution should be enough to give you an idea about the error and the solution steps that needs to be taken to solve the problem. If in case, you are still getting the same Kubectl error: You must logged in to the Server (Unauthorized) then I would request you to please provide your feedback in the comment box so that I can take a look into your error as well.

Leave a Comment