HTTP Token 认证:通过一个 Token 来识别合法用户 HTTP Token 的认证是用一个很长的特殊编码方式的并且难以被模仿的字符串 - Token 来表达客户的一 种方式。Token 是一个很长的很复杂的字符串,每一个 Token 对应一个用户名存储在 API Server 能访 问的文件中。当客户端发起 API 调用请求时,需要在 HTTP Header 里放入 Token
HTTP Base 认证:通过 用户名+密码 的方式认证 用户名+:+密码 用 BASE64 算法进行编码后的字符串放在 HTTP Request 中的 Heather Authorization 域里发送给服务端,服务端收到后进行编码,获取用户名及密码 最严格的 HTTPS 证书认证:基于 CA 根证书签名的客户端身份认证方式
Controller Manager、Scheduler 与 API Server 在同一台机器,所以直接使用 API Server 的非安全端口访问
kubectl、kubelet、kube-proxy 访问 API Server 就都需要证书进行 HTTPS 双向认证
因Pod是动态调度的,所以Pod访问通过ServiceAccount进行。
默认情况下,每个 namespace 都会有一个 ServiceAccount,如果 Pod 在创建时没有指定 ServiceAccount, 就会使用 Pod 所属的 namespace 的 ServiceAccount
Pod使用的serviceaccount认证
例:给prometheus容器添加rbac权限
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata:name: prometheusrules:- apiGroups: [""]resources:- nodes- nodes/proxy- services- endpoints- podsverbs: ["get", "list", "watch"] ##选择权限范围- apiGroups:- extensionsresources:- ingressesverbs: ["get", "list", "watch"]- nonResourceURLs: ["/metrics"]verbs: ["get"]---apiVersion: v1kind: ServiceAccountmetadata:name: prometheusnamespace: kube-system---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: prometheusroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: prometheussubjects:- kind: ServiceAccount ##subjects选择ServiceAccountname: prometheusnamespace: kube-system
用户使用User或Group认证
例:添加新用户
https://zhuanlan.zhihu.com/p/43237959
https://blog.csdn.net/cbmljs/article/details/102953428
https://www.cnblogs.com/Tempted/p/13457595.html
查看pod的serviceaccount信息
kubectl get pods my-nginx-649dc4bbdd-6blgh -o yaml —export
查看pod的安全文件,默认目录如下:
/run/secrets/kubernetes.io/serviceaccount
不单独指定的话这三个目录为默认,可在describe中查看![XN0SN%8Q3{%X2W0`YZI[]HS.png](/uploads/projects/mingyi-10t0e@mwo5qi/867e7c38a1c62e06a7f1d12c9c423388.png)
鉴权是确定请求方有哪些资源的权 限。
API Server 目前支持以下几种授权策略 (通过 API Server 的启动参数 “—authorization-mode” 设置) AlwaysDeny:表示拒绝所有的请求,一般用于测试
AlwaysAllow:允许接收所有请求,如果集群不需要授权流程,则可以采用该策略
ABAC(Attribute-Based Access Control):基于属性的访问控制,表示使用用户配置的授权规则对用户 请求进行匹配和控制
Webbook:通过调用外部 REST 服务对用户进行授权
RBAC(Role-Based Access Control):基于角色的访问控制,现行默认规则

Kubenetes 并不会提供用户管理,那么 User、Group、ServiceAccount 指定的用户又是从哪里 来的呢? Kubenetes 组件(kubectl、kube-proxy)或是其他自定义的用户在向 CA 申请证书时,需要提供一个 证书请求文件.
{
“CN”: “admin”,
“hosts”: [],
“key”: {
“algo”: “rsa”,
“size”: 2048
},
“names”: [
{
“C”: “CN”,
“ST”: “HangZhou”,
“L”: “XS”,
“O”: “system:masters”,
“OU”: “System”
}
]
}
API Server会把客户端证书的 CN 字段作为User,把 names.O 字段作为Group.
Role and ClusterRole
在 RBAC API 中,Role 表示一组规则权限,;Role 可以定义在一个 namespace 中,如果想要跨 namespace 则可以创建 ClusterRole.
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [“”] # “” indicates the core API group
resources: [“pods”]
verbs: [“get”, “watch”, “list”]
ClusterRole 具有与 Role 相同的权限角色控制能力,不同的是 ClusterRole 是集群级别的,ClusterRole 可以用于集群级别的资源控制( 例如 node 访问权限 )
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
# “namespace” omitted since ClusterRoles are not namespaced
name: secret-reader
rules:
- apiGroups: [“”]
resources: [“secrets”]
verbs: [“get”, “watch”, “list”]
RoloBinding 可以将角色中定义的权限授予用户或用户组,RoleBinding 包含一组权限列表(subjects),权限列 表中包含有不同形式的待授予权限资源类型(users, groups, or service accounts);RoloBinding 同样包含对被 Bind 的 Role 引用;
RoleBinding 适用于某个命名空间内授权,而 ClusterRoleBinding 适用于集群范围内的授 权 将 default 命名空间的 pod-reader Role 授予 jane 用户,此后 jane 用户在 default 命名空间中将具有 podreader 的权限
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: read-pods
namespace: default
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
使用 ClusterRoleBinding 可以对整个集群中的所有命名空间资源权限进行授权.
如果RoleBinding 引用了一个 ClusterRole,这个 ClusterRole 具有整个集群内对XX的访问权限; 但是其授权用户user只能访问 namespace空间中的 XX.
Resources
Kubernetes 集群内一些资源一般以其名称字符串来表示,这些字符串一般会在 API 的 URL 地址中出现;同时某些 资源也会包含子资源,例如 logs 资源就属于 pods 的子资源
GET /api/v1/namespaces/{namespace}/pods/{name}/log
例:
resources: [“pods/log”]
Subjects
RoleBinding 和 ClusterRoleBinding 可以将 Role 绑定到 Subjects;Subjects 可以是 groups、users 或者 service accounts
https://blog.csdn.net/weixin_34102807/article/details/92184312
