57 lines
2.3 KiB
Markdown
57 lines
2.3 KiB
Markdown
---
|
||
title: "Pod Security Context"
|
||
type: concept
|
||
tags: [Kubernetes, Security, Container, Pod, RBAC]
|
||
last_updated: 2026-04-24
|
||
---
|
||
|
||
## Definition
|
||
Pod Security Context(Pod 安全上下文)是 Kubernetes 中定义 Pod 和容器级别安全设置的机制,通过 YAML 配置在 Pod Spec 中声明容器的运行权限和访问控制。
|
||
|
||
## Common Security Context Fields
|
||
|
||
### Container-Level Settings
|
||
```yaml
|
||
securityContext:
|
||
readOnlyRootFilesystem: true # 容器根文件系统设为只读
|
||
runAsNonRoot: true # 禁止以 root 用户运行
|
||
runAsUser: 1000 # 指定运行用户 UID
|
||
runAsGroup: 1000 # 指定运行用户组 GID
|
||
allowPrivilegeEscalation: false # 禁止权限提升
|
||
capabilities:
|
||
drop: ["ALL"] # 移除所有 Linux capabilities
|
||
```
|
||
|
||
### Pod-Level Settings
|
||
```yaml
|
||
securityContext:
|
||
hostNetwork: false # 不使用宿主机网络
|
||
hostIPC: false # 不使用宿主机 IPC
|
||
hostPID: false # 不使用宿主机 PID 命名空间
|
||
automountServiceAccountToken: false # 不自动挂载 ServiceAccount Token
|
||
```
|
||
|
||
## Key Concepts from CTP Topic 49
|
||
|
||
### readOnlyRootFilesystem: true
|
||
将容器根文件系统设为只读,防止攻击者在容器内创建或修改文件。Demo 演示:设置此标志后,容器内尝试 `touch /tmp/test` 会失败。
|
||
|
||
### automountServiceAccountToken: false
|
||
禁用 Kubernetes ServiceAccount Token 的自动挂载,防止容器自动获得对 Kubernetes API 的访问权限。如果容器应用需要访问 API,应显式创建带有精确权限的 ServiceAccount 并通过 RBAC 绑定。
|
||
|
||
### hostNetwork: false / hostPort
|
||
避免使用宿主机网络和宿主机端口:
|
||
- 防止端口冲突
|
||
- 维护网络隔离
|
||
- 减少容器逃逸攻击面
|
||
- 注意:在受限网络环境(如 Lab Landing Zone)中可能有例外需求(参见 [[ctp-topic-39-implementing-eks-in-the-aws-lab-landing-zone]])
|
||
|
||
## Relationship to Kubernetes RBAC
|
||
Pod Security Context 与 [[Kubernetes RBAC]] 配合使用:
|
||
- Security Context 控制容器的运行时权限
|
||
- RBAC 控制 ServiceAccount 对 Kubernetes API 的访问权限
|
||
- 两者共同实现最小权限原则
|
||
|
||
## Sources
|
||
- [[ctp-topic-49-container-lifecycle-hardening-standards]]
|