12.1 什么是RBAC?
负责k8s整个集群控制的;不同人员权限的管控:开发,测试,管理员等;


12.2 RBAC配置解析


12.3 RBAC常用配置示例

参考官方文档:使用 RBAC 鉴权 | Kubernetes
正常是通过yaml文件创建,命令行创建简单的示例如下



12.4 聚合ClusterRole

[root@k8s-master01 13st]# cat a-clusterrule.yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: test-aggregation
aggregationRule:
clusterRoleSelectors:
- matchLabels:
rbac.example.com/aggregate-to-test-aggregation: "true"
rules: []
[root@k8s-master01 13st]#
kubectl create -f a-clusterrule.yaml



kubectl create clusterrolebinding test-aggregation --clusterrole=test-aggregation --serviceaccount=default:test-aggregation
https://192.168.1.107:32548/#/deployment?namespace=default
可以登录dashboard,但是serviceaccout test-aggregation没有权限,且查看不了namespace

a-clusterrule.yaml 这个聚合clusterrole没权限,我们现在创建含有聚合clusterrole指定标签的且拥有指定权限的clusterrole,验证权限能否注入到聚合clusterrole中去。
[root@k8s-master01 13st]# cat b-clusterrole.yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: b-aggregation
labels:
rbac.example.com/aggregate-to-test-aggregation: "true"
# These rules will be added to the "monitoring" role.
rules:
- apiGroups : [""]
resources: ["services", "endpoints", "pods","namespaces"]
verbs: ["get", "list", "watch"]
您在 /var/spool/mail/root 中有新邮件
[root@k8s-master01 13st]#
kubectl create -f b-clusterrole.yaml
再次登录dashboard进行权限验证:https://192.168.1.107:32548/#/overview?namespace=default
pod,sevice等权限正常
12.5 RBAC实践
[root@k8s-master01 13st]# cat dp-clusterrole.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
# "namespace" 被忽略,因为 ClusterRoles 不受名字空间限制
name: deployment-clusterrole
rules:
- apiGroups: ["apps","extensions"]
# 在 HTTP 层面,用来访问 Secret 资源的名称为 "secrets"
resources: ["deployments","daemonsets","statefulsets"]
verbs: ["create"]
您在 /var/spool/mail/root 中有新邮件
[root@k8s-master01 13st]#
kubectl create -f dp-clusterrole.yaml


将前面clusterrule的权限绑定到用户上(sa:cicd-token)。
kubectl create rolebinding deployment-rolebinding --clusterrole=deployment-clusterrole --serviceaccount=app-team1:cicd-token -n app-team1



登录dashboard:https://192.168.1.107:32548/#/login
查看deployment等create权限

12.6 RBAC企业实战:如何管理权限和用户



12.7 RBAC企业实战:不同用户不同权限

用sa为admin-user的token登录dashboard;


[root@k8s-master01 13st]# cat namespace-list.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: namespace-readonly
rules:
- apiGroups: [""]
resources:
- namespaces
verbs:
- get
- list
- watch
- apiGroups:
- metrics.k8s.io
resources:
- pods
verbs:
- get
- list
- watch
[root@k8s-master01 13st]#
kubectl create -f namespace-list.yaml

[root@k8s-master01 13st]# cat pod-exec.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: pod-exec
rules:
- apiGroups: [""]
resources:
- pods
verbs:
- get
- list
- apiGroups: [""]
resources:
- pods/exec
verbs:
- create
[root@k8s-master01 13st]#
kubectl create -f pod-exec.yaml
[root@k8s-master01 13st]# cat pod-log.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: pod-log
rules:
- apiGroups: [""]
resources:
- pods
- pods/log
verbs:
- get
- list
- watch
[root@k8s-master01 13st]#
kubectl create -f pod-log.yaml




通过ClusterRoleBinding授权

[root@k8s-master01 13st]# cat clusterrolebinding-namespaces.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: namespace-readonly-name
subjects:
- kind: Group
#具体参考https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/rbac/#rolebinding-and-clusterrolebinding
name: system:serviceaccounts:kube-users
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: namespace-readonly
apiGroup: rbac.authorization.k8s.io
[root@k8s-master01 13st]#
kubectl create -f clusterrolebinding-namespaces.yaml

此时再次验证dashboard


#给用户授权指定命名空间下pod 日志的查看权限
kubectl create rolebinding dotbalo-pod-log --clusterrole=pod-log --serviceaccount=kube-users:dotbalo --namespace=kube-system
kubectl create rolebinding dotbalo-pod-log --clusterrole=pod-log --serviceaccount=kube-users:dotbalo --namespace=default
登录dashboard测试


继续测试

kubectl create rolebinding dukuan-pod-exec --clusterrole=pod-exec --serviceaccount=kube-users:dukuan --namespace=default
kubectl create rolebinding dukuan-pod-delete --clusterrole=pod-delete --serviceaccount=kube-users:dukuan --namespace=default
dashboard切花=换token登录dukuan,删除pod后新pod重建;


可以进入容器执行命令,exec生效

dukuan用户无日志查看权限,因为未授权

12.8 K8s多集群图形化管理工具Ratel介绍使用
[root@k8s-master01 ratel]# kubectl create secret generic ratel-config --from-file=study.config --from-file=servers.yaml -n kube-system
secret/ratel-config created
[root@k8s-master01 ratel]# cat servers.yaml
serverName: 'study'
serverAddress: 'https://192.168.1.236:8443'
#serverAdminUser: 'xxx'
#serverAdminPassword: 'xxx#'
serverAdminToken: 'null'
serverDashboardUrl: "https://192.168.1.107:32548/#"
production: 'false'
kubeConfigPath: "/mnt/study.config"
harborConfig: "HarborUrl, HarborUsername, HarborPassword, HarborEmail"
[root@k8s-master01 ratel]#

按照文档操作:ratel-doc/Install.md at master · dotbalo/ratel-doc · GitHub
12.9 图形化管理K8s多租户权限
--------------教程来源:51cto 杜宽老师k8s课程的学习笔记 -----------
2296



被折叠的 条评论
为什么被折叠?



