368 字
1 分钟
Kubernetes 安全与 RBAC
一、RBAC 权限模型
1.1 核心概念
| 概念 | 说明 |
|---|---|
| Subject | 被授权的主体(User、Group、ServiceAccount) |
| Verb | 操作动作(get、list、create、delete) |
| Resource | 资源类型(pods、services、deployments) |
| Role/ClusterRole | 权限定义 |
| RoleBinding/ClusterRoleBinding | 权限绑定 |
1.2 Role 与 RoleBinding
# Role - 命名空间级别权限apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: namespace: default name: pod-readerrules: - apiGroups: [""] resources: ["pods"] verbs: ["get", "list", "watch"] - apiGroups: [""] resources: ["pods/log"] verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: read-pods namespace: defaultsubjects: - kind: ServiceAccount name: default namespace: default - kind: User name: alice apiGroup: rbac.authorization.k8s.ioroleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io1.3 ClusterRole 与 ClusterRoleBinding
# ClusterRole - 集群级别权限apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: node-readerrules: - apiGroups: [""] resources: ["nodes"] verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: node-reader-bindingsubjects: - kind: Group name: system:nodes apiGroup: rbac.authorization.k8s.ioroleRef: kind: ClusterRole name: node-reader apiGroup: rbac.authorization.k8s.io1.4 常用 RBAC 操作
# 查看 Rolekubectl get roles -n namespacekubectl describe role role-name -n namespace
# 查看 RoleBindingkubectl get rolebindings -n namespacekubectl describe rolebinding binding-name -n namespace
# 测试权限kubectl auth can-i get pods --as=alicekubectl auth can-i create pods --as=system:serviceaccount:default:default
# 编辑 RBACkubectl edit role pod-reader -n namespace1.5 内置角色
| 角色 | 说明 |
|---|---|
| view | 只读访问命名空间资源 |
| edit | 大部分资源读写,不能修改 RBAC |
| admin | 命名空间级别的管理权限 |
| cluster-admin | 集群完全管理权限 |
二、Security Context
2.1 Pod 级别 Security Context
apiVersion: v1kind: Podspec: securityContext: runAsNonRoot: true # 必须以非 root 运行 runAsUser: 10000 # 指定用户 ID runAsGroup: 10000 # 指定组 ID fsGroup: 10000 # 文件系统组 seccompProfile: type: RuntimeDefault # 使用默认 seccomp 配置 containers: - name: app image: app:latest2.2 Container 级别 Security Context
apiVersion: v1kind: Podspec: containers: - name: app image: app:latest securityContext: allowPrivilegeEscalation: false # 禁止特权提升 readOnlyRootFilesystem: true # 只读根文件系统 capabilities: drop: # 删除能力 - ALL add: # 添加能力 - NET_ADMIN2.3 常用 Linux 能力
| 能力 | 说明 |
|---|---|
| NET_ADMIN | 网络管理(修改路由、防火墙) |
| SYS_ADMIN | 系统管理(挂载文件系统) |
| SYS_MODULE | 加载内核模块 |
| DAC_READ_SEARCH | 绕过文件权限检查 |
| ALL | 所有能力(危险!) |
三、Pod Security Standards
3.1 安全策略级别
| 级别 | 说明 |
|---|---|
| privileged | 完全不受限制 |
| baseline | 最低安全要求 |
| restricted | 严格安全要求 |
3.2 PSP Baseline 要求
apiVersion: policy/v1beta1kind: PodSecurityPolicymetadata: name: baselinespec: privileged: false # 禁止特权容器 hostPID: false # 禁止使用宿主 PID hostIPC: false # 禁止使用宿主 IPC hostNetwork: false # 禁止使用宿主机网络 seLinux: rule: RunAsAny runAsUser: rule: MustRunAsNonRoot # 必须非 root fsGroup: rule: RunAsAny volumes: - "configMap" - "emptyDir" - "projected" - "secret" - "downwardAPI" - "persistentVolumeClaim"四、网络安全
4.1 默认网络策略
# 拒绝所有入站流量apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: default-deny-ingressspec: podSelector: {} policyTypes: - Ingress4.2 命名空间级别策略
# 允许命名空间内流量apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: allow-namespacespec: podSelector: {} policyTypes: - Ingress ingress: - from: - namespaceSelector: matchLabels: name: production4.3 应用网络策略
# 只允许前端访问后端apiVersion: networking.k8s.io/v1kind: NetworkPolicymetadata: name: backend-policyspec: podSelector: matchLabels: app: backend policyTypes: - Ingress - Egress ingress: - from: - podSelector: matchLabels: app: frontend ports: - protocol: TCP port: 8080 egress: - to: - podSelector: matchLabels: app: database ports: - protocol: TCP port: 5432 - to: - namespaceSelector: {} ports: - protocol: UDP port: 53五、Secret 管理
5.1 Secret 类型
| 类型 | 说明 |
|---|---|
| Opaque | 通用密钥值对 |
| kubernetes.io/tls | TLS 证书 |
| kubernetes.io/dockerconfigjson | Docker 配置 |
| kubernetes.io/service-account-token | ServiceAccount token |
5.2 创建 Secret
# 从文件创建kubectl create secret generic db-password \ --from-literal=password=secret123
# 从文件kubectl create secret generic tls-cert \ --from-file=tls.crt=cert.pem \ --from-file=tls.key=key.pem
# 从 .env 文件kubectl create secret generic env-secrets \ --from-env-file=secrets.env5.3 Secret 使用方式
# 环境变量方式(易被日志暴露)apiVersion: v1kind: Podspec: containers: - name: app image: app:latest env: - name: DB_PASSWORD valueFrom: secretKeyRef: name: db-password key: password
---
apiVersion: v1kind: Podspec: containers: - name: app image: app:latest volumeMounts: - name: secrets mountPath: /etc/secrets readOnly: true volumes: - name: secrets secret: secretName: db-password六、ServiceAccount
6.1 默认 ServiceAccount
# 每个命名空间有一个 default ServiceAccountkubectl get serviceaccount -n defaultkubectl describe serviceaccount default -n default6.2 自定义 ServiceAccount
apiVersion: v1kind: ServiceAccountmetadata: name: my-app-sa namespace: production---
apiVersion: v1kind: Podmetadata: name: my-appspec: serviceAccountName: my-app-sa containers: - name: app image: app:latest6.3 RBAC 配合 ServiceAccount
# 限制 Pod 权限apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: name: my-app-rolerules: - apiGroups: [""] resources: ["configmaps"] verbs: ["get"]
---apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: my-app-role-bindingsubjects: - kind: ServiceAccount name: my-app-sa namespace: productionroleRef: kind: Role name: my-app-role七、安全最佳实践
7.1 镜像安全
# 1. 使用私有镜像仓库imagePullSecrets: - name: my-registry-secret
# 2. 禁止使用 latest 标签# 3. 定期扫描镜像漏洞# 4. 使用可信基础镜像7.2 运行时安全
# 使用 Falco 监控容器行为# 使用 Tracee 追踪系统调用# 使用 Sysdig 检查容器活动7.3 审计日志
apiVersion: audit.k8s.io/v1kind: Policyrules: # 不记录只读请求到某些路径 - level: None users: ["system:kube-proxy"] verbs: ["watch"] resources: - group: "" resources: ["endpoints", "services"]
# 记录所有变更 - level: RequestResponse resources: - group: "" resources: ["pods"] verbs: ["create", "update", "delete"]支持与分享
如果这篇文章对你有帮助,欢迎支持作者或分享给更多人
Kubernetes 安全与 RBAC
https://blog.souloss.com/posts/interview/kubernetes-security-and-rbac/ 部分信息可能已经过时
相关文章 智能推荐
1
Python 函数与高级特性
面试 Python 函数高级特性——装饰器原理、生成器与迭代器协议、上下文管理器、偏函数与可调用对象。
2
Kubernetes 网络与存储
面试 Kubernetes 网络模型、Service 类型、Ingress、NetworkPolicy、存储卷与持久化存储。
3
Python 并发与异步编程
面试 Python 并发编程——GIL 机制、asyncio 异步编程、threading 多线程、multiprocessing 多进程、协程与生成器。
4
Python 基础语法与核心概念
面试 Python 基础语法核心概念——数据类型对比、is 与 == 区别、深拷贝与浅拷贝、命名空间与作用域。
5
Kubernetes 核心架构与组件
面试 Kubernetes 核心架构——Control Plane 组件、Node 组件、Pod 生命周期、Deployment 管理。






