Kubernetes 在名字空間級別應用Pod安全標準

2022-06-20 11:53 更新

在名字空間級別應用 Pod 安全標準

Note
本教程僅適用于新集群。

Pod 安全準入(PSA)在 v1.23 及更高版本默認啟用, 因為它升級到測試版(beta)。 Pod 安全準入是在創(chuàng)建 Pod 時應用 Pod 安全標準的準入控制器。 在本教程中,你將應用 ?baseline ?Pod 安全標準,每次一個名字空間。

你還可以在集群級別一次將 Pod 安全標準應用于多個名稱空間。

在開始之前

在你的工作站中安裝以下內容:

創(chuàng)建集群

  1. 按照如下方式創(chuàng)建一個 ?KinD ?集群:
  2. kind create cluster --name psa-ns-level --image kindest/node:v1.23.0
    

    輸出類似于:

    Creating cluster "psa-ns-level" ...
     ? Ensuring node image (kindest/node:v1.23.0) ?? 
     ? Preparing nodes ??  
     ? Writing configuration ?? 
     ? Starting control-plane ??? 
     ? Installing CNI ?? 
     ? Installing StorageClass ?? 
    Set kubectl context to "kind-psa-ns-level"
    You can now use your cluster with:
    
    kubectl cluster-info --context kind-psa-ns-level
    
    Not sure what to do next? ??  Check out https://kind.sigs.k8s.io/docs/user/quick-start/
  3. 將 kubectl 上下文設置為新集群:
  4. kubectl cluster-info --context kind-psa-ns-level
    

    輸出類似于:

    Kubernetes control plane is running at https://127.0.0.1:50996
    CoreDNS is running at https://127.0.0.1:50996/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
    
    To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

創(chuàng)建名字空間

創(chuàng)建一個名為 ?example ?的新名字空間:

kubectl create ns example

輸出類似于:

namespace/example created

應用 Pod 安全標準 

  1. 使用內置 Pod 安全準入所支持的標簽在此名字空間上啟用 Pod 安全標準。 在這一步中,我們將根據(jù)最新版本(默認值)對基線 Pod 安全標準發(fā)出警告。
  2. kubectl label --overwrite ns example \
      pod-security.kubernetes.io/warn=baseline \
      pod-security.kubernetes.io/warn-version=latest
  3. 可以使用標簽在任何名字空間上啟用多個 Pod 安全標準。 以下命令將強制(?enforce?) 執(zhí)行基線(?baseline?)Pod 安全標準, 但根據(jù)最新版本(默認值)對受限(?restricted?)Pod 安全標準執(zhí)行警告(?warn?)和審核(?audit?)。
  4. kubectl label --overwrite ns example \
      pod-security.kubernetes.io/enforce=baseline \
      pod-security.kubernetes.io/enforce-version=latest \
      pod-security.kubernetes.io/warn=restricted \
      pod-security.kubernetes.io/warn-version=latest \
      pod-security.kubernetes.io/audit=restricted \
      pod-security.kubernetes.io/audit-version=latest

驗證 Pod 安全標準

  1. 在 ?example ?名字空間中創(chuàng)建一個最小的 pod:
  2. cat <<EOF > /tmp/pss/nginx-pod.yaml
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
    spec:
      containers:
        - image: nginx
          name: nginx
          ports:
            - containerPort: 80
    EOF
  3. 將 Pod 規(guī)約應用到集群中的 ?example ?名字空間中:
  4. kubectl apply -n example -f /tmp/pss/nginx-pod.yaml
    

    輸出類似于:

    Warning: would violate PodSecurity "restricted:latest": allowPrivilegeEscalation != false (container "nginx" must set securityContext allowPrivilegeEscalation=false), unrestricted capabilities (container "nginx" must set securityContext.capabilities.drop=["ALL"]), runAsNonRoot != true (pod or container "nginx" must set securityContext.runAsNonRoot=true), seccompProfile (pod or container "nginx" must set securityContext seccompProfile.type to "RuntimeDefault" or "Localhost")
    pod/nginx created
  5. 將 Pod 規(guī)約應用到集群中的 ?default ?名字空間中:
  6. kubectl apply -n default -f /tmp/pss/nginx-pod.yaml
    

    輸出類似于:

    pod/nginx created
    

以上 Pod 安全標準僅被應用到 ?example ?名字空間。 你可以在沒有警告的情況下在 ?default ?名字空間中創(chuàng)建相同的 Pod。

清理 

運行 ?kind delete cluster -name psa-ns-level? 刪除創(chuàng)建的集群。


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號