Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

部署以后查看namespace报”You must be logged in to the server (Unauthorized)“错误 #2

Open
dc-weizhenhua opened this issue Nov 24, 2022 · 24 comments

Comments

@dc-weizhenhua
Copy link

dc-weizhenhua commented Nov 24, 2022

环境:
k8s版本是1.23.9,按照你的文档部署keycloak后,修改了kube-apiserver文件,创建clusterrolebinding,同时也配置了credential和context:
image
image

image

报如下错误:
image

请帮忙看下,谢谢!

@cr7258
Copy link
Owner

cr7258 commented Nov 30, 2022

请求带的 token 和你获取的 id_token 不一致。
image

@dc-weizhenhua
Copy link
Author

我可能解的图,不对,我后面手动refresh了token,然后又获取新的id_token。

@dc-weizhenhua
Copy link
Author

那我现在用kubeconfig里面的id_token能访问吗(能自动刷新吗),还是说需要重写获取。

@dc-weizhenhua
Copy link
Author

另外为什么会报"you must be logged in to the server"错误

@dc-weizhenhua
Copy link
Author

dc-weizhenhua commented Nov 30, 2022

我重新试了一下,还是不行
image

应该是授权没过,ClusterRole 和 ClusterRoleBinding 发出来看看

@cr7258
Copy link
Owner

cr7258 commented Nov 30, 2022

那我现在用kubeconfig里面的id_token能访问吗(能自动刷新吗),还是说需要重写获取。

用 Kubelogin 插件可以自动获取

@dc-weizhenhua
Copy link
Author

那我现在用kubeconfig里面的id_token能访问吗(能自动刷新吗),还是说需要重写获取。

用 Kubelogin 插件可以自动获取

关键是我刚获取的id-token都不行

@cr7258
Copy link
Owner

cr7258 commented Nov 30, 2022

应该是 RBAC 的问题,看下 ClusterRole 和 ClusterRoleBinding

@dc-weizhenhua
Copy link
Author

image
image
我一开始就给了namespace get/watch/list的权限,后面改成放开所有了也不行

@cr7258
Copy link
Owner

cr7258 commented Dec 1, 2022

你把 id_token 黏贴到 jwt.io 看下内容
image

@dc-weizhenhua
Copy link
Author

image

@cr7258
Copy link
Owner

cr7258 commented Dec 1, 2022

token 的有效时长,你有调整吗
image

@dc-weizhenhua
Copy link
Author

调整了

@dc-weizhenhua
Copy link
Author

重新通过
image
获取id-token,能使用这个id_token访问namespace吗;(我用这个id token还是报401错误!!!ri)
另外我的k8s apiserver是使用外部的负载均衡没影响吧。

@dc-weizhenhua
Copy link
Author

我直接用--token指定idtoken就没问题,但我通过切换context,就会报“You must be logged in to the server (Unauthorized)”错误。
image
image
image

另外通过curl获取namespace还是报之前的错误!

请问大佬知道是什么问题吗

@cr7258
Copy link
Owner

cr7258 commented Dec 4, 2022

执行看下是不是有获取 node 的权限:kubectl auth can-i list nodes --as tom

@cr7258
Copy link
Owner

cr7258 commented Dec 4, 2022

我直接用--token指定idtoken就没问题,但我通过切换context,就会报“You must be logged in to the server (Unauthorized)”错误。 image image image

另外通过curl获取namespace还是报之前的错误!

请问大佬知道是什么问题吗

--token 后面故意跟一个错误的 token 看下还能获取 node 吗

@dc-weizhenhua
Copy link
Author

--token 后面故意跟一个错误的 token 看下还能获取 node 吗

还真能获取node,这nm啥问题

另外一个kubectl auth can-i list nodes --as tom有个警告,但是yes
image

@cr7258
Copy link
Owner

cr7258 commented Dec 5, 2022

kubectl get namespace --user tom --token=<id_token> 这样应该就不行了,没指定 --user

@cr7258
Copy link
Owner

cr7258 commented Dec 5, 2022

你可以看下 apiserver 的日志,看下有啥异常

@dc-weizhenhua
Copy link
Author

E1205 08:02:39.410489 1 authentication.go:63] "Unable to authenticate the request" err="invalid bearer token"

token不对,新获取的idtoken,而且是重新set crenditial

@dc-weizhenhua
Copy link
Author

image
使用kubelogin还是一样的错误。

@dc-weizhenhua
Copy link
Author

image
我apiserver是高可用部署的,在一台上看不到问题,在另外一台看到这个问题

@cr7258
Copy link
Owner

cr7258 commented Dec 5, 2022

image 我apiserver是高可用部署的,在一台上看不到问题,在另外一台看到这个问题

原因是 API Server 不信任 keycloak 的证书,需要把签发 keycloak 证书的 CA 添加到 api server 的配置文件中,从而让 API Server 信任 keycloak 的证书。

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants