Files
co-lite/openspec/changes/add-k8s-deployment/design.md
T
ngqileandClaude Opus 5.5 c7bc7a8dcb feat(deploy): Kubernetes manifests, secret and release scripts, and a manifest check
deploy/k8s holds a Kustomize base (a Deployment and Service for each of the ten
images, with the probes, settings and mounts from docs/deployment.md) and a prod
overlay that pins every image to one commit SHA and routes only the admin portal
and mobile-api's exact WhatsApp webhook path through Traefik with TLS.

Secrets never enter git: deploy/k8s/secrets.example holds placeholders, the filled
copy in deploy/k8s/secrets is ignored, and deploy/create-secrets.sh loads it
without printing a value. deploy/set-release.sh pins or rolls back the release.

deploy/check.sh (also a CI step) renders the overlay in one pinned container,
validates it with kubeconform and refuses public routes to internal services,
latest or untagged images, Secret data or literal secret env values, mixed SHAs
and a second or rolling service-counter; --release also refuses unfilled cluster
facts. --self-test proves each rule fires on its fixture.

OpenSpec change: add-k8s-deployment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
2026-12-03 19:03:28 +08:00

8.0 KiB

Design

Context

  • Images: CI publishes ten images to gitea.containerverse.dev/solvway/, each tagged with the commit SHA and with latest.
  • What docs/deployment.md already fixes:
    • the Service names and ports;
    • the probes;
    • every environment variable, and whether it is secret or plain;
    • the two mounted files (appkeys.json for the gateway, the Pub/Sub key.json for the other Java images);
    • the setup order;
    • the Traefik routes (portal on co-lite.containerverse.dev, plus the webhook path).
  • Outside the cluster manifests:
    • MySQL, prepared by the DBA;
    • Redis ("use the existing Redis");
    • Google Pub/Sub.
  • Tools: the repository has no Kubernetes files, and this machine has Docker but no kubectl.

Goals / Non-Goals

Goals:

  • One kubectl apply -k deploy/k8s/overlays/prod deploys the whole system once the Secrets exist.
  • A check that fails on the mistakes that would hurt in production:
    • a public route to an internal service;
    • an image on latest;
    • secret data in git;
    • two service-counter replicas.

Non-Goals:

  • Helm charts, GitOps tooling (Argo CD or Flux), or automatic deploys from CI. A person applies each release.
  • Running MySQL, Redis or a Pub/Sub emulator in the cluster.
  • A local Kubernetes preview. The owner previews the system with the docker-compose stack.
  • Autoscaling, resource tuning beyond sensible requests, and NetworkPolicies. These can follow once the cluster's policy is known.

Decisions

1. Kustomize, with a base and a prod overlay

  • Choice: plain YAML under deploy/k8s/base/, plus deploy/k8s/overlays/prod/ for the image SHA, the plain settings, the namespace and the routes.
  • Why:
    • Kustomize is built into kubectl, so the deployer installs nothing.
    • The YAML reads like the handover doc.
  • Rejected:
    • Helm: templating for one environment adds code, and Helm would need installing.
    • Raw YAML without overlays: the SHA would then be edited in ten files.

2. The image SHA is set in one place

  • How: the overlay's images: list sets newTag for all ten images. A small deploy/set-release.sh <sha> rewrites those ten lines.
  • Rollback: run set-release.sh <previous sha> and apply again. Every release is a commit on main, so the SHA in git history is the record of what ran.

3. Secrets are created by a script from git-ignored files, not stored as manifests

  • Templates: deploy/k8s/secrets.example/ holds one .env template per Secret (from docs/deployment.md section 9), plus an appkeys.json template.
  • Filling them in: the deployer copies the folder to deploy/k8s/secrets/, which git ignores, and fills it in.
  • Creating them: deploy/create-secrets.sh runs kubectl create secret generic --from-env-file / --from-file ... --dry-run=client -o yaml | kubectl apply -f -. It never prints a value.
  • Rejected:
    • Sealed Secrets or SOPS: new tooling and keys to manage, for a two-developer team. They can come later.
    • A Kustomize secretGenerator: it would put the values into the rendered output, which the check must stay able to forbid.

