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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下