Kubernetes 权限管理


1. 概述

Kubernetes 中用户分登陆用户和 service account。登陆用户可通过 kubectl config 查看上下文,以及当前上下文:

[root@chunqiu ~ (Master)]# kubectl config view
apiVersion: v1
clusters:
- cluster:
    certificate-authority: /etc/kubernetes/ssl/ca.pem
    server: https://k8s-apiserver:8443
  name: bcmt-kubernetes
contexts:
- context:
    cluster: bcmt-kubernetes
    namespace: default
    user: kubectl
  name: kubectl-context
current-context: kubectl-context
kind: Config
preferences: {}
users:
- name: kubectl
  user:
    client-certificate: /etc/kubernetes/ssl/cluster-admin.pem
    client-key: /etc/kubernetes/ssl/cluster-admin-key.pem

可以在当前上下文中通过 --user 选项使用其它用户访问当前上下文。

serviceaccout 是默认命名空间就会创建,如果不显示指定 serviceaccount,kubernetes 会为 pod 配置默认 serviceaccount。

serviceaccount 是进程访问 APIServer 的方式,那进程有什么权限访问呢?这就涉及到鉴权了,kubernetes 有多种鉴权方式,RBAC 是常见的鉴权方式,RBAC 定义了进程及用户能够以什么方式访问 kubernetes 的哪些资源。RBAC 和 serviceaccount 之间的桥梁即是 rolebinding。

通过鉴权的访问请求最后会进入到准入控制插件的“筛查”,准入控制插件会配置 pod 的相关信息,如果缺省配置默认值,如果有查看是否准入等。

根据对 APIServer 不同层面的访问,APIServer 会提示不一样的报错,鉴权失败,权限不够等等。

psp 是准入控制的插件之一,配置了集群内 pod 的安全属性。

2. 环境实践

[root@chunqiu ~ (Master)]# kubectl describe clusterrolebindings.rbac.authorization.k8s.io all:psp:restricted
Name:         all:psp:restricted
Labels:       <none>
Annotations:  <none>
Role:
  Kind:  ClusterRole
  Name:  psp:restricted
Subjects:
  Kind   Name                    Namespace
  ----   ----                    ---------
  Group  system:serviceaccounts
  Group  system:authenticated

// 注意这里以组为单位,通过 APIServer 鉴权的 serviceaccount 和 kubectl user 将被添加到 group system:serviceaccounts 和 system:authenticated 中,使得组内的“用户”天然用上了 psp 的 security 属性。

[root@chunqiu ~ (Master)]# kubectl describe clusterroles.rbac.authorization.k8s.io psp:restricted
Name:         psp:restricted
Labels:       <none>
Annotations:  <none>
PolicyRule:
  Resources                       Non-Resource URLs  Resource Names  Verbs
  ---------                       -----------------  --------------  -----
  podsecuritypolicies.extensions  []                 [restricted]    [use]

[root@chunqiu ~ (Master)]# kubectl get psp
NAME                PRIV    CAPS                                                                                                                                                SELINUX    RUNASUSER          FSGROUP     SUPGROUP    READONLYROOTFS   VOLUMES
...
restricted          true    NET_ADMIN,SYS_RAWIO,SYS_NICE,SYS_RESOURCE,SYS_TIME,IPC_LOCK,NET_RAW,IPC_OWNER,NET_BIND_SERVICE,DAC_OVERRIDE,KILL,CHOWN,FOWNER,SETUID,SETGID,MKNOD   RunAsAny   RunAsAny           RunAsAny    RunAsAny    false            configMap,emptyDir,projected,secret,downwardAPI,persistentVolumeClaim,hostPath

[root@chunqiu ~ (Master)]# kubectl get clusterrolebindings.rbac.authorization.k8s.io | grep restricted
all:psp:restricted                                       ClusterRole/psp:restricted                                         152d
harbor-harbor:psp:restricted                             ClusterRole/psp:restricted                                         152d

[root@chunqiu ~ (Master)]# kubectl describe clusterrolebindings.rbac.authorization.k8s.io harbor-harbor:psp:restricted
Name:         harbor-harbor:psp:restricted
Labels:       <none>
Annotations:  <none>
Role:
  Kind:  ClusterRole
  Name:  psp:restricted
Subjects:
  Kind            Name                          Namespace
  ----            ----                          ---------
  ServiceAccount  harbor-harbor-serviceaccount  ncms

参考文章:

Controlling Access to the Kubernetes API
Cluster Administration

芝兰生于空谷,不以无人而不芳。
原文地址:https://www.cnblogs.com/xingzheanan/p/15205565.html