第 10 章 配置 seccomp 配置集

OpenShift Container Platform 容器或 pod 运行一个应用程序,它执行一个或多个定义明确的任务。应用程序通常只需要底层操作系统内核 API 的一小部分。seccomp、安全计算模式是一种 Linux 内核功能,可用于将容器中运行的进程限制为仅调用可用系统调用的一个子集。可以通过创建应用到容器或 pod 的配置集来配置这些系统调用。seccomp 配置集作为 JSON 文件存储在磁盘上。

重要

OpenShift 工作负载默认运行 unconfined,不应用任何 seccomp 配置集。

重要

seccomp 配置集不能应用到特权容器。

10.1. 为所有 pod 启用默认 seccomp 配置集

OpenShift Container Platform 附带了一个默认的 seccomp 配置集,它被引用为 runtime/default。您可以通过创建自定义安全性上下文约束(SCC),为 pod 或容器工作负载启用默认的 seccomp 配置集。

注意

需要创建自定义 SCC。不要编辑默认 SCC。编辑默认 SCC 可能会导致升级某些平台 Pod 或 OpenShift Container Platform 时出现问题。如需更多信息,请参阅标题为"默认安全性上下文约束"部分。

按照以下步骤为所有 pod 启用默认 seccomp 配置集:

  1. 将可用的 受限 SCC 导出至 yaml 文件中:

    $ oc get scc restricted -o yaml > restricted-seccomp.yaml
  2. 编辑创建的 restricted SCC yaml 文件:

    $ vi restricted-seccomp.yaml
  3. 如本例所示更新:

    kind: SecurityContextConstraints
    metadata:
      name: restricted  1
    <..snip..>
    seccompProfiles:    2
    - runtime/default   3
    1
    进入 restricted-seccomp
    2
    添加 seccompProfile:
    3
    添加 - 运行时/默认
  4. 创建自定义 SCC:

    $ oc create -f restricted-seccomp.yaml

    预期输出

    securitycontextconstraints.security.openshift.io/restricted-seccomp created

  5. 将自定义 SCC 添加到 ServiceAccount:

    $ oc adm policy add-scc-to-user restricted-seccomp -z default
    注意

    默认服务帐户是应用的 ServiceAccount,除非用户配置不同的对象。OpenShift Container Platform 根据 SCC 中的信息配置 pod 的 seccomp 配置集。

    预期输出

    clusterrole.rbac.authorization.k8s.io/system:openshift:scc:restricted-seccomp added: "default"

在 OpenShift Container Platform 4.6 中,添加 pod 注解 seccomp.security.alpha.kubernetes.io/pod: runtime/defaultcontainer.seccomp.security.alpha.kubernetes.io/<container_name>: runtime/default 已被弃用。