Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubectl: error You must be logged in to the server (Unauthorized)

Tags:

kubernetes

I've created a service account for CI purposes and am testing it out. Upon trying any kubectl command, I get the error:

error: You must be logged in to the server (Unauthorized)

Below is my .kube/config file

apiVersion: v1
clusters:
- cluster:
    server: <redacted>
  name: kubernetes
contexts:
- context:
    cluster: kubernetes
    user: bamboo
  name: default
current-context: 'default'
kind: Config
preferences: {}
users:
- name: bamboo
  user:
    token: <redacted>

The service account exists and has a cluster role: edit and cluster role binding attached.

What am I doing wrong?

like image 350
bear Avatar asked Nov 30 '25 02:11

bear


1 Answers

I reproduce the error if I copy the token directly without decoding. Then applied the following steps to decode and set the token and it is working as expected.


$ TOKENNAME=`kubectl -n <namespace> get serviceaccount/<serviceaccount-name> -o jsonpath='{.secrets[0].name}'` 

$ TOKEN=`kubectl -n <namespace> get secret $TOKENNAME -o jsonpath='{.data.token}'| base64 --decode`

$ kubectl config set-credentials <service-account-name> --token=$TOKEN

So, I think it might be your case.

like image 164
hoque Avatar answered Dec 03 '25 05:12

hoque



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!