From acd6483019fd8698177c61eaf5169a3303d4e768 Mon Sep 17 00:00:00 2001 From: ngqile Date: Mon, 30 Nov 2026 10:16:27 +0800 Subject: [PATCH] docs: deployment handover for the Kubernetes deployer Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM --- docs/deployment.md | 259 ++++++++++++++++++ .../changes/add-release-ci-packaging/tasks.md | 2 +- 2 files changed, 260 insertions(+), 1 deletion(-) create mode 100644 docs/deployment.md diff --git a/docs/deployment.md b/docs/deployment.md new file mode 100644 index 0000000..ff52f8e --- /dev/null +++ b/docs/deployment.md @@ -0,0 +1,259 @@ +# Co-Lite Deployment Handover + +This is for whoever deploys Co-Lite to the Kubernetes cluster. CI builds and publishes the images. The deployer writes the manifests and rolls them out. The database package goes to the DBA. + +Spec: `openspec/specs/release-packaging/spec.md` (until the change is archived: `openspec/changes/add-release-ci-packaging/specs/release-packaging/spec.md`). Local stack for comparison: `local/README.md`. + +## 1. Images + +CI (`.gitea/workflows/ci.yml`) builds every module on each push to `main`, runs all tests, and only then pushes. Each image is tagged with the commit SHA and with `latest`. Deploy SHA tags. Roll back by deploying the previous SHA. + +| Image (`gitea.containerverse.dev/solvway/…`) | Kubernetes Service name (must match) | Port | Exposed | +|---|---|---|---| +| `co-lite-admin-portal` | `admin-portal` | 3000 | Yes, through Traefik | +| `co-lite-mobile-api` | `mobile-api` | 80 | No, internal only | +| `co-lite-api-gateway` | `api-gateway` | 80 | No, internal only | +| `co-lite-service-auth` | `service-auth` | 80 | No | +| `co-lite-service-system` | `service-system` | 80 | No | +| `co-lite-service-counter` | `service-counter` | 80 | No | +| `co-lite-service-member` | `service-member` | 80 | No | +| `co-lite-service-log` | `service-log` | 80 | No | +| `co-lite-service-email` | `service-email` | 80 | No | + +The Service names matter: +- The gateway routes to `http://service-auth`, `http://service-system` and so on, on port 80. The names are fixed in its config. +- mobile-api and the portal find the gateway and mobile-api through `APIGATEWAYURL` and `MOBILE_API_BASE_URL`. + +**Health probes:** +- **Java services:** liveness `GET /actuator/health/liveness`, readiness `GET /actuator/health/readiness`. Spring Boot turns these on by itself on Kubernetes. `GET /actuator/health` also works everywhere. +- **Admin portal:** it has no health endpoint. Use a TCP probe on port 3000, or `GET /login`, which returns 200 without a session. + +**Images carry no configuration or secrets.** Everything below is supplied at runtime. CI fails a build whose image holds a key file or a secret with a built-in default (`local/image-check.sh`). Leave `BUILD_PROFILE` unset. `JAVA_OPTS` is optional, for example `-Xmx512m`. + +## 2. Order of setup + +1. The DBA prepares MySQL (section 3). +2. Create the Pub/Sub topics and the service-account key (section 5). +3. Create the Secrets and mounted files (sections 6 and 7). +4. Deploy, in this order: + 1. `service-system`, `service-auth`, `service-counter`, `service-member`, `service-log`, `service-email`; + 2. `api-gateway`; + 3. `mobile-api`; + 4. `admin-portal`. +5. Add the Traefik route (section 8). +6. Sign in as the first admin at `https://co-lite.containerverse.dev` and set the real password. Staff users can be created only once an email provider is configured (section 10), because each new user is emailed a temporary password. + +## 3. Database (for the DBA) + +**Server:** MySQL 8.0, with these two settings: +- `log_bin_trust_function_creators = 1`. The schema creates functions and triggers, and with binary logging on that otherwise needs the SUPER privilege, which managed MySQL does not grant. +- `innodb_strict_mode = OFF`. Some inherited tables are wide `utf8mb3` tables that strict mode rejects. + +**Package:** `openspec/changes/add-release-ci-packaging/sql/`. After the change is archived, it is under `openspec/changes/archive/-add-release-ci-packaging/sql/`. Run the files in this order, on an empty server: + +| File | What it does | +|---|---| +| `00-schemas.sql` | Creates the five schemas: `service-counter`, `service-email`, `service-log`, `service-member`, `service-system` | +| `01-schema.sql` | All tables, functions and triggers. No data. | +| `02-reference.sql` | Reference data: countries, currencies, banks, messages, settings, roles, modules. No customer data. | +| `03-colite.sql` | The Co-Lite company, the lead and event tables, the lead index, and the back-office modules. | +| `04-first-admin.sql` | The first administrator. Fill in `` and `` first (see below). | + +**Making the hash.** The password never has to appear in shell history: + +```sh +read -rs PW; printf '%s' "$PW" | docker run --rm -i httpd:2.4-alpine htpasswd -inBC 10 "" | tr -d ':\n'; unset PW +``` + +After running `04`, `SELECT LENGTH(password) FROM \`service-system\`.\`user\` WHERE id = 1;` must return 60. The administrator signs in as `admin` and must set a new password before anything else. + +**Database user:** +- Create one user with `SELECT, INSERT, UPDATE, DELETE, EXECUTE` on the five schemas. EXECUTE is needed: service-member calls a stored function. +- Every Java service reads the same variables (section 6): `MYSQLHOST`, `MYSQLPORT`, `MYSQLUSER`, `MYSQLPWD`. +- Each service connects to its own schema. + +## 4. Redis + +Use the existing Redis. Every Java service reads `REDISHOST`; the port is fixed at 6379. + +## 5. Google Pub/Sub + +The services publish and subscribe through Google Pub/Sub. It is not replaced in phase 1. + +- **Project:** use a Google Cloud project, and give its id as `GCP_PROJECT`. +- **Service account:** create one with the Pub/Sub Publisher and Pub/Sub Subscriber roles, and download a JSON key. Mount the key into every Java image except `api-gateway` (section 7), and set `GCP_CREDENTIAL_FILE=file:/var/secrets/gcp/key.json`. +- **Topics and subscriptions:** use `local/pubsub/topics.txt`. Each line is a topic name, optionally followed by a subscription name. Some topics appear on several lines with different subscriptions (e.g. `update-sync-topic`): create each topic once, then every subscription listed for it. Subscriptions use default settings. + +## 6. Environment variables + +**Secret** values go in a Kubernetes Secret. **Plain** values can go in a ConfigMap or straight into the Deployment. + +**Shared by the seven services and mobile-api** (every Java image except `api-gateway`, which needs only `JWT_TOKEN`): + +| Variable | Kind | Value | +|---|---|---| +| `JWT_TOKEN` | Secret | HS512 signing key, at least 32 characters. Must be the same value as `JWT_SECRET` on mobile-api. | +| `GCP_PROJECT` | Plain | Google Cloud project id | +| `GCP_CREDENTIAL_FILE` | Plain | `file:/var/secrets/gcp/key.json` (the mounted key) | +| `REDISHOST` | Plain | Redis host | +| `XXL_JOB_TOKEN` | Secret | Optional. Leave unset. | + +**Also read by `service-system`, `service-counter`, `service-member`, `service-log` and `service-email`:** + +| Variable | Kind | Value | +|---|---|---| +| `MYSQLHOST` | Plain | MySQL host | +| `MYSQLPORT` | Plain | `3306` | +| `MYSQLUSER` | Secret | Database user (section 3) | +| `MYSQLPWD` | Secret | Its password | + +**`service-system` only:** `JUMIO_TOKEN` and `JUMIO_SECRET` (Secret). These are for identity checks Co-Lite does not use. Leave them unset. + +**`service-auth` only:** + +| Variable | Kind | Value | +|---|---|---| +| `AES_SECRET_KEY` | Secret | Random, 32 characters | +| `USER_AUTH_API_SECRET` | Secret | Random, at least 32 characters | + +**`service-email` only.** No email or SMS provider has been chosen yet (see section 10). Choosing one takes more than its key: the provider, the sender addresses and the SMTP host are written in `application.yml` with Tastefully and Containerverse values. Spring lets an environment variable override any setting (upper-cased, `.` becomes `_` and `-` is dropped). So when a provider is chosen, also set: +- `EMAIL_PROVIDER`: one of `MAIL_JET`, `SEND_GRID`, `MAIL_GUN`, `SMTP` (today it is `MAIL_JET`); +- `EMAIL_SENDER` and `EMAIL_SENDERNAME`: the From address and name; +- for Mailjet, `MAILJET_SENDER`; for SendGrid, `SENDGRID_SENDER`; +- for SMTP, `SPRING_MAIL_HOST`, `SPRING_MAIL_PORT` and `SPRING_MAIL_USERNAME`; +- for Mailgun, `MAILGUN_API_BASE_URL` and `MAILGUN_API_MESSAGES_URL`, if the domain is not `mail.containerverse.dev`. + +The keys: + +| Variable | Kind | +|---|---| +| `MAILGUN_API_KEY` | Secret | +| `SENDGRID_API_KEY` | Secret | +| `MAILJET_API_KEY` | Secret | +| `MAILJET_SECRET_KEY` | Secret | +| `SMTP_PASSWORD` | Secret | +| `SMSS360_APIKEY` | Secret | +| `SMSS360_EMAIL` | Plain | + +**`mobile-api`:** + +| Variable | Kind | Value | +|---|---|---| +| `APIGATEWAYURL` | Plain | `http://api-gateway` | +| `APIKEY` | Secret | The key mobile-api sends to the gateway. Must equal the `key` of an entry in `appkeys.json` (section 7). | +| `AUTH_API_KEYS` | Secret | The keys mobile-api accepts from its clients, comma-separated, each `:APP`. The admin portal's `MOBILE_API_KEY` must be one of them. | +| `JWT_SECRET` | Secret | Same value as `JWT_TOKEN` | +| `CUSTOMER_PORTAL_BASE_URL` | Plain | The customer portal is not deployed in phase 1. When unset, it falls back to a Tastefully address that only the member referral link uses, which Co-Lite does not use. Set it once a customer portal exists. | + +**`api-gateway`:** only `JWT_TOKEN` (same value as everywhere). No Pub/Sub, Redis or database settings, and no `key.json` mount. It reads its client keys from the mounted `appkeys.json` (section 7). + +**`admin-portal`:** + +| Variable | Kind | Value | +|---|---|---| +| `MOBILE_API_BASE_URL` | Plain | `http://mobile-api` (no trailing slash) | +| `MOBILE_API_KEY` | Secret | One of the keys in mobile-api's `AUTH_API_KEYS` (without the `:APP` suffix) | +| `SESSION_SECRET` | Secret | Required. Random, at least 32 characters. Signs the session cookie. If it is unset, the portal falls back to `MOBILE_API_KEY`, which ties the two together. | +| `SESSION_COOKIE_NAME` | Plain | Optional (default `colite_admin`). The cookie is always `__Host-` prefixed and Secure, so the portal must be served over HTTPS. | +| `SESSION_IDLE_TTL` | Plain | Optional, seconds (default 3600) | +| `SESSION_ABSOLUTE_TTL` | Plain | Optional, seconds (default 28800) | +| `SESSION_REFRESH_GRACE` | Plain | Optional, seconds (default 300) | + +Generate each random value separately, for example with `openssl rand -base64 48`. + +## 7. Mounted files + +| File | Mounted into | Path | Content | +|---|---|---|---| +| `appkeys.json` | `api-gateway` | `/deployments/appkeys/appkeys.json` | The clients allowed to call the gateway (example below) | +| `key.json` | every Java image except `api-gateway` | `/var/secrets/gcp/key.json` | The Pub/Sub service-account key (section 5) | + +`appkeys.json` holds one entry, for mobile-api: + +```json +[ + { + "key": "", + "name": "Co-Lite mobile-api", + "desc": "mobile-api calls to the gateway", + "type": "BO", + "allowList": ["/"], + "ipList": [""] + } +] +``` + +- `type`, `allowList` and `ipList` are required. The gateway reads all three on every request, and a missing one fails every call. +- `allowList` entries are matched anywhere in the request URL (`["/"]` allows everything). `ipList` is a list of CIDR ranges. +- `type` is one of `BO`, `APP`, `MB`, `SK`, `TP`, `PG`, `RS`, `KYC`. Use `BO`. **Never use `TP`: it skips login-token checks entirely.** + +## 8. Traefik + +Route `co-lite.containerverse.dev` to the `admin-portal` Service on port 3000, with TLS. Only the admin portal is public: the browser talks to the portal, and the portal talks to `mobile-api` inside the cluster. Do not expose `mobile-api`, `api-gateway` or the services. + +## 9. Secret templates + +Kubernetes Secrets hold base64 values. Encode each one with `printf '%s' 'value' | base64 -w0`. These are templates: replace every `<…>` and never commit the filled-in files. + +```yaml +apiVersion: v1 +kind: Secret +metadata: + name: co-lite-shared +type: Opaque +data: + JWT_TOKEN: + MYSQLUSER: + MYSQLPWD: +--- +apiVersion: v1 +kind: Secret +metadata: + name: co-lite-service-auth +type: Opaque +data: + AES_SECRET_KEY: + USER_AUTH_API_SECRET: +--- +apiVersion: v1 +kind: Secret +metadata: + name: co-lite-mobile-api +type: Opaque +data: + APIKEY: + AUTH_API_KEYS: + JWT_SECRET: +--- +apiVersion: v1 +kind: Secret +metadata: + name: co-lite-admin-portal +type: Opaque +data: + MOBILE_API_KEY: + SESSION_SECRET: +--- +apiVersion: v1 +kind: Secret +metadata: + name: co-lite-gateway-appkeys +type: Opaque +data: + appkeys.json: +--- +apiVersion: v1 +kind: Secret +metadata: + name: co-lite-gcp-key +type: Opaque +data: + key.json: +``` + +## 10. Known gaps at go-live + +- **No email provider yet, and staff onboarding needs one.** Creating a staff user emails them a temporary password, and forgot-password sends an email too. There is no way for an admin to set someone else's password. Until a provider is configured in `service-email`, only the first administrator from `04-first-admin.sql` can sign in. +- **Reference data from Tastefully.** It still holds some Tastefully business text (message templates with a phone number, bank account and form links; a public image bucket URL; Tastefully usernames in audit columns). Review it before go-live. +- **Other Tastefully defaults.** The sender addresses in `service-email` and the `CUSTOMER_PORTAL_BASE_URL` fallback point at Tastefully. Override them as described in section 6. +- **Deploy by SHA, not `latest`.** CI pushes the images one by one, so if a push fails halfway, `latest` can differ between images. diff --git a/openspec/changes/add-release-ci-packaging/tasks.md b/openspec/changes/add-release-ci-packaging/tasks.md index 2f771ca..931601f 100644 --- a/openspec/changes/add-release-ci-packaging/tasks.md +++ b/openspec/changes/add-release-ci-packaging/tasks.md @@ -37,7 +37,7 @@ Design: `design.md`. All tasks run serially: each touches a build file, CI or SQ ## 3. Handover -- [ ] 3.1 Write `docs/deployment.md` (design decision 7). +- [x] 3.1 Write `docs/deployment.md` (design decision 7). - Cross-check every environment variable against each module's `application.yml`, the portal's `process.env` uses and the gateway's `appkeys.json` path, so none is missing or extra. - Secret templates use placeholders only, never real values. - Scope: `docs/deployment.md`