写点什么

Kubernetes 身份认证和授权操作全攻略:上手操作 Kubernetes 授权

  • 2020-05-16
  • 本文字数:2477 字

    阅读完需:约 8 分钟

Kubernetes身份认证和授权操作全攻略:上手操作Kubernetes授权

这是本系列文章中的第三篇,前两篇文章分别介绍了Kubernetes访问控制以及身份认证。本文将通过上手实践的方式,带你理解 Kubernetes 授权这一概念。


在文章正式开始之前,我们先快速回顾一下我们实操过程中的环境和场景。我们正在处理生产环境中的集群,其中每个部分都与命名空间相关联。现在,组里新来了一位同事叫 Bob,我们在上篇教程中帮助 Bob 以 engineering 命名空间管理员的身份加入集群。并且他已经获得私钥以及签名证书来访问集群。


如果你还没有完成上述操作,请查看上篇教程,运行其中的命令以完成环境设置以及为 Bob 配置证书。


好,我们正式开始本篇教程。


现在我们要给 Bob 授权,以控制属于 engineering 命名空间的资源。


首先,我们要为 kubectl 创建一个上下文(context),方便它在不同的环境之间切换。


kubectl config set-context eng-context \  --cluster=minikube \  --namespace=engineering \  --user=bobContext "eng-context" created.
复制代码


上面的命令使用 Bob 在 minikube 集群中的凭据创建了一个指向 engineering 命名空间的新上下文。这会导致在〜/ .kube / config 文件中添加一个新的部分。



我们现在在 engineering 命名空间中创建一个简单的 pod:


apiVersion: v1kind: Podmetadata:  name: myapp  namespace: engineering  labels:    app: myappspec:  containers:  - name: myapp    image: busybox    command: ["/bin/sh", "-ec", "while :; do echo '.'; sleep 5 ; done"]
复制代码


 kubectl create -f myapp.yamlpod/myapp created
复制代码


kubectl get pods -n=engineeringNAME    READY   STATUS    RESTARTS   AGEmyapp   1/1     Running   0          89s
复制代码


虽然您可以作为集群管理员在工程命名空间中创建和操作 pod,但 Bob 甚至无法在同一名称空间中列出 pod。


kubectl get pods --namespace engineering --as bobError from server (Forbidden): pods is forbidden: User "bob" cannot list resource "pods" in API group "" in the namespace "engineering"
复制代码


为了使得 Bob 可以在 engineering 命名空间中访问资源,我们需要给他授权。这可以通过创建具有适当权限的角色然后将其绑定到用户 Bob 来完成。实质上,我们使用的是基于角色访问控制(RBAC)来允许 Bob 对 engineering 命名空间中的某些 Kubernetes 资源执行特定操作。


创建一个名为 eng-reader 的 Kubernetes 角色,允许其在 engineering 命名空间中列出 pod。


apiVersion: rbac.authorization.k8s.io/v1metadata:  namespace: engineering   name: eng-readerrules:- apiGroups: [""] # "" indicates the core API group  resources: ["pods", "services", "nodes"]  verbs: ["get", "watch", "list"]
复制代码


kubectl create -f role.yamlrole.rbac.authorization.k8s.io/eng-reader created
复制代码


kubectl get roles --namespace=engineeringNAME         AGEeng-reader   58s
复制代码


注意,这一角色目前和 Bob 毫无关联。我们需要通过角色绑定将角色中指定的权限应用于 Bob。


kind: RoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata:  name: eng-read-access  namespace: engineeringsubjects:- kind: User  name: bob # Name is case sensitive  apiGroup: rbac.authorization.k8s.ioroleRef:  kind: Role #this must be Role or ClusterRole  name: eng-reader # this must match the name of the Role or ClusterRole you wish to bind to  apiGroup: rbac.authorization.k8s.io
复制代码


kubectl create -f role-binding.yamlrolebinding.rbac.authorization.k8s.io/eng-read-access created
复制代码


kubectl get rolebindings --namespace=engineeringNAME              AGEeng-read-access   31s
复制代码


让我们来检查一下 Bob 现在是否可以访问 pod。


kubectl get pods --namespace engineering --as bobNAME    READY   STATUS    RESTARTS   AGEmyapp   1/1     Running   0          11m
复制代码


既然他现在已经关联了 eng-reader 角色,那么他就获得了 pod 列表的权限。


此时,Bob 在集群中的访问权限依旧十分有限。他所能做的只是在 engineering 命名空间中列出 pod。这对 Bob 帮助不大。他想要检查集群中的节点数量,但是令他失望的是,他遇到了 forbidden error。


kubectl get nodes --as bobError from server (Forbidden): nodes is forbidden: User "bob" cannot list resource "nodes" in API group "" at the cluster scope
复制代码


在 Kubernetes 中角色和角色绑定既可以应用在命名空间层面也可以应用在集群层面。我们现在创建一个集群角色以及一个与 Bob 关联的角色绑定,以使他能够列出节点。


