任务

任务
管理集群
Debug DNS 方案
Enabling Service Topology (EN)
IP Masquerade Agent 用户指南
Kubernetes 云管理控制器
Safely Drain a Node while Respecting the PodDisruptionBudget (EN)
为 Kubernetes 运行 etcd 集群
为系统守护进程预留计算资源
为节点发布扩展资源
使用 CoreDNS 进行服务发现
使用 KMS 提供商进行数据加密
使用 Kubernetes API 访问集群
关键插件 Pod 的调度保证
启用端点切片
命名空间演练
在 Kubernetes 集群中使用 NodeLocal DNSCache
在 Kubernetes 集群中使用 sysctl
在实时集群上重新配置节点的 Kubelet
声明网络策略
开发云控制器管理器
控制节点上的 CPU 管理策略
控制节点上的拓扑管理策略
搭建高可用的 Kubernetes Masters
改变默认 StorageClass
更改 PersistentVolume 的回收策略
自定义 DNS 服务
访问集群上运行的服务
通过命名空间共享集群
通过配置文件设置 Kubelet 参数
配置 API 对象配额
配置多个调度器
配置资源不足时的处理方式
限制存储消耗
集群 DNS 服务自动伸缩
集群安全
集群管理
静态加密 Secret 数据
用插件扩展 kubectl
管理巨页(HugePages)
调度 GPUs

Edit This Page

为 Windows 的 pod 和容器配置 RunAsUserName

FEATURE STATE: Kubernetes v1.17 beta

本页展示如何为运行在 Windows 节点上的 pod 和容器启用并使用 RunAsUserName 功能。此功能旨在成为 Windows 版的 runAsUser(Linux),允许用户使用与默认用户名不同的用户名运行容器 entrypoint。

注意:

该功能目前处于 beta 状态。 RunAsUserName 的整体功能不会出现变更,但是关于用户名验证的部分可能会有所更改。

准备开始

你必须有一个 Kubernetes 集群,并且 kubectl 必须能和集群通信。集群应该要有 Windows 工作节点,将在其中调度运行 Windows 工作负载的 pod 和容器。

为 Pod 设置 Username

要指定运行 Pod 容器时所使用的用户名,请在 Pod 声明中包含 securityContextPodSecurityContext)字段,并在其内部包含 windowsOptionsWindowsSecurityContextOptions)字段的 runAsUserName 字段。

您为 Pod 指定的 Windows SecurityContext 选项适用于该 Pod 中(包括 init 容器)的所有容器。

这儿有一个已经设置了 runAsUserName 字段的 Windows Pod 的配置文件:

windows/run-as-username-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: run-as-username-pod-demo
spec:
  securityContext:
    windowsOptions:
      runAsUserName: "ContainerUser"
  containers:
  - name: run-as-username-demo
    image: mcr.microsoft.com/windows/servercore:ltsc2019
    command: ["ping", "-t", "localhost"]
  nodeSelector:
    kubernetes.io/os: windows

创建 Pod:

kubectl apply -f https://k8s.io/examples/windows/run-as-username-pod.yaml

验证 Pod 容器是否在运行:

kubectl get pod run-as-username-pod-demo

获取该容器的 shell:

kubectl exec -it run-as-username-pod-demo -- powershell

检查运行 shell 的用户的用户名是否正确:

echo $env:USERNAME

输出结果应该是这样:

ContainerUser

为容器设置 Username

要指定运行容器时所使用的用户名,请在容器清单中包含 securityContextSecurityContext)字段,并在其内部包含 windowsOptionsWindowsSecurityContextOptions)字段的 runAsUserName 字段。

您为容器指定的 Windows SecurityContext 选项仅适用于该容器,并且它会覆盖 Pod 级别设置。

这儿有一个 Pod 的配置文件,其只有一个容器,并且在 Pod 级别和容器级别都设置了 runAsUserName

windows/run-as-username-container.yaml
apiVersion: v1
kind: Pod
metadata:
  name: run-as-username-container-demo
spec:
  securityContext:
    windowsOptions:
      runAsUserName: "ContainerUser"
  containers:
  - name: run-as-username-demo
    image: mcr.microsoft.com/windows/servercore:ltsc2019
    command: ["ping", "-t", "localhost"]
    securityContext:
        windowsOptions:
            runAsUserName: "ContainerAdministrator"
  nodeSelector:
    kubernetes.io/os: windows

创建 Pod:

kubectl apply -f https://k8s.io/examples/windows/run-as-username-container.yaml

验证 Pod 容器是否在运行:

kubectl get pod run-as-username-container-demo

获取该容器的 shell:

kubectl exec -it run-as-username-container-demo -- powershell

检查运行 shell 的用户的用户名是否正确(应该是容器级别设置的那个):

echo $env:USERNAME

输出结果应该是这样:

ContainerAdministrator

Windows Username 的局限性

想要使用此功能,在 runAsUserName 字段中设置的值必须是有效的用户名。它必须是 DOMAIN\USER 这种格式,其中 DOMAIN\ 是可选的。Windows 用户名不区分大小写。此外,关于 DOMAINUSER 还有一些限制:

  • runAsUserName 字段不能为空,并且不能包含控制字符(ASCII 值:0x00-0x1F0x7F
  • DOMAIN 必须是 NetBios 名称或 DNS 名称,每种名称都有各自的局限性:
    • NetBios 名称:最多 15 个字符,不能以 .(点)开头,并且不能包含以下字符:\ / : * ? " < > |
    • DNS 名称:最多 255 个字符,只能包含字母、数字、点和中划线,并且不能以 .(点)或 -(中划线)开头和结尾。
  • USER 最多不超过 20 个字符,不能 包含点或空格,并且不能包含以下字符:" / \ [ ] : ; | = , + * ? < > @

runAsUserName 字段接受的值的一些示例:ContainerAdministratorContainerUserNT AUTHORITY\NETWORK SERVICENT AUTHORITY\LOCAL SERVICE

关于这些限制的更多信息,可以查看这里这里

ERROR

You must define a steps

This template requires that you provide text that lists a sequence of numbered steps that accomplish the task.'. The text in this block will be displayed under the heading . To get rid of this message and take advantage of this template, capture the steps variable and populate it with content.

接下来

反馈