企业网站建设基本原则,广告大全,dede 后门暴网站,未备案域名在 k8s 集群的 RBAC 里有用到用户、组的概念#xff0c;但是它又不直接管理这些资源#xff0c;而是通过外部身份验证机制#xff08;Authentication Mechanisms#xff09;来管理和定义的#xff0c;比如证书进行签名时#xff0c;将其配置为 Subject: O system:master…在 k8s 集群的 RBAC 里有用到用户、组的概念但是它又不直接管理这些资源而是通过外部身份验证机制Authentication Mechanisms来管理和定义的比如证书进行签名时将其配置为 Subject: O system:masters, CN kubernetes-admin。O 代表用户组CN 是用户这些都是通过签署证书管理的。 但是新版本可以直接通过命令去创建我用的是 1.31 这些在集群管理权限的时候很重要如何给用户生成相应的 kubeconfig 文件下面就这个问题说明下刚接触往往这块会比较懵逼
查看当前的用户组
可以把新建的用户放到这些组下或者新建组都可以
kubectl get rolebindings rolebinding-name -n namespace -o json | jq .subjects[] | select(.kind Group) | .name
kubectl get clusterrolebindings clusterrolebinding-name -o json | jq .subjects[] | select(.kind Group) | .name创建用户和组并授权
用户授权
k8s 里kubeadm 命令有提供给用户创建 conf 的命令下面我们来创建用户jane的 conf所在的组是podreader这个组也是我们新创建的没有任何权限生成 conf 文件存在pod-reader-group.conf
kubeadm kubeconfig user --org podreader --client-name jane --validity-period 24h pod-reader.conf我们先通过上面这个 conf 文件查看下 pod会爆没有权限的错误属于预期范围因为咱们的用户jane没有任何权限
# kubectl get pod --kubeconfig pod-reader.conf
Error from server (Forbidden): pods is forbidden: User janegroup cannot list resource pods in API group in the namespace default这一步我们给jane授权通过创建 role rolebinding
# cat pod-reader.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:namespace: defaultname: pod-reader
rules:
- apiGroups: [] # 标明 core API 组resources: [pods]verbs: [get, watch, list]# cat pod-reader-binding.yaml
apiVersion: rbac.authorization.k8s.io/v1
# 此角色绑定允许 jane 读取 default 名字空间中的 Pod
# 你需要在该名字空间中有一个名为 “pod-reader” 的 Role
kind: RoleBinding
metadata:name: read-podsnamespace: default
subjects:
# 你可以指定不止一个“subject主体”
- kind: Username: jane # name 是区分大小写的apiGroup: rbac.authorization.k8s.io
roleRef:# roleRef 指定与某 Role 或 ClusterRole 的绑定关系kind: Role # 此字段必须是 Role 或 ClusterRolename: pod-reader # 此字段必须与你要绑定的 Role 或 ClusterRole 的名称匹配apiGroup: rbac.authorization.k8s.io现在我们再执行下第二步的命令会发现可以查看了已经有查看 default ns 下pod 的权限但是其他的 ns 仍然没有因为 role 没有给其他的 ns 授权
kubectl apply -f pod-reader.yaml
kubectl apply -f pod-reader-binding.yaml上面是给单个用户授权的但是它所在的组podreader没有任何权限所以需要再给他授权方法和上面给用户授权类似
组授权
创建一个新的用户janegroup所属组在podreader里然后执行get pod 命令是无法查看的因为所属的组是没有权限的
# kubeadm kubeconfig user --org podreader --client-name janegroup --validity-period 24h pod-reader-group.conf
# kubectl get pod --kubeconfig pod-reader-group.conf
Error from server (Forbidden): pods is forbidden: User janegroup cannot list resource pods in API group in the namespace default下面我们给组podreader绑定权限role 文件和上一步是一样的
cat pod-reader-binding-group.yaml
apiVersion: rbac.authorization.k8s.io/v1
# 此角色绑定允许 jane 读取 default 名字空间中的 Pod
# 你需要在该名字空间中有一个名为 “pod-reader” 的 Role
kind: RoleBinding
metadata:name: read-podsnamespace: default
subjects:
# 你可以指定不止一个“subject主体”
- kind: Groupname: podreader # name 是区分大小写的apiGroup: rbac.authorization.k8s.io
roleRef:# roleRef 指定与某 Role 或 ClusterRole 的绑定关系kind: Role # 此字段必须是 Role 或 ClusterRolename: pod-reader # 此字段必须与你要绑定的 Role 或 ClusterRole 的名称匹配apiGroup: rbac.authorization.k8s.io再次执行查看命令就可以查看了说明权限已生效
# kubectl get pod --kubeconfig pod-reader-group.conf
No resources found in default namespace.