我们可以提供一个需要由 Kubernetes 中的 pod/容器运行的用户名吗?

问题描述

Pod规范中,有一个选项可以指定需要由所有容器运行的用户 ID

podSecurityContext:
  runAsUser: <a numeric Id>

有没有办法我们也可以更改用户名,就像我们对 windows pods 和容器所做的那样,如下

  securityContext:
    windowsOptions:
      runAsUserName: "ContainerUser"

解决方法

不幸的是,没有这样的方法。 WindowsSecurityContextOptions 包含Windows 特定 选项和凭据。 PodSecurityContext 允许您使用:

  • securityContext.runAsUser (int64)

运行容器进程入口点的 UID。默认为 如果未指定,则在图像元数据中指定用户。也可以设置在 安全上下文。如果在 SecurityContext 和 PodSecurityContext,SecurityContext 中指定的值取 该容器的优先级。

  • securityContext.runAsNonRoot(布尔值)

表示容器必须以非 root 用户身份运行。如果为真,则 Kubelet 将在运行时验证图像以确保它不会 以 UID 0 (root) 身份运行,如果启动则无法启动容器。如果 未设置或为 false,则不会执行此类验证。也可以设置 在安全上下文中。如果在 SecurityContext 和 PodSecurityContext,SecurityContext 中指定的值取 优先级。

  • securityContext.runAsGroup (int64)

运行容器进程入口点的 GID。使用运行时 如果未设置,则默认。也可以在 SecurityContext 中设置。如果两者都设置 SecurityContext 和 PodSecurityContext 中指定的值 SecurityContext 优先于该容器。

尝试对 runAsUser 使用 String 而不是 Integer 会导致错误:

invalid type for io.k8s.api.core.v1.SecurityContext.runAsUser: got "string",expected "integer"