4. Traefik IngressRoute for the public routes

  • What: the prod overlay holds two IngressRoutes on co-lite.containerverse.dev:
    • / to admin-portal:3000;
    • Path(/api/public/whatsapp/webhook) to mobile-api:80.
  • Why: the handover doc already states the routes as Traefik rules, and the cluster runs Traefik.
  • TLS: a tls: block whose cert resolver (or cert-manager secret) name is a single field in the overlay (see Open Questions).
  • Rejected: a standard Ingress with Traefik annotations. Exact-path matching is less clear there, and nothing else in the cluster needs portability.

5. The check runs from one pinned container

  • What: deploy/check.sh sends the deploy/ folder through stdin into alpine/k8s:1.33.4, pinned by digest. That one image has kubectl and kustomize, kubeconform, and python3 with PyYAML.
  • Why stdin: it also works against CI's remote Docker daemon, where bind mounts would not reach the files.
  • Steps:
    1. Kustomize renders the overlay.
    2. kubeconform validates the result. The Traefik schemas come from the CRDs catalog.
    3. check_rules.py applies the rules.
  • Modes:
    • --self-test runs every rule against its fixture in deploy/check-fixtures/.
    • --release also refuses any <…> placeholder. CI runs the plain check, because the cluster facts are still placeholders in git; the deployer runs --release before applying.
  • Rejected: separate kubectl, kubeconform and Python images. That is three pulls and three pins for the same result.

6. Pod settings

  • Common to all pods:
    • one replica each;
    • TZ left to the image;
    • GCP_CREDENTIAL_FILE and the key.json mount on every Java pod except api-gateway and service-whatsapp;
    • imagePullSecrets set to one named pull secret.
  • service-counter: strategy: Recreate, so a rollout never runs two copies at once and the daily summary is never sent twice.
  • Probes: the startup probe uses /actuator/health/liveness and allows up to 5 minutes. The overall /actuator/health is 503 on service-email while no mail server is configured.
    • mobile-api does not turn its probe groups on by itself, so the settings set MANAGEMENT_HEALTH_PROBES_ENABLED=true for every Java pod.
    • Both facts were measured on the local containers.
  • WhatsApp Secret: referenced with optional: true, so mobile-api and service-whatsapp start while WhatsApp is off.
  • Image tags: quoted in the overlay. A SHA made only of digits, or shaped like 1e45…, would otherwise be read as a number.
  • Requests: modest (100m CPU and 512Mi for the Java pods; 256Mi for the portal), with no CPU limit. JAVA_OPTS=-Xmx384m keeps the heap inside the memory limit.
  • Security context: the Java images run as root (documented). The manifests do not set runAsNonRoot. If the cluster enforces it, that is a finding for the image, not a manifest workaround.

Risks / Trade-offs

  • [A wrong cluster fact breaks the first apply] (CRD version, pull-secret name, TLS method). Mitigation: those facts are single overlay fields, listed under Open Questions, and the first apply is done together with the owner.
  • [Secrets created by hand drift from the templates] Mitigation: create-secrets.sh refuses to run if any key in a template is missing from the filled file.
  • [No automatic deploy means releases lag main] This is accepted for go-live. Every release is a deliberate act with a recorded SHA.
  • [The check uses an image pulled from Docker Hub] Mitigation: it is pinned by version and digest.
  • [kubeconform fetches the Traefik schema from GitHub] Without network access the check fails rather than skipping. CI already needs the network to pull images.

Migration Plan

  1. The DBA prepares MySQL, and the Pub/Sub topics and key are created (handover sections 3 and 5).
  2. Fill in deploy/k8s/secrets/ and run deploy/create-secrets.sh. Create the registry pull secret.
  3. Run deploy/set-release.sh <sha> and deploy/check.sh, then kubectl apply -k deploy/k8s/overlays/prod.
  4. Wait for every Deployment to roll out (kubectl rollout status), then sign in as the first admin.
  5. Rollback: run set-release.sh <previous sha>, apply again, and wait for the rollout. Database changes are forward-only. A release that ran new SQL rolls back only to a SHA that works with that schema; the SQL files say which ones are safe.

Open Questions

These are single overlay fields. They are needed before the first real apply, not to build or check the manifests.

  • Namespace: the default is co-lite.
  • TLS: the Traefik cert resolver name (for example letsencrypt), or a cert-manager ClusterIssuer.
  • Pull secret: its name for gitea.containerverse.dev, if the cluster already has one.
  • Redis: the address of "the existing Redis".
  • Public-pages hostname: the separate one for /r, /i, /c and /p (handover section 8), if it should exist at go-live.
  • Access: who has kubectl access to the cluster for the first apply.