美文网首页
Kubernetes YAML 详解之 RBAC(Role, C

Kubernetes YAML 详解之 RBAC(Role, C

作者: 河码匠 | 来源:发表于2023-02-21 09:15 被阅读0次

一、 角色和绑定方式

角色:

Role: 针对单个命名空间的权限控制
ClusterRole:针对集群的权限控制

绑定方式:

RoleBinding: 使用 RoleBindingUser 和权限(可以是 RoleClusterRole)绑定,用户只能操作 RoleBinding 所在的命名空间。

ClusterRoleBinding: 通过 ClusterRoleBindingUserClusterRole 进行绑定,User 有操作所有命名空间的权限

二、 关于准入控制器

官方文档
默认开启: LimitRangerResourceQuotaServiceAccount
开启方式: /etc/kubernetes/manifests/kube-apiserver.yaml

关于准入控制器限制资源参考 Kubernetes YAML 详解之资源限制(ResourceQuota、LimitRange)

apiVersion: v1
kind: Pod
metadata:
  ......
  name: kube-apiserver
  namespace: kube-system
spec:
  containers:
  - command:
    - kube-apiserver
    ......
    - --enable-admission-plugins=xxxx # 开启指定的准入插件,具体有什么参考官方文档
    ......

三、 用户 UserAccountServiceAccount

UserAccountkubectl 使用的用户,在 /root/.kube/config 中配置
ServiceAccount 是 k8s 的一个资源 kubectl explain sa

四、Role YAML

  • 查看 Role 相关 yaml 字段说明

kubectl explain Role

字段说明

字段 值类型 说明
apiVersion string rbac.authorization.k8s.io/v1
kind string Role
metadata Object 与 pod 相同\color{red}{需要}指定 namespace
rules []Object 规则,\color{blue}{查看 `Role.rules` 相关 yaml 字段说明}
  • 查看 Role.rules 相关 yaml 字段说明

kubectl explain Role.rules

字段说明

字段 值类型 说明
apiGroups []string 支持的 api 列表,使用 kubectl api-versions / 前面的就是 group
nonResourceURLs []string 不需要的资源,排除
resources []string 指定资源类型,如: pod,deployment
resourceNames []string 指定具体的资源名称(资源白名单),[] 表示都可以
* verbs []string 具体的权限。指定 ["*"] 就是所有权限,如: ["get", "list", "watch"]

五、ClusterRole YAML

  • 查看 ClusterRole 相关 yaml 字段说明

kubectl explain ClusterRole

字段说明

字段 值类型 说明
apiVersion string rbac.authorization.k8s.io/v1
kind string ClusterRole
metadata Object 与 pod 相同\color{red}{不需要}指定 namespace
aggregationRule Object
rules []Object 规则,与 Role 相同,\color{blue}{查看 `Role.rules` 相关 yaml 字段说明}

六、RoleBinding YAML

  • 查看 RoleBinding 相关 yaml 字段说明

kubectl explain RoleBinding

字段说明

字段 值类型 说明
apiVersion string rbac.authorization.k8s.io/v1
kind string RoleBinding
metadata Object 元数据,与 pod 相同\color{red}{需要}指定 namespace
* roleRef Object 定义引用的 Role

* apiGroup: 指定授权的 api 地址

* kind: 指定使用权限资源的类型。如:PodDeployment

* name: clusterRole.matadata.name 的值或 Role.matadata.name 的值
subjects []Object 定义使用权限者

apiGroup: 指定授权的 api 地址。默认 rbac.authorization.k8s.io

* kind: 指定使用权限资源的类型,三种类型:UserGroupServiceAccount

* name: 对应 RoleBinding.subjects.kind 的值

namespace: 命名空间,如果 kindUserGroupnamespace 为空

七、ClusterRoleBinding YAML

  • 查看 ClusterRoleBinding 相关 yaml 字段说明

kubectl explain ClusterRoleBinding

字段说明

字段 值类型 说明
apiVersion string rbac.authorization.k8s.io/v1
kind string RoleBinding
metadata Object 元数据,与 Role 相同
* roleRef Object 定义引用的 Role

* apiGroup: 指定授权的 api 地址

* kind: 指定使用权限资源的类型。如:PodDeployment

* name: clusterRole.matadata.name 的值
subjects []Object 定义使用权限者

apiGroup: 指定授权的 api 地址。默认 rbac.authorization.k8s.io

* kind: 指定使用权限资源的类型,三种类型:UserGroupServiceAccount

* name: 对应 ClusterRoleBinding.subjects.kind 的值

namespace: 命名空间,如果 kindUserGroupnamespace 为空

八、示例

ServiceAccount 通过 RoleBindingClusterRoleBinding 分别与 RoleClusterRole 进行绑定

  • 创建 namespace
apiVersion: v1
kind: Namespace
metadata:
  name: rbac-test-ns
root@master:~/k8s_test/rbac# kubectl get ns
NAME              STATUS   AGE
default           Active   7d4h
rbac-test-ns      Active   49m
  • 创建 ServiceAcount
apiVersion: v1
kind: ServiceAccount
metadata:
  namespace: rbac-test-ns  # 指定命名空间
  name: sa-test
root@master:~/k8s_test/rbac# kubectl get sa -n rbac-test-ns
NAME      SECRETS   AGE
default   0         49m
sa-test   0         12m
  • 创建 Role
apiVersion: rbac.authorization.k8s.io/v1
kind: Role                      # 定义 Role
metadata:
  namespace: rbac-test-ns       # 指定命名空间
  name: role-test
rules:
- apiGroups: [""]               # 指定 api 列表,如果 [""] 会根据 resources 的内容自动获取
  resources: ["pods"]           # 指定 pod 资源类型
  resourceNames: []             # 指定到具体的资源名称. [] 表示所有都可以
  verbs: ["get","watch","list"] # 给予的权限
root@master:~/k8s_test/rbac# kubectl get role -n rbac-test-ns
NAME        CREATED AT
role-test   2023-02-17T06:07:37Z
  • 创建 RoleBindingServiceAccountsa-testRolerole-test 绑定
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: role-binding-test
  labels:
    binding: role
  namespace: rbac-test-ns
subjects:
- kind: ServiceAccount      # 指定 ServiceAccount 用户类型
  name: sa-test             # ServiceAccount.metadata.name 值
  namespace: rbac-test-ns   # 指定命名空间,如果 kind 是 User 或 Group 不用 namespace 参数
  apiGroup: ""              # kind 是 ServiceAccount 时 ""
roleRef:
  kind: Role                # 引用 Role 类型的角色
  apiGroup: rbac.authorization.k8s.io
  name: role-test           # Role.metadata.name 值
root@master:~/k8s_test/rbac# kubectl get rolebinding -n rbac-test-ns
NAME                ROLE             AGE
role-binding-test   Role/role-test   2m17s
  • 创建 pod 测试,使用 ServiceAccountsa-test 账户
apiVersion: v1
kind: Pod
metadata:
  name: sa-test-pod
  namespace: rbac-test-ns          # 指定命名空间
  labels:
    app: nginx
spec:
  serviceAccountName: sa-test      # 使用指定 sa
  containers:
  - name: nginx
    ports:
    - containerPort: 80
    image: nginx
    imagePullPolicy: IfNotPresent
root@master:~/k8s_test/rbac# kubectl exec -it sa-test-pod -n rbac-test-ns -- /bin/bash

root@sa-test-pod:/# cd /var/run/secrets/kubernetes.io/serviceaccount/

root@sa-test-pod:/var/run/secrets/kubernetes.io/serviceaccount# ls
ca.crt  namespace  token

root@sa-test-pod:/var/run/secrets/kubernetes.io/serviceaccount# curl --cacert ./ca.crt  -H "Authorization: Bearer $(cat ./token)"  https://kubernetes.default/api/v1/namespaces/rbac-test-ns/pods
  • 创建 ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole               # 定义 ClusterRole
metadata:
  name: cluster-role-test
rules:
- apiGroups: [""]               # 指定 api 列表,如果 [""] 会根据 resources 的内容自动获取
  resources: ["deployment"]     # 指定 deployment 资源类型
  resourceNames: []             # 指定到具体的资源名称. [] 表示所有都可以
  verbs: ["get","watch","list"] # 给予的权限
root@master:~/k8s_test/rbac# kubectl get clusterrole -n rbac-test-ns | grep cluster-role-test
cluster-role-test                 2023-02-17T05:46:51Z
  • 创建 ClusterRolebindingServiceAccountsa-testClusterRolecluster-role-test 绑定
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cluster-role-binding-test
  labels:
    binding: cluster-role-test
subjects:
- kind: ServiceAccount              # 指定 ServiceAccount 用户类型
  name: sa-test                     # ServiceAccount.metadata.name 值
  namespace: rbac-test-ns           # 指定命名空间,如果 kind 是 User 或 Group 不用 namespace 参数
  apiGroup: ""                      # kind 是 ServiceAccount 时 ""
roleRef:
  kind: ClusterRole                 # 引用 ClusterRole 类型的角色
  apiGroup: rbac.authorization.k8s.io
  name: cluster-role-test 
root@master:~/k8s_test/rbac# kubectl get clusterrolebinding | grep cluster-role
cluster-role-binding-test                              ClusterRole/cluster-role-test                                                      2m54s
  • 重新创建 pod 测试
apiVersion: v1
kind: Pod
metadata:
  name: sa-test-pod
  namespace: rbac-test-ns          # 指定命名空间
  labels:
    app: nginx
spec:
  serviceAccountName: sa-test      # 使用指定 sa
  containers:
  - name: nginx
    ports:
    - containerPort: 80
    image: nginx
    imagePullPolicy: IfNotPresent
root@master:~/k8s_test/rbac# kubectl exec -it sa-test-pod -n rbac-test-ns -- /bin/bash

root@sa-test-pod:/# cd /var/run/secrets/kubernetes.io/serviceaccount/

root@sa-test-pod:/var/run/secrets/kubernetes.io/serviceaccount# ls
ca.crt  namespace  token

root@sa-test-pod:/var/run/secrets/kubernetes.io/serviceaccount# curl --cacert ./ca.crt  -H "Authorization: Bearer $(cat ./token)"  https://kubernetes.default/api/v1/namespaces/rbac-test-ns/pods

相关文章

网友评论

      本文标题:Kubernetes YAML 详解之 RBAC(Role, C

      本文链接:https://www.haomeiwen.com/subject/uvlpkdtx.html