1. 首页 > Kubernetes教程 > 正文

Kubernetes教程FG058-Kubernetes RBAC与授权实战解析

目录大纲

Part01-基础概念与理论知识

1.1 RBAC概念

RBAC(基于角色的访问控制)是Kubernetes中用于控制资源访问权限的授权模式。它通过定义角色(Role)和角色绑定(RoleBinding)来实现对资源的精细访问控制。

1.2 RBAC核心组件

  • Role:在命名空间级别定义的权限集合
  • ClusterRole:在集群级别定义的权限集合
  • RoleBinding:将Role绑定到用户、组或服务账户
  • ClusterRoleBinding:将ClusterRole绑定到用户、组或服务账户
  • ServiceAccount:Kubernetes中的服务账户,用于Pod访问API

1.3 授权模式

Kubernetes支持多种授权模式:

  • Node:用于节点访问API
  • RBAC:基于角色的访问控制
  • ABAC:基于属性的访问控制
  • Webhook:通过外部服务进行授权
  • AlwaysAllow:始终允许所有请求
  • AlwaysDeny:始终拒绝所有请求

1.4 RBAC工作原理

RBAC通过以下步骤工作:

  • 定义Role或ClusterRole,指定允许的操作和资源
  • 创建RoleBinding或ClusterRoleBinding,将角色绑定到用户、组或服务账户
  • 当用户或服务账户请求访问资源时,Kubernetes API服务器检查RBAC规则,决定是否允许访问

Part02-生产环境规划与建议

2.1 RBAC应用场景

RBAC适用于以下场景:

  • 多租户环境:隔离不同租户的访问权限
  • 生产环境:限制用户和服务账户的权限,提高安全性
  • 合规要求:满足PCI DSS、HIPAA等合规要求
  • ,风哥提示:。

  • 最小权限原则:只授予必要的权限

2.2 权限规划

在规划RBAC权限时,需要考虑以下因素,风哥提示:。

  • 角色设计:根据用户职责设计不同的角色
  • 权限范围:合理设置权限的范围,避免过度授权
  • 命名空间隔离:使用命名空间隔离不同应用的权限
  • 服务账户权限:为服务账户设置最小必要的权限

2.3 资源规划

在规划RBAC资源时,需要考虑以下因素。。

  • 角色层次:设计合理的角色层次结构
  • 权限继承:利用ClusterRole和RoleBinding实现权限继承
  • 审计日志:开启审计日志,记录权限使用情况
  • 定期审查:定期审查RBAC规则,确保其有效性

Part03-生产环境项目实施方案

3.1 创建Role和RoleBinding

3.1.1 创建Role

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: pod-reader
  namespace: default
spec:
  rules:
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "watch", "list"]

3.1.2 创建RoleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods
  namespace: default
spec:
  subjects:
  - kind: User
    name: fgedu
    apiGroup: rbac.authorization.k8s.io
  roleRef:
    kind: Role
    name: pod-reader,学习交流加群风哥微信: itpux-com。
    apiGroup: rbac.authorization.k8s.io

3.2 创建ClusterRole和ClusterRoleBinding

3.2.1 创建ClusterRole

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: cluster-pod-reader
spec:
  rules:
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "watch", "list"]

3.2.2 创建ClusterRoleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cluster-read-pods
spec:
  subjects:
  - kind: User
    name: fgedu
    apiGroup: rbac.authorization.k8s.io
  roleRef:
    kind: ClusterRole
    name: cluster-pod-reader
    apiGroup: rbac.authorization.k8s.io

3.3 创建ServiceAccount

3.3.1 创建ServiceAccount

apiVersion: v1
kind: ServiceAccount
metadata:
  name: fgedu-sa
  namespace: default

3.3.2 绑定Role到ServiceAccount

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: sa-read-pods
  namespace: default
spec:
  subjects:
  - kind: ServiceAccount
    name: fgedu-sa,学习交流加群风哥QQ113257174。
    namespace: default
  roleRef:
    kind: Role
    name: pod-reader
    apiGroup: rbac.authorization.k8s.io

Part04-生产案例与实战讲解

4.1 实战案例:创建管理员角色

4.1.1 创建管理员ClusterRole

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: cluster-admin-custom
spec:
  rules:
  - apiGroups: [""]
    resources: ["*"]
    verbs: ["*"]
  - apiGroups: ["apps"]
    resources: ["*"]
    verbs: ["*"]
  - apiGroups: ["rbac.authorization.k8s.io"]
    resources: ["*"]
    verbs: ["*"]

4.1.2 创建管理员ClusterRoleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-binding
spec:
  subjects:
  - kind: User
    name: admin
    apiGroup: rbac.authorization.k8s.io
  roleRef:
    kind: ClusterRole
    name: cluster-admin-custom
    apiGroup: rbac.authorization.k8s.io

4.2 实战案例:创建开发者角色

