我有以下用户清单,我希望允许 myapp-user 获取集群中所有命名空间的列表。从我查找的内容来看,我应该创建一个 ClusterRole,但我真的找不到足够的详细信息。是否有所有 apiGroups 和相应资源和动词的列表?
apiVersion: v1
kind: ServiceAccount
metadata:
name: myapp-user
namespace: myapp
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: myapp-user-role
namespace: myapp
rules:
- apiGroups: ["", "extensions", "apps"]
resources: ["*"]
verbs: ["*"]
- apiGroups: ["batch"]
resources:
- jobs
- cronjobs
verbs: ["*"]
- apiGroups: ["networking.k8s.io"]
resources:
- ingress
verbs: ["*"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: myapp-user
namespace: myapp
subjects:
- kind: ServiceAccount
name: myapp-suer
namespace: myapp
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: myapp-user-role
Run Code Online (Sandbox Code Playgroud)
我认为将其添加到 role.rules 可能会有所帮助,但不幸的是没有
- apiGroups: [""]
resources: ["namespaces"]
verbs: ["GET"]
Run Code Online (Sandbox Code Playgroud)
您可以通过以下方式获取 API 资源
kubectl api-resources
NAME SHORTNAMES APIGROUP NAMESPACED KIND
bindings true Binding
componentstatuses cs false ComponentStatus
configmaps cm true ConfigMap
endpoints ep true Endpoints
events ev true Event
limitranges limits true LimitRange
namespaces ns false Namespace
nodes no false Node
persistentvolumeclaims pvc true PersistentVolumeClaim
persistentvolumes pv false PersistentVolume
Run Code Online (Sandbox Code Playgroud)
对于创建 clusterrole 和 clusterolebinding 下面的命令应该可以工作。
kubectl create clusterrole cr --verb=get,list --resource=namespaces
kubectl create clusterrolebinding crb --clusterrole=cr --serviceaccount=default:default
Run Code Online (Sandbox Code Playgroud)
然后测试一下
kubectl auth can-i get ns --as=system:serviceaccount:default:default
kubectl auth can-i list ns --as=system:serviceaccount:default:default
Run Code Online (Sandbox Code Playgroud)
感谢@abhishek-jaisingh 和@arghya-sadhu 的回答,我能够弄清楚并将命令重写为清单。
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: myapp-user-cr
rules:
- apiGroups: [""]
resources: ["namespaces"]
verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: myapp-user-crb
subjects:
- kind: ServiceAccount
name: myapp-user
roleRef:
kind: ClusterRole
name: myapp-user-cr
apiGroup: rbac.authorization.k8s.io
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3070 次 |
| 最近记录: |