실습
Security Context
cat <<EOF | kubectl apply -f - apiVersion: v1 kind: Pod metadata: name: nginx spec: containers: - image: nginx name: nginx EOFkubectl exec nginx -- idkubectl exec nginx -- bash -c "apt update && apt install -y procps"kubectl exec nginx -- ps auxkubectl exec nginx -- id nginxcat <<EOF | kubectl replace --force -f - apiVersion: v1 kind: Pod metadata: name: nginx spec: securityContext: runAsNonRoot: true containers: - image: nginx name: nginx EOFkubectl get pod nginxkubectl describe pod nginxcat <<EOF | kubectl replace --force -f - apiVersion: v1 kind: Pod metadata: name: nginx spec: securityContext: runAsNonRoot: true runAsUser: 101 containers: - image: nginx name: nginx EOFkubectl get pod nginxkubectl describe pod nginxkubectl logs nginxkubectl run nginx-tmp --image=nginx --rm -it --restart=Never \ -- cat /etc/nginx/nginx.confcat <<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 EOFkubectl get pod nginxkubectl describe pod nginxkubectl logs nginxFROM 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;"]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 EOFkubectl get pod nginxkubectl logs nginxcat <<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 EOFkubectl get pod nginxkubectl exec nginx -- curl -s localhost:8080cat <<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"] EOFkubectl exec ubuntu -- idkubectl exec ubuntu -- ps auxkubectl exec ubuntu -- bash -c "apt update && apt install -y nginx"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 EOFkubectl get pod ubuntukubectl describe pod ubuntucat <<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 EOFkubectl exec ubuntu -- idcat <<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 EOFkubectl exec alpine -- head /mnt/dev/xvdakubectl exec ubuntu -- idcat <<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 EOFkubectl exec alpine -- head /mnt/dev/xvda{ kubectl delete pod ubuntu alpine nginx kubectl delete cm nginx }
Seccomp
Network Policy
Last updated