猿创征文|云原生|kubernetes学习之RBAC(6.2)

本文涉及的产品
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介: 猿创征文|云原生|kubernetes学习之RBAC

(3)创建rolebinding命令行形式:

kubectl create rolebinding test-pods --role=test --user=zsk
yaml文件形式:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: test-pods
roleRef: # 涉及到的role
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: test
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: User
  name: zsk
可以绑定一个不存在的role,但显然是没有任何意义的

角色绑定将一个角色中定义的各种权限授予一个或者一组用户。角色绑定包含了一组相关主体(即 subject, 包括用户——User、用户组——Group、或者服务账户——Service Account)以及对被授予角色的引用。在命名空间中可以通过 RoleBinding 对象授予权限,而集群范围的权限授予则通过 ClusterRoleBinding 对象完成。上例是对用户zsk授权,此用户不用手动去创建。

subjects下面的kind当然也可以是serveraccount或者是group。

现在zsk这个用户对default这个namespace内的pod拥有verbs: ["list", "watch","get","update","create","ptach"] 这些权限。对statefulsets有 verbs: ["list", "watch"]这些权限了。

(4)创建clusterrolebinding

和rolebinding是一样的,没什么区别,只是只能绑定clusterrole到user,group,serveraccount,下面就演示一下如何绑定用户到clusterrolebinding:

绑定集群角色cluster-admin 到用户kubernetes上,绑定的形式是clusterrolebinding,绑定的名称是kubernetes,这个名字可以任意

k create clusterrolebinding  kubernetes --clusterrole=cluster-admin  --user=kubernetes
[root@k8s-master ~]# k create clusterrolebinding  my --clusterrole=cluster-admin  --user=kubernetes
clusterrolebinding.rbac.authorization.k8s.io/my created

OK,查询绑定情况:

[root@k8s-master ~]# k get clusterrolebindings.rbac.authorization.k8s.io -A |grep cluster-ad
cluster-admin                                          ClusterRole/cluster-admin                                          31d
kubelet-bootstrap                                      ClusterRole/cluster-admin                                          31d
kubernetes                                             ClusterRole/cluster-admin                                          154m
my                                                     ClusterRole/cluster-admin                                          72s
system:kube-proxy                                      ClusterRole/cluster-admin                                          31d
system:node:k8s-node2                                  ClusterRole/cluster-admin                                          31d
zsk                                                    ClusterRole/cluster-admin                                          6h10m

四,集群内的系统内置role和clusterrole


内置的role:

NAMESPACE              NAME                                             CREATED AT
kube-public            system:controller:bootstrap-signer               2022-08-27T01:22:55Z
kube-system            extension-apiserver-authentication-reader        2022-08-27T01:22:55Z
kube-system            system::leader-locking-kube-controller-manager   2022-08-27T01:22:55Z
kube-system            system::leader-locking-kube-scheduler            2022-08-27T01:22:55Z
kube-system            system:controller:bootstrap-signer               2022-08-27T01:22:55Z
kube-system            system:controller:cloud-provider                 2022-08-27T01:22:55Z
kube-system            system:controller:token-cleaner                  2022-08-27T01:22:55Z
kubernetes-dashboard   kubernetes-dashboard                             2022-09-04T02:39:34Z

这些role有一个共同点,同时建立的,建立的时间是kube-apiserver服务第一次启动的时候,随机挑选一个,看看它的权限吧:

[root@master ~]# k describe role system:controller:token-cleaner -n kube-system
Name:         system:controller:token-cleaner
Labels:       kubernetes.io/bootstrapping=rbac-defaults
Annotations:  rbac.authorization.kubernetes.io/autoupdate: true
PolicyRule:
  Resources             Non-Resource URLs  Resource Names  Verbs
  ---------             -----------------  --------------  -----
  events                []                 []              [create patch update]
  events.events.k8s.io  []                 []              [create patch update]
  secrets               []                 []              [delete get list watch]

可以看到,system:controller:token-cleaner这个role的权限针对的是events和secrets和events.events.k8s.io,并且只作用于kube-system这个namespace里。

内置的clusterrole:

集群角色比较多,挑重点的来说,主要是cluster-admin admin edit view 这几个集群角色。

