docker - 如何使用 SELinux 在 Kubernetes 中挂载 HostPath 卷
问题描述
我正在尝试将hostPath 卷挂载到 Kubernetes Pod 中。下面显示了一个hostPath
卷规范的示例,该示例取自文档。我正在部署到运行 RHEL 7 并启用 SELinux 的主机。
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /test-pd
name: test-volume
volumes:
- name: test-volume
hostPath:
# directory location on host
path: /data
# this field is optional
type: Directory
当我的 Pod 尝试读取从底层主机挂载的文件时,我收到“权限被拒绝”错误。当我运行setenforce 0
关闭 SELinux 时,错误消失了,我可以访问该文件。当我将目录绑定到 Docker 容器时,我得到了同样的错误。
该问题在此处进行了描述,并且在使用 Docker 时,可以通过使用z
或Z
绑定挂载标志来修复,在此处的 Docker 文档中进行了描述。
虽然我可以通过运行来解决问题
chcon -Rt svirt_sandbox_file_t /path/to/my/host/dir/to/mount
我认为这是一个令人讨厌的 hack,因为我需要在我的 Kubernetes 集群中的每个主机上执行此操作,并且还因为我在 YAML 规范中描述的 Kubernetes 部署并没有完整描述需要做什么才能获得我的 YAML 正确运行。关闭 SELinux 不是一种选择。
我可以看到 Kubernetes 在此处的文档中提到了 SELinux 安全上下文,但是如果没有出现权限被拒绝错误,我无法成功地将 hostPath 卷挂载到 pod 中。
YAML 需要什么样的外观才能成功使容器能够从运行 SELinux 的底层主机挂载 HostPath 卷?
更新:
我正在访问的文件是具有以下标签的 CA 证书:
system_u:object_r:cert_t:s0
当我使用以下选项时:
securityContext:
seLinuxOptions:
level: "s0:c123,c456"
然后通过 检查访问控制审计错误ausearch -m avc -ts recent
,我可以看到容器的级别标签为 时出现权限被拒绝错误s0:c123,c456
,因此我可以看到级别标签有效。我已将标签设置为s0
.
但是,如果我尝试将type
标签更改为cert_t
,则容器甚至不会启动,则会出现错误:
container_linux.go:247: starting container process caused "process_linux.go:364: container init caused \"write /proc/self/task/1/attr/exec: invalid argument\""
我似乎无法更改容器的类型标签。
解决方案
您可以使用以下方法分配 SELinux 标签seLinuxOptions
:
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: k8s.gcr.io/test-webserver
name: test-container
volumeMounts:
- mountPath: /test-pd
name: test-volume
securityContext:
seLinuxOptions: # it may don’t have the desired effect
level: "s0:c123,c456"
securityContext:
seLinuxOptions:
level: "s0:c123,c456"
volumes:
- name: test-volume
hostPath:
# directory location on host
path: /data
# this field is optional
type: Directory
根据文件:
感谢菲尔指出这一点。它似乎仅Pod.spec.securityContext
根据问题评论起作用
- seLinuxOptions:支持 SELinux 标签的卷被重新标记为可通过 seLinuxOptions 下指定的标签访问。通常您只需要设置级别部分。这将为 Pod 中的所有 Container 以及 Volumes设置多类别安全 (MCS) 标签。
推荐阅读
- android - Moshi Json 注释不适用于 proguard
- python - 箱线图多索引
- angular - 将 nginx 配置为 Angular 中的服务器资产文件
- docker - Docker 注册表管理界面
- react-native - react-native-reanimated 和 react-native-gesture-handler X/Y 坐标
- angular - NgIf 性能问题应用于 div 而不是 ng-container
- javascript - JQuery 根据 Contact Form 7 中的选择值自动填充文本字段
- powerbi - Power Bi DAX:相对日期过滤
- python - 当其中一个是无限的时如何获得两个可迭代的笛卡尔积
- r - Shiny:它如何处理并发请求?