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