[root@master ~]# k get clusterrole -A
NAME                                                                   CREATED AT
admin                                                                  2022-08-27T01:22:54Z
cluster-admin                                                          2022-08-27T01:22:53Z
edit                                                                   2022-08-27T01:22:54Z
flannel                                                                2022-08-27T08:05:45Z
kubernetes-dashboard                                                   2022-09-04T02:39:34Z
system:aggregate-to-admin                                              2022-08-27T01:22:54Z
system:aggregate-to-edit                                               2022-08-27T01:22:54Z
system:aggregate-to-view                                               2022-08-27T01:22:54Z
system:auth-delegator                                                  2022-08-27T01:22:54Z
system:basic-user                                                      2022-08-27T01:22:54Z
system:certificates.k8s.io:certificatesigningrequests:nodeclient       2022-08-27T01:22:54Z
system:certificates.k8s.io:certificatesigningrequests:selfnodeclient   2022-08-27T01:22:54Z
system:certificates.k8s.io:kube-apiserver-client-approver              2022-08-27T01:22:54Z
system:certificates.k8s.io:kube-apiserver-client-kubelet-approver      2022-08-27T01:22:54Z
system:certificates.k8s.io:kubelet-serving-approver                    2022-08-27T01:22:54Z
system:certificates.k8s.io:legacy-unknown-approver                     2022-08-27T01:22:54Z
system:controller:attachdetach-controller                              2022-08-27T01:22:54Z
system:controller:certificate-controller                               2022-08-27T01:22:54Z
system:controller:clusterrole-aggregation-controller                   2022-08-27T01:22:54Z
system:controller:cronjob-controller                                   2022-08-27T01:22:54Z
system:controller:daemon-set-controller                                2022-08-27T01:22:54Z
system:controller:deployment-controller                                2022-08-27T01:22:54Z
system:controller:disruption-controller                                2022-08-27T01:22:54Z
system:controller:endpoint-controller                                  2022-08-27T01:22:54Z
system:controller:endpointslice-controller                             2022-08-27T01:22:54Z
system:controller:expand-controller                                    2022-08-27T01:22:54Z
system:controller:generic-garbage-collector                            2022-08-27T01:22:54Z
system:controller:horizontal-pod-autoscaler                            2022-08-27T01:22:54Z
system:controller:job-controller                                       2022-08-27T01:22:54Z
system:controller:namespace-controller                                 2022-08-27T01:22:54Z
system:controller:node-controller                                      2022-08-27T01:22:54Z
system:controller:persistent-volume-binder                             2022-08-27T01:22:54Z
system:controller:pod-garbage-collector                                2022-08-27T01:22:54Z
system:controller:pv-protection-controller                             2022-08-27T01:22:54Z
system:controller:pvc-protection-controller                            2022-08-27T01:22:54Z
system:controller:replicaset-controller                                2022-08-27T01:22:54Z
system:controller:replication-controller                               2022-08-27T01:22:54Z
system:controller:resourcequota-controller                             2022-08-27T01:22:54Z
system:controller:route-controller                                     2022-08-27T01:22:54Z
system:controller:service-account-controller                           2022-08-27T01:22:54Z
system:controller:service-controller                                   2022-08-27T01:22:54Z
system:controller:statefulset-controller                               2022-08-27T01:22:54Z
system:controller:ttl-controller                                       2022-08-27T01:22:54Z
system:coredns                                                         2022-08-27T12:45:57Z
system:discovery                                                       2022-08-27T01:22:54Z
system:heapster                                                        2022-08-27T01:22:54Z
system:kube-aggregator                                                 2022-08-27T01:22:54Z
system:kube-apiserver-to-kubelet                                       2022-08-28T02:23:48Z
system:kube-controller-manager                                         2022-08-27T01:22:54Z
system:kube-dns                                                        2022-08-27T01:22:54Z
system:kube-scheduler                                                  2022-08-27T01:22:54Z
system:kubelet-api-admin                                               2022-08-27T01:22:54Z
system:node                                                            2022-08-27T01:22:54Z
system:node-bootstrapper                                               2022-08-27T01:22:54Z
system:node-problem-detector                                           2022-08-27T01:22:54Z
system:node-proxier                                                    2022-08-27T01:22:54Z
system:persistent-volume-provisioner                                   2022-08-27T01:22:54Z
system:public-info-viewer                                              2022-08-27T01:22:54Z
system:volume-scheduler                                                2022-08-27T01:22:54Z
testclusterrole                                                        2022-09-04T12:30:29Z
view                                                                   2022-08-27T01:22:54Z

