Pod安全 #
一、Pod安全概述 #
Pod安全涉及容器运行时的安全配置,包括用户权限、文件系统、能力控制等。
1.1 安全层次 #
text
Pod安全层次
│
├── Pod级别
│ └── securityContext
│
├── 容器级别
│ └── securityContext
│
└── 集群级别
└── PodSecurityPolicy/PodSecurityStandards
1.2 安全配置项 #
| 配置项 | 说明 |
|---|---|
| runAsUser | 运行用户 |
| runAsGroup | 运行组 |
| fsGroup | 文件系统组 |
| allowPrivilegeEscalation | 权限提升 |
| readOnlyRootFilesystem | 只读根文件系统 |
| capabilities | Linux能力 |
| privileged | 特权模式 |
二、安全上下文 #
2.1 Pod级别安全上下文 #
yaml
apiVersion: v1
kind: Pod
metadata:
name: security-context-pod
spec:
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
containers:
- name: app
image: nginx
2.2 容器级别安全上下文 #
yaml
apiVersion: v1
kind: Pod
metadata:
name: container-security
spec:
containers:
- name: app
image: nginx
securityContext:
runAsUser: 1000
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
2.3 配置优先级 #
text
优先级
│
├── 容器级别配置优先
│
└── Pod级别作为默认值
三、用户和组配置 #
3.1 指定运行用户 #
yaml
apiVersion: v1
kind: Pod
metadata:
name: user-pod
spec:
securityContext:
runAsUser: 1000
runAsGroup: 3000
containers:
- name: app
image: nginx
command: ["sh", "-c", "id && sleep 3600"]
3.2 文件系统组 #
yaml
apiVersion: v1
kind: Pod
metadata:
name: fsgroup-pod
spec:
securityContext:
fsGroup: 2000
containers:
- name: app
image: nginx
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
emptyDir: {}
3.3 阻止root用户 #
yaml
apiVersion: v1
kind: Pod
metadata:
name: non-root-pod
spec:
securityContext:
runAsNonRoot: true
containers:
- name: app
image: nginx
四、文件系统安全 #
4.1 只读根文件系统 #
yaml
apiVersion: v1
kind: Pod
metadata:
name: readonly-pod
spec:
containers:
- name: app
image: nginx
securityContext:
readOnlyRootFilesystem: true
volumeMounts:
- name: cache
mountPath: /var/cache/nginx
- name: run
mountPath: /var/run
volumes:
- name: cache
emptyDir: {}
- name: run
emptyDir: {}
4.2 权限提升控制 #
yaml
apiVersion: v1
kind: Pod
metadata:
name: no-escalation-pod
spec:
containers:
- name: app
image: nginx
securityContext:
allowPrivilegeEscalation: false
五、Linux能力 #
5.1 能力概述 #
text
Linux能力
│
├── 默认能力
│ └── 容器默认拥有部分能力
│
├── 添加能力
│ └── add字段
│
└── 删除能力
└── drop字段
5.2 添加能力 #
yaml
apiVersion: v1
kind: Pod
metadata:
name: add-capabilities
spec:
containers:
- name: app
image: nginx
securityContext:
capabilities:
add:
- NET_ADMIN
- SYS_TIME
5.3 删除能力 #
yaml
apiVersion: v1
kind: Pod
metadata:
name: drop-capabilities
spec:
containers:
- name: app
image: nginx
securityContext:
capabilities:
drop:
- ALL
5.4 常用能力 #
| 能力 | 说明 |
|---|---|
| NET_ADMIN | 网络管理 |
| SYS_TIME | 系统时间 |
| CHOWN | 文件所有者修改 |
| DAC_OVERRIDE | 文件权限绕过 |
| FOWNER | 文件所有者检查绕过 |
| KILL | 发送信号 |
| SETUID | 设置用户ID |
| SETGID | 设置组ID |
六、特权模式 #
6.1 特权容器 #
yaml
apiVersion: v1
kind: Pod
metadata:
name: privileged-pod
spec:
containers:
- name: app
image: nginx
securityContext:
privileged: true
6.2 特权模式风险 #
text
特权模式风险
│
├── 访问所有设备
│
├── 修改内核参数
│
├── 操作网络栈
│
└── 绕过安全限制
6.3 替代方案 #
yaml
# 使用特定能力代替特权模式
securityContext:
capabilities:
add:
- NET_ADMIN
- SYS_ADMIN
七、Pod安全标准 #
7.1 安全级别 #
| 级别 | 说明 |
|---|---|
| Privileged | 不限制 |
| Baseline | 基本限制 |
| Restricted | 严格限制 |
7.2 Restricted示例 #
yaml
apiVersion: v1
kind: Pod
metadata:
name: restricted-pod
labels:
pod-security.kubernetes.io/enforce: restricted
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 2000
seccompProfile:
type: RuntimeDefault
containers:
- name: app
image: nginx
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
7.3 命名空间级别配置 #
yaml
apiVersion: v1
kind: Namespace
metadata:
name: my-namespace
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/enforce-version: latest
八、实际应用示例 #
8.1 Web应用安全配置 #
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
securityContext:
runAsNonRoot: true
runAsUser: 1000
fsGroup: 2000
containers:
- name: web
image: nginx:1.25
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
volumeMounts:
- name: cache
mountPath: /var/cache/nginx
- name: run
mountPath: /var/run
volumes:
- name: cache
emptyDir: {}
- name: run
emptyDir: {}
8.2 数据库安全配置 #
yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: mysql
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
securityContext:
runAsUser: 999
runAsGroup: 999
fsGroup: 999
containers:
- name: mysql
image: mysql:8.0
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
volumeMounts:
- name: data
mountPath: /var/lib/mysql
- name: tmp
mountPath: /tmp
- name: run
mountPath: /var/run/mysqld
volumes:
- name: tmp
emptyDir: {}
- name: run
emptyDir: {}
九、故障排查 #
9.1 常见问题 #
bash
# 查看Pod状态
kubectl describe pod <pod-name>
# 查看容器日志
kubectl logs <pod-name>
# 进入容器检查
kubectl exec -it <pod-name> -- id
kubectl exec -it <pod-name> -- ls -la /
9.2 问题诊断 #
| 问题 | 原因 | 解决方案 |
|---|---|---|
| 权限拒绝 | 用户权限不足 | 检查runAsUser配置 |
| 文件只读 | 只读文件系统 | 挂载可写目录 |
| 能力不足 | 能力被删除 | 添加必要能力 |
十、最佳实践 #
10.1 安全配置清单 #
yaml
securityContext:
runAsNonRoot: true
runAsUser: 1000
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop:
- ALL
seccompProfile:
type: RuntimeDefault
10.2 安全建议 #
text
安全建议
│
├── 避免使用root用户
│
├── 使用只读文件系统
│
├── 删除不必要的Linux能力
│
├── 禁止权限提升
│
└── 启用seccomp配置
十一、总结 #
11.1 核心要点 #
| 配置 | 说明 |
|---|---|
| runAsUser | 运行用户 |
| readOnlyRootFilesystem | 只读文件系统 |
| capabilities | Linux能力 |
| privileged | 特权模式 |
11.2 下一步 #
掌握了Pod安全后,让我们学习 网络隔离,了解网络安全策略配置。
最后更新:2026-03-28