下内容均来自个人笔记并重新梳理,如有错误欢迎指正!
如果对您有帮助,烦请点赞、关注、转发!如果您有其他想要了解的,欢迎私信联系我~
基本介绍
在 Kubernetes 中,Security Context(安全上下文)是一种重要的安全机制。
Security Context 通过定义 Pod 或 Pod 内容器的权限来控制容器的运行方式,从而限制不必要的访问、增强容器的安全性。
Security Context 包括但不限于以下配置字段:
官方文档:https://kubernetes.io/docs/tasks/configure-pod-container/security-context/
API文档:https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.31/#securitycontext-v1-core
为 Pod 设置 Security Context
1、资源清单(示例)
apiVersion: v1
kind: Pod
metadata:
name: demo-security-context
spec:
containers:
- name: demo1
image: busybox
command: [ "sh", "-c", "sleep 1h" ]
containers:
- name: demo2
image: busybox
command: [ "sh", "-c", "sleep 1h" ]
volumeMounts:
- name: data
mountPath: /data/demo
volumes:
- name: data
emptyDir: {}
securityContext:
runAsUser: 1000
runAsGroup: 3000
fsGroup: 2000
supplementalGroups: [4000]
2、配置示例说明
为 Container 设置 Security Context
1、资源清单(示例)
apiVersion: v1
kind: Pod
metadata:
name: demo-security-context
spec:
containers:
- name: demo1
image: busybox
command: [ "sh", "-c", "sleep 1h" ]
securityContext:
privileged: true # 容器以特权模式运行
capabilities: # 添加或删除功能
add / drop:
- SYS_TIME / SYS_PTRACE / SYS_ADMIN
allowPrivilegeEscalation: false
containers:
- name: demo2
image: busybox
command: [ "sh", "-c", "sleep 1h" ]
volumeMounts:
- name: data
mountPath: /data/demo
volumes:
- name: data
emptyDir: {}
2、配置示例说明
书籍推荐
最后推荐一本笔者从 Docker 进阶到 Kubernetes 自学过程中,受益较深的书籍。笔者经常复读,并结合工作实践不断加深理解和体会,可谓常读常新。希望这本书可以帮助到更多对 Kubernetes 感兴趣或刚开始学习的读者。