查看admin 的权限,茫茫多的权限,说明该角色权限非常大:

[root@master ~]# k describe clusterrole admin
Name:         admin
Labels:       kubernetes.io/bootstrapping=rbac-defaults
Annotations:  rbac.authorization.kubernetes.io/autoupdate: true
PolicyRule:
  Resources                                       Non-Resource URLs  Resource Names  Verbs
  ---------                                       -----------------  --------------  -----
  rolebindings.rbac.authorization.k8s.io          []                 []              [create delete deletecollection get list patch update watch]
  roles.rbac.authorization.k8s.io                 []                 []              [create delete deletecollection get list patch update watch]
  configmaps                                      []                 []              [create delete deletecollection patch update get list watch]
  endpoints                                       []                 []              [create delete deletecollection patch update get list watch]
  persistentvolumeclaims                          []                 []              [create delete deletecollection patch update get list watch]
  pods                                            []                 []              [create delete deletecollection patch update get list watch]
  replicationcontrollers/scale                    []                 []              [create delete deletecollection patch update get list watch]
  replicationcontrollers                          []                 []              [create delete deletecollection patch update get list watch]
  services                                        []                 []              [create delete deletecollection patch update get list watch]
  daemonsets.apps                                 []                 []              [create delete deletecollection patch update get list watch]
  deployments.apps/scale                          []                 []              [create delete deletecollection patch update get list watch]
  deployments.apps                                []                 []              [create delete deletecollection patch update get list watch]
  replicasets.apps/scale                          []                 []              [create delete deletecollection patch update get list watch]
  replicasets.apps                                []                 []              [create delete deletecollection patch update get list watch]
  statefulsets.apps/scale                         []                 []              [create delete deletecollection patch update get list watch]
  statefulsets.apps                               []                 []              [create delete deletecollection patch update get list watch]
  horizontalpodautoscalers.autoscaling            []                 []              [create delete deletecollection patch update get list watch]
  cronjobs.batch                                  []                 []              [create delete deletecollection patch update get list watch]
  jobs.batch                                      []                 []              [create delete deletecollection patch update get list watch]
  daemonsets.extensions                           []                 []              [create delete deletecollection patch update get list watch]
  deployments.extensions/scale                    []                 []              [create delete deletecollection patch update get list watch]
  deployments.extensions                          []                 []              [create delete deletecollection patch update get list watch]
  ingresses.extensions                            []                 []              [create delete deletecollection patch update get list watch]
  networkpolicies.extensions                      []                 []              [create delete deletecollection patch update get list watch]
  replicasets.extensions/scale                    []                 []              [create delete deletecollection patch update get list watch]
  replicasets.extensions                          []                 []              [create delete deletecollection patch update get list watch]
  replicationcontrollers.extensions/scale         []                 []              [create delete deletecollection patch update get list watch]
  ingresses.networking.k8s.io                     []                 []              [create delete deletecollection patch update get list watch]
  networkpolicies.networking.k8s.io               []                 []              [create delete deletecollection patch update get list watch]
  poddisruptionbudgets.policy                     []                 []              [create delete deletecollection patch update get list watch]
  deployments.apps/rollback                       []                 []              [create delete deletecollection patch update]
  deployments.extensions/rollback                 []                 []              [create delete deletecollection patch update]
  localsubjectaccessreviews.authorization.k8s.io  []                 []              [create]
  pods/attach                                     []                 []              [get list watch create delete deletecollection patch update]
  pods/exec                                       []                 []              [get list watch create delete deletecollection patch update]
  pods/portforward                                []                 []              [get list watch create delete deletecollection patch update]
  pods/proxy                                      []                 []              [get list watch create delete deletecollection patch update]
  secrets                                         []                 []              [get list watch create delete deletecollection patch update]
  services/proxy                                  []                 []              [get list watch create delete deletecollection patch update]
  bindings                                        []                 []              [get list watch]
  events                                          []                 []              [get list watch]
  limitranges                                     []                 []              [get list watch]
  namespaces/status                               []                 []              [get list watch]
  namespaces                                      []                 []              [get list watch]
  persistentvolumeclaims/status                   []                 []              [get list watch]
  pods/log                                        []                 []              [get list watch]
  pods/status                                     []                 []              [get list watch]
  replicationcontrollers/status                   []                 []              [get list watch]
  resourcequotas/status                           []                 []              [get list watch]
  resourcequotas                                  []                 []              [get list watch]
  services/status                                 []                 []              [get list watch]
  controllerrevisions.apps                        []                 []              [get list watch]
  daemonsets.apps/status                          []                 []              [get list watch]
  deployments.apps/status                         []                 []              [get list watch]
  replicasets.apps/status                         []                 []              [get list watch]
  statefulsets.apps/status                        []                 []              [get list watch]
  horizontalpodautoscalers.autoscaling/status     []                 []              [get list watch]
  cronjobs.batch/status                           []                 []              [get list watch]
  jobs.batch/status                               []                 []              [get list watch]
  daemonsets.extensions/status                    []                 []              [get list watch]
  deployments.extensions/status                   []                 []              [get list watch]
  ingresses.extensions/status                     []                 []              [get list watch]
  replicasets.extensions/status                   []                 []              [get list watch]
  ingresses.networking.k8s.io/status              []                 []              [get list watch]
  poddisruptionbudgets.policy/status              []                 []              [get list watch]
  serviceaccounts                                 []                 []              [impersonate create delete deletecollection patch update get list watch]

