kubernetes rbac 权限管理

文章目录

  • 访问控制概述
  • kubernetes 下的 rbac
  • ServiceAccount
  • K8s角色&角色绑定
    • 角色(Role和ClusterRole)
    • 角色绑定(RoleBinding和ClusterRoleBinding)

访问控制概述

访问控制是云原生中的一个重要组成部分,也是一个 Kubernetes 集群在多租户环境下必须要采取的一个基本的安全架构手段。

那么在概念上可以抽象的定义为谁在何种条件下可以对什么资源做什么操作。这里的资源就是在 Kubernetes 中我们熟知的:Pod、ConfigMaps、Deployment、Secrets 等等这样的资源模型。

kubernetes 支持的认证鉴权方式有几个,这里只讲 RBAC。

第一要素是 Subjects,也就是主体。可以是开发人员、集群管理员这样的自然人,也可以是系统组件进程,或者是 Pod 中的逻辑进程;
第二个要素是 API Resource,也就是请求对应的访问目标。在 Kubernetes 集群中也就是各类资源;
第三要素是 Verbs,对应为请求对象资源可以进行哪些操作,包括增删改查、list、get、watch 等。
这里举个例子,假设有个通过合法认证的用户 Bob,他请求 list 某个 namespace下的 Pods,改请求的鉴权语义记为:Can Bob list pods?其中 Bob 即为请求中的 Subject,list 为对应的请求动作 Action,而 pods 为对应的请求资源 Resource。

角色(Role和ClusterRole)

Role针对特定的命名空间,ClusterRole在整个集群范围内都生效。

Role示例如下:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: default
  name: pod-role
rules:
- apiGroups: [""] # "" indicates the core API group
  resources: ["pods"]
  verbs: ["get", "watch", "list"]

ClusterRole示例如下:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: pod-clusterrole
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "watch", "list"]

相关参数
1、Role、ClsuterRole Verbs可配置参数

“get”, “list”, “watch”, “create”, “update”, “patch”, “delete”, “exec”

2、Role、ClsuterRole Resource可配置参数

“services”, “endpoints”, “pods”,“secrets”,“configmaps”,“crontabs”,“deployments”,“jobs”,“nodes”,“rolebindings”,“clusterroles”,“daemonsets”,“replicasets”,“statefulsets”,“horizontalpodautoscalers”,“replicationcontrollers”,“cronjobs”

3、Role、ClsuterRole APIGroup可配置参数

“”,“apps”, “autoscaling”, “batch”

要确定资源对象API端点请求的动词,请查看HTTP动词以及请求是否对单个资源或资源集合进行操作,参考下表:

HTTP Verb

Request Verb

POST

create

GET,HEAD

get (for individual resources), list (for collections)

PUT

update

PATCH

patch

DELETE

delete (for individual resources), deletecollection (for collections)


角色绑定(RoleBinding和ClusterRoleBinding)

定义好了角色也就是一个权限的集合,然后创建了一个ServiceAccount也就是一个服务账号,然后将这两个东西绑定起来,就是授权的过程了。

kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: rb
  namespace: default
subjects:
- kind: ServiceAccount
  name: zhangsan
  namespace: default
roleRef:
  kind: Role
  name: pod-role
  apiGroup: rbac.authorization.k8s.io

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: crb
subjects:
- kind: ServiceAccount
  name: mark
  namespace: default
roleRef:
  kind: ClusterRole
  name: pod-clusterrole
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: mark
  namespace: default


未经允许不得转载:木盒主机 » kubernetes rbac 权限管理

赞 (0)

相关推荐

    暂无内容!