네임스페이스 수준에서 Pod 보안 표준 적용하기

네임스페이스 수준에서 Pod 보안 표준 적용하기 (Apply Pod Security Standards at the namespace level)

[NOTE] 참고

이 튜토리얼은 새 클러스터에만 적용됩니다.

Pod Security Admission은 파드가 생성될 때 Pod Security Standards를 적용하는 admission controller입니다. v1.25에서 GA된 기능입니다. 이 튜토리얼에서는 baseline Pod Security Standard를 네임스페이스 하나씩 적용할 거예요.

클러스터 수준에서 여러 네임스페이스에 한 번에 Pod Security Standards를 적용할 수도 있습니다. 지침은 클러스터 수준에서 Pod Security Standards 적용을 참조하세요.

출처: 문서

본문

시작하기 전에

워크스테이션에 다음을 설치한다:

클러스터 만들기

  1. 다음과 같이 kind 클러스터를 만든다:

    kind create cluster --name psa-ns-level
    

    출력은 다음과 비슷합니다:

    Creating cluster "psa-ns-level" ...
     ✓ Ensuring node image (kindest/node:v) 🖼 
     ✓ 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/
    
  2. kubectl 컨텍스트를 새 클러스터로 설정한다:

    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'.
    

네임스페이스 만들기

example라는 새 네임스페이스를 만든다:

kubectl create ns example

출력은 다음과 비슷합니다:

namespace/example created

그 네임스페이스에 대해 Pod Security Standards 검사 활성화하기

  1. 내장된 Pod Security Admission이 지원하는 라벨을 사용해 이 네임스페이스에서 Pod Security Standards를 활성화한다. 이 단계에서는 baseline 파드 보안 표준의 최신 버전을 충족하지 않는 파드에 warn하는 검사를 구성합니다.

    kubectl label --overwrite ns example \
       pod-security.kubernetes.io/warn=baseline \
       pod-security.kubernetes.io/warn-version=latest
    
  2. 라벨을 사용해 어떤 네임스페이스에서든 여러 pod 보안 표준 검사를 구성할 수 있습니다. 다음 명령은 baseline Pod Security Standard를 enforce 하고, 최신 버전(기본값)에 따라 restricted Pod Security Standard에 대해 warnaudit을 합니다.

    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 Security Standard 적용 확인

  1. example 네임스페이스에 baseline 파드를 만든다:

    kubectl apply -n example -f https://k8s.io/examples/security/example-baseline-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
    
  2. default 네임스페이스에 baseline 파드를 만든다:

    kubectl apply -n default -f https://k8s.io/examples/security/example-baseline-pod.yaml
    

    출력은 다음과 비슷합니다:

    pod/nginx created
    

Pod Security Standards 적용과 경고 설정은 example 네임스페이스에만 적용되었습니다. default 네임스페이스에서는 경고 없이 같은 파드를 만들 수 있습니다.

정리하기

이제 위에서 만든 클러스터를 다음 명령으로 삭제한다:

kind delete cluster --name psa-ns-level

더 알아보기 (Learn more)