cluste-admin 这个角色是给其它角色赋权,并使用其它所有资源,比admin还要牛 比的存在,真正的生杀予夺的角色(狠角色)

[root@master ~]# k describe clusterrole cluster-admin
Name:         cluster-admin
Labels:       kubernetes.io/bootstrapping=rbac-defaults
Annotations:  rbac.authorization.kubernetes.io/autoupdate: true
PolicyRule:
  Resources  Non-Resource URLs  Resource Names  Verbs
  ---------  -----------------  --------------  -----
  *.*        []                 []              [*]
             [*]                []              [*]

view角色,查看一切的角色:

[root@master ~]# k describe clusterrole view
Name:         view
Labels:       kubernetes.io/bootstrapping=rbac-defaults
              rbac.authorization.k8s.io/aggregate-to-edit=true
Annotations:  rbac.authorization.kubernetes.io/autoupdate: true
PolicyRule:
  Resources                                    Non-Resource URLs  Resource Names  Verbs
  ---------                                    -----------------  --------------  -----
  bindings                                     []                 []              [get list watch]
  configmaps                                   []                 []              [get list watch]
  endpoints                                    []                 []              [get list watch]
  events                                       []                 []              [get list watch]
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、。、、、、、、、、、、。。

edit角色,修改一切的角色:

root@master ~]# k describe clusterrole edit
Name:         edit
Labels:       kubernetes.io/bootstrapping=rbac-defaults
              rbac.authorization.k8s.io/aggregate-to-admin=true
Annotations:  rbac.authorization.kubernetes.io/autoupdate: true
PolicyRule:
  Resources                                    Non-Resource URLs  Resource Names  Verbs
  ---------                                    -----------------  --------------  -----
  configmaps                                   []                 []              [create delete deletecollection patch update get list watch]
  endpoints                                    []                 []              [create delete deletecollection patch update get list watch]
  persistentvolumeclaims                       []                 []              [create delete deletecollection patch update get list watch]
  pods                                         []                 []              [create delete deletecollection patch update get list watch]
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

这些内置的角色存在的意义是为了维护系统的和平(像不像说维护世界和平?),其实有时候不知道怎么分配权限了,看看它们也就会配置了。

我可以明确的说,k8s的权限管理系统和数据库尤其是Oracle数据库是非常非常相似的,如果对数据库比较熟悉的话,上手这个k8s的权限管理系统也是非常容易的哦。

