基于RBAC的k8s服务账号授权示例

文章详细介绍了如何使用kubectl命令和yaml文件在Kubernetes中基于RBAC模型为服务账号分配权限,包括创建命名空间、服务账号、集群角色、角色绑定,以及如何编辑权限和测试权限变更的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基于RBAC的k8s服务账号授权示例

服务账号(ServiceAccount)是Kubernetes API所管理的用户。它们被绑定到特定的名字空间,或者由API服务器自动创建,或者通过API调用创建。服务账号与一组以Secret保存的凭据相关,这些凭据会被挂载到Pod中,从而允许集群内的进程访问Kubernetes API。

使用kubectl命令

下面的例子展示了如何为服务账号分配只能创建deployment、statefulset和daemonset的权限。

# 创建命名空间
kubectl create ns app-team1

# 创建服务账号
kubectl create sa cicd-token -n app-team1

# 创建集群角色
kubectl create clusterrole deployer-clus --verb=create --resource=deployments,daemonsets,statefulsets

# 服务账号绑定角色
kubectl create rolebinding rb-cicd-token --serviceaccount=app-team1:cicd-token --clusterrole=deployer-clus -n app-team1

# 测试服务账号权限
kubectl create deploy webserver --image=nginx --as=system:serviceaccount:app-team1:cicd-token -n app-team1
kubectl get deploy,pods -n app-team1

使用yaml文件

上面的流程写成对应的yaml文件为:

apiVersion:v1
kind: ServiceAccount
metadata:
  name: cicd-token
  namespace: app-team1

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: deployer-clus
rules:
- apiGroups: ["apps"]
  resources: ["deployments", "daemonsets", "statefulsets"]
  verbs: ["create"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: rb-cicd-token
  namespace: app-team1
subjects:
- kind: ServiceAccount
  name: cicd-token
  namespace: app-team1
roleRef:
  kind: ClusterRole
  name: deployer-clus
  apiGroup: rbac.authorization.k8s.io

修改服务账号权限

绑定了deployer-clus角色后,服务账号cicd-token只有create权限,但是无法查看资源:

[root@k8s-master ~]# kubectl get deploy,pods --as=system:serviceaccount:app-team1:cicd-token -n app-team1
Error from server (Forbidden): deployments.apps is forbidden: User "system:serviceaccount:app-team1:cicd-token" cannot list resource "deployments" in API group "apps" in the namespace "app-team1"
Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:app-team1:cicd-token" cannot list resource "pods" in API group "" in the namespace "app-team1"

使用kubectl edit命令修改deployer-clus角色来为服务账号添加读权限:

[root@k8s-master ~]# kubectl edit clusterrole/deployer-clus
clusterrole.rbac.authorization.k8s.io/deployer-clus edited

在打开的yaml文件中添加对deployments和pods的读权限:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  creationTimestamp: "2023-01-01T07:59:26Z"
  name: deployer-clus
  resourceVersion: "547284"
  uid: 6dea3a32-3bc1-4f85-b3c8-1261ba31d632
rules:
- apiGroups:
  - apps
  resources:
  - deployments
  - daemonsets
  - statefulsets
  verbs:
  - create
- apiGroups:
  - apps
  resources:
  - deployments
  - daemonsets
  - statefulsets
  verbs:
  - get
  - list
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
  - list

测试服务账号的权限修改是否生效:

[root@k8s-master ~]# kubectl get deploy,pods --as=system:serviceaccount:app-team1:cicd-token -n app-team1
NAME                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/webserver   0/1     1            0           26m

NAME                             READY   STATUS             RESTARTS   AGE
pod/webserver-7c4f9bf7bf-c4qjp   0/1     ImagePullBackOff   0          26m

References
【1】https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/authentication/
【2】https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/rbac/
【3】https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/authorization/#determine-the-request-verb

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GottdesKrieges

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值