본문 바로가기

Container/Kubernetes

[K8S] Security Context

컨테이너와 호스트는 같은 커널을 공유해서 완전히 isolate 되어있지는 않고, Linux에서 namespace를 이용해서 Isolate된다. 이러한 환경에서 쿠버네티스의 "Security Context"는 파드(Pod) 또는 컨테이너(Container)에서 사용할 수 있는 보안 설정을 정의한다.

 

  • Pod의 user ID 변경하기
# Pod에서 사용하고 있는 user 확인
controlplane ~ ➜  kubectl exec ubuntu-sleeper -- whoami
root

# User 변경하기
controlplane ~ ➜  kubectl get pod ubuntu-sleeper -o yaml > ubuntu-sleeper.yaml
controlplane ~ ➜  vi ubuntu-sleeper.yaml
  securityContext: 
    runAsUser: 1010

controlplane ~ ➜  kubectl delete pod ubuntu-sleeper --force
Warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "ubuntu-sleeper" force deleted

controlplane ~ ➜  kubectl apply -f ubuntu-sleeper.yaml 
pod/ubuntu-sleeper created

controlplane ~ ➜  kubectl get pods
NAME             READY   STATUS    RESTARTS   AGE
ubuntu-sleeper   1/1     Running   0          6s
  • Pod에 대해 지정하는 보안 설정은 Pod의 모든 컨테이너에 적용된다.
apiVersion: v1
kind: Pod
metadata:
  name: multi-pod
spec:
  securityContext:  
    runAsUser: 1001 # Pod 레벨 (Pod의 모든 컨테이너에 대해 모든 프로세스가 사용자 ID 1001으로 실행되도록 지정)
  containers:      # 컨테이너 레벨
  -  image: ubuntu
     name: web
     command: ["sleep", "5000"]
     securityContext:
      runAsUser: 1002

  -  image: ubuntu
     name: sidecar
     command: ["sleep", "5000"] # 지정이 안되면 Default(Pod레벨)로 User가 설정된다.
  • 컨테이너에 대한 기능 설정
    • 루트 사용자의 모든 권한을 부여하지 않고도 프로세스에 특정 권한을 부여할 수 있다.
    • 컨테이너에 대한 Linux 기능을 추가하거나 제거하려면 컨테이너의  capabilities에 필드를 포함한다.
# Capabilities: 리눅스 커널 기능(capabilities)을 추가하거나 제거하여 프로세스에 필요한 최소한의 권한만 부여할 수 있다.
# ubuntu-sleeper를 SYS_TIME capability와 함께 Root user로 동작하도록 수정하기
controlplane ~ ➜  vi ubuntu-sleeper.yaml
spec:
  containers:
  - command:
    - sleep
    - "4800"
    image: ubuntu
    imagePullPolicy: Always
    name: ubuntu
    securityContext:
      capabilities:
        add: ["SYS_TIME"]

 

참고: https://kubernetes.io/docs/tasks/configure-pod-container/security-context/

반응형

'Container > Kubernetes' 카테고리의 다른 글

[K8S] Image Security  (0) 2025.02.03
[K8S] Service Account(SA)  (0) 2025.02.03
[K8S] ClusterRole/ClusterRoleBinding  (0) 2025.02.03
[K8S] api-resources  (0) 2025.01.23
[K8S] Authorization  (1) 2025.01.20