相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
4天前
|
Kubernetes Cloud Native 开发者
云原生技术入门:Kubernetes和Docker的协作之旅
【10月更文挑战第22天】在数字化转型的浪潮中,云原生技术成为推动企业创新的重要力量。本文旨在通过浅显易懂的语言,引领读者步入云原生的世界,着重介绍Kubernetes和Docker如何携手打造弹性、可扩展的云环境。我们将从基础概念入手,逐步深入到它们在实际场景中的应用,以及如何简化部署和管理过程。文章不仅为初学者提供入门指南,还为有一定基础的开发者提供实践参考,共同探索云原生技术的无限可能。
17 3
|
3天前
|
运维 Kubernetes Cloud Native
云原生入门:Kubernetes和容器化的未来
【10月更文挑战第23天】本文将带你走进云原生的世界,探索Kubernetes如何成为现代软件部署的心脏。我们将一起揭开容器化技术的神秘面纱,了解它如何改变软件开发和运维的方式。通过实际的代码示例,你将看到理论与实践的结合,感受到云原生技术带来的革命性影响。无论你是初学者还是有经验的开发者,这篇文章都将为你开启一段新的旅程。让我们一起踏上这段探索之旅,解锁云原生技术的力量吧!
|
10天前
|
Kubernetes Cloud Native 开发者
探秘云原生计算:Kubernetes与Docker的协同进化
在这个快节奏的数字时代,云原生技术以其灵活性和可扩展性成为了开发者们的新宠。本文将带你深入了解Kubernetes和Docker如何共同塑造现代云计算的架构,以及它们如何帮助企业构建更加敏捷和高效的IT基础设施。
|
18天前
|
Kubernetes 安全 Cloud Native
云上攻防-云原生篇&K8s安全-Kubelet未授权访问、API Server未授权访问
本文介绍了云原生环境下Kubernetes集群的安全问题及攻击方法。首先概述了云环境下的新型攻击路径,如通过虚拟机攻击云管理平台、容器逃逸控制宿主机等。接着详细解释了Kubernetes集群架构,并列举了常见组件的默认端口及其安全隐患。文章通过具体案例演示了API Server 8080和6443端口未授权访问的攻击过程,以及Kubelet 10250端口未授权访问的利用方法,展示了如何通过这些漏洞实现权限提升和横向渗透。
云上攻防-云原生篇&K8s安全-Kubelet未授权访问、API Server未授权访问
|
22天前
|
Kubernetes Cloud Native Docker
云原生入门:Kubernetes和Docker的协同之旅
【10月更文挑战第4天】在这篇文章中,我们将通过一次虚拟的旅行来探索云原生技术的核心——Kubernetes和Docker。就像乘坐一艘由Docker驱动的小船启航,随着波浪(代码示例)起伏,最终抵达由Kubernetes指挥的宏伟舰队。这不仅是一段技术上的旅程,也是理解现代云架构如何支撑数字世界的冒险。让我们扬帆起航,一探究竟!
|
22天前
|
运维 Kubernetes Cloud Native
云原生时代的容器编排:Kubernetes入门与实践
【10月更文挑战第4天】在云计算的浪潮中,云原生技术以其敏捷、可扩展和高效的特点引领着软件开发的新趋势。作为云原生生态中的关键组件,Kubernetes(通常被称为K8s)已成为容器编排的事实标准。本文将深入浅出地介绍Kubernetes的基本概念,并通过实际案例引导读者理解如何利用Kubernetes进行高效的容器管理和服务部署。无论你是初学者还是有一定经验的开发者,本文都将为你打开云原生世界的大门,并助你一臂之力在云原生时代乘风破浪。
|
运维 Kubernetes Cloud Native
如何轻松学习 Kubernetes?
《深入浅出 Kubernetes》一书共汇集 12 篇技术文章,帮助你一次搞懂 6 个核心原理,吃透基础理论,一次学会 6 个典型问题的华丽操作!
如何轻松学习 Kubernetes?
|
运维 Kubernetes 负载均衡
如何轻松学习 Kubernetes?
本文分享阿里技术专家关于 Kubernetes 的一些观点和看法,并给出学习 Kubernetes 的方法建议 ,最后分享 Kubernetes 集群上的问题排查经验。
11411 0
如何轻松学习 Kubernetes?
|
6天前
|
JSON Kubernetes 容灾
ACK One应用分发上线:高效管理多集群应用
ACK One应用分发上线,主要介绍了新能力的使用场景
|
7天前
|
Kubernetes 持续交付 开发工具
ACK One GitOps:ApplicationSet UI简化多集群GitOps应用管理
ACK One GitOps新发布了多集群应用控制台,支持管理Argo CD ApplicationSet,提升大规模应用和集群的多集群GitOps应用分发管理体验。