K8S serviceAccount(服务账号)-安全

创建一个命名空间test

[root@cka-master ~]# kubectl create ns test

 

创建pod为web-test并指定命名空间test

[root@cka-master ~]# kubectl create deployment web-test --image=nginx -n test

 

创建服务账号libin

[root@cka-master ~]# kubectl create serviceaccount libin -n test

[root@cka-master ~]# kubectl get sa -n test

 

创建一个角色libin-rc(集群角色有两种clusterrole 和role)

[root@cka-master ~]# kubectl create clusterrole libin-rc --verb=create --resource=deployment,daemonsets,statefulsets

 

将服务账号与集群角色clusterrole进行绑定(libin-libin-rc与libin-rc进行绑定指定命名空间:服务账号)

 

[root@cka-master ~]# kubectl create rolebinding libin-libin-rc --serviceaccount=test:libin \

> --clusterrole=libin-rc -n test

 

测试:

查看在test命名空间内创建的sa,rolebinding

[root@cka-master ~]# kubectl get sa,rolebinding -n test

 

 

这样服务账号就具备创建deployment,daemonsets,statefulsets的权限

(默认格式:kubectl --as=system:serviceaccount:命名空间:服务账号)

[root@cka-master ~]# kubectl --as=system:serviceaccount:test:libin create deployment libin-pod --image=nginx -n test

 

加上get pods的权限(先创建集群角色libin-rc2,赋予get和list权限)

[root@cka-master ~]# kubectl create clusterrole libin-rc2 --verb=get,list  --resource=pods

 

(通过rolebinding 的libin-libin-rc2将命名空间:服务账号 与集群角色libin-rc2进行绑定,并指定命名空间)

[root@cka-master ~]# kubectl create rolebinding libin-libin-rc2 --serviceaccount=test:libin --clusterrole=libin-rc2 -n test

 

测试:

[root@cka-master ~]# kubectl --as=system:serviceaccount:test:libin get pods -n test 

 

 

 

posted @   LB_运维技术  阅读(86)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示