diff --git a/deploy/production/README.md b/deploy/production/README.md index 29b5bbf..4935f60 100644 --- a/deploy/production/README.md +++ b/deploy/production/README.md @@ -26,9 +26,8 @@ Git 밖에서 만든다. 레지스트리에서 다시 받는다. - 세 파드 모두 `priorityClassName: platform-critical`(platform-core `services/priority-classes`)이고 seccomp는 `RuntimeDefault`다. -- **frontend non-root는 남은 일이다.** nginx 공식 이미지는 root로 시작해 80번을 열고 worker를 - `nginx`로 내린다. 권한을 버리거나 non-root로 돌리려면 이미지를 비특권 nginx(8080)로 바꾸고 Service - `targetPort`와 probe를 같이 바꿔야 한다. +- frontend non-root는 `frontend-nonroot/` component로 둔다(아래). 지금 운영 이미지 `7a4e201`은 root로 + 80번을 열기 때문에 아직 켜지 않는다. ## 적용 @@ -124,3 +123,35 @@ curl -s -o /dev/null -w '%{http_code}\n' https://hyeonworks.com/api/v1/public/ho 되돌리기: `kubectl -n hyeonworks-prod rollout undo deploy/techlog-backend`. 그다음 `components`를 빼는 커밋을 만든다. ConfigMap `techlog-backend-prod-env`는 남아 있어도 해가 없다. + +## frontend non-root (`frontend-nonroot/`, 아직 켜지 않음) + +이 repo의 `Dockerfile`은 runtime을 `nginxinc/nginx-unprivileged:1.27.5-alpine`(예전 base와 같은 NGINX 판·Alpine)으로 +만든다. master가 uid 101로 돌고 8080을 연다(`scripts/generate-nginx-config.ts`). `frontend-nonroot/` +component는 그 이미지에 맞춰 다음을 바꾼다. + +| 객체 | 바뀌는 것 | +| --- | --- | +| Deployment `techlog-frontend` | `containerPort` 8080, liveness·readiness probe 8080, `runAsNonRoot`·uid/gid 101, `drop: [ALL]` | +| Service `techlog-frontend` | `targetPort` 80 → 8080(`port`는 80 그대로라 Ingress는 안 바뀐다) | +| NetworkPolicy `techlog-frontend-ingress` | Traefik → pod 포트 80 → 8080 | + +### 켜는 순서 + +1. 이 Dockerfile이 들어간 commit에서 frontend 이미지를 만든다. **운영 `7a4e201`에만 있는 커밋(로컬 + 브랜치 `chore/template-sync-v0.2.0`, main보다 4개 앞)이 그 commit에 들어 있어야 한다.** 빠지면 그 + 4개(다크 모드 글씨, 2KB 넘는 본문 저장, 제목 문구, 템플릿 동기화)가 운영에서 되돌아간다. +2. 그 이미지를 레지스트리에 push한다(위 「적용」의 전제 1과 같은 방법). +3. 한 커밋에서 `frontend-deployment.yaml`의 이미지 태그를 새 태그로 바꾸고, `kustomization.yaml`에 + `components: [frontend-nonroot]`를 더한다(`prod-profile`이 켜져 있으면 목록에 같이 둔다). +4. 적용과 확인: + + ```bash + kubectl diff -k deploy/production + kubectl apply -k deploy/production + kubectl -n hyeonworks-prod rollout status deploy/techlog-frontend + curl -s -o /dev/null -w '%{http_code}\n' https://hyeonworks.com/config.json + ``` + +되돌리기: `kubectl -n hyeonworks-prod rollout undo deploy/techlog-frontend`. 그다음 3의 커밋을 되돌리는 +커밋을 만들고 다시 적용한다(Service와 NetworkPolicy 포트도 80으로 돌아가야 한다). diff --git a/deploy/production/frontend-nonroot/frontend-deployment-patch.yaml b/deploy/production/frontend-nonroot/frontend-deployment-patch.yaml new file mode 100644 index 0000000..9f10c17 --- /dev/null +++ b/deploy/production/frontend-nonroot/frontend-deployment-patch.yaml @@ -0,0 +1,35 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: techlog-frontend +spec: + template: + spec: + securityContext: + runAsNonRoot: true + runAsUser: 101 + runAsGroup: 101 + seccompProfile: + type: RuntimeDefault + containers: + - name: frontend + # replace, not merge: ports merge on containerPort, which would + # keep the old 80 entry next to 8080 under the same name. + ports: + - $patch: replace + - name: http + containerPort: 8080 + protocol: TCP + securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - ALL + livenessProbe: + httpGet: + path: /config.json + port: 8080 + readinessProbe: + httpGet: + path: /config.json + port: 8080 diff --git a/deploy/production/frontend-nonroot/frontend-network-policy-patch.yaml b/deploy/production/frontend-nonroot/frontend-network-policy-patch.yaml new file mode 100644 index 0000000..08111e0 --- /dev/null +++ b/deploy/production/frontend-nonroot/frontend-network-policy-patch.yaml @@ -0,0 +1,20 @@ +# NetworkPolicy ports are pod ports: Traefik now reaches the pod on 8080. +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: techlog-frontend-ingress +spec: + podSelector: + matchLabels: + app: techlog-frontend + ingress: + - from: + - namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: kube-system + podSelector: + matchLabels: + app.kubernetes.io/name: traefik + ports: + - protocol: TCP + port: 8080 diff --git a/deploy/production/frontend-nonroot/frontend-service-patch.yaml b/deploy/production/frontend-nonroot/frontend-service-patch.yaml new file mode 100644 index 0000000..ab3c0b2 --- /dev/null +++ b/deploy/production/frontend-nonroot/frontend-service-patch.yaml @@ -0,0 +1,12 @@ +# The Service keeps port 80, so the Ingress and anything that calls +# techlog-frontend:80 do not change; only the pod side moves to 8080. +apiVersion: v1 +kind: Service +metadata: + name: techlog-frontend +spec: + ports: + - name: http + port: 80 + protocol: TCP + targetPort: 8080 diff --git a/deploy/production/frontend-nonroot/kustomization.yaml b/deploy/production/frontend-nonroot/kustomization.yaml new file mode 100644 index 0000000..80e3408 --- /dev/null +++ b/deploy/production/frontend-nonroot/kustomization.yaml @@ -0,0 +1,15 @@ +apiVersion: kustomize.config.k8s.io/v1alpha1 +kind: Component + +# Opt-in: runs techlog-frontend as uid 101 on port 8080 with every capability +# dropped. Needs an image built from a commit that has the unprivileged +# Dockerfile; the 7a4e201 image listens on 80 as root and would fail every +# probe. Enable it in the same commit that moves the frontend image tag, by +# adding +# components: +# - frontend-nonroot +# to ../kustomization.yaml. See ../README.md ("frontend non-root"). +patches: + - path: frontend-deployment-patch.yaml + - path: frontend-service-patch.yaml + - path: frontend-network-policy-patch.yaml