From ee7253b3c1be20a9e016f0c879fd853bb4751a03 Mon Sep 17 00:00:00 2001 From: Sophy417 <53026875+Sophy417@users.noreply.github.com> Date: Tue, 15 Oct 2019 17:19:52 +0800 Subject: [PATCH] Create configure-gmsa.md (#16816) --- .../configure-pod-container/configure-gmsa.md | 354 ++++++++++++++++++ 1 file changed, 354 insertions(+) create mode 100644 content/zh/docs/tasks/configure-pod-container/configure-gmsa.md diff --git a/content/zh/docs/tasks/configure-pod-container/configure-gmsa.md b/content/zh/docs/tasks/configure-pod-container/configure-gmsa.md new file mode 100644 index 0000000000..0a50330fa4 --- /dev/null +++ b/content/zh/docs/tasks/configure-pod-container/configure-gmsa.md @@ -0,0 +1,354 @@ +--- +title: 为 Windows pod 和容器配置 GMSA +content_template: templates/task +weight: 20 +--- + + +{{% capture overview %}} + +{{< feature-state for_k8s_version="v1.14" state="alpha" >}} + + +本页显示了如何将在 Windows 节点上运行的 pod 和容器配置[组管理服务账户](https://docs.microsoft.com/en-us/windows-server/security/group-managed-service-accounts/group-managed-service-accounts-overview) (GMSA) 。 +组管理服务账户是一种特定类型的 Active Directory 账户,它提供自动密码管理,简化服务主体名称(SPN)管理,以及将管理委派给跨多个服务器的其他管理员的功能。 + + +在 Kubernetes 中,在 Kubernetes 集群范围内将 GMSA 凭据规范配置为自定义资源。Windows Pod 以及 Pod 中的各个容器可以配置为在与其他 Windows 服务交互时将 GMSA 用于基于域的功能(例如,Kerberos 身份验证)。从 v1.14开始,Dockershim 是支持 Windows 工作负载 GMSA 的唯一容器运行时接口。在未来计划通过 CRI 和其他运行时刻执行 GMSA。 + + +{{< note >}} +当前,此功能处于 Alpha 状态。尽管总体目标和功能不会改变,但在 pod 规范中指定 GMSA credspec 引用的方式可能会从注释更改为 API 字段。在测试或采用此功能时,请考虑到这一点。 +{{< /note >}} + +{{% /capture %}} + +{{% capture prerequisites %}} + + +您需要拥有一个 Kubernetes 集群,并且必须配置 kubectl 命令行工具才能与您的集群通信。 +预计该集群将具有 Windows 辅助节点,在该节点上将安排运行需要 GMSA 凭据的 Windows 工作负载的容器的 pod。 +本节介绍了每个集群一次所需的一组初始步骤: + + +### 启用 WindowsGMSA 功能门 + +在 Alpha 状态下,需要在 Windows 节点上的 kubelet 上启用`WindowsGMSA`功能门。 +这是将 GMSA 凭据规范从集群范围配置传递到容器运行时所必需的。 +有关启用功能门的说明,请参见[功能门](https://kubernetes.io/docs/reference/command-line-tools-reference/feature-gates/) 。 +请确保 kubelet.exe 命令行中存在`--feature-gates=WindowsGMSA=true`参数。 + + +### 安装 GMSACredentialSpec CRD + +GMSA 凭证规范资源的[CustomResourceDefinition](https://kubernetes.io/docs/tasks/access-kubernetes-api/custom-resources/custom-resource-definitions/) (CRD)需要在集群上配置来定义自定义资源类型`GMSACredentialSpec`。 +下载 GMSA CRD [YAML](https://github.com/kubernetes-sigs/windows-gmsa/blob/master/admission-webhook/deploy/gmsa-crd.yml),并将其另存为 gmsa-crd.yaml。 +接下来,使用`kubectl apply -f gmsa-crd.yaml`安装 CRD。 + + +### 安装 webhooks 以验证 GMSA 用户 + +在 Kubernetes 集群上在 Pod 或容器级别需要配置两个 webhook 来填充和验证 GMSA 凭证规范参考: + + +1. 一个变异的 webhook,它将对 GMSA 的引用(按 Pod 规范中的名称)扩展为 Pod 规范中 JSON 形式的完整的凭据规范。 + + +1. 一个有效的 webhook 可以确保 Pod 服务账户可以使用所有对 GMSA 的引用。 + + +安装上述 webhook 和相关对象需要执行以下步骤: + + +1. 创建一个证书密钥对(将用于允许 webhook 容器与集群通信) + + +1. 安装带有上述证书的密钥。 + + +1. 为核心 webhook 逻辑创建部署。 + + +1. 创建参考部署的验证和变异 webhook 配置。 + + +可以使用[脚本](https://github.com/kubernetes-sigs/windows-gmsa/blob/master/admission-webhook/deploy/deploy-gmsa-webhook.sh)来部署和配置 GMSA webhook 和上面提到的相关对象。 +该脚本可以使用```--dry-run```选项运行,以允许您查看将对集群进行的更改。 + + +脚本使用的[YAML 模版](https://github.com/kubernetes-sigs/windows-gmsa/blob/master/admission-webhook/deploy/gmsa-webhook.yml.tpl)也可以用于手动部署 webhook 和关联的对象(对参数进行适当的替换) + +{{% /capture %}} + +{{% capture steps %}} + + +## 在活动目录中配置 GMSA 和 Windows 节点 + +在 kubernetes 中的 Pod 可以配置为使用 GMSA 之前,需要按照 [此处](https://docs.microsoft.com/en-us/windows-server/security/group-managed-service-accounts/getting-started-with-group-managed-service-accounts#BKMK_Step1)所述在活动目录中配置所需的 GMSA。 +需要在活动目录中配置 Windows 辅助节点(属于 Kubernetes集群),以访问与所需的 GMSA 关联的秘密凭据,如[此处](https://docs.microsoft.com/en-us/windows-server/security/group-managed-service-accounts/getting-started-with-group-managed-service-accounts#to-add-member-hosts-using-the-set-adserviceaccount-cmdlet)所述 + + +## 创建 GMSA 凭证规范资源 + +安装了 GMSACredentialSpec CRD(如前所述)后,可以配置包含 GMSA 凭证规范的自定义资源。 +GMSA 凭证规范不包含秘密或敏感数据。 +容器运行时可以使用该信息来向 Windows 描述所需的容器 GMSA。 +可以使用实用程序[PowerShell 脚本](https://github.com/kubernetes-sigs/windows-gmsa/tree/master/scripts/GenerateCredentialSpecResource.ps1)以 YAML 格式生成 GMSA 凭据规范。 + + +以下是以 JSON 格式手动生成 GMSA 凭证规范 YAML 并将其转换到步骤: + + +1. 导入 CredentialSpec [模块](https://github.com/MicrosoftDocs/Virtualization-Documentation/blob/live/windows-server-container-tools/ServiceAccounts/CredentialSpec.psm1): `ipmo CredentialSpec.psm1` + + +1. 使用`New-CredentialSpec`创建 JSON 格式的凭证规范。 + 要创建名为 WebApp1 的 GMSA 的凭证规范,请调用`New-CredentialSpec -Name WebApp1 -AccountName WebApp1 -Domain $(Get-ADDomain -Current LocalComputer)` + + +1. 使用 `Get-CredentialSpec`来显示 JSON 文件的路径 + + +1. 将 credspec 文件从 JSON 转换为 YAML 格式,并应用必要的标头字段`apiVersion`,`kind`,`metadata`和`credspec`,使其成为可在 Kubernetes 中配置的 GMSACredentialSpec 自定义资源。 + + +以下 YAML 配置描述了名为`gmsa-WebApp1`的 GMSA 凭据规范: + +``` +apiVersion: windows.k8s.io/v1alpha1 +kind: GMSACredentialSpec +metadata: + name: gmsa-WebApp1 #This is an arbitrary name but it will be used as a reference +credspec: + ActiveDirectoryConfig: + GroupManagedServiceAccounts: + - Name: WebApp1 #Username of the GMSA account + Scope: CONTOSO #NETBIOS Domain Name + - Name: WebApp1 #Username of the GMSA account + Scope: contoso.com #DNS Domain Name + CmsPlugins: + - ActiveDirectory + DomainJoinConfig: + DnsName: contoso.com #DNS Domain Name + DnsTreeName: contoso.com #DNS Domain Name Root + Guid: 244818ae-87ac-4fcd-92ec-e79e5252348a #GUID + MachineAccountName: WebApp1 #Username of the GMSA account + NetBiosName: CONTOSO #NETBIOS Domain Name + Sid: S-1-5-21-2126449477-2524075714-3094792973 #SID of GMSA +``` + + +以上凭证规范资源可以另存为`gmsa-Webapp1-credspec.yaml`,并使用`kubectl apply -f gmsa-Webapp1-credspec.yml`命令将其应用于集群 + + +## 配置集群角色以在特定 GMSA 凭据规范上启用 RBAC + +需要为每个 GMSA 凭证规范资源定义集群角色。这可以通过主题(通常是服务账号)授权特定 GMSA 资源上的`use`动词。 +以下示例显示了一个集群角色,该角色授权以上`gmsa-WebApp1`凭证规范的使用。 +将文件另存为 gmsa-webapp1-role.yaml 并使用`kubectl apply -f gmsa-webapp1-role.yaml` + +``` +#Create the Role to read the credspec +apiVersion: rbac.authorization.k8s.io/v1 +kind: ClusterRole +metadata: + name: webapp1-role +rules: +- apiGroups: ["windows.k8s.io"] + resources: ["gmsacredentialspecs"] + verbs: ["use"] + resourceNames: ["gmsa-WebApp1"] +``` + + +## 为服务账户分配角色以使用特定的 GMSA 信用规范 + +服务账户(将使用其配置 Pod)需要绑定到上面创建的集群角色。 +这授权服务账户“使用”所需的 GMSA 凭据规范资源。 +以下显示了默认服务账户绑定到集群角色`webapp1-role`以使用上面创建的`gmsa-WebApp1`凭证规范资源。 + +``` +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: allow-default-svc-account-read-on-gmsa-WebApp1 + namespace: default +subjects: +- kind: ServiceAccount + name: default + namespace: default +roleRef: + kind: ClusterRole + name: webapp1-role + apiGroup: rbac.authorization.k8s.io +``` + + +## 在 Pod 规范中配置 GMSA 凭证规范参考 + +在功能的 Alpha 阶段,注释`pod.alpha.windows.kubernetes.io/gmsa-credential-spec-name`用于在 Pod 规范中指定对所需 GMSA 凭证规范自定义资源的引用。 +这将在 pod 规范中配置所有容器来使用指定的 GMSA。 +一个带有注释的 pod 规范示例填充为链接到`gmsa-WebApp1`: + +``` +apiVersion: apps/v1beta1 +kind: Deployment +metadata: + labels: + run: with-creds + name: with-creds + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + run: with-creds + template: + metadata: + labels: + run: with-creds + annotations: + pod.alpha.windows.kubernetes.io/gmsa-credential-spec-name: gmsa-WebApp1 # This must be the name of the cred spec you created + spec: + containers: + - image: mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019 + imagePullPolicy: Always + name: iis + nodeSelector: + beta.kubernetes.io/os: windows +``` + + +容器规范中的各个容器还可以使用注释`.container.alpha.windows.kubernetes.io/gmsa-credential-spec`来指定所需的 GMSA 信用规范。例如: + +``` +apiVersion: apps/v1beta1 +kind: Deployment +metadata: + labels: + run: with-creds + name: with-creds + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + run: with-creds + template: + metadata: + labels: + run: with-creds + annotations: + iis.container.alpha.windows.kubernetes.io/gmsa-credential-spec-name: gmsa-WebApp1 # This must be the name of the cred spec you created + spec: + containers: + - image: mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019 + imagePullPolicy: Always + name: iis + nodeSelector: + beta.kubernetes.io/os: windows +``` + + +当具有 GMSA 注释的 pod 规范(如上所述)应用于为 GMSA 配置的集群中时,将发生以下事件序列: + + +1. 变异 Webhook 解析并扩展了对 GMSA 凭证规范资源的所有引用,使其扩展到 GMSA 凭证规范的内容。 + + +1. 验证 Webhook 确保与 pod 相关的服务账号在 GMSA 凭证规范上被授权用于“使用”动词。 + + +1. 容器运行时使用指定的 GMSA 凭证规范配置每个 WIndows 容器,以便容器可以在活动目录中采用 GMSA 的身份,并使用该身份访问域中的服务。 + +{{% /capture %}}