[k8s] 쿠버네티스 Pod 보안을 위해 root 가 아닌 사용자로 컨테이너 실행하기
Pod의 Security context에서 runAsUser를 설정하면 컨테이너에서 실행할 사용자를 지정할 수 있다.
Pod의 Security context에서 runAsNonRoot를 설정하면 root로 실행하는 컨테이너의 실행을 차단할 수 있다.
- 차단 시 CreateContainerConfigError 에러와
Error: container has runAsNonRoot and image will run as root
에러 발생
- 차단 시 CreateContainerConfigError 에러와
1. runAsUser로 사용자 지정 전
- pod 설정 yaml 파일
xxxxxxxxxx
piVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test
labels:
app: test
spec:
replicas: 1
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
containers:
- name: testcontainer
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
- 컨테이너 내 프로세스 출력 결과 : 프로세스가 root로 실행 중
xxxxxxxxxx
$ docker exec -it d3b5893dd94d /bin/sh
/ # ps -ef
PID USER TIME COMMAND
1 root 0:00 sleep 3600
13 root 0:00 /bin/sh
19 root 0:00 ps -ef
/ #
2. runAsUser로 사용자 지정 후
- pod 설정 yaml 파일 : securityContext 내 실행 사용자 2000으로 설정
x...
spec:
containers:
- name: testcontainer
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
securityContext:
runAsUser: 2000
...
- yaml 파일에서 UID 2000 으로 설정 후 프로세스 목록 조회 결과 모든 프로세스가 UID 2000으로 실행된다.
x$ docker exec -it c6ace4bd4ceb /bin/sh
/ $ ps -ef
PID USER TIME COMMAND
1 2000 0:00 sleep 3600
7 2000 0:00 /bin/sh
13 2000 0:00 ps -ef
/ $
- 실제 컨테이너 내 UID 2000 이 없어도 문제 없다.
xxxxxxxxxx
/ $ cat /etc/passwd
root:x:0:0:root:/root:/bin/sh
daemon:x:1:1:daemon:/usr/sbin:/bin/false
bin:x:2:2:bin:/bin:/bin/false
sys:x:3:3:sys:/dev:/bin/false
sync:x:4:100:sync:/bin:/bin/sync
mail:x:8:8:mail:/var/spool/mail:/bin/false
www-data:x:33:33:www-data:/var/www:/bin/false
operator:x:37:37:Operator:/var:/bin/false
nobody:x:65534:65534:nobody:/home:/bin/false
/ $ id
uid=2000 gid=0(root)
3. runAsNonRoot 으로 root 계정 실행 컨테이너 차단 시
파드 보안 설정에서 runAsNonRoot 을 true로 설정하면 root 를 사용하는 컨테이너의 실행을 차단할 수 있다.
pod 설정 yaml 파일 : runAsNonRoot을 true로 설정
x
...
spec:
containers:
- name: testcontainer
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
securityContext:
runAsNonRoot: true
...
runAsNonRoot 설정 후 실행 시 "Error: container has runAsNonRoot and image will run as root" 에러가 발생함
x
Chois-MacBook-Pro:k8s choikyunghyun$ kubectl describe pod test-554b755595-nls66 | tail
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 56s default-scheduler Successfully assigned test-554b755595-nls66 to docker-for-desktop
Normal SuccessfulMountVolume 56s kubelet, docker-for-desktop MountVolume.SetUp succeeded for volume "default-token-q92r8"
Normal Pulled 5s (x6 over 55s) kubelet, docker-for-desktop Container image "busybox:1.28" already present on machine
Warning Failed 5s (x6 over 55s) kubelet, docker-for-desktop Error: container has runAsNonRoot and image will run as root
Chois-MacBook-Pro:k8s choikyunghyun$
- runAsUser를 일반 사용자로 지정하면 runAsNonRoot가 true 이더라도 문제 없이 실행된다.
- 이 경우에도 컨테이너를 실행하는 docker 프로세스 자체는 root 계정으로 실행 됨
'k8s' 카테고리의 다른 글
[k8s] kubespray로 쿠버네티스 설치 후 calico-node 에서 CrashLoopBackOff 에러가 날 경우 (0) | 2019.09.06 |
---|---|
[k8s] kubespray 를 사용한 bare-metal 서버에 쿠버네티스 설치하기 (0) | 2019.09.06 |
[K8S] Azure AKS 클러스터에서 helm 차트 사용하기 (0) | 2019.08.26 |
[K8S] Azure CLI 로 AKS 클러스터 구성 및 로컬 환경에서 kubectl 로 AKS 클러스터 연결하기 (0) | 2019.08.23 |
[k8s] 공개용 Docker Image를 다운로드하여 사설 Registry 로 옮기기 (0) | 2019.07.19 |