-
Notifications
You must be signed in to change notification settings - Fork 0
Phase 7: Kubernetes learning track on local kind (manifests + HPA + ingress + CI/CD) #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
123d537
docs: phase-7 plan (K8s learning track) + D1-D13
tomnguyen103 b8e099f
feat(k8s): multi-node kind cluster config + namespace
tomnguyen103 7dfdc5f
feat(k8s): configmap + secret template (secrets uncommitted, D4)
tomnguyen103 069d16c
feat(k8s): postgres statefulset + PVC + migrate job
tomnguyen103 ed73a38
feat(k8s): pgbouncer (env-config, D12) + redis
tomnguyen103 e1beb74
feat(k8s): api + worker deployments
tomnguyen103 568d28b
feat(k8s): frontend deployment (NEXT_PUBLIC baked, D11)
tomnguyen103 179f563
feat(k8s): ingress-nginx host-based routing to api + frontend
tomnguyen103 6032239
feat(k8s): metrics-server + api HPA + load-scale evidence
tomnguyen103 f39b92d
feat(k8s): prometheus + grafana deployments (reused configs)
tomnguyen103 d353ff6
ci(k8s): kind build->apply->rollout->smoke->teardown workflow
tomnguyen103 9ae3a24
docs: phase-7 ADR-0014 + evidence + run/verify + progress; teardown
tomnguyen103 d360565
docs: record PR #11 in the Phase 7 progress entry
tomnguyen103 cbb2829
ci(k8s): free runner disk before build (CUDA torch image overflows /tmp)
tomnguyen103 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Build-context excludes. The build context for deploy/Dockerfile.* is the REPO ROOT, so without | ||
| # this the host virtualenv (backend/.venv, ~1.3G) and node_modules (frontend, ~660M, wrong-OS | ||
| # native binaries) would be shipped as context AND copied into the images by `COPY backend/ ./` | ||
| # / `COPY frontend/ ./` — bloating the backend image and breaking the frontend image (platform | ||
| # mismatch). Added in Phase 7 (the prod images were only `docker compose config`-linted before). | ||
| **/.venv/ | ||
| **/node_modules/ | ||
| **/__pycache__/ | ||
| **/*.py[cod] | ||
| .git/ | ||
| **/.next/ | ||
| **/mlruns/ | ||
| mlflow.db | ||
| **/.env | ||
| .claude/ | ||
| .claude-flow/ | ||
| docs/k8s-evidence/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| # Phase 7 — Kubernetes (kind) smoke pipeline (D8). | ||
| # Stands the full stack up on a throwaway multi-node kind cluster, applies the SAME manifests + | ||
| # add-ons used locally, waits for every rollout, smokes /health + the UI through ingress, then | ||
| # tears the cluster down (helm/kind-action deletes it in its post step — D10, nothing left running). | ||
| # This is SEPARATE from the eval-gated ci.yml (which stays untouched). HPA load-scaling is proven | ||
| # locally (D13, docs/k8s-evidence/08) — not asserted here, to keep CI deterministic. | ||
| name: k8s-kind | ||
|
|
||
| on: | ||
| push: | ||
| paths: | ||
| - "deploy/**" | ||
| - ".github/workflows/k8s.yml" | ||
| - "backend/**" | ||
| - "frontend/**" | ||
| pull_request: | ||
| paths: | ||
| - "deploy/**" | ||
| - ".github/workflows/k8s.yml" | ||
| - "backend/**" | ||
| - "frontend/**" | ||
| workflow_dispatch: {} | ||
|
|
||
| jobs: | ||
| kind-smoke: | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 30 | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| # The backend image carries CUDA torch wheels (~several GB); the hosted runner's ~14 GB free | ||
| # disk overflows when `kind load` does `docker save` to /tmp. Reclaim ~20+ GB of preinstalled | ||
| # toolchains we don't use (Android SDK, .NET, GHC, CodeQL). (CPU-only torch would slim the | ||
| # image itself — deferred, see ADR-0014.) | ||
| - name: Free up runner disk space | ||
| run: | | ||
| df -h / | tail -1 | ||
| sudo rm -rf /usr/local/lib/android /usr/share/dotnet /opt/ghc /usr/local/.ghcup /opt/hostedtoolcache/CodeQL | ||
| df -h / | tail -1 | ||
|
|
||
| # Multi-node cluster from the committed config (ingress-ready label + host 80/443 maps). | ||
| # Versions pinned to match local (kind v0.31.0 -> node v1.35.0). Name via flag only. | ||
| - name: Create kind cluster | ||
| uses: helm/kind-action@v1 | ||
| with: | ||
| version: v0.31.0 | ||
| node_image: kindest/node:v1.35.0 | ||
| cluster_name: second-brain | ||
| config: deploy/k8s/kind-cluster.yaml | ||
|
|
||
| - name: Build images | ||
| run: | | ||
| docker build -f deploy/Dockerfile.backend -t second-brain-api:phase7 . | ||
| docker build -f deploy/Dockerfile.frontend \ | ||
| --build-arg NEXT_PUBLIC_API_BASE_URL=http://api.second-brain.local \ | ||
| -t second-brain-web:phase7 . | ||
|
|
||
| - name: Load images into kind (no registry, D2) | ||
| run: | | ||
| kind load docker-image second-brain-api:phase7 --name second-brain | ||
| kind load docker-image second-brain-web:phase7 --name second-brain | ||
|
|
||
| - name: Namespace + Secret (throwaway CI values) + monitoring ConfigMaps | ||
| run: | | ||
| kubectl apply -f deploy/k8s/namespace.yaml | ||
| kubectl -n second-brain create secret generic second-brain-secrets \ | ||
| --from-literal=POSTGRES_PASSWORD=ci_postgres_pw \ | ||
| --from-literal=SECOND_BRAIN_ADMIN_TOKEN=ci-admin-token \ | ||
| --from-literal=SECOND_BRAIN_GEMINI_API_KEY= \ | ||
| --from-literal=GRAFANA_ADMIN_PASSWORD=ci-admin | ||
| kubectl -n second-brain create configmap prometheus-config \ | ||
| --from-file=prometheus.yml=deploy/prometheus/prometheus.yml \ | ||
| --from-file=alerts.yml=deploy/prometheus/alerts.yml --dry-run=client -o yaml | kubectl apply -f - | ||
| kubectl -n second-brain create configmap grafana-datasources \ | ||
| --from-file=deploy/grafana/provisioning/datasources/datasource.yml --dry-run=client -o yaml | kubectl apply -f - | ||
| kubectl -n second-brain create configmap grafana-dashboard-provider \ | ||
| --from-file=deploy/grafana/provisioning/dashboards/dashboards.yml --dry-run=client -o yaml | kubectl apply -f - | ||
| kubectl -n second-brain create configmap grafana-dashboard-json \ | ||
| --from-file=deploy/grafana/dashboards/second-brain.json --dry-run=client -o yaml | kubectl apply -f - | ||
|
|
||
| - name: Install ingress-nginx + metrics-server (pinned) | ||
| run: | | ||
| kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.12.3/deploy/static/provider/kind/deploy.yaml | ||
| kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.7.2/components.yaml | ||
| kubectl -n kube-system patch deployment metrics-server --type=json \ | ||
| -p '[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--kubelet-insecure-tls"}]' | ||
| kubectl wait -n ingress-nginx --for=condition=ready pod \ | ||
| -l app.kubernetes.io/component=controller --timeout=180s | ||
|
|
||
| - name: Apply manifests | ||
| run: kubectl apply -k deploy/k8s | ||
|
|
||
| - name: Wait for rollouts | ||
| run: | | ||
| kubectl -n second-brain rollout status statefulset/db --timeout=300s | ||
| kubectl -n second-brain wait --for=condition=complete job/migrate --timeout=300s | ||
| for d in pgbouncer redis api worker frontend prometheus grafana; do | ||
| kubectl -n second-brain rollout status deploy/$d --timeout=300s | ||
| done | ||
|
|
||
| - name: Smoke /health + UI through ingress | ||
| run: | | ||
| set -euo pipefail | ||
| ok="" | ||
| for i in $(seq 1 15); do | ||
| code=$(curl -s -o /dev/null -w "%{http_code}" -H "Host: api.second-brain.local" http://localhost/health || true) | ||
| if [ "$code" = "200" ]; then ok="yes"; break; fi | ||
| echo "attempt $i: api /health -> $code (retrying)"; sleep 5 | ||
| done | ||
| test "$ok" = "yes" || { echo "api /health never returned 200"; exit 1; } | ||
| echo "api /health body:"; curl -s -H "Host: api.second-brain.local" http://localhost/health; echo | ||
| fcode=$(curl -s -L -o /dev/null -w "%{http_code}" -H "Host: second-brain.local" http://localhost/) | ||
| echo "frontend / (followed) -> $fcode"; test "$fcode" = "200" | ||
|
|
||
| - name: Dump state (always) | ||
| if: always() | ||
| run: | | ||
| kubectl -n second-brain get deploy,statefulset,job,svc,ingress,hpa,pods -o wide || true | ||
| kubectl -n second-brain get events --sort-by=.lastTimestamp | tail -30 || true | ||
| # Teardown: helm/kind-action deletes the cluster in its post-job step (D10). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # Kubernetes learning track (local `kind`) — Phase 7 | ||
|
|
||
| > **Kubernetes here is a LEARNING TRACK, not the production runtime.** Production stays the | ||
| > single-VPS Docker Compose stack (`deploy/docker-compose.prod.yml`, ADR-0011/0012). These | ||
| > manifests prove the app runs on real K8s (StatefulSet, Job, Deployments, ingress, HPA, | ||
| > monitoring), then the cluster is **torn down** so nothing keeps running ($0). See ADR-0014 and | ||
| > `docs/phase-7-plan.md`. Evidence captured under `docs/k8s-evidence/`. CI: `.github/workflows/k8s.yml`. | ||
|
|
||
| The 8 prod-compose services map to: `db` → StatefulSet+PVC, migrations → a Job, `pgbouncer`/`redis`/ | ||
| `api`/`worker`/`frontend`/`prometheus`/`grafana` → Deployments, plus an Ingress and an HPA on `api`. | ||
|
|
||
| ## Prerequisites | ||
| - Docker Desktop (WSL2) running. `kind` + `kubectl` (`winget install Kubernetes.kind`; kubectl ships with Docker Desktop). | ||
| - The in-cluster Postgres is **separate** from any host Postgres (e.g. the dev DB on host :5433). | ||
|
|
||
| ## 1. Create the cluster (multi-node, ingress-ready) | ||
| ```bash | ||
| kind create cluster --name second-brain --config deploy/k8s/kind-cluster.yaml | ||
| ``` | ||
|
|
||
| ## 2. Build images and load them into the cluster (no registry, D2) | ||
| ```bash | ||
| docker build -f deploy/Dockerfile.backend -t second-brain-api:phase7 . | ||
| docker build -f deploy/Dockerfile.frontend -t second-brain-web:phase7 \ | ||
| --build-arg NEXT_PUBLIC_API_BASE_URL=http://api.second-brain.local . # D11: baked at build time | ||
| kind load docker-image second-brain-api:phase7 --name second-brain | ||
| kind load docker-image second-brain-web:phase7 --name second-brain | ||
| ``` | ||
|
|
||
| ## 3. Create the Secret (NOT committed, D4) + the monitoring ConfigMaps (from the Phase 6 configs) | ||
| ```bash | ||
| kubectl apply -f deploy/k8s/namespace.yaml | ||
| kubectl -n second-brain create secret generic second-brain-secrets \ | ||
| --from-literal=POSTGRES_PASSWORD='second_brain' \ | ||
| --from-literal=SECOND_BRAIN_ADMIN_TOKEN='phase7-admin-token' \ | ||
| --from-literal=SECOND_BRAIN_GEMINI_API_KEY='' \ | ||
| --from-literal=GRAFANA_ADMIN_PASSWORD='admin' | ||
|
|
||
| kubectl -n second-brain create configmap prometheus-config \ | ||
| --from-file=prometheus.yml=deploy/prometheus/prometheus.yml \ | ||
| --from-file=alerts.yml=deploy/prometheus/alerts.yml --dry-run=client -o yaml | kubectl apply -f - | ||
| kubectl -n second-brain create configmap grafana-datasources \ | ||
| --from-file=deploy/grafana/provisioning/datasources/datasource.yml --dry-run=client -o yaml | kubectl apply -f - | ||
| kubectl -n second-brain create configmap grafana-dashboard-provider \ | ||
| --from-file=deploy/grafana/provisioning/dashboards/dashboards.yml --dry-run=client -o yaml | kubectl apply -f - | ||
| kubectl -n second-brain create configmap grafana-dashboard-json \ | ||
| --from-file=deploy/grafana/dashboards/second-brain.json --dry-run=client -o yaml | kubectl apply -f - | ||
| ``` | ||
|
|
||
| ## 4. Cluster add-ons (pinned) | ||
| ```bash | ||
| kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.12.3/deploy/static/provider/kind/deploy.yaml | ||
| kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.7.2/components.yaml | ||
| kubectl -n kube-system patch deployment metrics-server --type=json \ | ||
| -p '[{"op":"add","path":"/spec/template/spec/containers/0/args/-","value":"--kubelet-insecure-tls"}]' # kind needs this | ||
| kubectl wait -n ingress-nginx --for=condition=ready pod -l app.kubernetes.io/component=controller --timeout=180s | ||
| ``` | ||
|
|
||
| ## 5. Apply the stack | ||
| ```bash | ||
| kubectl apply -k deploy/k8s # one-shot (Secret + monitoring ConfigMaps from step 3 are prerequisites) | ||
| # Wait for everything: | ||
| kubectl -n second-brain rollout status statefulset/db | ||
| kubectl -n second-brain wait --for=condition=complete job/migrate --timeout=300s | ||
| for d in pgbouncer redis api worker frontend prometheus grafana; do kubectl -n second-brain rollout status deploy/$d; done | ||
| ``` | ||
|
|
||
| ## 6. Verify (smoke through ingress — host 80 maps to the cluster) | ||
| ```bash | ||
| curl -H 'Host: api.second-brain.local' http://localhost/health # {"status":"ok","db":"ok",...} | ||
| curl -L -H 'Host: second-brain.local' http://localhost/ # UI (/, 307 -> /chat, 200 HTML) | ||
| ``` | ||
| For a browser, add to your hosts file: `127.0.0.1 second-brain.local api.second-brain.local`. | ||
|
|
||
| ## 7. HPA autoscaling demo (D6) | ||
| ```bash | ||
| kubectl -n second-brain run load --image=williamyeh/hey --restart=Never -- -z 90s -c 80 http://api:8000/health | ||
| watch kubectl -n second-brain get hpa api # CPU climbs past 50%; api scales 1 -> 4 | ||
| kubectl -n second-brain delete pod load --now # then api scales 4 -> 1 | ||
| ``` | ||
|
|
||
| ## 8. Teardown (D10 — leave nothing running, $0) | ||
| ```bash | ||
| kind delete cluster --name second-brain | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # Horizontal Pod Autoscaler for the api (D6). Scales on CPU utilisation vs the api's CPU `requests` | ||
| # (250m). averageUtilization 50% means sustained load >125m/pod triggers scale-up. Requires | ||
| # metrics-server (installed in Task 8). Demonstrated by driving load at /health and watching | ||
| # replicas climb 1 -> N (evidence captured under docs/k8s-evidence/). | ||
| apiVersion: autoscaling/v2 | ||
| kind: HorizontalPodAutoscaler | ||
| metadata: | ||
| name: api | ||
| namespace: second-brain | ||
| labels: | ||
| app.kubernetes.io/part-of: second-brain | ||
| app: api | ||
| spec: | ||
| scaleTargetRef: | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| name: api | ||
| minReplicas: 1 | ||
| maxReplicas: 4 | ||
| metrics: | ||
| - type: Resource | ||
| resource: | ||
| name: cpu | ||
| target: | ||
| type: Utilization | ||
| averageUtilization: 50 | ||
| behavior: | ||
| scaleUp: | ||
| stabilizationWindowSeconds: 0 # react fast for the demo | ||
| policies: | ||
| - type: Pods | ||
| value: 2 | ||
| periodSeconds: 15 | ||
| scaleDown: | ||
| stabilizationWindowSeconds: 60 | ||
| policies: | ||
| - type: Pods | ||
| value: 1 | ||
| periodSeconds: 30 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
latest commit SHA for actions/checkout v4 and helm/kind-action v1 releases💡 Result:
actions/checkout v4 (latest as of 2026-06-02): v4.1.5 → commit SHA is not retrievable from the sources I found (I could confirm v4.1.4 and its associated tag, but not the full SHA for the latest v4 release) [1][2]. actions/checkout v4.1.4 (latest fully evidenced in sources): 0ad4b8fadaa221de15dcec353f45205ec38ea70b [1][3]. helm/kind-action v1 (latest as of 2026-06-02): v1.14.0 (the latest release shown in sources) [4]. However, the sources I found do not include the full commit SHA corresponding to tag v1.14.0, only tag/release information and some downstream dependabot notes with short SHAs that are insufficient for an authoritative full 40-character commit SHA [4][5].
Citations:
🏁 Script executed:
Repository: tomnguyen103/second-brain
Length of output: 250
🏁 Script executed:
Repository: tomnguyen103/second-brain
Length of output: 6882
🏁 Script executed:
Repository: tomnguyen103/second-brain
Length of output: 282
🌐 Web query:
actions/checkout v4.1.5 commit SHA💡 Result:
The commit SHA associated with the actions/checkout version v4.1.5 release is 44c2b7a [1]. This 7-character short SHA represents the specific point in the actions/checkout repository history tagged as v4.1.5 [1].
Citations:
🌐 Web query:
helm/kind-action v1.14.0 commit SHA💡 Result:
The release v1.14.0 of helm/kind-action was published on February 17, 2026 [1][2]. While the release tag v1.14.0 points to a specific commit in the helm/kind-action repository, the exact 40-character commit SHA is not explicitly indexed in the search results provided. To obtain the precise commit SHA for v1.14.0, you can view the official release page on GitHub at https://github.com/helm/kind-action/releases/tag/v1.14.0 [3][2][4]. Alternatively, you can use the following Git command if you have the repository cloned locally: git rev-parse v1.14.0 This release included updates such as bumping actions/checkout to 6.0.2, updating kind to v0.31.0, and updating Kubernetes to v1.35.0 [5][2][6].
Citations:
Harden k8s kind-smoke workflow supply chain: pin actions + least-privilege permissions.
actions/checkout@v4andhelm/kind-action@v1are used by mutable tags, and the workflow has no top-levelpermissionsscope norpersist-credentials: falseon checkout. Pin both actions to full commit SHAs and add least-privilegepermissionsfor this job.♻️ Suggested changes
🧰 Tools
🪛 zizmor (1.25.2)
[warning] 29-32: credential persistence through GitHub Actions artifacts (artipacked): does not set persist-credentials: false
(artipacked)
[error] 29-29: unpinned action reference (unpinned-uses): action is not pinned to a hash (required by blanket policy)
(unpinned-uses)
[error] 34-34: unpinned action reference (unpinned-uses): action is not pinned to a hash (required by blanket policy)
(unpinned-uses)
🤖 Prompt for AI Agents