yaml示例:k8s如何为ServiceAccount授予权限?

您可以使用Role和RoleBinding(或ClusterRole和ClusterRoleBinding)来为ServiceAccount授予权限。

首先,您需要创建一个Role来定义ServiceAccount所需的权限。下面是一个示例:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: my-namespace
  name: my-role
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "watch", "list"]

在上面的示例中,我们创建了一个名为my-role的Role,它允许在my-namespace命名空间中对pods资源执行getwatchlist操作。

接下来,您需要创建一个RoleBinding来将ServiceAccount与Role关联起来。下面是一个示例:

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: my-rolebinding
  namespace: my-namespace
subjects:
- kind: ServiceAccount
  name: my-sa
  namespace: my-namespace
roleRef:
  kind: Role
  name: my-role
  apiGroup: rbac.authorization.k8s.io

在上面的示例中,我们创建了一个名为my-rolebinding的RoleBinding,它将名为my-sa的ServiceAccount与名为my-role的Role关联起来。这样,ServiceAccount就拥有了在my-namespace命名空间中对pods资源执行getwatchlist操作的权限。

如果您希望授予ServiceAccount跨命名空间的权限,您可以使用ClusterRole和ClusterRoleBinding来代替Role和RoleBinding。

猜你喜欢

转载自blog.csdn.net/a772304419/article/details/130195611