0

尝试在使用时设置自定义 seccomp 配置文件kubectl apply,尽管容器中存在该文件,但 pod 不会以以下错误启动:

Error: failed to create containerd container: cannot load seccomp profile "/var/lib/kubelet/seccomp/custom_profile.json": open /var/lib/kubelet/seccomp/custom_profile.json: no such file or directory

K8 部署 YAML

...
containers:
  - name: container-name
    image: container-image:version
    securityContext:
      seccompProfile:
        type: Localhost
        localhostProfile: custom_profile.json
...

该文件在创建容器并进入 pod 的外壳时被复制,我可以看到它确实存在于那里(当不尝试加载它并且 pod 启动时)

Dockerfile

...
COPY custom_profile.json /var/lib/kubelet/seccomp/custom_profile.json
...

我也尝试过更改所有者(chown)并以 root 权限运行,但只要该localhostProfile: custom_profile.json行在 YAML 中,就会再次出现相同的错误。

我错过了什么阻止文件被找到?YAML 中缺少什么,容器/dockerfile 中缺少什么?

以下文章让我走到了这一步,但仍然无法设置配置文件:https ://docs.openshift.com/container-platform/4.8/security/seccomp-profiles.html

4

1 回答 1

3

如果type: Localhost使用 seccomp 配置文件,则seccomp配置文件必须存在于调度 pod 的节点上。此外,路径是相对于路径的/var/lib/kubelet/seccomp。这/var/lib/kubelet/kubelet配置的默认路径。

以下是官方文档中的相关片段:

localhost/<path>- 将配置文件指定为位于 <seccomp_root>/ 的节点上的文件,其中 <seccomp_root> 是通过 --seccomp-profile-rootKubelet 上的标志定义的。如果--seccomp-profile-root未定义该标志,将使用默认路径,即 /seccomp 其中由 --root-dir 标志指定。

示例 1:为了使以下工作,custom_profile.json文件必须存在于 /var/lib/kubelet/seccomp节点上的路径中。

securityContext:
  seccompProfile:
    type: Localhost
    localhostProfile: custom_profile.json

示例 2:为了使以下工作,custom_profile.json文件必须存在于 /var/lib/kubelet/seccomp/profiles节点上的路径中。

securityContext:
  seccompProfile:
    type: Localhost
    localhostProfile: profiles/custom_profile.json

这是一个最小的工作示例:

seccomp配置文件被复制到工作节点上。

ps@worker-node:~$ sudo ls -lrt /var/lib/kubelet/seccomp/profiles
[sudo] password for ps:
total 12
-rw-r--r-- 1 root root   39 Sep 10 13:54 audit.json
-rw-r--r-- 1 root root   41 Sep 10 13:54 violation.json
-rw-r--r-- 1 root root 1657 Sep 10 13:54 fine-grained.json
ps@worker-node:~$

使用以下路径创建 pod,注意路径是相对于/var/lib/kubelet/seccomp.

apiVersion: v1
kind: Pod
metadata:
  name: audit-pod
  labels:
    app: audit-pod
spec:
  securityContext:
    seccompProfile:
      type: Localhost
      localhostProfile: profiles/audit.json
  containers:
  - name: test-container
    image: nginx
    securityContext:
      allowPrivilegeEscalation: false
于 2021-09-30T02:53:00.797 回答