【发布时间】:2018-12-02 16:34:02
【问题描述】:
我正在尝试将 hostPath volume 挂载到 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 容器时,我得到了同样的错误。
在here 中描述了该问题,并且在使用 Docker 时,可以通过使用 Docker 文档here 中描述的z 或Z 绑定挂载标志来修复。
虽然我可以通过运行来解决问题
chcon -Rt svirt_sandbox_file_t /path/to/my/host/dir/to/mount
我认为这是一个令人讨厌的 hack,因为我需要在我的 Kubernetes 集群中的每台主机上执行此操作,还因为我在 YAML 规范中描述的 Kubernetes 部署并没有完整描述它需要做什么完成以使我的 YAML 正确运行。关闭 SELinux 不是一种选择。
我可以看到 Kubernetes 在文档 here 中提到了 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\""
我似乎无法更改容器的类型标签。
【问题讨论】:
标签: docker kubernetes rhel selinux