kind: ClusterRoleapiVersion: rbac.authorization.k8s.io/v1metadata:  # "namespace" omitted since ClusterRoles are not namespaced  name: cluster-node-readerrules:- apiGroups: [""]  resources: ["nodes"]  verbs: ["get", "watch", "list"]
复制代码


kubectl create -f cluster-role.yamlclusterrole.rbac.authorization.k8s.io/cluster-node-reader created
复制代码


kubectl get clusterroles cluster-node-readerNAME                  AGEcluster-node-reader   49s
复制代码


kind: ClusterRoleBindingapiVersion: rbac.authorization.k8s.io/v1metadata:  name: read-cluster-nodessubjects:- kind: User  name: bob # Name is case sensitive  apiGroup: rbac.authorization.k8s.ioroleRef:  kind: ClusterRole  name: cluster-node-reader  apiGroup: rbac.authorization.k8s.io
复制代码


kubectl create -f cluster-role-binding.yamlclusterrolebinding.rbac.authorization.k8s.io/read-cluster-nodes created
复制代码


kubectl get clusterrolebindings read-cluster-nodesNAME                 AGEread-cluster-nodes   35s
复制代码


现在,Bob 已经设置为可以在集群中列出节点。


kubectl get nodes --as bobNAME       STATUS   ROLES    AGE   VERSIONminikube   Ready    master   52m   v1.15.2
复制代码


本篇教程的目的是为了帮助你理解角色以及角色绑定如何在 Kubernetes 中工作的。在本系列下一篇文章中,我们将来看看 service account,保持关注哟~


2020-05-16 17:181067

评论

发布
暂无评论
发现更多内容

深入理解HTTPS协议

android 程序员 移动开发

渣渣二本的辛酸面试之路:从深圳外包到杭州蚂蚁金服,4年小Android的爬坑历程

android 程序员 移动开发

疫情结束后,会影响程序员年后找工作吗?

android 程序员 移动开发

原来一个 Map 就能搞定注册表了

悟空聊架构

Eureka 源码剖析 注册中心 悟空聊架构 11月日更

漫谈MVVM(1)ViewModel_DataBinding核心原理

android 程序员 移动开发

牛掰!阿里P7大佬爆肝半个月,把安卓源码解析编成了508页的PDF

android 程序员 移动开发

深度探索 Gradle 自动化构建技术(四、自定义 Gradle 插件)

android 程序员 移动开发

深入解析Flutter架构

android 程序员 移动开发

【设计模式】第十三篇 - 享元模式 - 连连看的图片共享

Brave

设计模式 享元模式 11月日更

混合开发框架最全对比,为什么我更推荐Flutter?

android 程序员 移动开发

漫谈MVVM(1)ViewModel_DataBinding核心原理(2)

android 程序员 移动开发

玩转AppBarLayout,更酷炫的顶部栏(1)

android 程序员 移动开发

用MVP模式构建Android代码

android 程序员 移动开发

深入浅出协程、线程和并发问题

android 程序员 移动开发

滴滴国际化项目 Android 端演进

android 程序员 移动开发

疫情下中年IT的焦虑

android 程序员 移动开发

疫情下,中年IT的焦虑

android 程序员 移动开发

深度探索 Gradle 自动化构建技术(二、Groovy 筑基篇)

android 程序员 移动开发

源码解析,Glide加载GIF图的原理竟然这么简单

android 程序员 移动开发

EMQ 获评“最具潜力边缘计算企业”,推动边缘计算生态发展

EMQ映云科技

物联网 IoT mqtt

灵魂拷问:Android开发初期之后怎么提升?怎么才能叫精通?方向在哪

android 程序员 移动开发

疫情被裁3个月,看我如何拿下腾讯offer(附面经+面试心得

android 程序员 移动开发

疫情过后,想找工作的你还不看这份资料就晚了!!史上最强总结

android 程序员 移动开发

深入学习-Gradle-自动化构建技术(二)Groovy-筑基

android 程序员 移动开发

漫谈MVVM(1)ViewModel_DataBinding核心原理(1)

android 程序员 移动开发

深度思考:已经开发8年的你,为何跳槽被多家大厂拒绝?为什么会迷茫Android开发还有什么能学习的

android 程序员 移动开发

玩转AppBarLayout,更酷炫的顶部栏

android 程序员 移动开发

炸裂!一次Android实习经历告诉你:老爸不是张一鸣,该用什么技巧进字节

android 程序员 移动开发

温故而知新:重新认识Activity的生命周期

android 程序员 移动开发

炒冷饭之Https 建立链接

android 程序员 移动开发

深入理解JobScheduler与JobService的使用

android 程序员 移动开发

Kubernetes身份认证和授权操作全攻略:上手操作Kubernetes授权_文化 & 方法_Rancher_InfoQ精选文章