실습

Security Context

  1. Pod 생성

    cat <<EOF | kubectl apply -f -
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
    EOF
  2. 컨테이너에서 프로세스를 실행중인 유저 확인

    kubectl exec nginx -- id 
  3. 프로세스 유틸리티 설치

    kubectl exec nginx -- bash -c "apt update && apt install -y procps"
  4. 실행중인 프로세스 확인

    kubectl exec nginx -- ps aux
  5. nginx 유저 확인

    kubectl exec nginx -- id nginx
  6. Pod 재생성

    cat <<EOF | kubectl replace --force -f -
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
    spec:
      securityContext:
        runAsNonRoot: true
      containers:
      - image: nginx
        name: nginx
    EOF
  7. Pod가 생성되었는지 확인

    kubectl get pod nginx
  8. Pod에 명시된 컨테이너가 실행 되지 않는다면 그 이유를 확인

    kubectl describe pod nginx
  9. Pod 재생성

    cat <<EOF | kubectl replace --force -f -
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx
    spec:
      securityContext:
        runAsNonRoot: true
        runAsUser: 101
      containers:
      - image: nginx
        name: nginx
    EOF
  10. Pod가 생성되었는지 확인

    kubectl get pod nginx
  11. Pod에 명시된 컨테이너가 실행 되지 않는다면 그 이유를 확인

    kubectl describe pod nginx
  12. Pod 로그 확인

    kubectl logs nginx
  13. NGINX 설정 파일 확인

    kubectl run nginx-tmp --image=nginx --rm -it --restart=Never \
    -- cat /etc/nginx/nginx.conf
  14. Pod 재생성

    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
    EOF
  15. Pod가 생성되었는지 확인

    kubectl get pod nginx
  16. Pod에 명시된 컨테이너가 실행 되지 않는다면 그 이유를 확인

    kubectl describe pod nginx
  17. Pod 로그 확인

    kubectl logs nginx
  18. 아래의 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;"]
  19. 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
    EOF
  20. Pod 상태 확인

    kubectl get pod nginx
  21. Pod 로그 확인

    kubectl logs nginx
  22. Pod 재생성

    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
    EOF
  23. Pod 상태 확인

    kubectl get pod nginx
  24. 컨테이너가 정상적으로 실행중인지 확인

    kubectl exec nginx -- curl -s localhost:8080
  25. 새로운 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
  26. 컨테이너에서 프로세스를 실행중인 유저 확인

    kubectl exec ubuntu -- id 
  27. 실행중인 프로세스 확인

    kubectl exec ubuntu -- ps aux
  28. NGINX 설치 시도

    kubectl exec ubuntu -- bash -c "apt update && apt install -y nginx"
  29. 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
    EOF
  30. Pod가 정상적으로 생성되었는지 확인

    kubectl get pod ubuntu
  31. Pod에 명시된 컨테이너가 실행되지 않는다면 그 이유를 확인

    kubectl describe pod ubuntu
  32. Pod 재생성

    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
  33. 컨테이너에서 프로세스를 실행중인 유저 확인

    kubectl exec ubuntu -- id 
  34. 새로운 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
  35. 생성된 컨테이너를 통해 호스트의 디스크 디바이스 접근 시도

    kubectl exec alpine -- head /mnt/dev/xvda
  36. 컨테이너에서 프로세스를 실행중인 유저 확인

    kubectl exec ubuntu -- id 
  37. Pod 재생성

    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
  38. 생성된 컨테이너를 통해 호스트의 디스크 디바이스 접근 시도

    kubectl exec alpine -- head /mnt/dev/xvda
  39. 리소스 삭제

    {
        kubectl delete pod ubuntu alpine nginx
        kubectl delete cm nginx
    }

Seccomp

  1. Pod 생성

  2. 새로운 리눅스 네임스페이스를 만드는 명령어 실행

  3. Pod 생성

Network Policy

  1. Tigera Calico Operator 설치

  2. Calico Operator 설치 확인

  3. Calico CNI 설치 확인

  4. 모든 Calico 구성 요소들이 정상 동작중인지 확인 - 에러메시지가 없으면 정상으로 판단

  5. 리소스 생성

  6. web Namespace에 있는 server Pod의 IP 주소 확인

  7. red Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출

  8. redblue Namespace에 Network Policy 생성

  9. red Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정

  10. red Namespace에 새로운 Network Policy 생성

  11. 위에서 생성한 Network Policy 확인

  12. red Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정

  13. blue Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정

  14. blue Namespace에 새로운 Network Policy 생성

  15. 위에서 생성한 Network Policy 확인

  16. blue Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정

  17. 위에서 생성한 Network Policy 수정

  18. 위에서 수정한 Network Policy 확인

  19. blue Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정

  20. web Namespace에 Network Policy 생성

  21. blue Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정

  22. web Namespace에 새로운 Network Policy 생성 - Egress 정책

  23. web Namespace에 새로운 Network Policy 생성 - Ingress 정책

  24. blue Namespace에 있는 client Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정

  25. blue Namespace에 새로운 Pod를 생성

  26. 위에서 생성한 Pod에서 web Namespace에 있는 server Pod 호출 - timeout을 1초로 설정

  27. 리소스 삭제

  28. Calico 삭제

  29. Calico가 삭제되었는지 확인

  30. 한개의 Node로 Session Manager 연결

  31. Calico CNI가 생성한 iptable 규칙들이 남아 있는지 확인

  32. Session Manager 종료

  33. 모든 Node 삭제 - 관리형 노드그룹에 의해서 새로운 Node가 자동으로 생성됨

  34. Node를 새로 생성할수 없는 경우에는 해당 문서를 참고 - https://github.com/projectcalico/calico/blob/master/calico/hack/remove-calico-policy/remove-policy.md

  35. 새로운 Node가 생성되었는지 확인

  36. 한개의 Node로 Session Manager 연결

  37. Calico CNI가 생성한 iptable 규칙들이 남아 있는지 확인

  38. Session Manager 종료

Last updated