k8s通过sa和自建角色实现权限精细化分配

文章目录

权限精细化分配---通过sa和自建角色实现权限精细化分配

1.新建sa

复制代码
kubectl create sa lishanbin -n planck

2.建立一个角色,并将该角色绑定到sa上

角色role-sa 具有的权限仅仅是namespace planck内的所有pod的查看权限,以及deployment的查看权限,无权删除修改这些资源

复制代码
[root@k8s-master ~]# cat sa-role-binding.yaml 
#k8s 1.22.10
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: role-sa
  namespace: planck                         #指定 Namespace
rules:                                      #权限分配
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "watch", "list"]
  - apiGroups: [""]
    resources: ["pods/log"]
    verbs: ["get","list","watch"]
  - apiGroups: [""]
    resources: ["pods/attach"]
    verbs: ["get","list","watch"]
  - apiGroups: [""]
    resources: ["pods/exec"]
    verbs: ["get","list","watch"]
  - apiGroups: [""]
    resources: ["pods/status"]
    verbs: ["get","list","watch"]
  - apiGroups: [""]
    resources: ["podtemplates"]
    verbs: ["get","list","watch"]
  - apiGroups: ["extensions", "apps"]
    resources: ["deployments","statefulsets"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["configmaps"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["replicationcontrollers"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["replicationcontrollers/status"]
    verbs: ["get"]
  - apiGroups: [""]
    resources: ["services"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["services/status"]
    verbs: ["get", "list", "watch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: rbac-role-binding
  namespace: planck                 #指定 Namespace
subjects:
  - kind: ServiceAccount
    name: lishanbin                 #指定 ServiceAccount
    namespace: planck              #指定 Namespace
roleRef:
  kind: Role
  name: role-sa
  apiGroup: rbac.authorization.k8s.io

3.授权namespace的权限,设置ClusterRole和ClusterRolebinding

为什么要授权是因为sa内的secrets里的token只有在dashboard内使用,而上面的角色和角色绑定都是dev这个namespace内的,这样绑定后,拿到token才可以登录到dashboard的首页,否则都无法选择namespace。

复制代码
cat rbac-cluster-role-binding.yaml 
#k8s 1.22.10
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: rbac-namespace-role
rules:
  - apiGroups: [""]                     #配置权限,配置其只用于 namespace 的 list 权限
    resources: ["namespaces"]
    verbs: ["list"]
  - apiGroups: [""]
    resources: ["namespaces/status"]
    verbs: ["get"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: rbac-default-role-binding
subjects:
  - kind: ServiceAccount
    name: lishanbin                     #配置为自定义的 ServiceAccount
    namespace: planck                  #指定为服务账户所在的 Namespace
roleRef:
  kind: ClusterRole
  name: rbac-namespace-role             #配置上面的 Role
  apiGroup: rbac.authorization.k8s.io



kubectl -n planck describe secret $(kubectl get secret -n planck | grep lishanbin | awk '{print $1}')

kubernetes的dashboard提供Token和kubeconfig两种认证方式,因此上面拿到token以后可以通过token进行访问planck这个ns下的资源了。

相关推荐
间彧3 分钟前
Kubernetes声明式API相比传统命令式API在故障恢复场景下的具体优势有哪些?
kubernetes·github
间彧8 分钟前
为什么说Kubernetes的API设计是其成功的关键因素之一?
kubernetes
听风吟丶24 分钟前
MyBatis 深度实战:从基础映射到企业级性能优化
java·tomcat
间彧29 分钟前
Kubernetes Deployment 配置简化实战:从复杂到高效
kubernetes
仟濹1 小时前
【Java 基础】面向对象 - 继承
java·开发语言
6***83051 小时前
微服务搭建----springboot接入Nacos2.x
java
likuolei2 小时前
XML 元素 vs. 属性
xml·java·开发语言
自不量力的A同学2 小时前
Spring Boot 4.0.0 正式发布
java·spring boot·后端
d***29242 小时前
【spring】Spring事件监听器ApplicationListener的使用与源码分析
java·后端·spring
5***b972 小时前
解决报错net.sf.jsqlparser.statement.select.SelectBody
java