4.2.1 创建开发者Role

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: developer,更多视频教程www.fgedu.net.cn。
  namespace: default
spec:
  rules:
  - apiGroups: [""]
    resources: ["pods", "services", "configmaps", "secrets"]
    verbs: ["get", "watch", "list", "create", "update", "delete"]
  - apiGroups: ["apps"]
    resources: ["deployments", "replicasets", "statefulsets"]
    verbs: ["get", "watch", "list", "create", "update", "delete"]

4.2.2 创建开发者RoleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: developer-binding
  namespace: default
spec:
  subjects:
  - kind: User
    name: developer
    apiGroup: rbac.authorization.k8s.io
  roleRef:
    kind: Role
    name: developer
    apiGroup: rbac.authorization.k8s.io

4.3 实战案例:创建只读角色

4.3.1 创建只读ClusterRole

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: view-only
spec:
  rules:
  - apiGroups: [""]
    resources: ["pods", "services", "configmaps", "secrets", "nodes"]
    verbs: ["get", "watch", "list"]
  - apiGroups: ["apps"]
    resources: ["deployments", "replicasets", "statefulsets"]
    verbs: ["get", "watch", "list"]

4.3.2 创建只读ClusterRoleBinding

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: view-only-binding
spec:
  subjects:
  - kind: User,更多学习教程公众号风哥教程itpux_com。
    name: viewer
    apiGroup: rbac.authorization.k8s.io
  roleRef:
    kind: ClusterRole
    name: view-only
    apiGroup: rbac.authorization.k8s.io

4.4 测试RBAC权限

4.4.1 测试管理员权限

# 使用admin用户创建Pod
kubectl --user=admin run test-pod --image=nginx
# 使用admin用户查看节点
kubectl --user=admin get nodes

执行 →

pod/test-pod created
NAME       STATUS   ROLES                  AGE   VERSION
master-1   Ready    control-plane,master   1d    v1.25.0
node-1     Ready    <none>                 1d    v1.25.0
node-2     Ready    <none>                 1d    v1.25.0

4.4.2 测试开发者权限

# 使用developer用户创建Pod
kubectl --user=developer run dev-pod --image=nginx
# 使用developer用户查看Pod
kubectl --user=developer get pods
# 使用developer用户尝试查看节点(应该失败)
kubectl --user=developer get nodes

执行 →

pod/dev-pod created
NAME       READY   STATUS    RESTARTS   AGE
dev-pod    1/1     Running   0          1m
test-pod   1/1     Running   0          5m
Error from server (Forbidden): nodes is forbidden: User "developer" cannot list resource "nodes" in API group "" at the cluster scope

4.4.3 测试只读权限

# 使用viewer用户查看Pod
kubectl --user=viewer get pods
# 使用viewer用户尝试创建Pod(应该失败)
kubectl --user=viewer run view-pod --image=nginx

执行 →

NAME       READY   STATUS    RESTARTS   AGE
dev-pod    1/1     Running   0          2m
test-pod   1/1     Running   0          6m
Error from server (Forbidden): pods is forbidden: User "viewer" cannot create resource "pods" in API group "" in the namespace "default"

。from K8S+DB视频:www.itpux.com。

Part05-风哥经验总结与分享

5.1 RBAC最佳实践

  • 最小权限原则:只授予必要的权限
  • 角色层次设计:设计合理的角色层次结构
  • 命名空间隔离:使用命名空间隔离不同应用的权限
  • 服务账户权限:为服务账户设置最小必要的权限
  • 定期审查:定期审查RBAC规则,确保其有效性

5.2 生产环境建议

  • 使用ClusterRole和RoleBinding:利用ClusterRole定义通用权限,使用RoleBinding在特定命名空间中应用
  • 开启审计日志:开启审计日志,记录权限使用情况
  • 使用工具管理RBAC:使用RBAC管理工具,如kubectl、kustomize等
  • 文档化:记录RBAC规则的设计和实现,便于维护
  • 版本控制:使用版本控制工具管理RBAC配置

5.3 常见问题与解决方案

  • 权限不足:检查Role或ClusterRole是否正确定义,RoleBinding或ClusterRoleBinding是否正确绑定
  • 权限过度:审查RBAC规则,移除不必要的权限
  • 服务账户权限:确保服务账户只拥有必要的权限
  • 命名空间隔离:确保不同命名空间的权限正确隔离
  • RBAC规则冲突:避免创建冲突的RBAC规则

5.4 性能优化建议

  • 减少Role和ClusterRole数量:合并相似的角色,减少角色数量
  • 优化RoleBinding:合理使用RoleBinding,避免过多的绑定
  • 使用ClusterRole:对于通用权限,使用ClusterRole减少重复定义
  • 定期清理:定期清理不再使用的RBAC资源
  • 监控性能:监控RBAC对API服务器性能的影响,及时调整

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息