

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Pod 安全內容
<a name="windows-security"></a>

 **Pod 安全政策 (PSP)** 和 **Pod 安全標準 (PSS)** 是在 Kubernetes 中強制執行安全性的兩種主要方式。請注意，自 PodSecurityPolicy 已棄用，並將在 v1.25 中移除，且 Pod 安全標準 (PSS) 是 Kubernetes 建議的方法，可強制執行安全性。

Pod 安全政策 (PSP) 是 Kubernetes 中實作安全政策的原生解決方案。PSP 是叢集層級的資源，可控制 Pod 規格的安全敏感層面。使用 Pod 安全政策，您可以定義一組 Pod 必須滿足的條件，才能由叢集接受。PSP 功能已從 Kubernetes 的早期開始提供，旨在封鎖在指定叢集上建立設定錯誤的 Pod。

如需 Pod 安全政策的詳細資訊，請參閱 Kubernetes [文件](https://kubernetes.io/docs/concepts/policy/pod-security-policy/)。根據 [Kubernetes 棄用政策](https://kubernetes.io/docs/reference/using-api/deprecation-policy/)，較舊版本將在功能棄用九個月後停止取得支援。

另一方面，Pod 安全標準 (PSS) 是建議的安全性方法，通常使用安全內容實作，在 Pod 資訊清單中定義為 Pod 和容器規格的一部分。PSS 是 Kubernetes 專案團隊為解決 Pod 的安全相關最佳實務而定義的官方標準。它定義了政策，例如基準 （最低限制性、預設性）、特權 （無限制性） 和限制性 （最高限制性）。

我們建議您從基準設定檔開始。PSS 基準描述檔在安全性和潛在摩擦之間提供穩固的平衡，需要最少的例外狀況清單，可作為工作負載安全性的良好起點。如果您目前正在使用 PSPs我們建議您切換到 PSS。如需 PSS 政策的詳細資訊，請參閱 Kubernetes [文件](https://kubernetes.io/docs/concepts/security/pod-security-standards/)。這些政策可以使用多種工具強制執行，包括來自 [OPA](https://www.openpolicyagent.org/) 和 [Kyverno](https://kyverno.io/) 的工具。例如，Kyverno [在此處](https://kyverno.io/policies/pod-security/)提供 PSS 政策的完整集合。

安全內容設定可讓 提供選取程序的權限、使用程式描述檔限制個別程式的功能、允許權限提升、篩選系統呼叫等。

在安全性內容方面，Kubernetes 中的 Windows Pod 與標準 Linux 型工作負載有一些限制和差異。

Windows 使用每個容器的任務物件搭配系統命名空間篩選條件，以包含容器中的所有程序，並提供與主機的邏輯隔離。如果沒有命名空間篩選，就無法執行 Windows 容器。這表示無法在主機的內容中宣告系統權限，因此 Windows 上無法使用特權容器。

以下是唯一記錄`windowsOptions`的 [Windows 安全內容選項](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#windowssecuritycontextoptions-v1-core)，其餘則是一般[安全內容選項](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.21/#securitycontext-v1-core) 

如需 Windows 與 linux 支援的安全內容屬性清單，請參閱[此處](https://kubernetes.io/docs/setup/production-environment/windows/_print/#v1-container)的官方文件。

Pod 特定設定會套用至所有容器。如果未指定，則會使用 PodSecurityContext 中的選項。如果同時在 SecurityContext 和 PodSecurityContext 中設定，則 SecurityContext 中指定的值優先。

例如，Pod 和容器的 runAsUserName 設定是 Windows 選項的大約相當於 Linux 特定 runAsUser 設定，並且在下列資訊清單中，Pod 特定安全內容會套用至所有容器

```
apiVersion: v1
kind: Pod
metadata:
  name: run-as-username-pod-demo
spec:
  securityContext:
    windowsOptions:
      runAsUserName: "ContainerUser"
  containers:
  - name: run-as-username-demo

  nodeSelector:
    kubernetes.io/os: windows
```

但在以下情況下，容器層級安全內容會覆寫 Pod 層級安全內容。

```
apiVersion: v1
kind: Pod
metadata:
  name: run-as-username-container-demo
spec:
  securityContext:
    windowsOptions:
      runAsUserName: "ContainerUser"
  containers:
  - name: run-as-username-demo
    ..
    securityContext:
        windowsOptions:
            runAsUserName: "ContainerAdministrator"
  nodeSelector:
    kubernetes.io/os: windows
```

runAsUserName 欄位可接受的值範例：ContainerAdministrator、ContainerUser、NT AUTHORITY\\NETWORK SERVICE、NT AUTHORITY\\LOCAL SERVICE

一般而言，最好使用 ContainerUser for Windows Pod 執行容器。使用者不會在容器和主機之間共用，但 ContainerAdministrator 在容器中具有 的其他權限。請注意，有使用者名稱[限制](https://kubernetes.io/docs/tasks/configure-pod-container/configure-runasusername/#windows-username-limitations)需要注意。

使用 ContainerAdministrator 的良好範例是設定 PATH。您可以使用 USER 指令來執行此操作，如下所示：

```
USER ContainerAdministrator
RUN setx /M PATH "%PATH%;C:/your/path"
USER ContainerUser
```

另請注意，秘密會在節點磁碟區上以純文字撰寫 （相較於 linux 上的 tmpfs/記憶體內）。這表示您必須執行兩件事
+ 使用檔案 ACLs 保護秘密檔案位置
+ 使用 [BitLocker](https://docs.microsoft.com/en-us/windows/security/information-protection/bitlocker/bitlocker-how-to-deploy-on-windows-server) 使用磁碟區層級加密 