更多kubernetes文章:
k8s专栏目录
访问到权限分两部分
1.Authenticating认证
授权配置由kube-apiserver管理
这里使用的是 Static Password File 方式。
apiserver启动yaml里配置basic-auth-file即可,容器启动apiserver的话要注意这个文件需要是在容器内能访问到的。(可以通过挂载文件,或者在已经挂载的路径里增加配置文件)
basic-auth-file文件格式见官方文档
https://kubernetes.io/docs/admin/authentication/#static-password-file 注意password在第一个,之前没注意被卡了很久。
修改了文件以后需要重启apiserver才能生效。这里kubectl delete 删除pod有问题,使用docker命令查看apiserver容器其实没有重启。解决方案是使用docker kill杀掉容器。自动重启。
[root@tensorflow1 ~]# curl -u admin:admin "https://localhost:6443/api/v1/pods" -k
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "pods is forbidden: User \"system:anonymous\" cannot list pods at the cluster scope",
"reason": "Forbidden",
"details": {
"kind": "pods"
},
"code": 403
}
这个就是没有通过认证的报错
2.Authorization授权
k8s RBAC授权规则
简单来说就是 权限--角色--用户绑定
需要配置两个文件
一个是role/clusterRole,定义角色以及其权限
一个是roleBinding/clusterRoleBinding,定义用户和角色的关系
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "pods is forbidden: User \"admin\" cannot list pods at the cluster scope",
"reason": "Forbidden",
"details": {
"kind": "pods"
},
"code": 403
}
这个就是没有权限访问,需要配置RBAC
文件如下:
- 方案1:role + roleBinding
kind: Role
apiVer