실습
Security Context
Pod 생성
cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: nginx spec: containers: - image: nginx name: nginx EOF컨테이너에서 프로세스를 실행중인 유저 확인
kubectl exec nginx -- id프로세스 유틸리티 설치
kubectl exec nginx -- bash -c "apt update && apt install -y procps"실행중인 프로세스 확인
kubectl exec nginx -- ps auxnginx 유저 확인
kubectl exec nginx -- id nginxPod 재생성
cat <<EOF | kubectl replace --force -f - apiVersion: v1 kind: Pod metadata: name: nginx spec: securityContext: runAsNonRoot: true containers: - image: nginx name: nginx EOFPod가 생성되었는지 확인
kubectl get pod nginxPod에 명시된 컨테이너가 실행 되지 않는다면 그 이유를 확인
kubectl describe pod nginxPod 재생성
cat <<EOF | kubectl replace --force -f - apiVersion: v1 kind: Pod metadata: name: nginx spec: securityContext: runAsNonRoot: true runAsUser: 101 containers: - image: nginx name: nginx EOFPod가 생성되었는지 확인
kubectl get pod nginxPod에 명시된 컨테이너가 실행 되지 않는다면 그 이유를 확인
kubectl describe pod nginxPod 로그 확인
kubectl logs nginxNGINX 설정 파일 확인
kubectl run nginx-tmp --image=nginx --rm -it --restart=Never \ -- cat /etc/nginx/nginx.confPod 재생성
cat <<EOF | kubectl replace --force -f - apiVersion: v1 kind: ConfigMap metadata: name: nginx data: nginx.conf: | worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; } --- apiVersion: v1 kind: Pod metadata: name: nginx spec: securityContext: runAsNonRoot: true runAsUser: 101 containers: - image: nginx name: nginx ports: - containerPort: 80 volumeMounts: - name: nginx-conf mountPath: /etc/nginx/nginx.conf subPath: nginx.conf volumes: - name: nginx-conf configMap: name: nginx items: - key: nginx.conf path: nginx.conf EOFPod가 생성되었는지 확인
kubectl get pod nginxPod에 명시된 컨테이너가 실행 되지 않는다면 그 이유를 확인
kubectl describe pod nginxPod 로그 확인
kubectl logs nginx아래의 Dockerfile로 새로운 컨테이너 이미지 생성
FROM nginx WORKDIR /app RUN chown -R nginx:nginx /app && chmod -R 755 /app && \ chown -R nginx:nginx /var/cache/nginx && \ chown -R nginx:nginx /var/log/nginx && \ chown -R nginx:nginx /etc/nginx/conf.d RUN touch /var/run/nginx.pid && \ chown -R nginx:nginx /var/run/nginx.pid USER nginx CMD ["nginx", "-g", "daemon off;"]Pod 재생성
cat <<EOF | kubectl replace --force -f - apiVersion: v1 kind: Pod metadata: name: nginx spec: securityContext: runAsNonRoot: true runAsUser: 101 containers: - image: youngwjung/nginx-nonroot name: nginx ports: - containerPort: 80 volumeMounts: - name: nginx-conf mountPath: /etc/nginx/nginx.conf subPath: nginx.conf volumes: - name: nginx-conf configMap: name: nginx items: - key: nginx.conf path: nginx.conf EOFPod 상태 확인
kubectl get pod nginxPod 로그 확인
kubectl logs nginxPod 재생성
cat <<EOF | kubectl replace --force -f - apiVersion: v1 kind: ConfigMap metadata: name: nginx data: nginx.conf: | worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; server { listen 8080; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } } } --- apiVersion: v1 kind: Pod metadata: name: nginx spec: securityContext: runAsNonRoot: true runAsUser: 101 containers: - image: youngwjung/nginx-nonroot name: nginx ports: - containerPort: 80 volumeMounts: - name: nginx-conf mountPath: /etc/nginx/nginx.conf subPath: nginx.conf volumes: - name: nginx-conf configMap: name: nginx items: - key: nginx.conf path: nginx.conf EOFPod 상태 확인
kubectl get pod nginx컨테이너가 정상적으로 실행중인지 확인
kubectl exec nginx -- curl -s localhost:8080새로운 Pod 생성
cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: ubuntu spec: securityContext:dddd runAsNonRoot: true runAsUser: 101 containers: - image: ubuntu name: ubuntu command: ["sleep", "3600"] EOF컨테이너에서 프로세스를 실행중인 유저 확인
kubectl exec ubuntu -- id실행중인 프로세스 확인
kubectl exec ubuntu -- ps auxNGINX 설치 시도
kubectl exec ubuntu -- bash -c "apt update && apt install -y nginx"Pod 재생성
cat <<EOF | kubectl replace --force -f - apiVersion: v1 kind: Pod metadata: name: ubuntu spec: securityContext: runAsNonRoot: true runAsUser: 101 containers: - image: ubuntu name: ubuntu command: ["sleep", "3600"] securityContext: runAsUser: 0 EOFPod가 정상적으로 생성되었는지 확인
kubectl get pod ubuntuPod에 명시된 컨테이너가 실행되지 않는다면 그 이유를 확인
kubectl describe pod ubuntuPod 재생성
cat <<EOF | kubectl replace --force -f - apiVersion: v1 kind: Pod metadata: name: ubuntu spec: securityContext: runAsUser: 101 containers: - image: ubuntu name: ubuntu command: ["sleep", "3600"] securityContext: runAsUser: 0 EOF컨테이너에서 프로세스를 실행중인 유저 확인
kubectl exec ubuntu -- id새로운 Pod 생성
cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: alpine spec: containers: - image: alpine name: alpine command: ["sleep", "3600"] volumeMounts: - name: dev mountPath: /mnt/dev volumes: - name: dev hostPath: path: /dev EOF생성된 컨테이너를 통해 호스트의 디스크 디바이스 접근 시도
kubectl exec alpine -- head /mnt/dev/xvda컨테이너에서 프로세스를 실행중인 유저 확인
kubectl exec ubuntu -- idPod 재생성
cat <<EOF | kubectl replace --force -f - apiVersion: v1 kind: Pod metadata: name: alpine spec: containers: - image: alpine name: alpine command: ["sleep", "3600"] volumeMounts: - name: dev mountPath: /mnt/dev securityContext: privileged: true volumes: - name: dev hostPath: path: /dev EOF생성된 컨테이너를 통해 호스트의 디스크 디바이스 접근 시도
kubectl exec alpine -- head /mnt/dev/xvda리소스 삭제
{ kubectl delete pod ubuntu alpine nginx kubectl delete cm nginx }
Seccomp
Pod 생성
새로운 리눅스 네임스페이스를 만드는 명령어 실행
Pod 생성
Network Policy
Tigera Calico Operator 설치
Calico Operator 설치 확인
Calico CNI 설치 확인
모든 Calico 구성 요소들이 정상 동작중인지 확인 - 에러메시지가 없으면 정상으로 판단
리소스 생성
web Namespace에 있는 server Pod의 IP 주소 확인
red Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출
red 및 blue Namespace에 Network Policy 생성
red Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정
red Namespace에 새로운 Network Policy 생성
위에서 생성한 Network Policy 확인
red Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정
blue Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정
blue Namespace에 새로운 Network Policy 생성
위에서 생성한 Network Policy 확인
blue Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정
위에서 생성한 Network Policy 수정
위에서 수정한 Network Policy 확인
blue Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정
web Namespace에 Network Policy 생성
blue Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정
web Namespace에 새로운 Network Policy 생성 - Egress 정책
web Namespace에 새로운 Network Policy 생성 - Ingress 정책
blue Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정
blue Namespace에 새로운 Pod를 생성
위에서 생성한 Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정
리소스 삭제
Calico 삭제
Calico가 삭제되었는지 확인
한개의 Node로 Session Manager 연결
Calico CNI가 생성한 iptable 규칙들이 남아 있는지 확인
Session Manager 종료
모든 Node 삭제 - 관리형 노드그룹에 의해서 새로운 Node가 자동으로 생성됨
Node를 새로 생성할수 없는 경우에는 해당 문서를 참고 - https://github.com/projectcalico/calico/blob/master/calico/hack/remove-calico-policy/remove-policy.md
새로운 Node가 생성되었는지 확인
한개의 Node로 Session Manager 연결
Calico CNI가 생성한 iptable 규칙들이 남아 있는지 확인
Session Manager 종료
Last updated