我正在尝试为需要持久卷的服务创建RBAC角色/规则,但仍因禁止错误而失败。

这是我的角色配置:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: logdrop-user-full-access
  namespace: logdrop
rules:
- apiGroups: ["", "extensions", "apps", "autoscaling"]
  resources: ["*"]
  verbs: ["*"]
- apiGroups: ["batch"]
  resources:
  - jobs
  - cronjobs
  verbs: ["*"]

这是我减少的PersistentVolume list :
apiVersion: v1
kind: PersistentVolume
metadata:
  name: logdrop-pv
  namespace: logdrop
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  claimRef:
    namespace: logdrop
    name: logdrop-pvc
  hostPath:
    path: /efs/logdrop/logdrop-pv

当我尝试应用它时,我收到了一个禁止的错误。
$ kubectl --kubeconfig ~/logdrop/kubeconfig-logdrop.yml apply -f pv-test.yml
Error from server (Forbidden): error when retrieving current configuration of:
Resource: "/v1, Resource=persistentvolumes", GroupVersionKind: "/v1, Kind=PersistentVolume"
Name: "logdrop-pv", Namespace: ""
Object: &{map["apiVersion":"v1" "kind":"PersistentVolume" "metadata":map["annotations":map["kubectl.kubernetes.io/last-applied-configuration":""] "name":"logdrop-pv"] "spec":map["accessModes":["ReadWriteMany"] "capacity":map["storage":"10Gi"] "claimRef":map["name":"logdrop-pvc" "namespace":"logdrop"] "hostPath":map["path":"/efs/logdrop/logdrop-pv"] "persistentVolumeReclaimPolicy":"Retain"]]}
from server for: "pv-test.yml": persistentvolumes "logdrop-pv" is forbidden: User "system:serviceaccount:logdrop:logdrop-user" cannot get resource "persistentvolumes" in API group "" at the cluster scope

在最后一行,它专门说resource "persistentvolumes" in API group ""-这就是我在规则中允许的内容!

我可以从同一个yaml文件中使用管理员凭据创建PV,并且可以使用logdrop权限创建任何其他资源( pods ,服务等)。由于某些原因,仅PersistentVolume无法正常工作。知道为什么吗?

我正在使用Kubernetes 1.15.0。

更新:

这是我根据要求绑定(bind)的角色:
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: logdrop-user-view
  namespace: logdrop
subjects:
- kind: ServiceAccount
  name: logdrop-user
  namespace: logdrop
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: logdrop-user-full-access

它不是ClusterRoleBinding,因为我的目的是仅授予用户访问一个 namespace (logdrop)的权限,而不是访问整个集群中所有 namespace 的权限。

最佳答案

PV, namespace ,节点和存储是集群作用域对象。作为最佳实践,要能够列出/监视这些对象,您需要创建ClusterRole并通过ClusterRoleBinding将它们绑定(bind)到ServiceAccount。举个例子;

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: <name of your cluster role>
rules:
- apiGroups: [""]
  resources:
  - nodes
  - persistentvolumes
  - namespaces
  verbs: ["list", "watch"]
- apiGroups: ["storage.k8s.io"]
  resources:
  - storageclasses
  verbs: ["list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: <name of your cluster role binding>
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: <name of your cluster role which should be matched with the previous one>
subjects:
  - kind: ServiceAccount
    name: <service account name>

关于Kubernetes PersistentVolume的RBAC规则,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56828876/

10-10 15:10