mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
368 字
1 分钟
Kubernetes 安全与 RBAC
2023-12-25

一、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/v1
kind: Role
metadata:
namespace: default
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["pods/log"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: default
subjects:
- kind: ServiceAccount
name: default
namespace: default
- kind: User
name: alice
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io

1.3 ClusterRole 与 ClusterRoleBinding#

# ClusterRole - 集群级别权限
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: node-reader
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: node-reader-binding
subjects:
- kind: Group
name: system:nodes
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: node-reader
apiGroup: rbac.authorization.k8s.io

1.4 常用 RBAC 操作#

# 查看 Role
kubectl get roles -n namespace
kubectl describe role role-name -n namespace
# 查看 RoleBinding
kubectl get rolebindings -n namespace
kubectl describe rolebinding binding-name -n namespace
# 测试权限
kubectl auth can-i get pods --as=alice
kubectl auth can-i create pods --as=system:serviceaccount:default:default
# 编辑 RBAC
kubectl edit role pod-reader -n namespace

1.5 内置角色#

角色说明
view只读访问命名空间资源
edit大部分资源读写,不能修改 RBAC
admin命名空间级别的管理权限
cluster-admin集群完全管理权限

二、Security Context#

2.1 Pod 级别 Security Context#

apiVersion: v1
kind: Pod
spec:
securityContext:
runAsNonRoot: true # 必须以非 root 运行
runAsUser: 10000 # 指定用户 ID
runAsGroup: 10000 # 指定组 ID
fsGroup: 10000 # 文件系统组
seccompProfile:
type: RuntimeDefault # 使用默认 seccomp 配置
containers:
- name: app
image: app:latest

2.2 Container 级别 Security Context#

apiVersion: v1
kind: Pod
spec:
containers:
- name: app
image: app:latest
securityContext:
allowPrivilegeEscalation: false # 禁止特权提升
readOnlyRootFilesystem: true # 只读根文件系统
capabilities:
drop: # 删除能力
- ALL
add: # 添加能力
- NET_ADMIN

2.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/v1beta1
kind: PodSecurityPolicy
metadata:
name: baseline
spec:
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/v1
kind: NetworkPolicy
metadata:
name: default-deny-ingress
spec:
podSelector: {}
policyTypes:
- Ingress

4.2 命名空间级别策略#

# 允许命名空间内流量
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-namespace
spec:
podSelector: {}
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
name: production

4.3 应用网络策略#

# 只允许前端访问后端
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: backend-policy
spec:
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/tlsTLS 证书
kubernetes.io/dockerconfigjsonDocker 配置
kubernetes.io/service-account-tokenServiceAccount 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.env

5.3 Secret 使用方式#

# 环境变量方式(易被日志暴露)
apiVersion: v1
kind: Pod
spec:
containers:
- name: app
image: app:latest
env:
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-password
key: password
---
apiVersion: v1
kind: Pod
spec:
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 ServiceAccount
kubectl get serviceaccount -n default
kubectl describe serviceaccount default -n default

6.2 自定义 ServiceAccount#

apiVersion: v1
kind: ServiceAccount
metadata:
name: my-app-sa
namespace: production
---
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
serviceAccountName: my-app-sa
containers:
- name: app
image: app:latest

6.3 RBAC 配合 ServiceAccount#

# 限制 Pod 权限
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: my-app-role
rules:
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: my-app-role-binding
subjects:
- kind: ServiceAccount
name: my-app-sa
namespace: production
roleRef:
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/v1
kind: Policy
rules:
# 不记录只读请求到某些路径
- 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/
作者
Souloss
发布于
2023-12-25
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时