Kubernetes的授权是基于插件形式的,其常用的授权插件有以下几种
1. Node(节点认证)
2. ABAC(基于属性的访问控制)
3. RBAC(基于角色的访问控制)
4. Webhook(基于http回调机制的访问控制)
如果通过rolebinding绑定role,只能对rolebingding所在的名称空间的资源有权限,上图user1这个用户绑定到role1上,只对role1这个名称空间的资源有权限,对其他名称空间资源没有权限,这个属于名称空间级别的授权。
通过配合NodeRestriction control准入控制插件来限制kubelet访问node,endpoint、pod、service以及secret、configmap、PV和PVC等相关的资源。
–authorization-mode=Node,RBAC –admission-control=…,NodeRestriction,…
这种模式的实现相对比较生硬,就是在master node保存一份policy文件,指定不同用户(或用户组)对不同资源的访问权限,当修改该文件后,需要重启apiserver,跟openstack 的ABAC类似。policy文件的格式如下:
# Alice can do anything to all resources:
{
"apiVersion": "abac.authorization.kubernetes.io/v1beta1",
"kind": "Policy",
"spec": {
"user": "alice",
"namespace": "*",
"resource": "*",
"apiGroup": "*"
}
}
# Kubelet can read any pods:
{
"apiVersion": "abac.authorization.kubernetes.io/v1beta1",
"kind": "Policy",
"spec": {
"user": "kubelet",
"namespace": "*",
"resource": "pods",
"readonly": true
}
}
# Kubelet can read and write events:
{
"apiVersion": "abac.authorization.kubernetes.io/v1beta1",
"kind": "Policy",
"spec": {
"user": "kubelet",
"namespace": "*",
"resource": "events"
}
}
–authorization-mode=ABAC –authorization-policy-file=SOME_FILENAME。
kubectl get roles --all-namespaces
kubectl get ClusterRoles
kubectl get rolebinding --all-namespaces
kubectl get clusterrolebinding
内容
[root@master1 test5]# kubectl get roles --all-namespaces
NAMESPACE NAME CREATED AT
kube-public kubeadm:bootstrap-signer-clusterinfo 2022-03-19T02:06:49Z
kube-public system:controller:bootstrap-signer 2022-03-19T02:06:47Z
kube-system extension-apiserver-authentication-reader 2022-03-19T02:06:47Z
kube-system kube-proxy 2022-03-19T02:06:49Z
kube-system kubeadm:kubelet-config-1.18 2022-03-19T02:06:47Z
kube-system kubeadm:nodes-kubeadm-config 2022-03-19T02:06:47Z
kube-system system::leader-locking-kube-controller-manager 2022-03-19T02:06:47Z
kube-system system::leader-locking-kube-scheduler 2022-03-19T02:06:47Z
kube-system system:controller:bootstrap-signer 2022-03-19T02:06:47Z
kube-system system:controller:cloud-provider 2022-03-19T02:06:47Z
kube-system system:controller:token-cleaner 2022-03-19T02:06:47Z
kubernetes-dashboard kubernetes-dashboard 2022-03-19T13:40:13Z
–authorization-webhook-config-file=SOME_FILENAME
–service-account-key-file=/etc/kubernetes/pki/apiserver-key.pem
# 获取所有名称空间的sa账号
kubectl get serviceaccount --all-namespaces
可以看到k8s集群为所有的namespace创建了一个默认的service account,利用命令describe会发现service account只是关联了一个secret作为token,也就是service-account-token。
kubectl describe serviceaccount/default -n kube-system
kubectl get secret default-token-7xqxg -o yaml -n kube-system
可以看到service-account-token的secret资源包含的数据有三部分:
ca.crt:
namespace:
token:
类似 OAuth2的认证方式,大致认证过程如下: