首页 > 解决方案 > 如何为 Kubernetes 创建受限访问令牌

问题描述

我希望有一个令牌用于一段代码,该代码对我的 k8s 集群的访问权限有限,并且只能读取 statefulsets 的副本,如果需要扩展它们,我不希望使用该代码的人成为能够启动新的东西或删除正在运行的东西。这可能吗?如果是,我该怎么做?

标签: authenticationkubernetes

解决方案


您需要RBAC(基于角色的访问控制)来完成这项工作。

示例 pod-reader 角色:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: default
  name: pod-reader
rules:
- apiGroups: [""] # "" indicates the core API group
  resources: ["pods"]
  verbs: ["get", "watch", "list"]

将此角色绑定到用户:

apiVersion: rbac.authorization.k8s.io/v1
# This role binding allows "jane" to read pods in the "default" namespace.
kind: RoleBinding
metadata:
  name: read-pods
  namespace: default
subjects:
- kind: User
  name: jane # Name is case sensitive
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role #this must be Role or ClusterRole
  name: pod-reader # this must match the name of the Role or ClusterRole you wish to bind to
  apiGroup: rbac.authorization.k8s.io

根据您的要求 更改apiGroupand 。verbs


推荐阅读