如何在 k8s 中为非服务帐户创建不记名令牌

How to create a bearer token for a non service account in k8s

我是 kubernetes 的新手,我正在尝试创建对其特定名称空间具有有限访问权限的普通用户。

我已按照文档 Certificate Signing Requests/Normal user 进行操作,并且已成功创建测试用户并且能够限制该用户的权限。我试图了解我是否可以为该用户提供对仪表板的访问权限,并且只能看到他自己的资源,例如只有他的命名空间。

通过我的过程,我发现所有文档都提到您需要有一个服务帐户才能访问仪表板 Creating sample user

然后我继续阅读那个服务帐户 Managing Service Accounts“用户帐户是针对人类的。服务帐户是针对进程的,运行 在 pods 中。”

在社区的相关问题中Is there a way to create a token for a normal user in Kubernetes?。同样,这个问题的答案是创建一个服务帐户,但正如官方文档中所记录的那样,我们应该为 运行 宁在 pods.

上的进程使用服务帐户

我对这个话题有点困惑,如果用户(非管理员)应该能够访问仪表板并查看与角色绑定下相同的资源,有人可以分享最佳实践和更多信息吗和他一起?

创建 bearer token 和连接到 Kubernetes Dashboard 的最常用方法是使用 ServiceAccount

但是,如果您想为 User Account 创建 Bearer Token,据我所知,您必须使用 Static Token File

在您想要允许 user account 访问 Kubernetes Dashboard 的情况下,您必须考虑使用 Authorization header 选项。

Using authorization header is the only way to make Dashboard act as an user, when accessing it over HTTP. Note that there are some risks since plain HTTP traffic is vulnerable to MITM attacks.

To make Dashboard use authorization header you simply need to pass Authorization: Bearer in every request to Dashboard. This can be achieved i.e. by configuring reverse proxy in front of Dashboard. Proxy will be responsible for authentication with identity provider and will pass generated token in request header to Dashboard. Note that Kubernetes API server needs to be configured properly to accept these tokens.

出于测试目的,您可以使用允许您修改请求的插件 headers。

旁注

Authorization header will not work if Dashboard is accessed through API server proxyAccessing Dashboard 指南中描述的 kubectl proxyAPI Server 访问仪表板的方式都将不起作用。这是因为一旦请求到达 API 服务器,所有额外的 headers 都会被丢弃。

reverse proxy 的例子是 Kublr。您可以在 Leveraging Client Certificates and Bearer Tokens to Authenticate in Kubernetes 博客中找到更好的描述。