gpt4 book ai didi

kubernetes - 您如何设置kubernetes RBAC资源,以便Pod可以通过客户端访问API?

转载 作者:行者123 更新时间:2023-12-02 11:53:03 24 4
gpt4 key购买 nike

问题

我有一个简单的RBAC配置,可以访问Kubernetes API集群。但是,我从kubectl中得到了似乎有冲突的信息。部署 list 后,似乎RBAC已正确设置。

$ kubectl exec -ti pod/controller -- kubectl auth can-i get namespaces
Warning: resource 'namespaces' is not namespace scoped
yes

但是,实际上发出请求会产生权限错误
$ kubectl exec -ti pod/controller -- kubectl get namespaces
Error from server (Forbidden): namespaces is forbidden: User "system:serviceaccount:default:controller" cannot list resource "namespaces" in API group "" at the cluster scope
command terminated with exit code 1

表现
apiVersion: 'v1'
kind: 'ServiceAccount'
metadata:
name: 'controller'
---

apiVersion: 'rbac.authorization.k8s.io/v1'
kind: 'Role'
metadata:
name: 'read-namespaces'
rules:
- apiGroups:
- ''
resources:
- 'namespaces'
verbs:
- 'get'
- 'watch'
- 'list'
---

apiVersion: 'rbac.authorization.k8s.io/v1'
kind: 'RoleBinding'
metadata:
name: 'read-namespaces'
roleRef:
apiGroup: ''
kind: 'Role'
name: 'read-namespaces'
subjects:
- kind: 'ServiceAccount'
name: 'controller'
---

apiVersion: 'v1'
kind: 'Pod'
metadata:
name: 'controller'
labels:
'app': 'controller'
spec:
containers:
- name: 'kubectl'
image: 'bitnami/kubectl:latest'
imagePullPolicy: 'Always'
command:
- 'sleep'
- '3600'
serviceAccountName: 'controller'
---

其他资讯

我试过了 kubectl auth reconcile -f manifest.yamlkubectl apply -f manifest.yaml,结果是一样的。

我还将“读取命名空间” RoleBinding.subjects[0].namespace设置为适当的命名空间(在这种情况下为“默认”)。输出无变化。

最佳答案

命名空间是群集范围的资源。因此,您需要一个ClusterRoleClusterRoleBinding

apiVersion: 'rbac.authorization.k8s.io/v1'
kind: 'ClusterRole'
metadata:
name: 'read-namespaces'
rules:
- apiGroups:
- ''
resources:
- 'namespaces'
verbs:
- 'get'
- 'watch'
- 'list'
---

apiVersion: 'rbac.authorization.k8s.io/v1'
kind: 'ClusterRoleBinding'
metadata:
name: 'read-namespaces'
roleRef:
apiGroup: 'rbac.authorization.k8s.io'
kind: 'ClusterRole'
name: 'read-namespaces'
subjects:
- kind: 'ServiceAccount'
name: 'controller'
---

关于kubernetes - 您如何设置kubernetes RBAC资源,以便Pod可以通过客户端访问API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61958879/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com